- See Code
class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if num <= 1:
return False
return sum(self.getFactors(num)) == num
def getFactors(self, num):
sqrt_num = int(math.sqrt(num))
# 1 is always a factor
factors = set([1])
# Start from 2
for i in range(2, sqrt_num+1):
if num % i == 0:
factors.add(i)
factors.add(num/i)
return factors