Skip to content

Instantly share code, notes, and snippets.

@mtayseer
Last active August 29, 2015 13:55
Show Gist options
  • Save mtayseer/8725256 to your computer and use it in GitHub Desktop.
Save mtayseer/8725256 to your computer and use it in GitHub Desktop.
Project euler #2
def fibonacci(max):
a, b = 1, 2
while a < max:
yield a
a, b = b, a+b
print sum(x for x in fibonacci(4000000) if x % 2 == 0)
@mtayseer
Copy link
Author

Another solution

from itertools import takewhile

def fibonacci2():
    a, b = 1, 2
    while True:
        yield a
        a, b = b, a+b

sum(x for x in takewhile(lambda x: x <= 4000000, fibonacci2()) if not x % 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment