Python program to check if a number is a perfect number or not

Python program to check if a number is a perfect number or not:

In this post, we will learn how to find if a number is a perfect number or not in Python. This program will take one number as input from the user, find out if it is a perfect number or not and print the result.

Before we start writing down the code, let’s understand what is a perfect number and how to find out if a number is a perfect number or not programmatically.

What is a perfect number:

A number is called a perfect number if the sum of its positive divisors excluding the number is equal to the number itself.

For example, 28 is a perfect number because the sum of its divisors, 1 + 2 + 4 + 7 + 14 is equal to 28.

The program can use the following algorithm to check if a number is a perfect number or not:

  • Take the number as an input from the user.
  • Initialize a variable as 1 to hold the sum of all divisors. We are initializing it as 1 because 1 will be always a divisor to the number.
  • By using a loop that runs from 2 to number - 1, find out all divisors. Also, add the divisors to the sum variable.
  • At the end of the loop, check if the number is equal to the calculated sum or not. If yes, it is a perfect number. Else, it is not a perfect number.

Python program to check if a number is a perfect number or not:

The below program will check if a number is a perfect number or not:

def is_perfect(no):
    sum = 1

    for i in range(2, no):
        if no % i == 0:
            sum += i

    return sum == no


no = int(input('Enter a number: '))
if(is_perfect(no)):
    print(f'{no} is a perfect number')
else:
    print(f'{no} is not a perfect number')
  • It asks the user to enter a number and assigns the number to the variable no.
  • The is_perfect method checks if a number is a perfect number or not. This method returns a boolean value. Based on this value, it prints a message.

    • The sum variable is initialized as 1 to hold the sum of all divisors of the number.
    • The for loop runs from 2 to no - 1 and on each iteration, it checks if the current value of i can divide no or not i.e. if it is a divisor or not. If yes, it adds this value to sum.
  • At the end of this method, it compares the value of sum and no and returns one boolean value, true if both are equal or if no is a perfect number and false otherwise.

If you run this program, it will give output as like below:

Enter a number: 28
28 is a perfect number

Python program to find all perfect numbers in a range:

Let’s change the above program to find all perfect numbers in a given range. We can use the same method to find out all perfect numbers in a given range.

def is_perfect(no):
    sum = 1

    for i in range(2, no):
        if no % i == 0:
            sum += i

    return sum == no


start = int(input('Enter a start value of the range: '))
end = int(input('Enter a end value of the range: '))

print(f'Perfect numbers in between {start} to {end - 1} are: ')

for i in range(start, end):
    if(is_perfect(i)):
        print(f'{i} ')

This program is reading the start and the end value of the range and assigns these to the start and the end variables. The for loop runs from start to end - 1 and finds all perfect numbers in this range. For each value in the loop, it uses the is_perfect method to check if it is a perfect number or not. If yes, i.e. if the method is_perfect returns True, it prints its value.

You can run this program to find all perfect numbers in any range. Python example to find all perfect numbers in a range

You might also like: