Foundations Working with digits and divisors
32. Perfect Number
Given a positive integer n, return true if n is a perfect number, and false otherwise.
A proper divisor of n is a positive whole number smaller than n that divides n with no remainder.
n is perfect when its proper divisors add up to exactly n.
For example, the proper divisors of 6 are 1, 2 and 3, and 1 + 2 + 3 = 6. So 6 is a perfect number.
Example 1
Input: n = 496
trueExplanation: The proper divisors of 496 are 1, 2, 4, 8, 16, 31, 62, 124 and 248, and they add up to 496.
Example 2
Input: n = 36
falseExplanation: The proper divisors of 36 are 1, 2, 3, 4, 6, 9, 12 and 18. They add up to 55, which is more than 36.
Example 3
Input: n = 137438691328
trueCheck your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 1012
Clarifying questions
What you would ask an interviewer before writing any code.