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 PHP 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 (int)(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 (int)(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 (int)(n / 2) | Add every proper divisor | Interview default — clearest |
| Pair to sqrt(n) | Add $i and (int)($n / $i) | Faster; careful with squares |
| sigma(n) = 2n | All-divisor sum | Equivalent definition |
| Goal | Pattern |
|---|---|
| Loop bound | for ($i = 1; $i <= (int)($n / 2); $i++) |
| Is divisor? | if ($n % $i === 0) { $sum += $i; } |
| Perfect? | return $sum === $n; |
| Deficient | $sum < $n |
| Abundant | $sum > $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 PHP 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.
<?php
function isPerfectNumber(int $number): bool
{
$sum = 0;
for ($i = 1; $i <= (int)($number / 2); $i++) {
if ($number % $i === 0) {
$sum += $i;
}
}
return $sum === $number;
}
$number = 28;
if (isPerfectNumber($number)) {
echo $number . " is a perfect number.\n";
} else {
echo $number . " is not a perfect number.\n";
}
?> 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.
<?php
function isPerfectNumber(int $num): bool
{
$sum = 0;
for ($i = 1; $i <= (int)($num / 2); $i++) {
if ($num % $i === 0) {
$sum += $i;
}
}
return $sum === $num;
}
echo "Perfect Numbers in the range 1 to 50:\n";
for ($i = 1; $i <= 50; $i++) {
if (isPerfectNumber($i)) {
echo $i . " ";
}
}
echo "\n";
?> 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.
<?php
function properDivisorSum(int $n): int
{
if ($n < 2) {
return 0;
}
$sum = 0;
for ($i = 1; $i <= (int)($n / 2); $i++) {
if ($n % $i === 0) {
$sum += $i;
}
}
return $sum;
}
function classify(int $n): string
{
$s = properDivisorSum($n);
if ($s === $n) {
return "perfect";
}
if ($s < $n) {
return "deficient";
}
return "abundant";
}
foreach ([6, 10, 12, 28, 1] as $value) {
echo $value . ": " . classify($value) . " (s=" . properDivisorSum($value) . ")\n";
}
?> 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 (int)(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 (int)(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 (int)(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 a half-open upper bound and missing (int)($n / 2).
→ Loop for ($i = 1; $i <= (int)($n / 2); $i++).
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 (int)(n / 2); perfect when that sum equals n.
| Task | Time | Extra space |
|---|---|---|
| Single check with (int)(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 (int)(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 (int)(n / 2)
Loop1 not perfect
Guarddeficient / abundant
NeighborsO(n) naive
AnalysisThe first four perfect numbers are 6, 28, 496, and 8128. Mathematicians have known for centuries that every even perfect number fits a pattern tied to special primes; whether any odd perfect number exists is still an open question.
Learn how to check whether a number is a perfect square in PHP.
9 people found this page helpful