We have named numbers based on their specialties. One such number is a perfect number. In this article, we will discuss the properties of perfect numbers. We will also implement a program to check for a perfect number in python.

## What Is A Perfect Number?

A number is called a perfect number if it is equal to the sum of all of its factors excluding the number itself. If we consider the number itself in the sum, we can say that the sum of all the factors of a perfect number is double the given number.

For example, consider the number 6. It has four factors i.e. 1,2,3, and 6. As we are excluding the number itself, the sum of other factors i.e. 1, 2, and 3 is 6. Hence, 6 is a perfect number.

Alternatively, the sum of all the factors of 6 is 1+2+3+6 i.e. 12, which is double the number itself. Hence, 6 is a perfect number.

Let us take another number 10. The factors of 10 are 1,2,5, and 10. The sum of all the factors of 10 equals 18, which is not double the given number. Hence, 10 is not a perfect number.

## Check For Perfect Number In Python

To check for a perfect number, we will first find its factors. After that, we will check if the sum of all the factors is double the given number or not.

To find the factors of the given number N, we will divide the number by all the numbers starting from 1 to N. The numbers that completely divide the given number will be declared as factors of N. We will store these factors in a list as follows.

```
dedef calculate_factors(N):
factors = []
for i in range(1, N + 1):
if N % i == 0:
factors.append(i)
return factors
input_number = 10
output = calculate_factors(input_number)
print("factors of {} are {}".format(input_number, output))
```

Output:

`factors of 10 are [1, 2, 5, 10]`

After finding the factors of the given number, we will find the sum of the factors using the sum() function. After finding the sum, we will check if the sum is double the given number or not. If yes, we will say that the given number is a perfect number. Otherwise not.

We can implement this logic to check for a Perfect number in python as follows.

```
def calculate_factors(N):
factors = []
for i in range(1, N + 1):
if N % i == 0:
factors.append(i)
return factors
def check_perfect_number(N):
factors = calculate_factors(N)
sumOfFactors = sum(factors)
if sumOfFactors // 2 == N:
return True
else:
return False
input_number = 10
output = check_perfect_number(input_number)
print("{} is a perfect number: {}".format(input_number, output))
input_number = 6
output = check_perfect_number(input_number)
print("{} is a perfect number: {}".format(input_number, output))
```

Output:

```
10 is a perfect number: False
6 is a perfect number: True
```

## Conclusion

In this article, we have discussed what a perfect number is. We have also implemented a program to check whether a given number is a perfect number or not. To know more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

## Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.