Skip to content

Instantly share code, notes, and snippets.

@nolleto
Last active March 30, 2020 13:10
Show Gist options
  • Save nolleto/c3599118d6e610782253c1f02f416c26 to your computer and use it in GitHub Desktop.
Save nolleto/c3599118d6e610782253c1f02f416c26 to your computer and use it in GitHub Desktop.
Check if number is perfect
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