【 topic description 】
【 原 文 】
All the factors of an integer are required.
I * I <=num; I * I <=num;
2. The initial value of sum is 1, and the initial value of I is 2. This avoids adding num to the loop.
【 Source code 】
class Solution {
public:
bool checkPerfectNumber(int num) {
if(num==0||num==1) return false;
int sum=1;
for(int i=2; i*i<=num; i++){if(num%i==0) sum=sum+i+num/i;
if(i*i==num) sum-=i;
if(sum>num) return false;
}
return sum==num?true:false; }};Copy the code