Created
August 7, 2018 12:13
-
-
Save Silverium/3b3acc94993e10ee7b9e3a9251b3e6b2 to your computer and use it in GitHub Desktop.
Amicable Numbers. Números amigos
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
function sumProperDivisors(num) { | |
return [...Array(Math.floor(num / 2))].reduce((acc,v, ind) => (num % (ind + 1)) === 0 ? (acc + ind + 1) : acc, 0) | |
} | |
function areAmicableNumbers(num1, num2) { | |
return sumProperDivisors(num1) === num2 && sumProperDivisors(num2) === num1 | |
} | |
function amicableNumbersInRange(start,end){ | |
let nums = [...Array(Number.parseInt((end - start) / 2))].map((v,i)=>start+i); | |
return [...nums.reduce((acc, v, i) => { | |
const possibleAmical = sumProperDivisors(v); | |
return !acc.has(possibleAmical) && areAmicableNumbers(v, possibleAmical) ? acc.set(v, possibleAmical) : acc; | |
},new Map()).entries()] | |
} | |
console.log(areAmicableNumbers(220,284)) | |
let amicableFrom200to1300 = amicableNumbersInRange(200,1300) | |
console.log(amicableFrom200to1300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version without input checking