Skip to content

Latest commit

 

History

History
41 lines (26 loc) · 681 Bytes

507.md

File metadata and controls

41 lines (26 loc) · 681 Bytes

507 Perfect Number

Description

link


Solution

  • See Code

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