Skip to content

Instantly share code, notes, and snippets.

@tuliofaria
Created February 15, 2017 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuliofaria/a92d387b68931d1294fddb1c4e259723 to your computer and use it in GitHub Desktop.
Save tuliofaria/a92d387b68931d1294fddb1c4e259723 to your computer and use it in GitHub Desktop.
// 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