- Divisors
- 1, 2, 3
- Sum
- 6
Check Perfect Number in Python
What you’ll learn
- What perfect, deficient, and abundant numbers mean.
- How to sum proper divisors in Python.
- How to check one number and scan a range.
Overview
A perfect number equals the sum of its proper divisors. For example, 6 is perfect because 1 + 2 + 3 = 6.
Two programs
One single-value check and one range scan.
Live preview
See divisors, sum, and classification immediately.
Interview-ready flow
Clear loop, edge case for 1, and complexity discussion.
Prerequisites
Python loops, modulo operator, and basic function syntax.
- Use
for i in range(...)loops comfortably. - Know that
n % i == 0means i divides n.
What is a perfect number?
A positive integer n is perfect when the sum of its proper divisors equals n exactly.
Using s(n) = sum of proper divisors:
Equivalent formulas
If sigma(n) is the sum of all positive divisors, then s(n) = sigma(n) - n. So s(n) = n is equivalent to sigma(n) = 2n.
Compute proper divisor sum and compare with n.
Quick examples
- Divisors
- 1, 2, 4, 7, 14
- Sum
- 28
- Divisors
- 1, 2, 5
- Sum
- 8
- Divisors
- 1, 2, 3, 4, 6
- Sum
- 16
Live preview
Enter a positive integer and inspect proper divisors, sum, and verdict.
Algorithm
Goal: check if proper divisor sum equals n.
Initialize sum
Start with sum = 0.
Scan divisors
For i from 1 to n // 2, if n % i == 0 then add i.
Compare
If sum == n, number is perfect.
📜 Pseudocode
function proper_divisor_sum(n):
sum = 0
for i from 1 to floor(n / 2):
if n mod i == 0:
sum = sum + i
return sum
function is_perfect(n):
if n < 2:
return false
return proper_divisor_sum(n) == n Check one number
Test a fixed value (28) with a helper function.
def is_perfect_number(number: int) -> bool:
total = 0
for i in range(1, number // 2 + 1):
if number % i == 0:
total += i
return total == number
number = 28
if is_perfect_number(number):
print(f"{number} is a perfect number.")
else:
print(f"{number} is not a perfect number.") Perfect numbers in range 1 to 50
Reuse helper and print all perfect numbers in a small interval.
def is_perfect_number(num: int) -> bool:
total = 0
for i in range(1, num // 2 + 1):
if num % i == 0:
total += i
return total == num
print("Perfect Numbers in the range 1 to 50:")
for i in range(1, 51):
if is_perfect_number(i):
print(i, end=" ") Notes
Faster check. You can test divisors only up to sqrt(n) and add divisor pairs.
Rarity. Perfect numbers are rare, so broad brute-force scans are expensive.
❓ FAQ
🔄 Input / output examples
Replace fixed values in code or read from input for interactive runs.
| Value | Output idea |
|---|---|
| 28 | Perfect number |
| 10 | Not perfect |
| 6 | Perfect number |
| 12 | Not perfect |
Edge cases
Not perfect
Proper divisor sum is 0, not 1.
Always deficient
Only proper divisor is 1, so sum is less than n.
Proper divisors only
Adding n itself breaks the definition.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| Single check with n // 2 scan | O(n) | O(1) |
| Single check with sqrt(n) pairing | O(sqrt(n)) | O(1) |
| Range scan 1..U (naive) | O(U^2) | O(1) |
Summary
- Definition: perfect means proper divisor sum equals n.
- Loop: check divisors from 1 to n // 2 with modulo.
- Edge case: 1 is not perfect.
The first four perfect numbers are 6, 28, 496, and 8128. Whether any odd perfect number exists is still unknown.
9 people found this page helpful
