Ai
1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_507.java 977 Bytes
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 2019-05-28 23:01 +08:00 . refactor 507
package com.fishercoder.solutions;
/**
* 507. Perfect Number
*
* 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)
*/
public class _507 {
public static class Solution1 {
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;
if (i != num / i) {
sum += num / i;
}
}
}
sum++;
return sum == num;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助