Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Forked from daiksy/problem2.scala
Created November 13, 2012 13:01
Show Gist options
  • Save xuwei-k/4065652 to your computer and use it in GitHub Desktop.
Save xuwei-k/4065652 to your computer and use it in GitHub Desktop.
Project Euler Probrem 2
/*
* 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