# 507. Perfect Number

We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.

Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.

Example:

``````Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14
``````

Note: The input number n will not exceed 100,000,000. (1e8)

``````class Solution {
public:
bool checkPerfectNumber(int num) {
int sum = 1;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
sum += i + (num / i == i ? 0 : num / i);
}
}
return num != 1 && sum == num;
}
};
``````

``````class Solution {
public:
bool checkPerfectNumber(int num) {
return num == 6 || num == 28 || num == 496 || num == 8128 || num == 33550336;
}
};
``````

Self Dividing Numbers

https://leetcode.com/problems/perfect-number/

https://leetcode.com/problems/perfect-number/discuss/98594/simple-java-solution

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation