Check Abundant Number in Java

Beginner
⏱️ 14 min read
📚 Updated: May 2026
🎯 2 Code Examples
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 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.
  • 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

Number theory often uses two sums: s(n) for proper divisors and σ(n) for all divisors. They are related by s(n) = σ(n) - n.

Equivalent abundant tests

n is abundant if s(n) > n, which is the same as σ(n) > 2n.

Intuition and examples

6Perfect
Proper divisors
1, 2, 3
Sum s(n)
6
12Abundant
Proper divisors
1, 2, 3, 4, 6
Sum s(n)
16
7Deficient
Proper divisors
1
Sum s(n)
1

Live preview

Enter a number, view its proper divisors, and compare s(n) with n.

Use whole numbers n ≥ 1.

Live result
Press "Run check" to see details.

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

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(sqrt(n)) time by enumerating divisor pairs up to the square root.

java
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.");
        }
    }
}
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 and very easy to understand.

java
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

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(sqrt(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 program prints one line. In interactive versions you can read input using Scanner.

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.

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

n <= 1

Not abundant

Return false early for non-positive values and 1.

Perfect squares

No double count

In sqrt optimization, add i once when i == n / i.

Overflow

Use wider sum type if needed

For huge values, prefer long for divisor-sum accumulation.

⏱️ Time and space complexity

ApproachTimeExtra space
Naive loop to n/2O(n)O(1)
Sqrt divisor pairingO(√n)O(1)

Summary

  • Definition: abundant means s(n) > n.
  • Code: simple loop is easiest; sqrt-pairing is faster.
  • Edge handling: guard n ≤ 1 and avoid duplicate square-root divisors.
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