Created
February 15, 2017 23:43
-
-
Save tuliofaria/a92d387b68931d1294fddb1c4e259723 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
// Veja mais em: https://www.youtube.com/watch?v=voV5OBCqlVs | |
function cycleLen(n){ | |
let steps = 1 | |
while(n !== 1){ | |
if ( n % 2 === 0){ | |
n = n / 2 | |
}else{ | |
n = 3 * n + 1 | |
} | |
steps++ | |
} | |
return steps | |
} | |
function maxCycle(i,j){ | |
let max = cycleLen(i) | |
for (let k = i + 1; k <= j; k++) { | |
let currentCycle = cycleLen(k) | |
if (currentCycle > max) { | |
max = currentCycle | |
} | |
} | |
return i+' '+j+' '+max | |
} | |
console.log(maxCycle(1,10)) | |
console.log(maxCycle(100,200)) | |
console.log(maxCycle(201,210)) | |
console.log(maxCycle(900,1000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment