forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfectNumber.js
More file actions
Latest commit
30 lines (26 loc) · 813 Bytes
/
Copy pathPerfectNumber.js
File metadata and controls
30 lines (26 loc) · 813 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
/**
* Author: dephraiim
* License: GPL-3.0 or later
*
* == Perfect Number ==
* In number theory, a perfect number is a positive integer that is equal to the sum of
* its positive divisors(factors), excluding the number itself.
* For example: 6 ==> divisors[1, 2, 3, 6]
* Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6
* So, 6 is a Perfect Number
* Other examples of Perfect Numbers: 28, 486, ...
*
* More on Perfect Number:
* https://en.wikipedia.org/wiki/Perfect_number
*
*/
constfactorsExcludingNumber=(n)=>{
return[...Array(n).keys()].filter((num)=>n%num===0)
}
constperfectNumber=(n)=>{
constfactorSum=factorsExcludingNumber(n).reduce((num,initialValue)=>{
returnnum+initialValue
},0)
returnfactorSum===n
}
export{perfectNumber}