Created
January 31, 2022 16:37
-
-
Save gkucmierz/ec951531f107ae307ecc0638fbb23fdf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 kaprekarNext = n => { | |
const a = [...n + ''].map(n => +n); | |
a.sort((a, b) => b - a); | |
return +a.join('') - +a.reverse().join(''); | |
}; | |
const kaprekarLoop = n => { | |
let last; | |
while (last !== n) { | |
last = n; | |
n = kaprekarNext(n); | |
} | |
return n; | |
}; | |
const blackHoles = new Map(); | |
for (let i = 1111; i <= 9999; ++i) { | |
const hole = kaprekarLoop(i); | |
const cnt = blackHoles.get(hole) || 0; | |
blackHoles.set(hole, cnt + 1); | |
} | |
console.log( | |
[...blackHoles].map(([n , times]) => `${n} - ${times} times`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment