Special Numbers Loops Posted May 09, 2020

A Perfect Number is a positive integer for which the sum of all of its positive factors except the number results in the number itself. For example, 6 is a perfect number because the factors of 6 excluding itself are 1, 2, 3 and sum of them is itself - 6. To achieve this, we shall make use of the factors logic and find the sum of all factors except the number itself and check if the sum is equal to the number or not.

```
public static bool IsPerfectNumber(int n)
{
int sum = 0;
Console.Write("Factors:");
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
Console.Write($"{i} ");
sum = sum + i;
}
}
Console.WriteLine($"Sum: {sum}");
return sum == n ? true : false;
}
```

**How it works:**

While calculating the factors, we exclude the input number from the loop (the reason why the condition is i < n and not i <=n) and for each time the counter is a factor of the input number we add it to the sum. Finally, we compare the sum with the input number for equality and the result tells us if its a Perfect Number or not.

```
Number: 6
Factors:1 2 3 Sum: 6
True
```

Compute and generate a compressed string for a given string containing repetitions

Find all possible natural numbers below a given limit such that a3+b3 = c3+d3

Find all the pairs in a given ordered set of numbers whose sum is equal to a given input sum

Find all the pairs in a given unordered set of numbers whose sum is equal to a given input sum

Sort the given unordered set of Binary digits

Implementing stream based communication with gRPC and ASP.NET Core

Building a simple Client-Server Interaction with gRPC in ASP.NET Core

Role-based and Claims-based Authorization in ASP.NET Core using Policies - Hands on

Understanding How Antiforgerytoken works in ASP.NET Core MVC

Implementing Cognito User Login and Signup in ASP.NET Core using AWS SDK