tinycodinglm / cp /cp-10016.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
1.37 kB
opencode trace
System You are an expert LeetCode problem solver. You solve problems efficiently with clean code.

You are an expert LeetCode problem solver. You solve problems efficiently with clean code.

User

Problem: Perfect Number (Difficulty: Easy)
Tags: Math

A 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.
Given an integer n, return true if n is a perfect number, otherwise return false.

Example 1:
Input: num = 28
Output: true
Explanation: 28 = 1 + 2 + 4 + 7 + 14
1, 2, 4, 7, and 14 are all divisors of 28.

Example 2:
Input: num = 7
Output: false

Constraints:

  • 1 <= num <= 10^8
Assistant

Time: O(sqrt(n))

Space: O(1)

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

markdown
sqrt_num = int(num ** 0.5)
    total = sum(i+num//i for i in range(1, sqrt_num+1) if num%i == 0)
    if sqrt_num ** 2 == num:
        total -= sqrt_num
    return total - num == num