- Date : 2021.10.03(일)
- Time : 20분
- 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
- 1 <= num <= 10^8
- Input: num = 28
- Output: true
- Explanation: 28 = 1 + 2 + 4 + 7 + 14
- 1, 2, 4, 7, and 14 are all divisors of 28.
- 1, 2, 4, 7, and 14 are all divisors of 28.
def checkPerfectNumber(self, num: int) -> bool:
divisors = self.getMyDivisor(num)
divisors.pop()
if sum(divisors) == num: return True
else: return False
def getMyDivisor(self, n: int) -> List[int]:
divisorsList = []
for i in range(1, int(n ** (1 / 2)) + 1):
if (n % i == 0):
divisorsList.append(i)
if ((i ** 2) != n):
divisorsList.append(n // i)
divisorsList.sort()
return divisorsList
: 여기서 말하는 perfect number는 자기 자신을 제외한 약수의 합이 자신과 같은 경우이다. 그렇기 때문에 나는 따로 약수를 구해서 배열로 return 해주는 `함수 getMyDivisor를 생성해주었다. ```N = A * B```로 나타낼 수 있다는 것을 이용한 풀이이다. 항상 약수를 구하면 그 짝이 되는 수가 존재한다 (ex. 6 = 2 * 3 ). for 문을 이용해 자연수 ```N의 제곱근```까지의 약수를 구하면 그 짝이 되는 약수는 자동으로 구할 수 있다. N = A * B 일 때, A == B 일 수 있기 때문에 (ex. 25 = 5 * 5 ) 값을 중복해서 넣어주지 않기 위해 if 문으로 제곱했을 때 n이 되지 않는지 검사를 해줬다. 이렇게 약수를 구한 후 맨 마지막에 자기 자신을 빼기 위해 pop 해주면 준비된다. 그리고 sum을 이용해 총 합과 같은지 확인해주면 된다.