Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save gkucmierz/4eed7db0b8a4b8db96efd79484fe6b6d to your computer and use it in GitHub Desktop.
Run this code instantly in your browser: https://instacode.app/gist/4eed7db0b8a4b8db96efd79484fe6b6d
const collatz = n => {
const res = [n];
while (n !== 1) {
if (n % 2 === 0) {
n /= 2;
} else {
n = n * 3 + 1;
}
res.push(n);
}
return res;
};
console.log(
collatz(9663),
collatz(341),
collatz(27),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment