A positive integer is called a "perfect number" if it is equal to the sum of all positive factors except itself.
Given a positive integer n, if it is a perfect number, return True, otherwise return False
Example:
Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14
Note:
The input number n will not exceed 100,000,000 . (1e8)
See: https://leetcode.com/problems/perfect-number/description/
C++:
class Solution { public: bool checkPerfectNumber(int num) { if(num==1) { return false; } int sum=1; for(int i=2;i*i<=num;++i) { if(num%i==0) { sum+=(i+num/i); } if(i*i==num) { sum-=i; } if(sum>num) { return false; } } return sum==num; } };
Reference: http://www.cnblogs.com/grandyang/p/6636879.html