easyNumber ProblemsJava

Check Whether a Number Is a Perfect Number

Determine whether a number equals the sum of its proper divisors.

Quick Answer

A perfect number equals the sum of its proper divisors (all divisors except itself). Sum the divisors of n and check whether that total equals n. For example 6 = 1 + 2 + 3, so 6 is perfect. Pair divisors around the square root to make it efficient.

Problem Statement

A perfect number is a positive integer that equals the sum of its proper divisors — its positive divisors excluding the number itself. The smallest example is 6, because 6 = 1 + 2 + 3. The next is 28 = 1 + 2 + 4 + 7 + 14.

Given a positive integer n, return true if it is perfect and false otherwise. To stay efficient, collect divisors in pairs by looping only up to the square root of n.

Input: A single positive integer n.

Output: true if n is a perfect number, otherwise false.

Examples

Example 1
Input:  6
Output: true

The proper divisors of 6 are 1, 2 and 3, and 1 + 2 + 3 = 6.

Example 2
Input:  12
Output: false

The proper divisors of 12 are 1, 2, 3, 4, 6 summing to 16, not 12.

Constraints

  • n fits in a 32-bit int; numbers below 2 are not perfect
  • Sum proper divisors only (exclude n itself)

Think Before You Code

Reveal the questions to ask yourself first
  • Which divisors count toward the sum, and which one is excluded?
  • How can you find divisors in pairs to avoid looping all the way to n?
  • Why should 1 be included but n itself left out?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Sum only the proper divisors — every divisor except n itself — and compare with n.
Hint 2
For each i that divides n, both i and n / i are divisors; add them in pairs up to the square root.
Hint 3
Start the sum at 1 (always a proper divisor for n > 1) and be careful not to double-count when i equals n / i.

Approach

Reveal the step-by-step approach

Sum proper divisors using square-root pairing.

  1. If n < 2, return false.
  2. Start sum = 1 (1 is a proper divisor of every n > 1).
  3. For i from 2 while i * i <= n:
    • If i divides n, add i to the sum.
    • Also add the paired divisor n / i, unless it equals i (a perfect square).
  4. Return whether sum equals n.

Dry Run

Walk through the example step by step

Checking n = 6:

i | i*i <= 6 | 6 % i == 0 | added        | sum
--+----------+------------+--------------+----
- | -        | -          | start 1      | 1
2 | yes (4)  | yes        | 2 and 6/2=3  | 6
3 | no (9)   | stop       | —            | 6

sum (6) == n (6) -> true.

Solution

Reveal the full Java solution
public class PerfectNumber {
    public static boolean isPerfect(int n) {
        if (n < 2) {
            return false;
        }
        int sum = 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;
                }
            }
        }
        return sum == n;
    }

    public static void main(String[] args) {
        System.out.println(isPerfect(6));  // true
        System.out.println(isPerfect(12)); // false
    }
}

Divisors come in pairs (i, n / i) straddling the square root, so looping only to sqrt(n) and adding both members of each pair finds every proper divisor in O(sqrt n). Guarding pair != i avoids counting the square root twice for perfect squares, and starting the sum at 1 (while never adding n itself) keeps it to proper divisors.

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

Common Mistakes

  • Including n itself in the sum, which makes no number ever look perfect.
  • Double-counting the square root for perfect squares (for example adding 6 twice for 36).

Edge Cases to Test

  • n = 1 is not perfect; its only divisor is itself, which is excluded.
  • n = 28 should return true (1 + 2 + 4 + 7 + 14 = 28).

Interview Follow-Ups

  • How would you print all perfect numbers within a given range?
  • How does the sum of proper divisors classify a number as deficient or abundant?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us