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.

s(n)=dnd<nds(n) = \sum_{\substack{d \mid n \\ d < n}} d

Based on this sum, every positive integer n>1n > 1 falls into one of three categories:

CategoryConditionExample
Perfects(n)=ns(n) = n66: 1+2+3=61+2+3 = 6
Abundants(n)>ns(n) > n1212: 1+2+3+4+6=16>121+2+3+4+6 = 16 > 12
Deficients(n)<ns(n) < n77: 1<71 < 7

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 2p1(2p1)2^{p-1}(2^p - 1) where 2p12^p - 1 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.