Leetcode 507. Perfect Number

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Perfect Number

2. Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
bool checkPerfectNumber(int num) {
if(num == 1) {
return false;
}
int sum = 1;
int root = sqrt(num) + 1;
for(int i = 2; i < root; i++) {
if(num % i == 0) {
sum += i;
if(i * i != num) {
sum += num / i;
}
}
}
return sum == num;
}
};

Reference

  1. https://leetcode.com/problems/perfect-number/description/
如果有收获,可以请我喝杯咖啡!