PHP Abundant Number Program
Definition
What Is an Abundant Number?
A positive integer n is abundant when the sum of its proper divisors (positive divisors less than n) is greater than n.
n is abundant ↔ sum(proper divisors of n) > n
Example: 12
Proper divisors: 1, 2, 3, 4, 6
Sum = 16 > 12 → abundant
Smallest abundant number: 12
Perfect contrast: 28 (1+2+4+7+14 = 28)
| n | Proper-divisor sum | Class |
|---|
12 | 16 | Abundant |
18 | 21 | Abundant |
28 | 28 | Perfect |
10 | 8 | Deficient |
Approach
How to Check an Abundant Number in PHP
Four clear steps — then pick a simple loop or the √n pair method.
- Reject
n <= 1 (not abundant). - Sum every proper divisor of
n. - Compare that sum with
n. - If
sum > n, the number is abundant.
| Method | Idea | Time | Best for |
|---|
Loop to n/2 | Add every divisor < n | O(n) | Interviews, clearest logic |
| Divisor pairs to √n | Add i and n/i carefully | O(√n) | Faster checks |
| Range scan | Reuse the helper for each value | O(N²) naive | Listing abundant numbers |
Pseudocode
function IsAbundant(n):
if n <= 1:
return false
sum = 0
for i from 1 to n / 2:
if n mod i == 0:
sum = sum + i
return sum > n
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Test | sum(proper divisors) > n |
| Proper divisors | Positive divisors of n except n |
| Basic loop | i = 1 .. n/2 |
| Smallest | 12 |
| Perfect | sum equals n (e.g. 28) |
| Deficient | sum less than n (e.g. 10) |
| 1..50 | 12 18 20 24 30 36 40 42 48 |
Try it
Live Preview
Enter a positive integer to see its proper divisors, their sum, and the verdict.
Trace
Worked Walkthrough — n = 12
List proper divisors, add them, then compare with 12.
Candidates i = 1 .. 12/2 = 6
1 | 12 → add 1
2 | 12 → add 2
3 | 12 → add 3
4 | 12 → add 4
5 ✗
6 | 12 → add 6
Sum = 1+2+3+4+6 = 16
16 > 12 → abundant
Code
PHP Programs
Start with the clear loop to n/2. Then list a range, and finish with the faster √n pair method.
Example 1 — Check One Number
Sum proper divisors with a loop to num / 2, then compare with num.
<?php
function isAbundant($num) {
if ($num <= 1) {
return false;
}
$sum = 0;
for ($i = 1; $i <= intdiv($num, 2); $i++) {
if ($num % $i === 0) {
$sum += $i;
}
}
return $sum > $num;
}
$number = 12;
if (isAbundant($number)) {
echo $number . " is an abundant number.\n";
} else {
echo $number . " is not an abundant number.\n";
}
?>
12 is an abundant number.
How It Works
- Return
false immediately when $num <= 1. - Loop
$i from 1 to intdiv($num, 2) — no proper divisor is larger. - Add every
$i that divides $num. - For 12 the sum is 16, so
$sum > $num is true. - Return that comparison as a boolean.
Example 2 — Abundant Numbers from 1 to 50
Reuse the helper and print every match on one line.
<?php
function isAbundant($num) {
if ($num <= 1) {
return false;
}
$sum = 0;
for ($i = 1; $i <= intdiv($num, 2); $i++) {
if ($num % $i === 0) {
$sum += $i;
}
}
return $sum > $num;
}
echo "Abundant numbers between 1 and 50 are: ";
for ($i = 1; $i <= 50; $i++) {
if (isAbundant($i)) {
echo $i . " ";
}
}
echo "\n";
?>
Abundant numbers between 1 and 50 are: 12 18 20 24 30 36 40 42 48
How It Works
- Outer loop visits every integer from 1 to 50.
- Each value is tested with the same
isAbundant helper. - Matches are printed with a trailing space.
- The first hit is 12 — a useful self-check.
- Keeping the helper pure makes range listing easy.
Example 3 — Sum Divisor Pairs up to √n
For each factor $i, also add $num / $i, but never add $num itself.
<?php
function isAbundant($num) {
if ($num <= 1) {
return false;
}
$sum = 1; // 1 is always a proper divisor for num > 1
for ($i = 2; $i * $i <= $num; $i++) {
if ($num % $i === 0) {
$sum += $i;
$partner = intdiv($num, $i);
if ($i !== $partner) {
$sum += $partner;
}
}
}
return $sum > $num;
}
echo isAbundant(12) ? "true\n" : "false\n"; // abundant
echo isAbundant(28) ? "true\n" : "false\n"; // perfect
?>
How It Works
- Start
$sum at 1 because every $num > 1 has divisor 1. - Loop while
$i * $i <= $num so you only walk up to √n. - When
$i divides $num, add $i and also the partner if they differ. - Because the loop starts at 2, the partner is never
$num itself. - 12 prints
true (abundant); 28 prints false (perfect, not abundant).
Edge Cases & Pitfalls
Check these before calling the solution done.
1Not abundant
No proper divisors; sum is 0. Guard with $n <= 1.
PrimesAlways deficient
A prime has only proper divisor 1, so it cannot be abundant.
PerfectSum equals n
28 is perfect, not abundant. Use strict >, not >=.
Sqrt methodDo not add n
When adding pairs, skip the number itself — proper divisors exclude $n.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Loop to n/2 | O(n) | O(1) |
| Divisor pairs to √n | O(√n) | O(1) |
| Range 1..N (basic) | O(N²) | O(1) |
In interviews, explain the simple loop first, then mention the √n optimization.
Remember
Key Takeaways
Definition: proper-divisor sum is strictly greater than n.
Classic: 12 → 1+2+3+4+6 = 16 > 12.
Two loops: basic 1..n/2, or optimized pairs to √n.
Test set: 12 and 18 abundant; 28 perfect; 10 deficient; 1 no.
One line: if the proper divisors add up to more than the number, it is abundant.
Frequently Asked Questions
A positive integer n is abundant when the sum of its proper divisors is greater than n. Example: 12 is abundant because 1+2+3+4+6 = 16, and 16 > 12.
Proper divisors are positive divisors smaller than the number itself. For 12 they are 1, 2, 3, 4, and 6.
Perfect: divisor sum equals n. Deficient: sum is less than n. Abundant: sum is greater than n.
No. 1 has no positive proper divisors, so the sum is 0, which is not greater than 1.
The basic loop to n/2 is O(n). Summing divisor pairs 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.
16 people found this page helpful