原题
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
代码分析
求出输入值的所有质因子(不包括自身),累加后是否等于输入值,等于便是Perfect Number
代码分析
public bool CheckPerfectNumber(int num)
{
int sum = 1;
int tmp = num;
if (num == 0 || num==1)//特别注意边界值
return false;
while (num%2 == 0)
{
num /= 2;
sum += num+tmp/num;
}
return sum==tmp;
}