Sunday, December 5, 2010

Project Euler - Python - Problem 2

Python has some very cool iteration features - ive never used (or even heard of) generator functions before so I find this concept quite powerful.

from itertools import takewhile

def fibonacci():
    a,b=0,1
    while 1:
        yield a
        a,b=b,a+b
print(sum([i for i in takewhile(lambda z: z<4000000, fibonacci()) if i%2==0]))


No comments: