-
-
Save xuwei-k/4065652 to your computer and use it in GitHub Desktop.
Project Euler Probrem 2
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
/* | |
* http://projecteuler.net/problem=2 | |
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%202 | |
*/ | |
val fib: List[Int] => List[Int] = { | |
case Nil | _ :: Nil => throw new IllegalArgumentException | |
case xs @ (a :: b :: tail) => { | |
val nextNum = a + b | |
if (nextNum >= 4000000) { | |
xs | |
} else { | |
fib(nextNum :: xs) | |
} | |
} | |
} | |
fib(List(2, 1)).filter(_ % 2 == 0).sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment