Perfect Number - Problem
A perfect number is a fascinating mathematical concept - it's a positive integer that equals the sum of all its positive divisors, excluding the number itself. Think of it as a number that is perfectly balanced with its factors!
For example, 6 is perfect because its divisors are 1, 2, and 3, and 1 + 2 + 3 = 6. Similarly, 28 is perfect because 1 + 2 + 4 + 7 + 14 = 28.
Your task: Given an integer n, determine if it's a perfect number. Return true if it is, false otherwise.
Note: A divisor of an integer x is any integer that divides x evenly (with no remainder).
Input & Output
example_1.py โ Basic Perfect Number
$
Input:
6
โบ
Output:
true
๐ก Note:
6 is a perfect number because its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6.
example_2.py โ Larger Perfect Number
$
Input:
28
โบ
Output:
true
๐ก Note:
28 is a perfect number because its proper divisors are 1, 2, 4, 7, and 14, and 1 + 2 + 4 + 7 + 14 = 28.
example_3.py โ Not Perfect Number
$
Input:
12
โบ
Output:
false
๐ก Note:
12 is not perfect because its proper divisors are 1, 2, 3, 4, and 6, and 1 + 2 + 3 + 4 + 6 = 16 โ 12.
Visualization
Tap to expand
Understanding the Visualization
1
Identify the Pattern
Perfect numbers equal the sum of their proper divisors
2
Find Divisor Pairs
Every divisor i has a pair n/i (except when i = โn)
3
Optimize Search
Only check numbers up to โn to find all divisors
4
Sum and Compare
Add all divisors and check if sum equals the original number
Key Takeaway
๐ฏ Key Insight: By recognizing that divisors come in pairs, we can reduce our search space from O(n) to O(โn), making the algorithm efficient enough to handle large numbers while maintaining correctness.
Time & Space Complexity
Time Complexity
O(โn)
We only check divisors up to the square root of n
โ Linear Growth
Space Complexity
O(1)
Only using constant extra space
โ Linear Space
Constraints
- 1 โค num โค 108
- Note: Perfect numbers are extremely rare
- The first few perfect numbers are: 6, 28, 496, 8128...
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code