-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path507.perfect-number.java
More file actions
53 lines (52 loc) · 1.16 KB
/
507.perfect-number.java
File metadata and controls
53 lines (52 loc) · 1.16 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
/*
* @lc app=leetcode id=507 lang=java
*
* [507] Perfect Number
*
* https://leetcode.com/problems/perfect-number/description/
*
* algorithms
* Easy (34.37%)
* Likes: 161
* Dislikes: 455
* Total Accepted: 43.2K
* Total Submissions: 124.9K
* Testcase Example: '28'
*
* 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)
*
*/
class Solution {
public boolean checkPerfectNumber(int num) {
if (num <= 1) {
return false;
}
int sum = 0;
for (int i = 1; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
sum += i;
if (i > 1 && i * i != num) {
sum += num / i;
}
}
}
return sum == num;
}
}
// Time complexity O(sqrt(N)), space complexity O(1)