Open In App

Perfect Number Program in Python

Last Updated : 26 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

A Perfect Number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself.

Example:

  • 6 is a perfect number because its divisors (excluding itself) are 1, 2, and 3, and their sum is 6 (1 + 2 + 3 = 6).
  • 28 is a perfect number because its divisors are 1, 2, 4, 7, 14, and their sum is 28 (1 + 2 + 4 + 7 + 14 = 28).

Below are some of the ways by which we can check if the number is a perfect number or not in Python:

Using for Loop

In this approach, we simply take a number from the user and make a variable named Sum initially its value is 0 and then we make a logic by using the for loop along with the conditional statement of if/ else. Finally, we get proper output.

n = 78
Sum = 0
for i in range(1, n):
    if(n % i == 0):
        Sum = Sum + i
if (Sum == n):
    print("Number is a Perfect Number.")
else:
    print("Number is not a Perfect Number.")

Output
Number is not a Perfect Number.

Explanation: It sums divisors of the number (excluding itself) and if the sum equals the number, it's a perfect number.

Using List Comprehension

Here, in this code, we leverage the power of list comprehension to efficiently find the divisors of the given number. The sum of those divisors is then compared to the original number to determine if it is a perfect number.

n = 6
divisors = sum([i for i in range(1, n) if n % i == 0])

if n == divisors:
    print("Number is a Perfect Number.")
else:
    print("Number is not a Perfect Number.")

Output
Number is a Perfect Number.

Explanation:

  • The list comprehension calculates the sum of divisors of n.
  • It checks if the sum of divisors equals n, which determines whether it's a perfect number.

Using filter() method

In this technique, we use filter() to find all divisors of the given number. The sum of those divisors is then compared to the original number to determine if it is a perfect number.

n = 25
divisors = list(filter(lambda x: n % x == 0, range(1, n)))

if sum(divisors) == n:
    print("Number is a Perfect Number.")
else:
    print("Number is not a Perfect Number.")

Output
Number is not a Perfect Number.

Explanation

  • filter() method is used to find all divisors of n.
  • sum of the divisors is then compared to n to check if it is a perfect number.

Perfect Number Program in Python - FAQs

What is a Perfect Number?

A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself.

Can a negative number be a perfect number?

No, perfect numbers are only defined for positive integers.

Can a number be perfect if it's not an integer?

No, perfect numbers are only defined for positive integers. Non-integer or fractional numbers cannot be perfect.

What are some common errors when checking for perfect numbers?

  • Edge cases: Make sure to handle cases like 1, which is not a perfect number.
  • Negative numbers: Remember that perfect numbers are only positive integers.

Practice Tags :

Similar Reads

three90RightbarBannerImg