leetcode507. 完美数

超出时间限制改进

28 = 1 + 2 + 4 + 7 + 14

循环从1到sqrt(28)

每次加上 i 和 num/i

比如 2 和 14 

class Solution:
    def checkPerfectNumber(self, num: int) -> bool:
        import math
        if num <= 1:
            return False
        ans = 0
        for i in range(1, int(math.sqrt(num)) + 1, 1):
            if num % i == 0:
                ans += i
                if i != 1 and num / i != i:
                    ans += num / i
        return ans == num

 

posted on 2019-05-25 11:37  小明明明不是企鹅  阅读(165)  评论(0编辑  收藏  举报