Java ProgramsNumbersCheck Abundant Number

Check Abundant Number in Java

beginner·  Numbers  ·  Number Theory

Problem

An abundant number is a number whose proper divisors add up to more than the number itself, unlike a perfect number, where they add up to exactly the number.

Given a number, determine whether it is an abundant number.

Input
12
Output
Abundant number: true

Java Program

Java
public class AbundantNumberCheck { public static void main(String[] args) { int n = 12; int sum = 0; for (int i = 1; i < n; i++) { if (n % i == 0) sum += i; // add every proper divisor into the running total } System.out.println("Abundant number: " + (sum > n)); } }

Output

Abundant number: true

Core Logic

Adding up every proper divisor of the number, then checking whether that total exceeds the number itself, is the same technique used to check a perfect number — just with a different comparison.

How It Works
  1. 1The loop tries every candidate i from 1 up to, but not including, n.
  2. 2n % i == 0 checks whether i is a proper divisor of n.
  3. 3Every divisor found is added into sum.
  4. 4After the loop, sum > n is the abundant-number condition.
For 12, the proper divisors are 1, 2, 3, 4, and 6 — adding them together gives 16, which is greater than 12, so it's reported as abundant.
💡

Key Point: This is the exact same divisor-summing loop as checking a perfect number — only the final comparison changes, from sum == n to sum &gt; n.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Every number from 1 up to n - 1 is tested as a potential divisor, so the loop's cost scales directly with n.

Key Concepts

proper divisorsrunning sumtrial division

Approach 2: Optimized (Divisor Pairs)

Java
public class AbundantNumberCheckOptimized { public static void main(String[] args) { int n = 12; int sum = 1; // 1 is a proper divisor of every number greater than 1 for (int i = 2; (long) i * i <= n; i++) { if (n % i == 0) { sum += i; int pair = n / i; if (pair != i) sum += pair; // add the matching divisor pair, unless it's the same value } } System.out.println("Abundant number: " + (sum > n)); } }

Output

Abundant number: true

Core Logic

The same divisor-pairs shortcut used to check a perfect number applies here too — every divisor below √n pairs up with one above it, so finding half the pairs finds all the divisors.

How It Works
  1. 1sum starts at 1, since 1 is a proper divisor of every number greater than 1.
  2. 2The loop tries candidates i from 2 up to √n only.
  3. 3When i divides n evenly, both i and its pair, n / i, are added into sum — unless they're equal, which would double-count a perfect square's middle divisor.
  4. 4The same sum &gt; n comparison confirms the result.
For 12, finding the divisor 2 also finds its pair 6, and finding 3 also finds its pair 4 — together with the initial 1, that's 1 + 2 + 6 + 3 + 4 = 16, matching the manual scan.
💡

Key Point: Just like the perfect-number check, this cuts the number of candidates tried from n down to about √n, without changing the final comparison at all.

Complexity
Time Complexity: O(√n)Space Complexity: O(1)

Why: Checking divisors only up to √n and adding both members of each divisor pair at once cuts the number of iterations from n down to about √n.

Key Concepts

divisor pairssquare-root bound

Approach 3: Java 8

Java
import java.util.stream.IntStream; public class AbundantNumberCheckStream { public static void main(String[] args) { int n = 12; // Sums every divisor from 1 up to n - 1 int sum = IntStream.range(1, n).filter(i -> n % i == 0).sum(); System.out.println("Abundant number: " + (sum > n)); } }

Output

Abundant number: true

Core Logic

The same divisor-summing idea can be expressed as a stream — keep only the divisors, then reduce them down to a single total.

How It Works
  1. 1IntStream.range(1, n) generates every candidate from 1 up to n - 1.
  2. 2.filter(i -> n % i == 0) keeps only the numbers that divide n evenly.
  3. 3.sum() reduces the filtered stream down to a single total, the sum of every proper divisor.
  4. 4Comparing that total against n confirms the abundant-number condition.
Filtering 1 through 11 down to divisors of 12 keeps 1, 2, 3, 4, and 6, and summing them gives the same 16 the loop version finds.
💡

Key Point: This is the same stream pipeline used to check a perfect number, just with &gt; in place of ==.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: The stream still checks every candidate divisor from 1 to n - 1, the same O(n) work as the manual loop, just expressed as a filter-and-sum pipeline.

Key Concepts

StreamIntStream.range()filter()sum()

Related Programs