Perfect Number
Java coding interview problem for Number Logic: Perfect Number.
Checking whether a number is a Perfect Number is one of the classic Java coding interview questions.
This problem helps interviewers evaluate your understanding of:
- Loops
- Conditional statements
- Divisibility
- Number theory
- Mathematical logic
- Problem-solving skills
Perfect Number problems are common in coding interviews, competitive programming, and mathematics.
What is a Perfect Number?
A Perfect Number is a positive integer that is equal to the sum of all its positive divisors excluding itself.
In other words,
Perfect Number
=
Sum of Proper Divisors
A proper divisor is any positive divisor of a number except the number itself.
Mathematical Definition
If
N
has proper divisors
d1, d2, d3 ...
then
d1 + d2 + d3 + ...
=
N
Example 1
Number
6
Divisors
1
2
3
Sum
1 + 2 + 3
=
6
Since
Sum = Number
Therefore
6 is a Perfect Number
Example 2
Number
28
Divisors
1
2
4
7
14
Sum
1 + 2 + 4 + 7 + 14
=
28
Therefore
28 is a Perfect Number
Example 3
Number
12
Divisors
1
2
3
4
6
Sum
1 + 2 + 3 + 4 + 6
=
16
Since
16 ≠ 12
Therefore
12 is NOT a Perfect Number
Some Perfect Numbers
| Number | Perfect? |
|---|---|
| 6 | ✅ Yes |
| 28 | ✅ Yes |
| 496 | ✅ Yes |
| 8128 | ✅ Yes |
| 12 | ❌ No |
| 20 | ❌ No |
| 100 | ❌ No |
Real Interview Question
Write a Java program to check whether a number is a Perfect Number.
Understanding the Logic
Suppose
Number = 28
Find all divisors except
28
Add them together.
If the sum equals the original number,
then it is a Perfect Number.
Otherwise,
it is not.
Visual Representation
Input
28
Processing
Start
↓
Find Divisors
↓
1
↓
2
↓
4
↓
7
↓
14
↓
Sum
↓
1 + 2 + 4 + 7 + 14
↓
28
↓
Compare
↓
28 == 28
↓
Perfect Number
Brute Force Approach
The simplest solution is
- Initialize the sum as 0.
- Check every number from 1 to number - 1.
- If it divides the number exactly, add it to the sum.
- Compare the final sum with the original number.
Algorithm
Step 1
Read the number.
Step 2
Initialize
sum = 0;
Step 3
Loop from
1
to
number - 1
Step 4
Check
number % i == 0
Step 5
If true,
add
i
to
sum
Step 6
Compare
sum == number
Step 7
Print the result.
Dry Run
Input
6
Initial
sum = 0
| i | Divides 6? | Sum |
|---|---|---|
| 1 | Yes | 1 |
| 2 | Yes | 3 |
| 3 | Yes | 6 |
| 4 | No | 6 |
| 5 | No | 6 |
Comparison
6 == 6
Output
Perfect Number
Another Dry Run
Input
12
Initial
sum = 0
| i | Divides 12? | Sum |
|---|---|---|
| 1 | Yes | 1 |
| 2 | Yes | 3 |
| 3 | Yes | 6 |
| 4 | Yes | 10 |
| 5 | No | 10 |
| 6 | Yes | 16 |
| 7 | No | 16 |
| 8 | No | 16 |
| 9 | No | 16 |
| 10 | No | 16 |
| 11 | No | 16 |
Comparison
16 ≠ 12
Output
Not a Perfect Number
Approach 1 — Using Iteration (Loop)
This is the most common beginner-friendly approach and is frequently asked in interviews.
Complete Java Program
public class PerfectNumber {
public static void main(String[] args) {
int number = 28;
int sum = 0;
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
}
}
if (sum == number) {
System.out.println(number + " is a Perfect Number");
} else {
System.out.println(number + " is NOT a Perfect Number");
}
}
}
Output
28 is a Perfect Number
Step-by-Step Code Explanation
Step 1
Declare the number.
int number = 28;
Current value
28
Step 2
Initialize the sum.
int sum = 0;
Initially
sum = 0
Step 3
Start the loop.
for (int i = 1; i < number; i++)
The loop checks every possible proper divisor.
Step 4
Check divisibility.
if (number % i == 0)
If the remainder is zero,
then
i
is a divisor.
Step 5
Add the divisor.
sum += i;
Example for
28
sum = 1
↓
3
↓
7
↓
14
↓
28
Step 6
Compare the sum.
if (sum == number)
If true,
the number is perfect.
Otherwise,
it is not.
Example Execution
Input
Number = 496
Proper Divisors
1
2
4
8
16
31
62
124
248
Sum
496
Output
496 is a Perfect Number
Input
Number = 20
Proper Divisors
1
2
4
5
10
Sum
22
Output
20 is NOT a Perfect Number
Why Does This Work?
The algorithm checks every possible proper divisor of the given number.
Whenever a divisor is found, it is added to the running total.
If the final sum equals the original number, then the number satisfies the mathematical definition of a Perfect Number.
Advantages of This Approach
- Very easy to understand.
- Simple implementation using loops.
- No extra data structures required.
- Ideal for beginners.
- Frequently asked in coding interviews.
Drawbacks
Although this approach is straightforward, it checks every number from 1 to N−1, making it inefficient for large inputs.
Interviewers often ask follow-up questions such as:
- Can you optimize this solution?
- Why don't we need to check every number?
- Can you stop at the square root of the number?
- How can you reduce the time complexity?
- Can you create a reusable method?
- What is the time complexity of both approaches?
In Part 2, we'll cover:
- Optimized Approach Using Square Root
- Reusable Method
- Time and Space Complexity
- Comparison of Approaches
- Common Interview Mistakes
- Interview Follow-up Questions
- Related Coding Problems
- Key Takeaways
- Interview Tips
Approach 2 — Optimized Approach (Using Square Root)
The brute-force solution checks every number from
1
to
N - 1
This is inefficient for large numbers.
A better solution is to check divisors only up to the square root of the number.
This reduces the number of iterations significantly.
Why Does Square Root Optimization Work?
Divisors always occur in pairs.
Example
28
Divisor pairs
1 × 28
2 × 14
4 × 7
Once we reach
√28 ≈ 5.29
all remaining divisors have already been discovered as pairs.
Instead of checking
1 → 27
we only check
1 → 5
Visual Representation
Input
28
1 ←→ 28
2 ←→ 14
4 ←→ 7
Only check
1
2
3
4
5
Add
1
2 + 14
4 + 7
Result
28
Algorithm
Step 1
Read the number.
Step 2
Initialize
sum = 1;
Since
1
is always a proper divisor for numbers greater than 1.
Step 3
Loop
2
to
√number
Step 4
If
number % i == 0
then
i
is a divisor.
Step 5
Add
i
Also add its paired divisor
number / i
if they are different.
Step 6
Compare
sum == number
Dry Run
Input
28
Initial
sum = 1
| i | Divides? | Added Values | Sum |
|---|---|---|---|
| 2 | Yes | 2 + 14 | 17 |
| 3 | No | - | 17 |
| 4 | Yes | 4 + 7 | 28 |
| 5 | No | - | 28 |
Comparison
28 == 28
Output
Perfect Number
Java Program
public class PerfectNumberOptimized {
public static void main(String[] args) {
int number = 28;
if (number <= 1) {
System.out.println("Not a Perfect Number");
return;
}
int sum = 1;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
sum += i;
if (i != number / i) {
sum += number / i;
}
}
}
if (sum == number) {
System.out.println(number + " is a Perfect Number");
} else {
System.out.println(number + " is NOT a Perfect Number");
}
}
}
Output
28 is a Perfect Number
Approach 3 — Using a Reusable Method
Reusable methods improve
- Code readability
- Maintainability
- Unit testing
- Code reuse
Java Program
public class PerfectNumberMethod {
static boolean isPerfect(int number) {
if (number <= 1) {
return false;
}
int sum = 1;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
sum += i;
if (i != number / i) {
sum += number / i;
}
}
}
return sum == number;
}
public static void main(String[] args) {
int number = 496;
if (isPerfect(number)) {
System.out.println(number + " is a Perfect Number");
} else {
System.out.println(number + " is NOT a Perfect Number");
}
}
}
Output
496 is a Perfect Number
Time Complexity
Brute Force Approach
| Operation | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) |
Optimized Approach
| Operation | Complexity |
|---|---|
| Time | O(√n) |
| Space | O(1) |
Comparison of Approaches
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Brute Force | O(n) | O(1) | Good for Beginners |
| Square Root Optimization | O(√n) | O(1) | ✅ Best for Interviews |
| Reusable Method | O(√n) | O(1) | Production Ready |
Common Mistakes
Mistake 1
Including the number itself.
Wrong
for (int i = 1; i <= number; i++)
Correct
for (int i = 1; i < number; i++)
or use the optimized approach.
Mistake 2
Starting the sum with
0
when using the optimized approach.
Correct
sum = 1;
because
1
is always a proper divisor (for numbers greater than 1).
Mistake 3
Ignoring paired divisors.
Wrong
sum += i;
Correct
sum += i;
sum += number / i;
Mistake 4
Adding the square root twice.
Example
36
The divisor
6
pairs with itself.
Correct
if (i != number / i)
before adding the paired divisor.
Mistake 5
Ignoring edge cases.
Examples
0
1
Negative Numbers
None of these are Perfect Numbers.
Handle them before processing.
Interview Follow-up Questions
Q1. What is a Perfect Number?
Q2. Why do divisor pairs help optimize the solution?
Q3. Why is the optimized solution O(√n)?
Q4. Can you list the first four Perfect Numbers?
Q5. Can you check whether every even number is perfect?
Q6. Can a Perfect Number be odd?
Q7. Write a reusable method for checking Perfect Numbers.
Q8. Print all Perfect Numbers in a given range.
Q9. Compare the brute-force and optimized approaches.
Q10. What are some real-world applications of divisor-based algorithms?
Related Coding Problems
- Prime Number
- Armstrong Number
- Strong Number
- Harshad Number
- GCD (HCF)
- LCM
- Factors of a Number
- Sum of Divisors
Key Takeaways
- A Perfect Number equals the sum of all its proper divisors.
- The first few Perfect Numbers are:
6
28
496
8128
- The brute-force solution checks every possible divisor and runs in O(n) time.
- The optimized solution checks divisors only up to √n, reducing the time complexity to O(√n).
- Divisors always appear in pairs, making square-root optimization possible.
- Handle edge cases such as 0, 1, and negative numbers before processing.
- Using a reusable method makes the code cleaner and easier to test.
Interview Tip
If an interviewer asks:
"Write a Java program to check whether a number is a Perfect Number."
Start with the straightforward loop-based solution to demonstrate your understanding of divisors. After completing it, explain that checking every number is inefficient and introduce the square-root optimization. Mention that divisors occur in pairs, allowing you to check only up to √n, which improves the time complexity from O(n) to O(√n). Finally, discuss edge cases (0, 1, and negative numbers) and show how to encapsulate the logic in a reusable method for production-quality code.