Skip to content

Instantly share code, notes, and snippets.

@Silverium
Created August 7, 2018 12:13
Show Gist options
  • Save Silverium/3b3acc94993e10ee7b9e3a9251b3e6b2 to your computer and use it in GitHub Desktop.
Save Silverium/3b3acc94993e10ee7b9e3a9251b3e6b2 to your computer and use it in GitHub Desktop.
Amicable Numbers. Números amigos
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)
@Silverium
Copy link
Author

Version without input checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment