-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckperfectnumber.js
More file actions
45 lines (34 loc) · 889 Bytes
/
checkperfectnumber.js
File metadata and controls
45 lines (34 loc) · 889 Bytes
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
/*
Perfect Number
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.
Given an integer n, return true if n is a perfect number, otherwise return false.
Example 1:
Input: num = 28
Output: true
Explanation: 28 = 1 + 2 + 4 + 7 + 14
1, 2, 4, 7, and 14 are all divisors of 28.
Example 2:
Input: num = 6
Output: true
Example 3:
Input: num = 496
Output: true
Example 4:
Input: num = 8128
Output: true
Example 5:
Input: num = 2
Output: false
*/
/**
* @param {number} num
* @return {boolean}
*/
var checkPerfectNumber = function (num) {
if (num == 1) return false;
let divisors = 0;
for (let i = 1; i < num / 2 + 1; i++) { // num / 2 + 1 => to reduce loops
if (num % i == 0) { divisors += i }
};
return divisors === num
};