Perfect Number

此题有一个数学规律的算法,普通点的方法就是O(√n)的算法

class Solution(object):
    def checkPerfectNumber(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num <= 1:
            return False

        result = 0
        for i in xrange(1, int(num ** 0.5) + 1):
            if num % i == 0:
                if i == 1:
                    result += 1
                elif i == num / i:
                    result += i
                else:
                    result += (i + num / i)

            if result > num:
                return False

        return result == num

results matching ""

    No results matching ""