Write a Program to Find a Perfect Number in Python

In this Python tutorial, I will show you how to write a program to find a perfect number in Python.

I have explained three different approaches to finding the perfect number in Python, one of the methods I have used that checks the computation performance of hardware and algorithm.

After completing this tutorial, you can write a program that can find the perfect number in three different ways.

Let’s start,

Perfect Number Program in Python

First, ‘What is the perfect number?’ A perfect number is equal to the sum of its divisors. For example, take number 6. Which numbers can divide it completely? The numbers are 1, 2, and 3, called divisors.

So, according to the definition, a perfect number is equal to the sum of its divisor; let’s sum the divisor of 6, which is 1 + 2 + 3, and it is equal to 6. Hence, 6 is the perfect number cause the sum of the divisor equals 6.

Let’s write a program.

First, initialize a variable with a value equal to 28 to be checked for the perfect number.

number = 28

Next, again, initialize a variable named sum_divisors with a value equal to 0; this divisor variable will store the sum of the divisors of the given number.

sum_divisors = 0

Now, initialize a for loop, which will take a number between 1 and n (which is the given number) and divide the given number to see which number divides the given number completely.

for i in range(1, number):
    if number % i == 0:
       sum_divisors = sum_divisors + i
if sum_divisors == number:
    print('The given number is perfect number')
else:
    print('The given number is not perfect number')
Creating Perfect Number Program in Python

Look at the output; 28 is a perfect number. Let’s understand how the above code works.

READ:  Python List count() method [With Examples]

First, a variable number is initialised with the value 28, and you want to check whether it is a perfect number. Then, a new variable sum_divisors initializes to 0. This sum_divisors will store the sum of the number’s divisors.

Next, using this line ‘for i in range(1, number)’, it starts a loop that runs from 1 to the value of a number.

Here, loops run from 1 to number-1 because it tries to find all the divisors of the given number, excluding the number itself.

Then, a first conditional statement with loop, which is ‘if number % i == 0’, checks if the current value of ‘i’ is a divisor of the given number or completely divides the number. Then that ‘i’ is added to ‘sum_divisors’ using the line ‘sum_divisors = sum_divisors + i’.

Here, sum_divisors+i adds the current divisors, which is represented by ‘i‘ with divisor values in sum_divisors.

Finally, the next condition, ‘if sum_divisors == number’ compares the sum_divisors with the given number to check if the sum of the divisors is equal to the given number. If that is true, it means the number is perfect.

When the number is perfect, it outputs on the terminal ‘The given number is perfect’; otherwise, it outputs ‘The given number is not perfect number’.

Perfect Number in Python using Function

In the above section, you learned to create a perfect number program in Python using the loop. I will show you how to make a Python perfect number program using a function here.

You can create a function by including all the logic you have done in the above section in a function block, as shown below.

def is_perfect(number):
  sum_divisors = 0
  for i in range(1, number):
      if number % i == 0:
        sum_divisors = sum_divisors + i
  if sum_divisors == number:
      print('The given number is perfect number')
  else:
      print('The given number is not perfect number')

In the above code, a function named ‘is_perfect’ is created; now, call this function with value 496, as shown below.

is_perfect(496)
Perfect Number in Python using Function

Look, you, it says, ‘The given number is a perfect number’. So, to know whether the number is perfect, you are just passing it to the function is_perfect(), which contains logic, and you already know how it works.

READ:  Python list len() method [With Examples]

How to Find Perfect Number in Python Using Recursion

Recursion is a concept in which a function calls itself repeatedly. You will see how to use recursion to check whether a given number is prime.

Execute the below Python code for the perfect number using recursion.

def sum_of_divisors(number, i=1):

    if i == number:
        return 0
    if number % i == 0:
        return i + sum_of_divisors(number, i + 1)
    else:
        return sum_of_divisors(number, i + 1)

Now call this function with the value 28

num = 28
if sum_of_divisors(num) == num:
    print(num, "is a perfect number.")
else:
    print(num, "is not a perfect number.")
How to Find Perfect Number in Python Using Recursion

Look at the output. 28 is a perfect number returned using recursion. Let’s understand each line of code.

  • Here, a function named ‘sum_of_divisors(number)’ is defined. It takes a number ‘number’ to be checked for perfect and ‘i’, the current divisor with a default value of 1.
    • Then, the first line is if i == number within the function. This means it checks if ‘i’ is equal to the number. Then, it stops the recursion.
  • After that following line of code, ‘if number % i == 0: return i + sum_of_divisors(number, i + 1)’, it checks if the given number ‘number’ is divisible by the ‘i’, which means after dividing, it becomes equal to 0, then it includes in sum and recurses, otherwise, it doesn’t include in sum, just recurse using the code ‘else: return sum_of_divisors(number, i + 1)’.
  • The next line of code, num = 28, initializes the variable num with the value 28; this is the number you want to know whether it is perfect.
  • The following line of code, ‘if sum_of_divisors(num) == num’, pass the num to the function sum_of_divisors(num) to compute the sum of divisors, which is then compared with the given number ‘num’ using the ==.
  • If the returned sum of divisors from the function equals the given number ‘num’, then ‘num’ is the perfect number. Otherwise, num is not a perfect number.
READ:  How NumPy Filter 2D Array by Condition in Python [6 Methods]

I hope that by following the above example, you understand how to find the perfect number in Python.

Conclusion

In this Python tutorial, you learned how to write a program to find perfect number in Python.

You learned what perfect numbers are, and then, using three different examples, you wrote a program that can find the perfect number using loops, functions, and recursion.

You may like to read: