Created
January 10, 2022 10:36
-
-
Save gkucmierz/4eed7db0b8a4b8db96efd79484fe6b6d 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 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