Check Abundant Number in PHP

Beginner
⏱️ 12 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Number Theory

What You’ll Learn

An abundant number has a proper-divisor sum greater than itself. For example, 12 is abundant because 1 + 2 + 3 + 4 + 6 = 16 and 16 > 12. This tutorial covers the definition, deficient/perfect neighbors, a live checker, worked PHP examples, edge cases, and complexity.

Definition

s(n) > n

Proper divisor sum exceeds the number.

Proper Divisors

Exclude n

Positive divisors of n except n itself.

Scan to n/2

Simple loop

No proper divisor exceeds intdiv(n, 2).

Classify

s(n) vs n

Deficient, perfect, or abundant.

Live Preview

Try 12 / 18

See divisors, sum, and verdict live.

Smallest

12 is first

12 is the smallest abundant number.

Introduction

An abundant number is a positive integer whose proper divisors add up to more than the number itself. Using s(n) for that sum: abundant means s(n) > n, perfect means s(n) = n, and deficient means s(n) < n.

In interviews you usually write a helper that sums proper divisors, then compare with n. One is never abundant: by convention s(1) = 0.

Why it matters?

It is a classic divisor-sum interview problem that connects loops, modulo, and number-theory vocabulary — and pairs naturally with perfect and amicable checks.

Key Highlights

s(n) > n

Proper divisors sum past n.

Exclude n

Do not add the number itself.

1 Is Not

s(1) = 0 by convention.

Class Neighbors

Deficient / perfect too.

In short: sum divisors from 1 to intdiv(n, 2); abundant when that sum is greater than n.

📝 Problem & Approach

Given a positive integer n, decide whether it is abundant by comparing the sum of its proper divisors with n.

php
# 12: 1+2+3+4+6 = 16       -> abundant
# 18: 1+2+3+6+9 = 21       -> abundant
# 6:  1 + 2 + 3 = 6        -> perfect
# 7:  1 = 1                -> deficient

Inputs & Outputs

ItemTypeDescription
$n / $numintPositive integer to classify.
Returnbooltrue when proper divisor sum is greater than n.
ClassificationtextDeficient, perfect, or abundant.

Minimal workflow

Pseudocode
function isAbundant(n):
    if n <= 1:
        return false

    divSum = 0
    for i from 1 to floor(n / 2):
        if n mod i == 0:
            divSum = divSum + i

    return divSum > n

Method comparison

MethodIdeaNotes
Scan to n / 2Add every proper divisorInterview default — clearest
Pair to sqrt(n)Add i and n / iFaster; careful with squares
Classify s(n) vs nOne sum, three labelsDeficient / perfect / abundant

⚡ Quick Reference

GoalPattern
Loop boundfor ($i = 1; $i <= intdiv($n, 2); $i++)
Is divisor?if ($n % $i === 0) { $divSum += $i; }
Abundant?return $divSum > $n;
Perfect$divSum === $n
Deficient$divSum < $n
Guard 1if ($n <= 1) return false;

📋 Abundant vs Perfect vs Deficient

Same divisor sum — different comparisons to n.

Abundant
s(n) > n

This page — e.g. 12, 18

Perfect
s(n) = n

e.g. 6, 28 — related topic

Deficient
s(n) < n

Most numbers, including primes

Interview tip
exclude n

Proper divisors only

Context

When This Problem Shows Up

Reach for a proper-divisor sum whenever you need to classify abundance.

  1. Number-theory warm-ups

    Divisors, sums, and classification.

  2. Modulo practice

    Find all divisors with %.

  3. Bridge to perfect

    Same sum, different comparison.

  4. Range hunting

    List abundant values in a band (e.g. 1..50).

  5. Not for huge scans

    Naive O(n) per check gets costly fast.

Key benefit: one clear loop that teaches proper divisors, classification, and the famous example 12.

🔮 Live Preview

Enter a positive integer and inspect proper divisors, sum, and verdict.

Use whole numbers n >= 1.

Live result
Press “Run check” to see the result.

Examples Gallery

Three complete PHP programs — check 12, list abundant numbers from 1 to 50, and classify deficient/perfect/abundant. Click View Output to reveal sample console results.

📚 Getting Started

A reusable helper and the classic example 12.

Example 1 — Check One Number

Test a fixed value (12) with a helper function.

php
<?php
function isAbundant(int $num): bool
{
    if ($num <= 1) {
        return false;
    }

    $divSum = 0;
    for ($i = 1; $i <= intdiv($num, 2); $i++) {
        if ($num % $i === 0) {
            $divSum += $i;
        }
    }

    return $divSum > $num;
}

$number = 12;

if (isAbundant($number)) {
    echo $number . " is an abundant number.";
} else {
    echo $number . " is not an abundant number.";
}
?>

How It Works

The loop adds every proper divisor of 12: 1, 2, 3, 4, and 6. Their sum is 16, which is greater than 12, so the helper returns true.

⚡ Hunting in a Range

Reuse the helper to find abundant values nearby.

Example 2 — Abundant Numbers in Range 1 to 50

Print all abundant numbers in a small interval.

php
<?php
function isAbundant(int $num): bool
{
    if ($num <= 1) {
        return false;
    }

    $divSum = 0;
    for ($i = 1; $i <= intdiv($num, 2); $i++) {
        if ($num % $i === 0) {
            $divSum += $i;
        }
    }

    return $divSum > $num;
}

echo "Abundant numbers between 1 and 50 are:\n";
for ($value = 1; $value <= 50; $value++) {
    if (isAbundant($value)) {
        echo $value . " ";
    }
}
?>

How It Works

Within 1..50 the abundant values start at 12 and include several even composites. Reusing isAbundant keeps the range scan short and readable.

Example 3 — Classify Deficient / Perfect / Abundant

Reuse the same sum to label each sample number.

php
<?php
function properDivisorSum(int $n): int
{
    if ($n < 2) {
        return 0;
    }

    $total = 0;
    for ($i = 1; $i <= intdiv($n, 2); $i++) {
        if ($n % $i === 0) {
            $total += $i;
        }
    }
    return $total;
}

function classify(int $n): string
{
    $s = properDivisorSum($n);
    if ($s === $n) {
        return "perfect";
    }
    if ($s < $n) {
        return "deficient";
    }
    return "abundant";
}

foreach ([6, 10, 12, 18, 1] as $value) {
    echo $value . ": " . classify($value) . " (s=" . properDivisorSum($value) . ")\n";
}
?>

How It Works

One sum drives three labels. Abundant is the greater-than case; perfect and deficient are equality and less-than.

🧠 How the Algorithm Decides

1

Guard tiny values

If n <= 1, return false immediately.

Init
2

Scan 1 .. n/2

Add i whenever n % i === 0.

Loop
3

Compare to n

Greater means abundant; else not.

Rule
=

Return the verdict

Bool for abundant, or a class label.

🔎 Worked Walkthrough — 12

Trace the proper-divisor sum for n = 12.

i12 % iAdd?divSum
10Yes1
20Yes3
30Yes6
40Yes10
60Yes16

divSum 16 is greater than n — abundant.

Use Cases

Where abundant-number checks show up beyond the interview prompt.

1. Interview Classics

Divisor loops and comparison checks.

Example: isAbundant(12).

2. Classification Sets

Label deficient / perfect / abundant.

Example: Example 3.

3. Range Searches

Find abundant values in a band.

Example: 12..48 in 1..50.

4. Teaching Divisors

Show what “proper” excludes.

Example: do not add n.

5. Bridge to Perfect

Same sum, equality test.

Example: related topic.

6. Next: Amicable

Another divisor-sum pairing problem.

Example: related CTA.

Pro Tip: open with “proper divisors exclude n; abundant means sum > n” before coding.

Advantages

Why the n/2 scan works well for beginners and interviews.

  1. 1. Easy to Trace

    Dry-run 12 or 18 on paper and watch the sum grow past n.

  2. 2. Clear Bound

    Stopping at n / 2 avoids adding n by mistake.

  3. 3. Extends to Classes

    Same sum powers deficient/perfect labels.

  4. 4. Speeds Up Later

    You can upgrade to sqrt pairing when needed.

Pro Tip: lead with the n/2 scan; mention sqrt pairing only as an optimization aside.

Usage Tips

Small habits that keep abundant-number solutions interview-ready.

  1. 1. Exclude n Itself

    Proper divisors stop at intdiv($n, 2).

  2. 2. Guard Small n

    Treat n <= 1 as not abundant.

  3. 3. Name the Classes

    Mention deficient and perfect alongside abundant.

  4. 4. Know 12 and 18

    Use them as quick sanity checks.

  5. 5. Optimize Later

    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.

Common Pitfalls

Mistakes that commonly break abundant-number programs.

  1. 1. Including n in the Sum

    Adding the number itself doubles the definition.

    → Stop at intdiv($n, 2).

  2. 2. Calling 1 Abundant

    Thinking 1 somehow overflows its divisor sum.

    → s(1) = 0; 1 is not abundant.

  3. 3. Using >= Instead of >

    That would incorrectly count perfect numbers as abundant.

    → Abundant requires a strict greater-than.

  4. 4. Off-by-One Bound

    Stopping before intdiv($n, 2) misses a valid divisor.

    → Use $i <= intdiv($num, 2).

  5. 5. Huge Naive Scans

    Checking every n up to millions with O(n) each.

    → Use smaller ranges or faster pairing.

Edge Cases

Handle these before claiming the check is complete.

n <= 1

Not abundant

Return false; s(1) = 0 by convention.

Prime

Always deficient

Only proper divisor is 1, so sum < n.

Exclude n

Proper divisors only

Adding n itself breaks the definition.

12 / 18

Classic yes cases

Use them as sanity checks.

6

Perfect sample

s(6) = 6 — equal, not abundant.

Large n

Use sqrt optimization

Divisor-pair logic is faster than scanning to n/2.

⚖️ Facts Worth Knowing

Handy follow-ups interviewers sometimes ask.

  • Smallest. 12 is the smallest abundant number.
  • Abundance. The abundance of n is s(n) − n (positive for abundant numbers).
  • Even bias. Most small abundant numbers are even; odd abundant numbers exist but are much larger.
  • Related trio. Deficient, perfect, and abundant partition the positive integers by s(n) vs n.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Prove 12

  • List proper divisors
  • Confirm sum 16 > 12

2. Find 1..50 abundants

  • Reproduce Example 2
  • Match 12 18 20 … 48

3. Classify 6, 10, 12

  • Perfect / deficient / abundant
  • Match Example 3

4. Reject 1 and primes

  • Show s(1) = 0
  • Assert primes are never abundant

Notes

  • Definition: abundant means proper divisor sum is greater than n.
  • Loop: check divisors from 1 to n/2 with modulo.
  • Edge case: 1 and primes are never abundant.
  • You can test divisors only up to sqrt(n) and add divisor pairs. Prefer the clear O(n) scan first in interviews.

Quick Takeaway: sum proper divisors to n/2; abundant when that sum is greater than n.

⏱️ Time and Space Complexity

TaskTimeExtra space
Single check with n/2 scanO(n)O(1)
Single check with sqrt(n) pairingO(√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.

Wrap Up

🎉 Conclusion

An abundant number has a proper-divisor sum greater than itself. Loop from 1 to intdiv($n, 2), add every divisor, and check $divSum > $n — remembering that 1 and primes are never abundant.

Practice the three examples above, then continue to amicable numbers for another classic divisor-sum pairing.

s(n) > n means abundant; exclude n from the divisor sum.

💡 Best Practices

✅ Do

  • Exclude n from the sum
  • Loop to n/2 inclusive
  • Reject n <= 1 early
  • Sanity-check with 12 and 18
  • Name deficient / perfect too

❌ Don’t

  • Add n into the divisor sum
  • Call 1 or primes abundant
  • Use >= and mix in perfect numbers
  • Miss the inclusive loop bound
  • Brute-force huge ranges blindly

Key Takeaways

Knowledge Unlocked

Five things to remember about abundant numbers

Classify divisor sums the interview-friendly way.

5
Core concepts
/ 02

Bound

to n / 2

Loop
1 03

Edge

1 not abundant

Guard
<> 04

Classes

def / perfect

Neighbors
O 05

Cost

O(n) naive

Analysis

❓ 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 18, proper divisors are 1, 2, 3, 6, and 9.
No. 1 has no positive proper divisors, so the sum is 0, which is not greater than 1.
No. A prime p only has proper divisor 1, so the sum is 1 and cannot be greater than p.
If the proper divisor sum is less than n it is deficient; if equal to n it is perfect; if greater than n it is abundant.
No proper divisor of n can be larger than n/2. So checking beyond n/2 is unnecessary in the simple approach.
Scan only up to sqrt(n) and add divisor pairs, being careful not to double-count squares.
Start with the simple method (easy to understand), then mention the sqrt optimization using divisor pairs for better performance.
Yes. 1 + 2 + 3 + 6 + 9 = 21, and 21 > 18.

Did you Know? 🔊

The smallest abundant number is 12 because its proper divisors are 1, 2, 3, 4, 6 and their sum is 16, which is greater than 12.

Continue to Amicable Number

Learn how to check whether two numbers form an amicable pair in PHP.

Amicable number tutorial →

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.

9 people found this page helpful