# 507. Perfect Number 完美数

@TOC

## # 题目描述

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)

## # 解题方法

Java解法如下：

``````public class Solution {
public boolean checkPerfectNumber(int num) {
if(num == 1) return false;
int sum = 0;
for(int i = 2; i < Math.sqrt(num);  i++){
if(num % i == 0){
sum += i + num / i;
}
}
sum++;
return sum == num;
}
}
``````

C++版本如下：

``````class Solution {
public:
bool checkPerfectNumber(int num) {
if(num <= 1) return false;
int sums = 1;
for(int i = 2; i < (int) sqrt(num) + 1; ++i){
if(num % i == 0){
sums += i + num / i;
}
}
return num == sums;
}
};
``````

Python版本如下：

``````class Solution(object):
def checkPerfectNumber(self, num):
"""
:type num: int
:rtype: bool
"""
if num <= 1: return False
sums = 1
for i in range(2, int(math.sqrt(num) + 1)):
if num % i == 0:
sums += i + num / i
return num == sums
``````

## # 日期

2017 年 5 月 16 日 2018 年 11 月 24 日 —— 周日开始！一周就过去了～