Check Whether a Number Is a Perfect Number
Determine whether a number equals the sum of its proper divisors.
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
Input: 6
Output: trueThe proper divisors of 6 are 1, 2 and 3, and 1 + 2 + 3 = 6.
Input: 12
Output: falseThe 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 perfectSum 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
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Sum proper divisors using square-root pairing.
- If
n < 2, returnfalse. - Start
sum = 1(1 is a proper divisor of everyn > 1). - For
ifrom 2 whilei * i <= n:- If
idividesn, addito the sum. - Also add the paired divisor
n / i, unless it equalsi(a perfect square).
- If
- Return whether
sumequalsn.
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.
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 →