Lesson 4 of 15
Perfect Numbers
Perfect, Abundant & Deficient Numbers
The ancient Greeks classified numbers by comparing them to the sum of their proper divisors — all divisors except the number itself.
Based on this sum, every positive integer falls into one of three categories:
| Category | Condition | Example |
|---|---|---|
| Perfect | : | |
| Abundant | : | |
| Deficient | : |
Perfect Numbers
A perfect number equals the sum of its proper divisors. The first four are: 6, 28, 496, 8128. Euler proved that all even perfect numbers have the form where is a Mersenne prime. No odd perfect number has ever been found.
def proper_divisor_sum(n):
return sum(i for i in range(1, n) if n % i == 0)
print(proper_divisor_sum(6)) # 6 → perfect
print(proper_divisor_sum(28)) # 28 → perfect
Classification
def is_perfect(n):
return proper_divisor_sum(n) == n
def classify(n):
s = proper_divisor_sum(n)
if s == n:
return "perfect"
elif s > n:
return "abundant"
else:
return "deficient"
print(classify(12)) # abundant
print(classify(7)) # deficient
Your Task
Implement proper_divisor_sum(n), is_perfect(n), and classify(n) which returns "perfect", "abundant", or "deficient".
Python runtime loading...
Loading...
Click "Run" to execute your code.