[LeetCode]#507. Perfect Number
Environment: Python 3.7
Key technique: for loop, math.sqrt()
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.
Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
Explanation: 28 = 1 + 2 + 4 + 7 + 14
Note: The input number n will not exceed 100,000,000. (1e8)
Due to input range is 1~~100,000,000. If you find factor directly, it may occur time exceed. We can use math.sqrt to determine compute binary and find his pair factor as below. We can see the for loop binary is 5 and we can find two pair [2,14] and [4,7]. Finally, sum all number is answer.
def checkPerfectNumber(self, num):
for i in range(2,scale+1):
if num % i==0:
ans+=i+num/ireturn num == ans