Check Abundant Number in JavaScript

Beginner
⏱️ 14 min read
📚 Updated: Aug 2026
🎯 2 Code Examples
🚀 Live Preview
Number theory

What You’ll Learn

Abundant numbers are a classic interview warm-up: proper divisors, sum comparisons, and optional O(√n) speedups. This tutorial covers the definition, two JavaScript approaches with Try it Yourself editors, a live preview, algorithm steps, worked examples, edge cases, and complexity.

Definition

s(n) > n

Sum of proper divisors strictly exceeds n — smallest is 12.

σ and s

All vs proper

s(n) = σ(n) − n; abundance is also σ(n) > 2n.

Sqrt Pairs

O(√n)

Walk to √n and add divisor pairs — interview-ready fast path.

Naive Scan

1 .. n/2

Simple loop matching the definition literally.

Live Preview

Try n

See proper divisors, s(n), and abundant / perfect / deficient.

Try it Yourself

Editors

Each example opens an interactive editor so you can Run and edit.

Introduction

An abundant number (also called an excessive number) is a positive integer that is strictly smaller than the sum of its proper divisors — those positive divisors of n that are strictly less than n.

In JavaScript interviews you are usually asked to compute that sum, compare it to n, optionally list abundant values in a range, and discuss an O(√n) optimization.

Why it matters?

It trains divisor loops, careful edge cases (n ≤ 1, perfect squares), and the classic deficient / perfect / abundant classification used across number-theory warm-ups.

Key Highlights

s(n) > n

Only the sum of proper divisors matters.

Smallest Is 12

Proper divisors 1+2+3+4+6 = 16 > 12.

Two Patterns

Naive to n/2, or pair divisors up to √n.

Try it Editors

Run and edit each sample in the browser.

In short: sum every divisor of n that is less than n; if that sum is greater than n, the number is abundant.

📝 Problem & Approach

Given a positive integer n, decide whether it is abundant; optionally list every abundant value in a closed interval.

JavaScript
// n = 12
// Proper divisors: 1, 2, 3, 4, 6
// s(12) = 16 > 12  → abundant
//
// Classification:
//   abundant  if s(n) > n
//   perfect   if s(n) = n
//   deficient if s(n) < n

Inputs & Outputs

ItemTypeDescription
n / numbernumberPositive integer to classify (Example 1).
Range boundsnumberInclusive interval such as [1, 50] (Example 2).
Resultboolean / textAbundant or not; or a printed list of abundant values.

Minimal workflow

Pseudocode
function properDivisorSum(n):
    if n < 1:
        return invalid
    sum ← 0
    for i from 1 to floor(n / 2):
        if n mod i = 0:
            sum ← sum + i
    return sum

function isAbundant(n):
    return properDivisorSum(n) > n

Method comparison

MethodIdeaTime (single n)
Naive scanLoop i from 1 to n/2, add divisorsO(n)
Sqrt pairingLoop to √n, add i and n/i carefullyO(√n)

⚡ Quick Reference

GoalPattern
Proper-divisor testif (n % i === 0) sum += i; for i < n
Naive boundfor (let i = 1; i <= Math.floor(n / 2); i++)
Sqrt loopfor (let i = 2; i * i <= n; i++) with pair add
Abundance checkreturn sum > n;
Equivalent σ formσ(n) > 2 * n

📋 Naive vs Sqrt vs Prefixed Table

All decide abundance — pick based on clarity and scale.

Naive to n/2
O(n)

Matches the definition; great first draft

Sqrt pairs
O(√n)

Interview upgrade for larger n

Sieve table
many queries

O(N log N) setup, then O(1) lookups

Interview tip
explain both

Start simple, then mention the √n idea

Context

When This Problem Shows Up

Reach for abundant-number drills when divisor sums matter.

  1. Interview warm-ups

    Quick check of loops, modulo, and edge cases.

  2. Perfect / deficient siblings

    Same s(n) helper classifies all three families.

  3. Amicable pairs

    Proper-divisor sums power amicable-number checks next.

  4. Range filters

    List or count abundant values in [L, R].

  5. Not “has many factors”

    Abundance is only the precise test s(n) > n.

Key benefit: one small problem that covers divisors, classification, and a clean complexity upgrade path.

🔮 Live Preview

Enter a positive integer and see proper divisors, how s(n) is added, and the abundant / perfect / deficient verdict.

Try 12, 7, or 6. Cap for this widget: n ≤ 999999.

Live result
Press “Run check”.

Examples Gallery

Two complete JavaScript programs — fast single check and a range scan. Use Try it Yourself to open an interactive editor, or View Output for the sample console result.

📚 Getting Started

O(√n) divisor-pair check for n = 12.

Example 1 — Check a Single Number

Returns false for n ≤ 1, then sums proper divisors with divisor pairs up to the square root.

JavaScript
/**
 * Returns true if num is abundant (proper divisor sum > num).
 * Fast O(√num) scan using divisor pairs.
 */
function isAbundant(num) {
  if (num <= 1) {
    return false;
  }
  let sum = 1;

  for (let i = 2; i * i <= num; i++) {
    if (num % i === 0) {
      sum += i;
      const pair = Math.floor(num / i);
      if (i !== pair) {
        sum += pair;
      }
    }
  }

  return sum > num;
}

const number = 12;

if (isAbundant(number)) {
  console.log(number + " is an abundant number.");
} else {
  console.log(number + " is not an abundant number.");
}

How It Works

Start sum at 1 (every n > 1 is divisible by 1). Walk i only up to √num; when i divides num, add both i and num/i unless they are the same perfect-square root. Then compare sum > num.

📈 Practical Patterns

List abundant numbers with the simple n/2 scan.

Example 2 — Abundant Numbers in [1, 50]

Inner test loops to num/2 (easy to explain). Listing uses process.stdout.write for Node; the Try it editor prints to the page instead.

JavaScript
function isAbundant(num) {
  if (num <= 1) {
    return false;
  }
  let sum = 0;

  for (let i = 1; i <= Math.floor(num / 2); i++) {
    if (num % i === 0) {
      sum += i;
    }
  }

  return sum > num;
}

process.stdout.write("Abundant numbers between 1 and 50 are: ");

for (let i = 1; i <= 50; i++) {
  if (isAbundant(i)) {
    process.stdout.write(i + " ");
  }
}

process.stdout.write("\n");

How It Works

For each i in the range, sum every proper divisor with a loop to i/2, then print i when sum > i. Change the bounds to scan any interval you need.

🧠 How the Algorithm Decides Abundance

1

Validate n

If n ≤ 1, return not abundant (or reject invalid input).

Guard
2

Sum proper divisors

Naive scan to n/2, or add pairs while i * i ≤ n.

s(n)
3

Compare

Abundant iff sum > n (perfect if equal, deficient if less).

Test
=

Verdict ready

For n = 12, s(12) = 16 > 12 — abundant.

🔎 Worked Walkthrough — n = 12

Trace the naive proper-divisor sum for the smallest abundant number.

i12 % iActionRunning sum
10add 11
20add 23
30add 36
40add 410
52skip10
60add 616

Compare: 16 > 12 → abundant. (Using σ: divisors sum to 28, and 28 > 24 = 2×12.)

Use Cases

Where abundant-number thinking shows up beyond the interview prompt.

1. Number Classification

Deficient / perfect / abundant from one s(n) helper.

Example: 7 deficient, 6 perfect, 12 abundant.

2. Amicable Numbers

Proper-divisor sums define amicable pairs.

Example: next page in this interview chain.

3. Divisor-Sum Practice

Build fluency with modulo loops and pair tricks.

Example: upgrade O(n) to O(√n) mid-interview.

4. Range Filters

List or count abundant values in an interval.

Example: all abundant in 1–50.

5. Prefetch Tables

Sieve-style σ tables for many queries up to N.

Example: Project Euler-style batch problems.

6. Teaching Edge Cases

Shows why primes and 1 are never abundant.

Example: s(p) = 1 for prime p.

Pro Tip: open Try it Yourself under each example to tweak number or the range bounds and re-run without leaving the site.

Advantages

Why these two styles earn interview points.

  1. 1. Definition Maps to Code

    The n/2 loop is literally “add every proper divisor.”

  2. 2. Sqrt Upgrade Is Clear

    Same answer with far fewer iterations for large n.

  3. 3. Reusable Helper

    One isAbundant powers single checks and range scans.

  4. 4. Interactive Editors

    Try it Yourself pages let you experiment without a local setup.

Pro Tip: lead with the naive loop for clarity, then offer the √n pairing as the production-friendly variant.

Usage Tips

Small habits that keep abundant-number code clean in interviews.

  1. 1. Guard n ≤ 1 Early

    Return false before any divisor loop runs.

  2. 2. Never Add n Itself

    Proper divisors stop before n — loops to n/2 do this automatically.

  3. 3. Handle Perfect Squares Once

    When i === num/i, add that divisor only once in the fast loop.

  4. 4. Use Try it Yourself

    Edit examples in the browser editors to lock in the logic faster.

  5. 5. Test 12, 6, and 7

    Abundant, perfect, and deficient cover the three outcomes.

Pro Tip: dry-run n = 12 on paper (table above) before coding — it catches off-by-one divisor bounds fast.

Common Pitfalls

Mistakes that commonly break abundant-number solutions in JavaScript.

  1. 1. Including n in the Sum

    That computes σ(n), not s(n) — every n would look “too big.”

    → Stop at n/2, or subtract n if you summed all divisors.

  2. 2. Double-Counting Square Roots

    When i * i === num, adding both i and pair doubles one divisor.

    → Only add the pair when i !== pair.

  3. 3. Forgetting sum = 1 in the Fast Loop

    Starting the loop at i = 2 skips divisor 1 unless you seed it.

    → Initialize sum = 1 for n > 1.

  4. 4. Treating Composites as Abundant

    Having factors is not the same as s(n) > n.

    → Always compute and compare the proper-divisor sum.

  5. 5. Safe-Integer Overflow

    Huge sums can lose precision past Number.MAX_SAFE_INTEGER.

    → Use BigInt for very large n in production.

Edge Cases

Check these inputs before calling the solution done.

n = 1

Not abundant

s(1) = 0 by convention here; return false early.

Primes

s(p) = 1

Always deficient — never abundant.

Perfect

n = 6

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

Squares

Sqrt pairing

Add the square-root divisor only once.

Non-positive

n ≤ 0

Reject or treat as not abundant — modulo behaves badly.

Precision

Huge n

Watch Number.MAX_SAFE_INTEGER; prefer BigInt if needed.

🔄 Sample Values

Known results for the single-number script.

Input nTypical line printed
1212 is an abundant number.
77 is not an abundant number.
11 is not an abundant number.
1818 is an abundant number.

🎯 Practice Problems

Try these variations — use the Try it editors as a starting point.

1. Read n from prompt

  • Adapt Example 1’s Try it page to take user input
  • Validate n ≥ 1

2. Classify three ways

  • Return abundant / perfect / deficient
  • Reuse one proper-divisor sum helper

3. Count in a range

  • How many abundant numbers in [1, 1000]?
  • Prefer the √n test for speed

4. Prefixed σ table

  • Build s(k) for all k ≤ N once
  • Answer abundance queries in O(1)

Notes

  • Smallest abundant. 12 is the first; memorize 1+2+3+4+6 = 16.
  • s(n) > nσ(n) > 2n — both forms are fine on a whiteboard.
  • Guard n ≤ 1; avoid double-counting at perfect squares in the fast loop.
  • Try it Yourself editors mirror each example — use them to experiment safely.

Quick Takeaway: sum proper divisors; if the sum exceeds n, it is abundant — know both the n/2 and √n implementations.

⏱️ Time and Space Complexity

ApproachTime (single n)Extra space
Naive: loop 1 .. n/2O(n)O(1)
Sqrt pairingO(√n)O(1)
Print all in [1, U] (naive each i)O(U²) worst caseO(1)
Print all in [1, U] (sqrt each i)O(U3/2)O(1)

Both scripts on this page use only a few locals — auxiliary space is constant aside from the runtime.

Wrap Up

🎉 Conclusion

Abundant numbers are a small divisor-sum exercise with clear interview payoff: proper divisors, classification, and an optional √n speedup. Master both the naive and pairing methods, and use the Try it Yourself editors to cement the logic.

Practice the two examples above, then continue to amicable numbers for pairs linked by proper-divisor sums.

Sum proper divisors; if s(n) > n, it is abundant — guard n ≤ 1, and prefer √n pairing for large inputs.

💡 Best Practices

✅ Do

  • State s(n) > n (or σ(n) > 2n) before coding
  • Guard n ≤ 1 and avoid double-counting square roots
  • Know both O(n) and O(√n) approaches
  • Use Try it Yourself to verify edits quickly
  • Test 12, 6, and a prime

❌ Don’t

  • Include n in the proper-divisor sum
  • Confuse “composite” with “abundant”
  • Skip the perfect-square pair check in the fast loop
  • Forget that 1 and primes are never abundant
  • Ship only a range scan without a clear single-n helper

Key Takeaways

Knowledge Unlocked

Five things to remember about abundant numbers in JavaScript

Classify them the interview-friendly way.

5
Core concepts
12 02

Smallest

First abundant is 12

Fact
03

Fast path

Pair divisors to √n

Code
n/2 04

Naive

Scan 1 .. n/2

Code
05

Try it

Editors for each sample

Practice

❓ Frequently Asked Questions

A positive integer n is abundant if the sum of its proper divisors (divisors d with 1 ≤ d < n) is strictly greater than n. The smallest abundant number is 12.
For a perfect number, the sum of proper divisors equals n (example: 6 = 1+2+3). For abundant numbers, that sum exceeds n.
Every integer n > 1 has 1 as a divisor. The loop starts at i = 2, so sum begins at 1 to include that divisor without a separate iteration.
No. The sum of proper divisors of 1 is 0 in the usual convention, and 0 is not greater than 1.
No. A prime p > 1 has only 1 as a proper divisor, so the sum is 1, which is never greater than p.
Be ready to explain both the O(√n) pairing method and the simple O(n) scan up to n/2. The sqrt version is preferred when n can be large.
σ(n) sums every positive divisor including n itself. s(n) is the sum of proper divisors only, so s(n) = σ(n) − n. Abundance is s(n) > n, equivalently σ(n) > 2n.
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit number or the range and Run.

Did you Know? 🔊

The ancient Greeks classified numbers as deficient, perfect, or abundant based on whether the sum of proper divisors was less than, equal to, or greater than the number. 6 is perfect (1+2+3 = 6); 12 is the smallest abundant number.

Continue to Amicable Number

Learn how proper-divisor sums define amicable pairs in JavaScript.

Amicable 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