- Proper divisors
- 1, 2, 3
- Sum s(n)
- 1 + 2 + 3 = 6
- Verdict
- s(6) = 6 — the smallest perfect number.
Check Perfect Number in PHP
What you’ll learn
- What a perfect number is, and how it sits beside deficient and abundant numbers.
- How to sum proper divisors in PHP with a simple loop and the
%operator. - A live preview that shows divisors and the verdict before you compile.
- Two complete programs: test one value (for example 28) and list perfect numbers in a range.
Overview
Picture a positive whole number n. List every proper divisor—positive pieces that divide n evenly and are smaller than n. Add those pieces. If the total equals n exactly, the number is perfect. The smallest example is 6, because 1 + 2 + 3 = 6.
Two programs
One answers “Is this number perfect?” The other scans a small interval (such as 1 to 50) and prints every hit.
Live preview
Type any modest n and see the divisor list, the sum, and whether the outcome is perfect, abundant, or deficient.
Interview-ready
Clean loop structure, clear edge case for n = 1, and optional speed-ups if the examiner asks about larger inputs.
Prerequisites
You should be comfortable with tiny PHP programs and what “divides evenly” means.
- Basic PHP syntax,
echo,forloops, and small helper functions. - The remainder operator
%:n % i == 0meansiis a divisor ofn. - Idea of a divisor: a whole number that splits another whole number without leaving a remainder.
What is a perfect number?
A perfect number is a positive integer n whose proper divisors add up to n itself. “Proper” means we skip n when we list divisors—we only use smaller ones.
Write s(n) for that sum. Greek mathematicians sorted numbers into three familiar buckets:
Same idea, two formulas
Books often write σ(n) for the sum of every positive divisor of n, including n. The sum of proper divisors is s(n) = σ(n) − n. Saying “n is perfect” is the same as either s(n) = n or σ(n) = 2n—multiply the second equation out and you will see why.
Add proper divisors. If the total equals n, print “perfect.” If the total is smaller, call it deficient; if larger, abundant. One loop can classify all three.
Numbers to try in your head
Each card shows proper divisors and the sum s(n), so you can see how the definition feels on real examples.
- Proper divisors
- 1, 2, 4, 7, 14
- Sum s(n)
- 1 + 2 + 4 + 7 + 14 = 28
- Verdict
- The next perfect number after 6; our sample programs use it often.
- Proper divisors
- 1, 2, 5
- Sum s(n)
- 1 + 2 + 5 = 8
- Verdict
- 8 < 10 — not perfect.
- Proper divisors
- 1, 2, 3, 4, 6
- Sum s(n)
- 1 + 2 + 3 + 4 + 6 = 16
- Verdict
- 16 > 12 — abundant (see the abundant number tutorial).
Live preview
This mirrors the PHP logic: walk from 1 to n/2, collect every divisor, add them, compare to n. Try 6, 28, 10, or 12 to see perfect versus deficient versus abundant.
Algorithm
Goal: decide whether a positive integer n is perfect by computing the sum of its proper divisors.
Start with sum = 0
You will add each proper divisor into this running total.
Try every candidate from 1 to n/2
If n % i == 0, then i is a proper divisor (the loop range keeps i < n automatically).
Compare sum to n
If sum == n, the number is perfect. Otherwise it is not (and you can also say whether it is deficient or abundant).
📜 Pseudocode
function properDivisorSum(n):
sum ← 0
for i from 1 to floor(n / 2):
if n mod i = 0:
sum ← sum + i
return sum
function isPerfect(n):
if n < 2:
return false // 1 is not perfect by convention here
return properDivisorSum(n) = nCheck one number
Classic interview layout: a helper returns true when the divisor sum hits n, and main prints a friendly sentence. The sample uses 28.
<?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";
}
?>Explanation
The loop visits every integer that could possibly be a proper divisor. When number % i == 0, we add i. After the loop, sum equals s(number); equality with number means perfect.
Perfect numbers in a range
Reuse the same helper: loop candidate values from 1 to 50 and print each perfect number you meet. Only 6 and 28 appear in that window.
<?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";
?>Explanation
for (int i = 1; i <= num / 2; ++i) { if (num % i == 0) sum += i; }Inner logic. Same divisor walk as Example 1; return sum == num is the perfect test.
for ($i = 1; $i <= 50; $i++) { if (isPerfectNumber($i)) echo $i . " "; }Outer loop. Try each integer in the interval. Swap 50 for any upper bound you need for homework or extensions.
Notes
Faster divisor scan. You can stop at √n and add divisor pairs instead of walking all the way to n/2. Handle square roots carefully so you do not double-count.
Overflow. For large n, the divisor sum can exceed a 32-bit int. For very large values, consider big-integer/string approaches depending on your runtime.
Rarity. Perfect numbers are sparse; brute-forcing huge ranges is slow and usually unnecessary in classroom tasks.
❓ FAQ
🔄 Input / output examples
Example 1 uses a fixed number in source. Swap it or read user input with trim(fgets(STDIN)) in PHP CLI.
| Value tested | Typical line printed (Example 1) |
|---|---|
| 28 | 28 is a perfect number. |
| 10 | 10 is not a perfect number. |
| 6 | 6 is a perfect number. |
| 12 | 12 is not a perfect number. |
Edge cases
These details keep your answer precise in labs and interviews.
n = 1No proper divisors
The sum stays 0, which does not equal 1, so 1 is not perfect. If your assignment requires special wording, mention this explicitly.
Always deficient
A prime p > 1 has only 1 as a proper divisor, so s(p) = 1 < p. No prime is perfect.
nProper divisors only
If you accidentally include n in the sum, almost every number would look “too heavy” and the test breaks. The loop to n/2 avoids that mistake.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Test one n (loop to n/2) | O(n) | O(1) |
Test one n (√n pairing) | O(√n) | O(1) |
Scan 1 .. U with naive test each i | O(U2) | O(1) |
Summary
- Definition:
nis perfect when the sum of its proper divisors equalsn(same asσ(n) = 2n). - Code pattern:
sumeverything withn % i == 0forifrom1ton/2, then comparesumton. - Remember: treat
n = 1separately, never addninto the divisor sum, and upgrade to wider integers if sums might overflow.
The 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.
9 people found this page helpful
