LeetCode

来源:互联网 发布:lol徐老师淘宝网站 编辑:程序博客网 时间:2024/06/16 17:15

Q:
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.

Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
Example:

Input: 28Output: TrueExplanation: 28 = 1 + 2 + 4 + 7 + 14

Note: The input number n will not exceed 100,000,000. (1e8)

A:

class Solution(object):    def checkPerfectNumber(self, num):        """        :type num: int        :rtype: bool        """        if num <= 1:            return False        sumNums = 1        k = 2        while k <= int(num ** 0.5):            if num % k == 0:                temp = num / k                sumNums += k                sumNums += temp            k += 1        if num == sumNums:            return True        else:            return False