Skip to content

Instantly share code, notes, and snippets.

@MichaelBlume
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelBlume/9045981 to your computer and use it in GitHub Desktop.
Save MichaelBlume/9045981 to your computer and use it in GitHub Desktop.
from itertools import takewhile, ifilter
def fibs():
fib0 = 1
fib1 = 1
while True:
yield fib0
newfib = fib0 + fib1
fib0 = fib1
fib1 = newfib
even_fibs = ifilter(lambda i: i % 2 == 0, fibs())
even_fibs_under_four_million = takewhile(lambda i: i < 4000000, even_fibs)
result = sum(even_fibs_under_four_million)
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment