Definition
s(n) = n
Proper divisor sum equals the number.
A perfect number equals the sum of its proper divisors. For example, 6 is perfect because 1 + 2 + 3 = 6. This tutorial covers the definition, deficient/abundant neighbors, a live checker, worked Python examples, edge cases, and complexity.
s(n) = n
Proper divisor sum equals the number.
Exclude n
Positive divisors of n except n itself.
Simple loop
No proper divisor exceeds n // 2.
s(n) vs n
Deficient, perfect, or abundant.
Try 28 / 12
See divisors, sum, and verdict live.
6, 28, 496…
Perfect numbers are uncommon.
A perfect number is a positive integer whose proper divisors add up to the number itself. Using s(n) for that sum: perfect means s(n) = n, deficient means s(n) < n, and abundant means s(n) > n.
Equivalently, if sigma(n) is the sum of all positive divisors, then perfect means sigma(n) = 2n. One is never perfect: by convention s(1) = 0.
It is a classic divisor-sum interview problem that connects loops, modulo, and number-theory vocabulary.
Proper divisors sum to n.
Do not add the number itself.
s(1) = 0 by convention.
Deficient / abundant too.
In short: sum divisors from 1 to n // 2; perfect when that sum equals n.
Given a positive integer n, decide whether it is perfect by comparing the sum of its proper divisors with n.
# 6: 1 + 2 + 3 = 6 -> perfect
# 28: 1+2+4+7+14 = 28 -> perfect
# 10: 1 + 2 + 5 = 8 -> deficient
# 12: 1+2+3+4+6 = 16 -> abundant | Item | Type | Description |
|---|---|---|
n / number | int | Positive integer to classify. |
| Return | bool | True when proper divisor sum equals n. |
| Classification | text | Deficient, perfect, or abundant. |
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 | Method | Idea | Notes |
|---|---|---|
| Scan to n // 2 | Add every proper divisor | Interview default — clearest |
| Pair to sqrt(n) | Add i and n // i | Faster; careful with squares |
| sigma(n) = 2n | All-divisor sum | Equivalent definition |
| Goal | Pattern |
|---|---|
| Loop bound | range(1, n // 2 + 1) |
| Is divisor? | if n % i == 0: total += i |
| Perfect? | return total == n |
| Deficient | total < n |
| Abundant | total > n |
| Guard 1 | if n < 2: return False |
Same divisor sum — different comparisons to n.
s(n) = nThis page — e.g. 6, 28
s(n) < nMost numbers, including primes
s(n) > ne.g. 12 — related topic
exclude nProper divisors only
Reach for a proper-divisor sum whenever you need to classify perfection.
Divisors, sums, and classification.
Find all divisors with %.
Same sum, different comparison.
Find the rare perfect values in a band.
Naive O(n) per check gets costly fast.
Key benefit: one clear loop that teaches proper divisors, classification, and the famous examples 6 and 28.
Enter a positive integer and inspect proper divisors, sum, and verdict.
Three complete Python programs — check 28, list perfect numbers from 1 to 50, and classify deficient/perfect/abundant. Click View Output to reveal sample console results.
A reusable helper and the classic example 28.
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.") The loop adds every proper divisor of 28: 1, 2, 4, 7, and 14. Their sum is 28, so the helper returns True.
Reuse the helper to find the rare perfect values nearby.
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=" ") Within 1..50 only 6 and 28 are perfect. That rarity is typical — the next known values jump much larger.
Reuse the same sum to label each sample number.
def proper_divisor_sum(n: int) -> int:
if n < 2:
return 0
total = 0
for i in range(1, n // 2 + 1):
if n % i == 0:
total += i
return total
def classify(n: int) -> str:
s = proper_divisor_sum(n)
if s == n:
return "perfect"
if s < n:
return "deficient"
return "abundant"
for value in (6, 10, 12, 28, 1):
print(f"{value}: {classify(value)} (s={proper_divisor_sum(value)})") One sum drives three labels. Perfect is the equality case; deficient and abundant are the strict inequalities.
Accumulate proper divisors only.
Add i whenever n % i == 0.
Equal means perfect; else deficient/abundant.
Bool for perfect, or a class label.
Trace the proper-divisor sum for n = 28.
| i | 28 % i | Add? | total |
|---|---|---|---|
1 | 0 | Yes | 1 |
2 | 0 | Yes | 3 |
4 | 0 | Yes | 7 |
7 | 0 | Yes | 14 |
14 | 0 | Yes | 28 |
total 28 equals n — perfect.
Where perfect-number checks show up beyond the interview prompt.
Divisor loops and equality checks.
Example: is_perfect(28).
Label deficient / perfect / abundant.
Example: Example 3.
Find rare perfect values in a band.
Example: 6 and 28 in 1..50.
Show what “proper” excludes.
Example: do not add n.
Same sum, greater-than test.
Example: related topic.
Another “perfect” naming cousin.
Example: related CTA.
Pro Tip: open with “proper divisors exclude n; perfect means sum equals n” before coding.
Why the n//2 scan works well for beginners and interviews.
Dry-run 6 or 28 on paper and watch the sum grow.
Stopping at n // 2 avoids adding n by mistake.
Same sum powers deficient/abundant labels.
You can upgrade to sqrt pairing when needed.
Pro Tip: lead with the n//2 scan; mention sqrt pairing only as an optimization aside.
Small habits that keep perfect-number solutions interview-ready.
Proper divisors stop at n // 2.
Treat n < 2 as not perfect.
Mention deficient and abundant alongside perfect.
Use them as quick sanity checks.
sqrt pairing is optional after the clear O(n) version.
Pro Tip: dry-run 6, 10, and 12 — if those three classes match, your sum logic is correct.
Mistakes that commonly break perfect-number programs.
Adding the number itself doubles the definition.
→ Stop at n // 2.
Thinking 1 divides itself uniquely.
→ s(1) = 0; 1 is not perfect.
Different “perfect” concept entirely.
→ This page is about divisor sums.
Using range(1, n // 2) and missing n // 2.
→ Use range(1, n // 2 + 1).
Checking every n up to millions with O(n) each.
→ Use smaller ranges or faster pairing.
Handle these before claiming the check is complete.
Proper divisor sum is 0, not 1.
Only proper divisor is 1, so sum < n.
Adding n itself breaks the definition.
Use them as sanity checks.
s(12) = 16 > 12.
No odd perfect number is known.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
Quick Takeaway: sum proper divisors to n // 2; perfect when that sum equals n.
| 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²) | O(1) |
For interview demos, the O(n) scan is fine; mention pairing when asked about speed.
A perfect number equals the sum of its proper divisors. Loop from 1 to n // 2, add every divisor, and compare with n — remembering that 1 is not perfect and that perfect values are rare.
Practice the three examples above, then continue to checking perfect squares.
s(n) = n means perfect; exclude n from the divisor sum.
Classify divisor sums the interview-friendly way.
s(n) = n
Definitionto n // 2
Loop1 not perfect
Guarddef / abun
NeighborsO(n) naive
AnalysisThe first four perfect numbers are 6, 28, 496, and 8128. Whether any odd perfect number exists is still unknown.
Learn how to check whether a number is a perfect square in Python.
9 people found this page helpful