You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A [perfect number](https://en.wikipedia.org/wiki/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 <= 108
풀이
function checkPerfectNumber(num) {
if (num <= 1) return false;
let total = 1; // 1은 항상 약수니까 미리 더해놓기
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) {
total += i;
if (i !== num / i) {
total += num / i;
}
}
}
return total === num;
}
연산식에 대한 이해도 및 스킬 부족
풀이 방법
1은 무조건 약수니까 미리 total에 넣고, 2부터 sqrt(num) = 제곱근까지 돌면서 약수 찾아서 더해주는 연산식
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
문제
풀이
풀이 방법
Beta Was this translation helpful? Give feedback.
All reactions