Created
May 5, 2015 21:44
-
-
Save rustyconover/15c2999f13730466fad6 to your computer and use it in GitHub Desktop.
Collatz sequence in Javascript
This file contains 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
'use strict'; | |
function collatz_count_until_1(n) { | |
var count = 0; | |
while(n !== 1) { | |
if(n % 2 === 0) { | |
n /= 2; | |
} else { | |
n = (3 * n) + 1; | |
} | |
count++; | |
} | |
return count; | |
} | |
var max; | |
for(var i = 1; i < 300000; i++) { | |
var l = collatz_count_until_1(i); | |
if(max === undefined || max[0] < l) { | |
max = [l, i] | |
} | |
} | |
console.log("Maximum stopping distance %d, starting number %d", max[0], max[1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment