2 Simple Python solutions


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

    Since the "Perfect Numbers" are quite a few (only 5 under 1e8 ), we can calculated in advance.

    class Solution(object):
        def checkPerfectNumber(self, num):
            """
            :type num: int
            :rtype: bool
            """
            return num == 6 or num == 28 or num == 496 or num == 8128 or num == 33550336
    

  • 0
    Z

    If there's a perfect number which is also a perfect square number, the sum of divisor will be incorrectly calculated. (Thankfully there's no such kind of number within this range)


Log in to reply
 

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