- Proper divisors
- 1, 2, 3
- Sum s(n)
- 6
Check Abundant Number in Java
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 Java 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(sqrt(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 Java.
Tables and 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 Java programs before following the examples below.
- Comfortable with Java syntax:
class Main,public static void main(String[] args),System.out.println, and simple methods. 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
Number theory often uses two sums: s(n) for proper divisors and σ(n) for all divisors. They are related by s(n) = σ(n) - n.
n is abundant if s(n) > n, which is the same as σ(n) > 2n.
Intuition and examples
- Proper divisors
- 1, 2, 3, 4, 6
- Sum s(n)
- 16
- Proper divisors
- 1
- Sum s(n)
- 1
Live preview
Enter a number, view its proper divisors, and compare s(n) with n.
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. In code, you usually return "not abundant" for n ≤ 1.
Start an accumulator
Set sum = 0. Add each proper divisor you find into sum.
Try every candidate divisor
Loop i from 1 to n / 2. Every proper divisor is at most n/2.
Add hits to sum
If n % i == 0, then i divides n. Add i to sum.
Decide abundant or not
After the loop, n is abundant iff sum > n.
📜 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) > nCheck a single number (program with explanation)
This version returns false for n ≤ 1, then sums proper divisors in O(sqrt(n)) time by enumerating divisor pairs up to the square root.
public class Main {
static boolean 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;
}
public static void main(String[] args) {
int number = 12;
if (isAbundant(number)) {
System.out.println(number + " is an abundant number.");
} else {
System.out.println(number + " is not an abundant number.");
}
}
}Abundant numbers in a range (program with explanation)
The inner test uses a straightforward loop to num/2. That is O(n) per value and very easy to understand.
public class Main {
static boolean 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;
}
public static void main(String[] args) {
System.out.print("Abundant numbers between 1 and 50 are: ");
for (int i = 1; i <= 50; i++) {
if (isAbundant(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}
}❓ FAQ
🔄 Input / output examples
Assume the single-number program prints one line. In interactive versions you can read input using Scanner.
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. |
Optimization
Single n: use divisor pairs up to √n for O(√n) time.
Many queries: precompute divisor sums with sieve-style logic and answer in O(1) per query.
Interview tip: explain simple n/2 approach first, then mention sqrt optimization.
Edge cases and pitfalls
Not abundant
Return false early for non-positive values and 1.
No double count
In sqrt optimization, add i once when i == n / i.
Use wider sum type if needed
For huge values, prefer long for divisor-sum accumulation.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Naive loop to n/2 | O(n) | O(1) |
| Sqrt divisor pairing | O(√n) | O(1) |
Summary
- Definition: abundant means
s(n) > n. - Code: simple loop is easiest; sqrt-pairing is faster.
- Edge handling: guard
n ≤ 1and avoid duplicate square-root divisors.
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
