Skip to content

Instantly share code, notes, and snippets.

@dbyrne
Created March 22, 2011 17:00
Show Gist options
  • Save dbyrne/881577 to your computer and use it in GitHub Desktop.
Save dbyrne/881577 to your computer and use it in GitHub Desktop.
Project Euler #2 - Scala
def fib(v1:Int, v2:Int): Stream[Int] = v1 match {
case 0 => Stream.cons(1,fib(1,0))
case 1 => Stream.cons(2,fib(2,1))
case _ => Stream.cons(v1+v2,fib(v1+v2,v1))
}
val fibStream = fib(0,0)
fibStream.filter(_%2 == 0).takeWhile(_ <= 4000000).foldLeft(0)(_+_) //returns 4613732
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment