Skip to content

Instantly share code, notes, and snippets.

@code-shoily
Created November 9, 2014 13:32
Show Gist options
  • Save code-shoily/984f73e5aa4f060e7dbf to your computer and use it in GitHub Desktop.
Save code-shoily/984f73e5aa4f060e7dbf to your computer and use it in GitHub Desktop.
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