File size: 1,368 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cp-10016", "name": "LeetCode 0507: Perfect Number"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Perfect Number (Difficulty: Easy)\nTags: Math\n\nA perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.\nGiven an integer n, return true if n is a perfect number, otherwise return false.\n\nExample 1:\nInput: num = 28\nOutput: true\nExplanation: 28 = 1 + 2 + 4 + 7 + 14\n1, 2, 4, 7, and 14 are all divisors of 28.\n\nExample 2:\nInput: num = 7\nOutput: false\n\nConstraints:\n- 1 <= num <= 10^8"}}
{"type": "message", "message": {"role": "assistant", "content": "# Time:  O(sqrt(n))\n# Space: O(1)\n\nclass Solution(object):\n    def checkPerfectNumber(self, num):\n        \"\"\"\n        :type num: int\n        :rtype: bool\n        \"\"\"\n        if num <= 0:\n            return False\n\n        sqrt_num = int(num ** 0.5)\n        total = sum(i+num//i for i in range(1, sqrt_num+1) if num%i == 0)\n        if sqrt_num ** 2 == num:\n            total -= sqrt_num\n        return total - num == num\n\n"}}