Skip to content

Instantly share code, notes, and snippets.

@weatheredwatcher
Created March 31, 2010 14:27
Show Gist options
  • Save weatheredwatcher/350387 to your computer and use it in GitHub Desktop.
Save weatheredwatcher/350387 to your computer and use it in GitHub Desktop.
n = prompt("Enter a number greater than 1: ");
var counter = 0
function collatz(n, counter) {
document.display.myCounter.value=counter;
if (n>1) {
if (n%2) {
t = 3 * n + 1;
counter++;
collatz(t, counter);
} else{
t = n/2;
counter++;
collatz(t, counter);
};
};
return counter;
}
collatz(n, counter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment