math and geometry December 27, 2022

Perfect number

Time O(sqrt(n)) Space O(1) Open original problem

We will find all the divisors of the number and then sum them up. If the sum is equal to the number, then it is a perfect number.

class Solution:
    def checkPerfectNumber(self, num: int) -> bool:
        if num <= 1:
            return False
        divisorSum = 1
        for i in range(2, int(sqrt(num)) + 1):
            if num % i == 0:
                divisorSum += i
                divisorSum += num // i
        return divisorSum == num

Time complexity: O(sqrt(n))
Space complexity: O(1)