Java ProgramsNumbersCheck Perfect Number

Check Perfect Number in Java

beginner·  Numbers  ·  Number Theory

Problem

A perfect number is a number that equals the sum of its own proper divisors — the divisors other than the number itself.

Given a number, determine whether it is a perfect number.

Input
28
Output
Perfect number: true

Java Program

Java
public class PerfectNumberCheck { public static void main(String[] args) { int n = 28; 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("Perfect number: " + (sum == n)); } }

Output

Perfect number: true

Core Logic

Adding up every number from 1 up to n - 1 that divides n evenly, then comparing that total against n itself, checks the definition directly.

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 perfect-number condition.
For 28, the proper divisors are 1, 2, 4, 7, and 14 — adding them together gives exactly 28, so it's reported as perfect.
💡

Key Point: This checks every number up to n - 1 as a candidate divisor, which is simple to follow but does more work than necessary — divisors always come in pairs around √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 PerfectNumberCheckOptimized { public static void main(String[] args) { int n = 28; 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; // avoid double-counting a perfect square's middle divisor } } System.out.println("Perfect number: " + (sum == n)); } }

Output

Perfect number: true

Core Logic

Every divisor below √n pairs up with a matching divisor above it — finding one half of each pair and adding both at once cuts the search down to the square root.

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 the same value, which would double-count a perfect square's middle divisor.
  4. 4The same sum == n comparison confirms the result.
For 28, finding the divisor 2 also finds its pair 14 (2 + 14 = 16), and finding 4 also finds its pair 7 (+ 4 + 7 = 27), plus the initial 1, giving sum = 28.
💡

Key Point: The pair != i check matters for perfect squares — without it, a divisor exactly at √n (like 5 for 25) would get added twice instead of once.

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 PerfectNumberCheckStream { public static void main(String[] args) { int n = 28; // Sums every divisor from 1 up to n - 1 int sum = IntStream.range(1, n).filter(i -> n % i == 0).sum(); System.out.println("Perfect number: " + (sum == n)); } }

Output

Perfect 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 perfect-number condition.
Filtering 1 through 27 down to divisors of 28 keeps 1, 2, 4, 7, and 14, and summing them gives the same 28 the loop version finds.
💡

Key Point: This does the same O(n) work as the primary loop, just expressed as a stream — the divisor-pairs optimization doesn't translate as directly into a single filter-and-sum pipeline.

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