Created
March 22, 2011 17:00
-
-
Save dbyrne/881577 to your computer and use it in GitHub Desktop.
Project Euler #2 - Scala
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
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