-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathL0507_PerfectNumber.java
More file actions
59 lines (51 loc) · 1.55 KB
/
L0507_PerfectNumber.java
File metadata and controls
59 lines (51 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* https://leetcode.cn/problems/perfect-number/
*
* 对于一个 正整数,如果它和除了它自身以外的所有 正因子 之和相等,我们称它为 「完美数」。
*
* 给定一个 整数 n, 如果是完美数,返回 true;否则返回 false。
*
* 示例 1:
* 输入:num = 28
* 输出:true
* 解释:28 = 1 + 2 + 4 + 7 + 14
* 1, 2, 4, 7, 和 14 是 28 的所有正因子。
*
* 示例 2:
* 输入:num = 7
* 输出:false
*
* 提示:
* - 1 <= num <= 10^8
*/
public class L0507_PerfectNumber {
/**
* 数学方法
* 只需要遍历到 sqrt(num)
*/
public boolean checkPerfectNumber(int num) {
if (num <= 1) {
return false;
}
int sum = 1; // 1 一定是因子
// 只需要遍历到 sqrt(num)
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
if (i * i != num) {
sum += num / i;
}
}
}
return sum == num;
}
public static void main(String[] args) {
L0507_PerfectNumber solution = new L0507_PerfectNumber();
// 测试用例 1
System.out.println(solution.checkPerfectNumber(28)); // 预期输出:true
// 测试用例 2
System.out.println(solution.checkPerfectNumber(7)); // 预期输出:false
// 测试用例 3
System.out.println(solution.checkPerfectNumber(6)); // 预期输出:true (1+2+3=6)
}
}