1. Description
A perfect number is a positive integer that is equal to the sum of positive divisors, excluding the number of 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.
constraints :
- 1 <= num <= \(10^{8}\)
2. Algorithms
- Start
- If num is 1, return False
- Declare variable named sum_ and initialize it to 1.
- Repeat the steps through integers range 2 - int(sqrt(num)) + 1.
- If i is divisor of num, increment sum_ by (i + num/i).
- Return True if sum_ is same with num.
- End
3. Codes
class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if num == 1 : return False
sum_ = 1
for i in range(2, int(math.sqrt(num)) + 1) :
if num % i == 0:
sum_ += (i + num/i)
return sum_ == num
4. Conclusion
'LeetCode > Easy' 카테고리의 다른 글
511 Game Play Analysis I (0) | 2022.09.03 |
---|---|
509 Fibonacci Number (0) | 2022.09.03 |
506 Relative Ranks (0) | 2022.09.03 |
504 Base 7 (0) | 2022.09.02 |
501 Find Mode in Binary Search Tree (0) | 2022.09.02 |