Created
November 9, 2014 13:32
-
-
Save code-shoily/984f73e5aa4f060e7dbf to your computer and use it in GitHub Desktop.
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
var LIMIT = 1000000; | |
var cache = []; | |
var output = [1, 1]; | |
for (var i = 0; i < LIMIT; i++) { | |
cache[i] = i==1 ? i : 0; | |
} | |
for (i = 1; i < LIMIT; i++) { | |
var n = i, ll = 0, Q = []; | |
while (n > (LIMIT - 1) || cache[n] < 1) { | |
ll++; | |
Q.push(n); | |
n = n%2 == 0 ? n/2 : 3*n+1; | |
} | |
var p = cache[n]; | |
for (var j = 0; j < ll; j++) { | |
var m = Q[j]; | |
if (m < LIMIT) { | |
newLength = cache[n] + ll - j; | |
cache[m] = newLength; | |
if (newLength > output[1]) output = [i, newLength]; | |
} | |
} | |
} | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment