Skip to content

Instantly share code, notes, and snippets.

@daiksy
Created November 13, 2012 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daiksy/4065593 to your computer and use it in GitHub Desktop.
Save daiksy/4065593 to your computer and use it in GitHub Desktop.
Project Euler Problem 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] = {xs =>
xs match {
case Nil => throw new IllegalArgumentException
case i::Nil => throw new IllegalArgumentException
case _ => {
val nextNum = xs.head + xs.tail.head
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