Check Deficient Number in Java
Problem
A deficient number is a number whose proper divisors add up to less than the number itself — most numbers are deficient, unlike the much rarer perfect and abundant numbers.
Given a number, determine whether it is a deficient number.
Java Program
public class DeficientNumberCheck {
public static void main(String[] args) {
int n = 8;
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("Deficient number: " + (sum < n));
}
}Output
Core Logic
Adding up every proper divisor of the number, then checking whether that total falls short of the number itself, reuses the same divisor-summing technique as checking a perfect or abundant number.
- 1The loop tries every candidate
ifrom1up to, but not including,n. - 2
n % i == 0checks whetheriis a proper divisor ofn. - 3Every divisor found is added into
sum. - 4After the loop,
sum < nis the deficient-number condition.
8, the proper divisors are 1, 2, and 4 — adding them together gives 7, which is less than 8, so it's reported as deficient.Key Point: The exact same divisor-summing loop drives all three checks — perfect, abundant, and deficient — only the final comparison operator changes between ==, >, and <.
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
Approach 2: Optimized (Divisor Pairs)
public class DeficientNumberCheckOptimized {
public static void main(String[] args) {
int n = 8;
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("Deficient number: " + (sum < n));
}
}
Output
Core Logic
The same divisor-pairs shortcut used to check perfect and abundant numbers applies here too, cutting the search down to the square root of n.
- 1
sumstarts at1, since 1 is a proper divisor of every number greater than 1. - 2The loop tries candidates
ifrom2up to√nonly. - 3When
idividesnevenly, bothiand its pair,n / i, are added intosum— unless they're equal, which would double-count a perfect square's middle divisor. - 4The same
sum < ncomparison confirms the result.
8, finding the divisor 2 also finds its pair 4 — together with the initial 1, that's 1 + 2 + 4 = 7, matching the manual scan.Key Point: Just like the perfect and abundant checks, this cuts the number of candidates tried from n down to about √n, without changing the final comparison at all.
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
Approach 3: Java 8
import java.util.stream.IntStream;
public class DeficientNumberCheckStream {
public static void main(String[] args) {
int n = 8;
// Sums every divisor from 1 up to n - 1
int sum = IntStream.range(1, n).filter(i -> n % i == 0).sum();
System.out.println("Deficient number: " + (sum < n));
}
}
Output
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.
- 1
IntStream.range(1, n)generates every candidate from1up ton - 1. - 2
.filter(i -> n % i == 0)keeps only the numbers that dividenevenly. - 3
.sum()reduces the filtered stream down to a single total, the sum of every proper divisor. - 4Comparing that total against
nconfirms the deficient-number condition.
7 the loop version finds.Key Point: This is the same stream pipeline used to check a perfect or abundant number, just with < in place of == or >.
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.