Python Program to Check Perfect Number
A perfect number is a number that equals the sum of its positive divisors excluding itself; use
sum_of_divisors = sum(i for i in range(1, n) if n % i == 0) and check sum_of_divisors == n to determine if n is perfect.Examples
Input6
Output6 is a perfect number
Input28
Output28 is a perfect number
Input10
Output10 is not a perfect number
How to Think About It
To check if a number is perfect, find all numbers less than it that divide it evenly. Add these divisors together. If the total equals the original number, it is perfect; otherwise, it is not.
Algorithm
1
Get the input number n2
Initialize sum_of_divisors to 03
For each number i from 1 to n-1, check if i divides n evenly4
If yes, add i to sum_of_divisors5
After the loop, compare sum_of_divisors with n6
If equal, return that n is perfect; else, return not perfectCode
python
n = int(input('Enter a number: ')) sum_of_divisors = 0 for i in range(1, n): if n % i == 0: sum_of_divisors += i if sum_of_divisors == n: print(f'{n} is a perfect number') else: print(f'{n} is not a perfect number')
Output
Enter a number: 6
6 is a perfect number
Dry Run
Let's trace the number 6 through the code
1
Input number
n = 6
2
Initialize sum
sum_of_divisors = 0
3
Check divisors from 1 to 5
i=1 divides 6, sum=1; i=2 divides 6, sum=3; i=3 divides 6, sum=6; i=4 no; i=5 no
4
Compare sum with n
sum_of_divisors = 6 equals n = 6
5
Print result
'6 is a perfect number'
| i | n % i == 0 | sum_of_divisors |
|---|---|---|
| 1 | True | 1 |
| 2 | True | 3 |
| 3 | True | 6 |
| 4 | False | 6 |
| 5 | False | 6 |
Why This Works
Step 1: Find divisors
We find all numbers less than n that divide n evenly using n % i == 0.
Step 2: Sum divisors
We add these divisors to get their total sum.
Step 3: Compare sum to number
If the sum equals the original number, it means the number is perfect.
Alternative Approaches
Using list comprehension and sum function
python
n = int(input('Enter a number: ')) sum_of_divisors = sum(i for i in range(1, n) if n % i == 0) if sum_of_divisors == n: print(f'{n} is a perfect number') else: print(f'{n} is not a perfect number')
This method is shorter and uses Python's built-in functions for clarity but may be less clear for beginners.
Optimized divisor check up to n//2
python
n = int(input('Enter a number: ')) sum_of_divisors = 0 for i in range(1, n//2 + 1): if n % i == 0: sum_of_divisors += i if sum_of_divisors == n: print(f'{n} is a perfect number') else: print(f'{n} is not a perfect number')
Checking divisors only up to half of n reduces unnecessary checks, improving performance.
Complexity: O(n) time, O(1) space
Time Complexity
The program checks all numbers from 1 to n-1 to find divisors, so it runs in linear time O(n).
Space Complexity
It uses a fixed amount of extra space for variables, so space complexity is O(1).
Which Approach is Fastest?
The optimized approach checking divisors only up to n//2 is faster than checking all numbers up to n-1.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic loop to n-1 | O(n) | O(1) | Simplicity and clarity |
| List comprehension | O(n) | O(1) | Concise code and uses constant space |
| Optimized loop to n//2 | O(n/2) ~ O(n) | O(1) | Better performance for large numbers |
Only check divisors up to half of the number to save time.
Including the number itself in the sum of divisors, which makes the check incorrect.