PHP Perfect Number Program
Definition
What Is a Perfect Number?
A positive integer is perfect when it equals the sum of its proper divisors — all positive divisors except the number itself.
n is perfect ⇔ sum(proper divisors of n) == n
Example: 28
Proper divisors: 1, 2, 4, 7, 14
Sum = 1 + 2 + 4 + 7 + 14 = 28 ← perfect
| n | Proper-divisor sum | Result |
|---|
6 | 1 + 2 + 3 = 6 | Perfect |
28 | 1 + 2 + 4 + 7 + 14 = 28 | Perfect |
12 | 1 + 2 + 3 + 4 + 6 = 16 | Abundant (not perfect) |
10 | 1 + 2 + 5 = 8 | Deficient (not perfect) |
Approach
How to Check a Perfect Number in PHP
Four clear steps — then pick a simple scan or an O(√n) divisor pairing.
- Require
n > 1 (1 is not perfect). - Find every divisor of
n that is strictly less than n. - Add those divisors together.
- Compare the sum with
n.
| Method | Idea | Time | Best for |
|---|
| Simple loop | Test 1 … n-1 | O(n) | Interviews, first explanation |
| √n pairing | Add i and n/i when they divide | O(√n) | Faster checks, larger n |
Pseudocode
function IsPerfect(n):
if n <= 1: return false
sum = 0
for i from 1 to n - 1:
if n mod i == 0:
sum = sum + i
return sum == n
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Proper divisors | Positive divisors excluding n |
| Test | sum == n |
| 1 | Not perfect |
| Class family | Deficient / perfect / abundant |
| First four | 6, 28, 496, 8128 |
| √n tip | Add both i and n/i; never add n |
Try it
Live Preview
Enter a positive integer to see its proper divisors, their sum, and the verdict.
Trace
Worked Walkthrough — n = 28
Collect every divisor less than 28, then compare the sum with 28.
Divisors of 28 less than 28:
1 divides 28
2 divides 28
4 divides 28
7 divides 28
14 divides 28
Sum = 1 + 2 + 4 + 7 + 14 = 28
28 == 28 → perfect
(√n view: pairs (1,28), (2,14), (4,7)
add 1+2+4+7+14 — skip 28 itself)
Code
PHP Programs
Start with the simple loop so every proper divisor is obvious. Then use the √n method when you want the same answer faster.
Example 1 — Simple Loop
Sum every divisor from 1 to n - 1, then compare with n.
<?php
function isPerfect(int $n): bool
{
if ($n <= 1) {
return false;
}
$sum = 0;
for ($i = 1; $i < $n; $i++) {
if ($n % $i === 0) {
$sum += $i;
}
}
return $sum === $n;
}
$number = 28;
echo $number . (isPerfect($number) ? " is perfect\n" : " is not perfect\n");
How It Works
- Return
false for n <= 1 — 1 is not perfect. - Loop
$i from 1 to n - 1 so n itself is never added. - When
$n % $i === 0, $i is a proper divisor — add it to $sum. - After the loop, compare
$sum === $n. - For
28, the sum is 28, so the function returns true.
Example 2 — √n Pairing
Walk only up to √n and add both halves of each divisor pair, skipping n.
<?php
function isPerfect(int $n): bool
{
if ($n <= 1) {
return false;
}
$sum = 1; // 1 is always a proper divisor for n > 1
for ($i = 2; $i * $i <= $n; $i++) {
if ($n % $i === 0) {
$sum += $i;
$pair = intdiv($n, $i);
if ($pair !== $i && $pair !== $n) {
$sum += $pair;
}
}
}
return $sum === $n;
}
echo isPerfect(6) ? "true\n" : "false\n"; // true
echo isPerfect(28) ? "true\n" : "false\n"; // true
echo isPerfect(12) ? "true\n" : "false\n"; // false
echo isPerfect(10) ? "true\n" : "false\n"; // false
How It Works
- Seed
$sum = 1 because every n > 1 is divisible by 1. - Loop while
$i * $i <= $n so you only scan up to √n. - When
$i divides $n, add $i and usually its pair $n / $i. - Skip the pair when it equals
$i (perfect square) or equals $n (would not be proper). - Same answers as Example 1, in O(√n) time.
Edge Cases & Pitfalls
Check these before calling the solution done.
1Not perfect
Treat n <= 1 as false before summing.
Include n?Exclude itself
Proper divisors never include n. Adding it breaks the definition.
√n pairDo not double-count
When i == n/i, add the square-root divisor once.
NegativesPositive only
Perfect numbers are defined for positive integers.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Simple loop | O(n) | O(1) |
| √n pairing | O(√n) | O(1) |
Both answers are the same. Prefer √n when n grows beyond small demo values.
Remember
Key Takeaways
Definition: sum of proper divisors equals n.
Exclude: never add n itself to the sum.
Family: deficient / perfect / abundant by comparing sum and n.
Test set: 6 → yes; 28 → yes; 12 → no; 10 → no.
One line: add every positive divisor of n except n, then check equality.
Frequently Asked Questions
A positive integer equal to the sum of its proper divisors (positive divisors excluding itself). Example: 6 = 1 + 2 + 3.
No. The sum of proper divisors of 1 is usually taken as 0, which is not equal to 1.
Compare the proper-divisor sum s with n: s < n deficient, s == n perfect, s > n abundant.
6, 28, 496, and 8128 are the first four perfect numbers.
A loop to n-1 is O(n). Pairing divisors up to √n is O(√n). Both use O(1) extra space.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful