A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).
Examples:
6 = 1 + 2 + 3 → Perfect
28 = 1 + 2 + 4 + 7 + 14 → Perfect
18 = 1 + 2 + 3 + 6 + 9 → Sum = 21 ≠ 18 → Not perfect
def is_perfect(n: int) -> bool:
if n <= 1:
return False
divisors = [i for i in range(1, n) if n % i == 0]
return sum(divisors) == n
# Read number from user
num = int(input("Enter a number: "))
# Check and display result
if is_perfect(num):
print(f"{num} is a perfect number.")
else:
print(f"{num} is not a perfect number.")
Input: 6
6 is a perfect number.
Input: 18
18 is not a perfect number.
- The program checks all proper divisors of the number
- If their sum equals the number itself, it is considered perfect
- The is_perfect function performs this check and returns the result