class Solution(object):
def checkPerfectNumber(self, num):
"""
:type num: int
:rtype: bool
"""
if num < 2:
return False
total = num
for i in xrange(1, num):
if i * i > num:
break
if num % i == 0:
total += i + num / i
if num / i == i:
total = i
return total == num
python simple solution

@zqfan I don't see why you need that nested if statement with in
num%1
, since you just trying to handle the case whichnum / i == i
, you can just changeif i * i > num
toif i * i >= num
. Here is a simplified version.class Solution(object): def checkPerfectNumber(self, num): total, i = 1, 2 while i * i < num: if num % i == 0: total += i + num / i i += 1 return num > 1 and total == num or False