Skip to content

Instantly share code, notes, and snippets.

@daiksy
Created November 25, 2012 08:23
Show Gist options
  • Save daiksy/4142827 to your computer and use it in GitHub Desktop.
Save daiksy/4142827 to your computer and use it in GitHub Desktop.
Project Euler Problem 14
/**
* http://projecteuler.net/problem=14
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2014
*/
object collatz {
def apply(i: Int) = calc(i :: Nil).reverse
private def calc(xs: List[Long]): List[Long] = {
xs.head match {
case 1 => xs
case n if (n % 2 == 0) => calc(n / 2 :: xs)
case n => calc((n * 3) + 1 :: xs)
}
}
}
val ret = (1 until 1000000).map(n => (n, collatz(n).size)).sortBy(_._2).last._1
println(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment