-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfectNumber.java
More file actions
89 lines (78 loc) · 1.43 KB
/
PerfectNumber.java
File metadata and controls
89 lines (78 loc) · 1.43 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.leetcode.editor.cn;;
//对于一个 正整数,如果它和除了它自身以外的所有 正因子 之和相等,我们称它为 「完美数」。
//
// 给定一个 整数 n, 如果是完美数,返回 true,否则返回 false
//
//
//
// 示例 1:
//
//
//输入:num = 28
//输出:true
//解释:28 = 1 + 2 + 4 + 7 + 14
//1, 2, 4, 7, 和 14 是 28 的所有正因子。
//
// 示例 2:
//
//
//输入:num = 6
//输出:true
//
//
// 示例 3:
//
//
//输入:num = 496
//输出:true
//
//
// 示例 4:
//
//
//输入:num = 8128
//输出:true
//
//
// 示例 5:
//
//
//输入:num = 2
//输出:false
//
//
//
//
// 提示:
//
//
// 1 <= num <= 108
//
// Related Topics 数学
// 👍 131 👎 0
/**
* 507 完美数
* @date 2021-12-31 10:52:30
* @author shang.liang
*/
public class PerfectNumber{
public static void main(String[] args) {
Solution solution = new PerfectNumber().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean checkPerfectNumber(int num) {
if (num == 1) {
return false;
}
int sum = 1;
for (int i = 2; i < Math.sqrt(num); i++) {
if (num % i == 0) {
sum += num / i + i;
}
}
return sum == num;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}