Abundant Number Program: C, Java and Python with √n Check
An abundant number has a proper-divisor sum greater than itself. Code in C, Java, and Python using the O(√n) loop, with worked examples for 12, 18, and 20.
A number is abundant when the sum of its proper divisors exceeds the number itself. For 12, divisors 1, 2, 3, 4, 6 sum to 16, and 16 is greater than 12.
This problem appears in placement coding rounds as a direct “classify this number” task and as a sub-step in number-theory problems (listing all abundant numbers below 1000, for example). The classification family it belongs to (perfect, deficient, abundant) is covered in Wikipedia’s entry on abundant numbers, which also traces the concept back to Greek mathematics.
What is an Abundant Number?
A proper divisor of n is any positive integer that divides n evenly, except n itself. The number n is abundant if the sum of its proper divisors is strictly greater than n.
Three canonical examples to build intuition:
- n = 12: proper divisors are 1, 2, 3, 4, 6. Sum = 16. Since 16 > 12, it is abundant.
- n = 18: proper divisors are 1, 2, 3, 6, 9. Sum = 21. Since 21 > 18, it is abundant.
- n = 20: proper divisors are 1, 2, 4, 5, 10. Sum = 22. Since 22 > 20, it is abundant.
For reference, a perfect number has divisor sum equal to n (e.g., 6 and 28), and a deficient number has divisor sum less than n (e.g., 15 with divisors 1, 3, 5 summing to 9).
The sequence of abundant numbers begins 12, 18, 20, 24, 30, 36 and is catalogued as OEIS A005101. According to Wikipedia, all abundant numbers below 1000 are even with the single exception of 945.
The Algorithm: O(sqrt(n)) Divisor Sum
The naive approach scans every integer from 1 to n-1 and checks divisibility. For large n this is O(n), which is acceptable for a single check on small inputs but unnecessary.
The better approach uses the fact that divisors come in pairs: if i divides n, then n divided by i also divides n. By looping only up to the square root of n, you collect both elements of each pair in one step:
- Initialise
sum = 1(since 1 is always a proper divisor for n > 1). - Loop i from 2 while
i * i <= n. - If n is divisible by i, add i to sum. If i is not equal to
n / i(not a perfect-square root), also addn / i. - After the loop, check whether
sum > n.
The guard i != n / i prevents double-counting when n is a perfect square. For n = 36, i = 6 satisfies 6 * 6 == 36, so only 6 is added, not both 6 and 6.
The space complexity of this algorithm is O(1) for both the naive and optimised versions. The time improvement from O(n) to O(sqrt(n)) is what matters in practice for large inputs.
The sum of digits pattern uses a similar loop-and-accumulate structure. The divisor version adds the pair optimisation on top.
Working Code in C, C++, Java, and Python
C
#include <stdio.h>
int isAbundant(int n) {
if (n <= 1) return 0;
int sum = 1;
for (int i = 2; (long long)i * i <= n; i++) {
if (n % i == 0) {
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum > n;
}
int main() {
int n;
scanf("%d", &n);
if (isAbundant(n))
printf("Abundant Number\n");
else
printf("Not Abundant Number\n");
return 0;
}
Sample run: input 12, output Abundant Number.
C++
#include <iostream>
using namespace std;
bool isAbundant(int n) {
if (n <= 1) return false;
int sum = 1;
for (int i = 2; (long long)i * i <= n; i++) {
if (n % i == 0) {
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum > n;
}
int main() {
int n;
cin >> n;
cout << (isAbundant(n) ? "Abundant Number" : "Not Abundant Number") << endl;
return 0;
}
Java
import java.util.Scanner;
public class AbundantNumber {
static boolean isAbundant(int n) {
if (n <= 1) return false;
int sum = 1;
for (int i = 2; (long) i * i <= n; i++) {
if (n % i == 0) {
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum > n;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(isAbundant(n) ? "Abundant Number" : "Not Abundant Number");
}
}
Python
def is_abundant(n):
if n <= 1:
return False
div_sum = 1
i = 2
while i * i <= n:
if n % i == 0:
div_sum += i
if i != n // i:
div_sum += n // i
i += 1
return div_sum > n
n = int(input())
print("Abundant Number" if is_abundant(n) else "Not Abundant Number")
Trace for n = 18 (Python):
- div_sum = 1
- i=2: 18 % 2 == 0, add 2 and 9. div_sum = 12
- i=3: 18 % 3 == 0, add 3 and 6. div_sum = 21
- i=4: 18 % 4 is not 0
- i=5: 5*5 = 25 > 18, stop
- 21 > 18: abundant
Listing All Abundant Numbers in a Range
Placement exams sometimes ask for all abundant numbers up to N (for example, “find all abundant numbers below 100”). Wrap the check in a loop:
def abundant_in_range(limit):
result = []
for num in range(1, limit + 1):
if is_abundant(num):
result.append(num)
return result
print(abundant_in_range(30))
# Output: [12, 18, 20, 24, 30]
The related problem of finding integers with exactly 9 divisors in a range uses the same O(sqrt(n)) divisor loop as its inner function. Understanding the abundant number check makes that problem straightforward.
Common Mistakes and How to Avoid Them
Mistake 1: Including n itself in the sum.
A common error initialises sum to 0 and loops from i = 1 to i = n. This adds n as a divisor (since n % n == 0), inflating the sum. Fix: initialise sum to 1 and start the loop at i = 2.
Mistake 2: Reversed comparison direction.
Some implementations write sum < n as the abundant condition. That is backwards. A number is abundant when sum > n. For n = 12, the correct sum is 16, and the check is 16 > 12. Writing 16 < 12 would classify 12 as non-abundant.
Mistake 3: Off-by-one on the sqrt boundary.
Using i * i < n instead of i * i <= n misses one divisor pair when n is a perfect square. For n = 4, the loop with < stops before i = 2 (since 4 < 4 is false) and misses divisor 2. Use <=.
Mistake 4: Integer overflow in the loop condition.
For large n near INT_MAX, i * i overflows a 32-bit integer. The C and Java examples above cast to (long long) or (long) before multiplying. In Python, integers are arbitrary precision, so no cast is needed.
Revisiting the trace for n = 12 with the correct implementation:
- sum = 1
- i=2: 12 % 2 == 0, add 2 and 6. sum = 9
- i=3: 12 % 3 == 0, add 3 and 4. sum = 16
- i=4: 4 * 4 = 16, which is greater than 12, stop
- 16 > 12: abundant
The divisor-sum loop is the same building block across number-theory coding problems: perfect number checks, GCD-based work, prime factorisation. TinkerLLM’s coding module walks through this pattern family before scaling to LLM-assisted code generation. At ₹499, it is the quickest path from “I can write the loop” to “I can explain the loop to an interviewer and extend it on the whiteboard.”
Primary sources
Frequently asked questions
What is the smallest abundant number?
12 is the smallest abundant number. Its proper divisors are 1, 2, 3, 4, and 6, which sum to 16, greater than 12.
What are proper divisors of a number?
Proper divisors of n are all positive integers that divide n evenly, excluding n itself. For n = 12, the proper divisors are 1, 2, 3, 4, and 6.
Is 20 an abundant number?
Yes. The proper divisors of 20 are 1, 2, 4, 5, and 10, which sum to 22. Since 22 is greater than 20, the number is abundant.
What is the time complexity of checking if a number is abundant?
The optimised divisor-sum loop runs from i = 2 to the square root of n, giving O(sqrt(n)) time complexity. The naive loop from 1 to n-1 runs in O(n) time.
What is the difference between an abundant number and a perfect number?
For a perfect number, the sum of proper divisors equals the number (e.g., 6 and 28). For an abundant number, the sum is strictly greater than the number (e.g., 12 with divisor sum 16).
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹499)