Skip to content

Instantly share code, notes, and snippets.

@todashuta
Created September 22, 2014 13:41
Show Gist options
  • Select an option

  • Save todashuta/eb9326d470047849efe5 to your computer and use it in GitHub Desktop.

Select an option

Save todashuta/eb9326d470047849efe5 to your computer and use it in GitHub Desktop.
;(function() {
var collatz = function(i) {
var list = [i];
while (list.slice(-1) > 1) {
var n = list.slice(-1);
if (n % 2 === 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
list.push(n);
}
return list;
}
console.log(collatz(3));
}());
@todashuta
Copy link
Copy Markdown
Author

[ 3, 10, 5, 16, 8, 4, 2, 1 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment