Problem statement

https://leetcode.com/problems/perfect-number/

Solution

Just find all divisors in O(sqrt{n}) and find their sum.

Complexity

Time complexity is (sqrt{n}), space complexity is O(1)

Code

class Solution:
    def checkPerfectNumber(self, num):
        sm = 0
        for i in range(1, int(sqrt(num)) + 1):
            if num % i == 0:
                sm += (i + num//i)
            if i*i == num: sm -= i
                
        return num*2 == sm

Remark

Or we can use the fact that there is very small number of perfect numbers and check them all.