507. 完美数

对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。

给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False

示例:

输入: 28
输出: True
解释: 28 = 1 + 2 + 4 + 7 + 14

注意:

输入的数字n不会超过 100,000,000. (1e8)

class Solution(object):
    def checkPerfectNumber(self, num):
        """
        :type num: int
        :rtype: bool
        """
        
        if num <= 1:
            return False
        
        lst = list()
        lst.append(1)
        
        from math import sqrt, ceil
        for i in range(2, ceil(sqrt(num))):
            if num % i == 0:
                lst.extend([i, num//i])
        
        return sum(lst) == num
posted @ 2018-07-24 12:59  yuyin  阅读(109)  评论(0编辑  收藏  举报