202609151227 Leetcode 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.
Given an integer n, return true if n is a perfect number, otherwise return false.
Example 1:
Input: num = 28
Output: true
Explanation: 28 = 1 + 2 + 4 + 7 + 14
1, 2, 4, 7, and 14 are all divisors of 28.
Example 2:
Input: num = 7
Output: false
Constraints:
1 <= num <= 10^8
Solution
The key insight to this one is that we only need to check up to the square root of a number since we can just add its pair when we find one. For instance, to get all the clean divisors of 28 we can check 1 through 6 since when we check 4 we also get 7 from the division check.
Other than that, there's some bookkeeping for adding 1 and also not adding the number n itself (though we could instead check against 2n for a match). This definition also excludes 1 from being a perfect number so in the second version below we short circuit that.
I like this solution a bit more but it's less performant than the following one, has hacky [0, 0] values, and an extra filter pass.
The core loop uses d * d <= num instead of sqrt because it's faster and it short-circuits on sum > num for many large numbers whose divisors will sum to greater than the number far before we check for all the divisors.