Skip to content

Instantly share code, notes, and snippets.

@zerosum
Created January 22, 2013 12:34
Show Gist options
  • Select an option

  • Save zerosum/4594315 to your computer and use it in GitHub Desktop.

Select an option

Save zerosum/4594315 to your computer and use it in GitHub Desktop.
Project Euler: Problem 14
package euler.zerosum
object Euler0014 {
def main(args: Array[String]) {
println(
(1 until 1000000).map(x => solveCollatz(x, x, 0)).maxBy(_._2)._1
)
}
def solveCollatz(i: Long, tmp: Long, counter: Long): (Long, Long) = {
if (tmp == 1) {
(i, counter)
} else if (tmp%2 == 0) {
solveCollatz(i, tmp/2, counter+1)
} else {
solveCollatz(i, tmp*3+1 ,counter+1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment