Perfect Number - Problem
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
A divisor of an integer x is an integer that can divide x evenly (with no remainder).
Given an integer n, return true if n is a perfect number, otherwise return false.
Input & Output
Example 1 — First Perfect Number
$
Input:
n = 6
›
Output:
true
💡 Note:
The divisors of 6 are 1, 2, and 3. Sum: 1 + 2 + 3 = 6, which equals the number itself.
Example 2 — Second Perfect Number
$
Input:
n = 28
›
Output:
true
💡 Note:
The divisors of 28 are 1, 2, 4, 7, and 14. Sum: 1 + 2 + 4 + 7 + 14 = 28.
Example 3 — Not Perfect
$
Input:
n = 12
›
Output:
false
💡 Note:
The divisors of 12 are 1, 2, 3, 4, and 6. Sum: 1 + 2 + 3 + 4 + 6 = 16, which is not equal to 12.
Constraints
- 1 ≤ n ≤ 108
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given number n = 6
2
Find Divisors
Find all proper divisors: 1, 2, 3
3
Sum & Check
Sum = 1+2+3 = 6, equals n, so perfect
Key Takeaway
🎯 Key Insight: Check divisors up to √n since they come in pairs, making the algorithm much faster
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code