PHP Abundant Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

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.

Remember
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)
nProper-divisor sumClass
1216Abundant
1821Abundant
2828Perfect
108Deficient

How to Check an Abundant Number in PHP

Four clear steps — then pick a simple loop or the √n pair method.

  1. Reject n <= 1 (not abundant).
  2. Sum every proper divisor of n.
  3. Compare that sum with n.
  4. If sum > n, the number is abundant.
MethodIdeaTimeBest for
Loop to n/2Add every divisor < nO(n)Interviews, clearest logic
Divisor pairs to √nAdd i and n/i carefullyO(√n)Faster checks
Range scanReuse the helper for each valueO(N²) naiveListing abundant numbers

Pseudocode

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

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Testsum(proper divisors) > n
Proper divisorsPositive divisors of n except n
Basic loopi = 1 .. n/2
Smallest12
Perfectsum equals n (e.g. 28)
Deficientsum less than n (e.g. 10)
1..5012 18 20 24 30 36 40 42 48

Live Preview

Enter a positive integer to see its proper divisors, their sum, and the verdict.

Positive whole numbers. Tap a chip or type a value.

Result
n = 12
Proper divisors: 1, 2, 3, 4, 6
Sum = 16
Verdict: ABUNDANT (16 > 12)

Worked Walkthrough — n = 12

List proper divisors, add them, then compare with 12.

Calculation
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

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
<?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";
}
?>

How It Works

  1. Return false immediately when $num <= 1.
  2. Loop $i from 1 to intdiv($num, 2) — no proper divisor is larger.
  3. Add every $i that divides $num.
  4. For 12 the sum is 16, so $sum > $num is true.
  5. 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
<?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";
?>

How It Works

  1. Outer loop visits every integer from 1 to 50.
  2. Each value is tested with the same isAbundant helper.
  3. Matches are printed with a trailing space.
  4. The first hit is 12 — a useful self-check.
  5. 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
<?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

  1. Start $sum at 1 because every $num > 1 has divisor 1.
  2. Loop while $i * $i <= $num so you only walk up to √n.
  3. When $i divides $num, add $i and also the partner if they differ.
  4. Because the loop starts at 2, the partner is never $num itself.
  5. 12 prints true (abundant); 28 prints false (perfect, not abundant).

Edge Cases & Pitfalls

Check these before calling the solution done.

1

Not abundant

No proper divisors; sum is 0. Guard with $n <= 1.

Primes

Always deficient

A prime has only proper divisor 1, so it cannot be abundant.

Perfect

Sum equals n

28 is perfect, not abundant. Use strict >, not >=.

Sqrt method

Do not add n

When adding pairs, skip the number itself — proper divisors exclude $n.

Time and Space Complexity

ProgramTimeExtra space
Loop to n/2O(n)O(1)
Divisor pairs to √nO(√n)O(1)
Range 1..N (basic)O(N²)O(1)

In interviews, explain the simple loop first, then mention the √n optimization.

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.

Next: Amicable Number

Check pairs where each number is the other’s divisor-sum.

Amicable number tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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