Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Last active May 20, 2026 07:08
Show Gist options
  • Select an option

  • Save gkucmierz/ec951531f107ae307ecc0638fbb23fdf to your computer and use it in GitHub Desktop.

Select an option

Save gkucmierz/ec951531f107ae307ecc0638fbb23fdf to your computer and use it in GitHub Desktop.
Run this code instantly in your browser: https://instacode.app/gist/ec951531f107ae307ecc0638fbb23fdf
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