requirements

For a positive integer, we call it a “perfect number” if it is equal to the sum of all the positive factors except itself.

Given an integer n, return true if it is perfect, false otherwise

Example 1:

Input: 28 Output: True Explanation: 28 = 1 + 2 + 4 + 7 + 14 1, 2, 4, 7, and 14 are all positive factors of 28.Copy the code

Example 2:

Input: num = 6 Output: trueCopy the code

Example 3:

Input: num = 496 Output: trueCopy the code

Example 4:

Input: num = 8128 Output: trueCopy the code

Example 5:

Input: num = 2 Output: falseCopy the code

The core code

class Solution:
    def checkPerfectNumber(self, num: int) - >bool:
        if num <= 1:
            return False
        import math
        s = 1
        for i in range(2.int(math.sqrt(num)+1)) :if num % i == 0:
                s += i + int(num / i)
        return s == num
Copy the code

Start at 2, go to the square root of the number, find all the factors, add them to the target number, and get a bool.