Simple python solution, easy to understand with explaination


  • 0
    Z

    We can think about this problem this way, the sum of the divisor for a particular number can be forms like this.
    e.g.
    28 = 1 + (2 + 14) + (4 + 7)

    We define a variable named total and starts from 1, then we start trying every integer i from [2 to sqrt(num)), whenever we found an integer that divisible by num, we calculate total += i + num / i.

    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

Log in to reply
 

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