Perfect number in Python
In this post, you will learn how to check the perfect number using the Python programming language. The perfect number program is generally asked in Python coding tests and interviews.
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. In other words, a number whose sum of factors is equal to the number, excluding the number itself is called a perfect number.
The sum of divisors of a number, excluding the number itself, is called its aliquot sum, so a perfect number is one that is equal to its aliquot sum. For example, 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number. 6 is the first perfect number.
Perfect Number | Sum of its Divisors |
6 | 1+2+3 |
28 | 1+2+4+7+14 |
496 | 1+2+4+8+16+31+62+124+248 |
8,128 | 1+2+4+8+16+32+64+127+254+508+1,016+2,032+4,064 |
Here, we have mentioned three different ways to find the perfect number in Python.
Perfect Number Program in Python using For loop
The given Python program asks the user to enter any number. We store it in a variable, num. Next, this will check whether the number is a perfect number or not using the for loop. Next, using an if statement, we check if the number divided by i gives the remainder as 0, which is basically the proper divisor of the integer. If the sum of the proper divisors of the number is equal to the original number, the number is a perfect number.
num = int(input("Enter any number: "))
n_sum = 0
for i in range(1, num):
if(num % i == 0):
n_sum = n_sum + i
if (n_sum == num):
print(num," is a Perfect number!")
else:
print(num," is not a Perfect number!")
Output of the above code:
Enter any number: 6
6 is a Perfect number!
Enter any number: 10
10 is not a Perfect number!
Perfect Number Program in Python using While loop
In the given example, we ask the user to enter any number. Next, we initialise the n_sum to 0 and i to 1. We iterate the loop till the value of i is less than the entered number. Within the loop, if a proper divisor is found, we add it to the n_sum. Finally, we compare the sum of divisors with the number and display the required result.
num = int(input("Enter any number: "))
n_sum = 0
i = 1
while(i < num):
if(num % i == 0):
n_sum = n_sum + i
i = i + 1
if (n_sum == num):
print(num," is a Perfect number.")
else:
print(num," is not a Perfect number.")
Output of the above code:
Enter any number: 496
496 is a Perfect number.
Enter any number: 30
30 is not a Perfect number.
Perfect Number Program in Python using Method
Here, we have asked the user to enter any integer value and defined a method PerfectNumber(). We have passed the user entered value to the method we created. Within the user-defined function, we have used the for loop to check whether the user input is a perfect number or not.
def PerfectNumber(number):
n_sum = 0
for i in range(1, number):
if(number % i == 0):
n_sum = n_sum + i
return n_sum
# taking user input
num = int(input("Enter any number: "))
if (num == PerfectNumber(num)):
print(num," is a Perfect number.")
else:
print(num," is not a Perfect number.")
Output of the above code:
Enter any number: 55
55 is not a Perfect number.
Enter any number: 6
6 is a Perfect number.
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary