- Proper divisors
- 1, 2, 3
- Sum s(n)
- 1 + 2 + 3 = 6
- Verdict
- s(6) = 6 — equal to
n, so 6 is perfect.
Check Abundant Number in C++
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 C++ programs (single check and range scan).
- 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 compiling C++.
Tables & rigor
I/O examples, edge cases, and time/space notes round out the usual follow-up questions.
Prerequisites
You should already be able to read and write small C++ programs before following the examples below.
- Comfortable with C++ syntax:
#include <iostream>,int main(),std::cout, andreturn 0;. forloops, integer arithmetic, and the modulo operator%to test divisibility.- Basic idea of divisors:
idividesnwhenn % 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:
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.
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 | ns(n)Same sum as above, but drop the term n. Those remaining divisors are exactly the proper divisors.
s(n) = σ(n) − nSo 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.
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) > n ↔ s(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.
n = 12- List all positive divisors of 12:
1, 2, 3, 4, 6, 12. - Sum them to get σ(12):
1 + 2 + 3 + 4 + 6 + 12 = 28. - Subtract 12 to get s(12) (proper divisors only):
s(12) = 28 − 12 = 16. - 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.
- 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).
- 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.
- Proper divisors
- 1 only (7 is prime)
- Sum s(n)
- 1
- Verdict
- 1 < 7 — every prime
phass(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 C++ programs 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.
- Type a positive integer
n(try 12, 7, or 6). - Press Run check (or Enter).
- Read the lines below: proper divisors, the sum written out as an addition,
s(n), the comparison ton, and whether the number is abundant, perfect, or deficient.
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.
List abundant numbers in a range
Pick bounds low and high. For each integer k from low to high, run the same five-step test on k. Whenever sum > k, print or store k. No change to the core test—only an outer loop wraps it.
📜 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 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.
#include <iostream>
// Returns true if num is abundant, false otherwise (num >= 1)
bool isAbundant(int num) {
if (num <= 1) {
return false;
}
int sum = 1;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
sum += i;
if (i != num / i) {
sum += num / i;
}
}
}
return sum > num;
}
int main() {
int number = 12;
if (isAbundant(number)) {
std::cout << number << " is an abundant number.\n";
} else {
std::cout << number << " is not an abundant number.\n";
}
return 0;
} 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 0;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.
int 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 (int 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 != num / i)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.
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.
#include <iostream>
bool isAbundant(int num) {
if (num <= 1) {
return false;
}
int sum = 0;
for (int i = 1; i <= num / 2; ++i) {
if (num % i == 0) {
sum += i;
}
}
return sum > num;
}
int main() {
std::cout << "Abundant numbers between 1 and 50 are: ";
for (int i = 1; i <= 50; ++i) {
if (isAbundant(i)) {
std::cout << i << " ";
}
}
std::cout << "\n";
return 0;
} 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 (int i = 1; i <= 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 (int i = 1; i <= 50; ++i) { if (isAbundant(i)) std::cout << i << " "; }Outer loop = try each candidate in the range. For each i, call isAbundant(i). If it returns true, print i. 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
🔄 Input / output examples
Assume the single-number program prints one line. For interactive versions you might use scanf("%d", &number); instead of a fixed literal.
Input n | Typical line printed |
|---|---|
| 12 | 12 is an abundant number. |
| 7 | 7 is not an abundant number. |
| 1 | 1 is not an abundant number. |
| 18 | 18 is an abundant number. |
For the range program with bounds 1 and 50, output is:
Abundant numbers between 1 and 50 are: 12 18 20 24 30 36 40 42 48 Edge cases and pitfalls
These are the mistakes graders and compilers catch most often. None of them change the math of abundance—they are about writing the loop safely and reading the definition correctly.
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.
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.
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.
Divisor sums can outgrow int
For very large n, the running sum of divisors may exceed a 32-bit signed int. Use a wider integer type (for example long long) when the problem statement allows large inputs.
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
| Approach | Time (single n) | Extra space |
|---|---|---|
Naive: loop 1 .. n/2 | O(n) | O(1) |
| Sqrt pairing | O(√n) | O(1) |
Print all abundant in [1, U] (naive test each i) | O(U²) worst case | O(1) |
Print all abundant in [1, U] (sqrt test each i) | O(U3/2) (i.e. sum of √i) | O(1) |
Both programs on this page use only a few integer variables: auxiliary space is constant aside from the program binary and call stack.
Summary
- Definition:
nis abundant when the sum of its proper divisors is greater thann. Same test asσ(n) > 2nbecauseσ(n) = s(n) + n. - Code: be comfortable with the naive loop to
n/2and the√npairing 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 widen the sum type ifn(ors(n)) can overflowint.
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.
9 people found this page helpful
