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
Perfect Number: Does sum of divisors equal the number?n = 6Find all proper divisors (excluding 6 itself)1236÷1=66÷2=36÷3=2Sum: 1 + 2 + 3 = 66 equals sum of divisors → Perfect Number!
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
Asked in
Google 15 Microsoft 12 Amazon 8
32.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen