Skip to content

Instantly share code, notes, and snippets.

@matinkaboli
Created September 25, 2017 16:29
Show Gist options
  • Save matinkaboli/6f00f6df0ad6c244440060746a8e2bc6 to your computer and use it in GitHub Desktop.
Save matinkaboli/6f00f6df0ad6c244440060746a8e2bc6 to your computer and use it in GitHub Desktop.
function collatz(n, times = 1) {
while (n > 1) {
times += 1;
if (n % 2 === 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
}
return times;
}
let number = 0;
let len = 0;
for (let i = 0; i < 1000000; i++) {
const c = collatz(i);
if (c > number) {
number = c;
len = i;
}
}
console.log(`${number} ${len}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment