Last active
December 11, 2015 08:16
-
-
Save zerosum/4571345 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
def fibo_iter(n): | |
l = [] | |
x, y = 1, 1 | |
while y < n: | |
l.append(y) | |
x, y = y, x+y | |
return l | |
def fibo_rec(x,y,n,l): | |
l.append(x) | |
return l if y>n else fibo_rec(y,x+y,n,l) | |
print sum([a for a in fibo_iter(4*10**6) if a%2==0]) | |
print sum([a for a in fibo_rec(1,2,4*10**6,[]) if a%2==0]) |
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
object Euler0002 { | |
def main(args: Array[String]): Unit = | |
println(fibo_rec(1,2,4000000).filter(_%2==0).sum) | |
def fibo_rec(x: Int, y: Int, max: Int): List[Int] = | |
if (y > max) Nil else y :: fibo_rec(y, x+y, max) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment