对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。
给定一个 整数 n
, 如果他是完美数,返回 True
,否则返回 False
示例:
输入: 28 输出: True 解释: 28 = 1 + 2 + 4 + 7 + 14
提示:
输入的数字 n
不会超过 100,000,000. (1e8)
题目标签:Math
题目链接:LeetCode / LeetCode中国
Language | Runtime | Memory |
---|---|---|
python3 | 52 ms | N/A |
class Solution:
def checkPerfectNumber(self, num):
"""
:type num: int
:rtype: bool
"""
if num < 2:
return False
ds = {1}
for i in range(2, int(num ** 0.5)+1):
if num % i == 0:
ds.add(i)
ds.add(num // i)
return num == sum(ds)