Created
September 25, 2017 16:29
-
-
Save matinkaboli/6f00f6df0ad6c244440060746a8e2bc6 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
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