Skip to content

Instantly share code, notes, and snippets.

@zerosum
Last active December 11, 2015 08:16
Show Gist options
  • Save zerosum/4571345 to your computer and use it in GitHub Desktop.
Save zerosum/4571345 to your computer and use it in GitHub Desktop.
Project Euler: Probrem 2
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])
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