# Simple Python

• One thing to keep in mind is the stopping condition of `for` loop is `less or equal to SQRT`.

``````class Solution(object):
def checkPerfectNumber(self, num):
"""
:type num: int
:rtype: bool
"""
if num <= 0: return False
ans, SQRT = 0, int(num ** 0.5)
ans = sum(i + num//i for i in range(1, SQRT+1) if not num % i)
if num == SQRT ** 2: ans -= SQRT
return ans - num == num
``````

• just for fun:

``````    perfect = set([6, 28, 496, 8128, 33550336, 8589869056])
def checkPerfectNumber(self, num):
return num in self.perfect
``````

• @lee215

``````class Solution(object):
checkPerfectNumber = {6, 28, 496, 8128, 33550336}.__contains__
``````

• @realisking
Can you please explain the logic you have employed

• @ash-ps312
Just check every `i` from `1` to `sqrt(num)`. When `num % i == 0`, we add both `i` and `num//i` to our result. And remember to exclude the `num` itself and check if we added a duplicate `sqrt(num)` in the end. Clear now?

• @realisking
It solves the question, but I am amazed by the logic you have used, may I know how did you come up with this?

• @ash.ps312
LOL, I'd say it's observation rather than logic. As an old saying goes, practice makes perfect. I know nothing about computer science several months ago actually. Keep going and good luck!

• @realisking Thanks for this simple solution, I reduced the code a bit more

``````if nums<2: return False
ans,sqrt=0,int(math.sqrt(nums))
ans=sum(i+nums//i for i in range(1,sqrt+1) if not nums % i)
return 2*nums-ans==0
``````

• @dhananjay6
Although from observation we know all 32-bits `perfect num` won't equal to its `sqrt ** 2`, I do think we need `extra` check before we have further mathematical proof. Thus your reduction is not appropriate IMHO at least for right now.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.