Check Abundant Number in JavaScript

Beginner
⏱️ 14 min read
📚 Updated: May 2026
🎯 2 Code examples + Try it
Number theory

What you’ll learn

  • The definition of an abundant number and how it differs from perfect and deficient numbers.
  • A precise mathematical formulation using the sum-of-divisors function and proper divisors.
  • A clear algorithm, pseudocode, and two complete JavaScript examples (single check and range scan), plus Try it Yourself links.
  • Time and space complexity, common edge cases, and practical optimizations for interviews.

Overview

This walkthrough is a compact interview-style answer: given a positive integer n, determine whether it is abundant, then optionally print every abundant value in a closed interval such as [1, 50].

Two implementations

A naive loop to n/2 for clarity, plus an O(√n) divisor-pair version you can cite for scale.

Live preview

An in-page check mirrors the proper-divisor sum so you can sanity-test values before running the scripts.

Tables & rigor

I/O examples, edge cases, and time/space notes round out the usual follow-up questions.

Prerequisites

You should already be comfortable with basic JavaScript (functions, loops, and %) before following the examples below.

  • Comfortable running JavaScript in Node.js or the browser console, and using function declarations (or arrow functions) plus console.log.
  • for loops, integer arithmetic, and the modulo operator % to test divisibility.
  • Basic idea of divisors: i divides n when n % i == 0.

What is an abundant number?

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.

Write s(n) for that sum of proper divisors. Greek number theory classifies n by comparing s(n) to n:

Abundant s(n) > n
Perfect s(n) = n
Deficient s(n) < n

Mathematical definition

To decide whether n is abundant we only need one quantity: the sum of its proper divisors. Number theory books usually write that sum as s(n). They also write σ(n) for the sum of every divisor, including n itself. The two symbols are linked by one short formula.

All divisors
σ(n)

Add every positive integer that divides n, including n. Example: divisors of 12 are 1, 2, 3, 4, 6, 12.

σ(n) = sum of all d with d | n
Proper divisors only
s(n)

Same sum as above, but drop the term n. Those remaining divisors are exactly the proper divisors.

s(n) = σ(n) − n

So if you already computed σ(n), you get s(n) by subtracting n once. In code you often skip σ entirely and add only proper divisors; the math is just naming the same idea two ways.

Formal condition (two equivalent forms)

A positive integer n is abundant if and only if s(n) > n. The same condition is often written as σ(n) > 2n because σ(n) = s(n) + n, so s(n) > ns(n) + n > 2nσ(n) > 2n.

Memory aid:σ counts all pieces including n; s is the ‘strict’ sum without the whole number.” Abundance only cares whether s(n) overshoots n.

Worked example: n = 12
  1. List all positive divisors of 12: 1, 2, 3, 4, 6, 12.
  2. Sum them to get σ(12): 1 + 2 + 3 + 4 + 6 + 12 = 28.
  3. Subtract 12 to get s(12) (proper divisors only): s(12) = 28 − 12 = 16.
  4. Compare: 16 > 12, so 12 is abundant. (Using the other test: σ(12) = 28 > 24 = 2×12, same conclusion.)

Intuition and examples

Picture n as one whole number. Its proper divisors are all the smaller “pieces” that divide it evenly (we never count n itself in that list).

Add those pieces: that total is s(n). If s(n) is still larger than n, the pieces weigh more than the whole—that is exactly what “abundant” means. If the total equals n, the number is perfect. If it is smaller, the number is deficient.

Each card below lists the proper divisors, their sum s(n), and a one-line verdict so you can see the comparison at a glance.

6 Perfect
Proper divisors
1, 2, 3
Sum s(n)
1 + 2 + 3 = 6
Verdict
s(6) = 6 — equal to n, so 6 is perfect.
12 Abundant
Proper divisors
1, 2, 3, 4, 6
Sum s(n)
1 + 2 + 3 + 4 + 6 = 16
Verdict
16 > 12 — proper divisors sum to more than n, so 12 is abundant (the smallest abundant number).
18 Abundant
Proper divisors
1, 2, 3, 6, 9
Sum s(n)
1 + 2 + 3 + 6 + 9 = 21
Verdict
21 > 18 — again s(n) > n, so 18 is abundant.
7 Deficient
Proper divisors
1 only (7 is prime)
Sum s(n)
1
Verdict
1 < 7 — every prime p has s(p) = 1, so primes are always deficient.

Takeaway: abundance is not about “having many divisors” in a vague sense—it is the precise check s(n) > n. The smallest abundant number is 12.

Live preview

Use this mini calculator to see the same rule the JavaScript examples use: add every proper divisor d (so 1 ≤ d ≤ n/2 and n % d == 0), call that sum s(n), then compare s(n) to n. The Live result box prints those divisors, shows the addition 1 + 2 + … that produces s(n), then the comparison and verdict (for large n with many divisors, the list may be omitted but the sum is still shown). No server round-trip—it runs in your browser, so keep n modest if you want instant feedback.

  1. Type a positive integer n (try 12, 7, or 6).
  2. Press Run check (or Enter).
  3. Read the lines below: proper divisors, the sum written out as an addition, s(n), the comparison to n, and whether the number is abundant, perfect, or deficient.

Use whole numbers n ≥ 1. For this page, n above 999 999 is blocked so the tab stays responsive.

Live result
Press “Run check” to see proper divisors, how s(n) is added up, and the verdict.

Algorithm

Goal: decide whether a given positive integer n is abundant. To do that, compute the sum of its proper divisors (call it sum or s(n) in math), then check if that total is greater than n.

Single value n (naive scan)

Validate n

If n < 1, stop or report invalid input. Abundant numbers are defined for ordinary counting numbers 1, 2, 3, …; in code you usually return “not abundant” for n ≤ 1.

Start an accumulator

Set sum = 0. You will add every proper divisor you find into sum.

Try every candidate divisor

Loop i from 1 up to n / 2 (integer division). Any proper divisor of n is at most n/2 (the number n itself is excluded), so this range hits every proper divisor exactly once.

Add hits to sum

Inside the loop, if n % i == 0, then i divides n evenly—add i to sum.

Decide abundant or not

After the loop finishes, n is abundant if and only if sum > n. If sum == n it is perfect; if sum < n it is deficient.

📜 Pseudocode

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
1

Check a single number (program with explanation)

This version returns false for n ≤ 1, then sums proper divisors in O(√n) time by enumerating 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.");
}
Try it Yourself

Explanation

This isAbundant version is the fast one: it still sums proper divisors, but it walks only up to √num and picks up divisor pairs. Below, each box is one idea from the function body.

if (num <= 1) return false;

Guard the edge cases. For num === 1 there is nothing to add except the empty sum, so it is never abundant. Non-positive values are not handled as abundant numbers here.

let sum = 1;

Count divisor 1 up front. Every integer > 1 is divisible by 1. The loop below starts at i = 2, so sum begins at 1 so that small divisor is not forgotten.

for (let i = 2; i * i <= num; i++)

Only test up to √num. If i divides num, then num / i is another divisor on the opposite side of the square root. That is why you never need i past √num to see every divisor pair.

if (i !== pair)

Avoid double-counting at a perfect square. When num is something like 36, i and num / i can be the same; add that divisor only once.

return sum > num;

Apply the definition. If the accumulated proper-divisor sum is strictly greater than num, return true (abundant); otherwise return false.

2

Abundant numbers in a range (program with explanation)

The inner test uses a straightforward loop to num/2. That is O(n) per value but is very easy to read in an interview and matches the pseudocode literally. The listing uses process.stdout.write for Node.js; in a browser, build a string and console.log it once, or print inside the loop.

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");
Try it Yourself

Explanation

This program has two layers: an inner function that matches the simple “add every proper divisor” idea, and an outer loop that reuses it for many values.

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

Inner loop = list every proper divisor. Any proper divisor of num is at most num/2, so scanning 1 through num/2 hits each one. Whenever num % i == 0, add i to sum. After the loop, sum is exactly s(num).

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

Outer loop = try each candidate in the range. For each i, call isAbundant(i). If it returns true, append i to the output (here via process.stdout.write in Node; in a browser you might build a string or use console.log once per hit). Change 50 (and the starting 1) to whatever interval you need.

Optimization

Single n. On large inputs, walk divisors only up to √n and use pairs instead of scanning to n/2—about O(√n) work per check instead of O(n).

Many queries. If every k is at most N, build a table of proper-divisor sums once (sieve-style), then each “is k abundant?” is an array lookup: O(1) per query after roughly O(N log N) setup.

Wide ranges. Huge intervals or overlapping subproblems benefit from caching or batching so you do not recompute the same divisor sums.

Interview: ship the clear n/2 solution first; bring up the √n idea only if they ask about time limits or very large n.

❓ FAQ

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.

🔄 Input / output examples

Assume the single-number script logs one line. For interactive versions you might read n from process.argv, a prompt, or an HTML input instead of a fixed literal.

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.

For the range program with bounds 1 and 50, output is:

Console
Abundant numbers between 1 and 50 are: 12 18 20 24 30 36 40 42 48

Edge cases and pitfalls

These are the slip-ups reviewers spot most often. None of them change the math of abundance—they are about writing the loop safely and reading the definition correctly.

Input

n ≤ 1 and non-positive values

Treat n ≤ 1 as not abundant. Return early so you never run a divisor loop with 0 or negative n, where modulo and bounds behave badly or waste time.

Sqrt loop

Perfect squares

When i * i == num, the divisor i appears only once as a factor pair with itself. Add i once; do not add the same value twice from i and num / i.

Naive loop

Do not include n in the sum

Proper divisors are strictly less than n. A loop to num / 2 stops before n, so you never accidentally add the whole number. If you loop to num, you must skip num explicitly.

Overflow

Sums beyond safe integers

JavaScript numbers are IEEE-754 doubles; above Number.MAX_SAFE_INTEGER (253−1), sums may lose precision. For huge n in production, use BigInt for the running sum or a bigint-aware library.

Concept

Abundant ≠ “composite”

Many abundant numbers are composite, but the definition is only about whether the sum of proper divisors exceeds n. Do not replace that test with “has a factor” or other shortcuts.

⏱️ Time and space complexity

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

Both scripts on this page use only a few local variables: auxiliary space is constant aside from the runtime and call stack.

Summary

  • Definition: n is abundant when the sum of its proper divisors is greater than n. Same test as σ(n) > 2n because σ(n) = s(n) + n.
  • Code: be comfortable with the naive loop to n/2 and the √n pairing version—interviewers often ask you to start simple, then optimize.
  • Watch-outs: guard n ≤ 1, avoid double-counting at perfect squares in the fast loop, and use BigInt or careful checks when n or s(n) approaches Number.MAX_SAFE_INTEGER.
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.

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