Last active
March 30, 2020 13:10
-
-
Save nolleto/c3599118d6e610782253c1f02f416c26 to your computer and use it in GitHub Desktop.
Check if number is perfect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getAllDivisors = n => { | |
const divisors = [] | |
let currentNumber = 1 | |
while (currentNumber < n) { | |
if ((28 % currentNumber) === 0) { | |
divisors.push(currentNumber) | |
} | |
currentNumber++ | |
} | |
return divisors | |
} | |
const sum = arr => { | |
let sum = 0; | |
arr.forEach(n => { | |
sum += n | |
}) | |
return sum | |
} | |
const isPerfectNumber = n => { | |
const divisors = getAllDivisors(n) | |
const divisorsSum = sum(divisors) | |
return divisorsSum === n | |
} | |
isPerfectNumber(28) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment