3 ms·
There is a bug in the fibonacci example: it sums odd numbers instead of even. from itertools import takewhile def ifibonacci(a=1, b=1): while True
by d0mine 17y ago
There is a bug in the fibonacci example: it sums odd numbers instead of even.
from itertools import takewhile
def ifibonacci(a=1, b=1):
while True:
yield a
a, b = b, a + b
candidates = takewhile(lambda n: n < 4000000, ifibonacci())
print sum(n for n in candidates if n % 2 == 0)