Skip to content

Instantly share code, notes, and snippets.

@gkucmierz
Created January 10, 2022 10:36
Show Gist options
  • Save gkucmierz/4eed7db0b8a4b8db96efd79484fe6b6d to your computer and use it in GitHub Desktop.
Save gkucmierz/4eed7db0b8a4b8db96efd79484fe6b6d to your computer and use it in GitHub Desktop.
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