Skip to content

Instantly share code, notes, and snippets.

@lykkin
Created April 2, 2017 18:27
Show Gist options
  • Save lykkin/76c8bd5ea76fe9934287d310c87b4e61 to your computer and use it in GitHub Desktop.
Save lykkin/76c8bd5ea76fe9934287d310c87b4e61 to your computer and use it in GitHub Desktop.
function collatz(n) {
var chain = [n]
while (n !== 1) {
if (n % 2 === 0) {
n /= 2
} else {
n = n * 3 + 1
}
chain.push(n)
}
chain.push(1)
return chain
}
Array.apply(null, Array(1000)).map(function(x, i) {return collatz(i + 1)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment