Created
November 25, 2012 08:23
-
-
Save daiksy/4142827 to your computer and use it in GitHub Desktop.
Project Euler Problem 14
This file contains hidden or 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
/** | |
* 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