Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Created January 31, 2022 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkucmierz/ec951531f107ae307ecc0638fbb23fdf to your computer and use it in GitHub Desktop.
Save gkucmierz/ec951531f107ae307ecc0638fbb23fdf to your computer and use it in GitHub Desktop.
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