Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
fib1 = 1
fib2 = 2
fib3 = fib1
while fib2 < 10:
print fib1
fib3 = fib1
fib2 += fib1
print fib2
1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10
fib1 = 1
fib2 = 2
while fib2 < 10:
fib2, fib1 = fib2 + fib1, fib2
print fib2
3 5 8 13
def fib_gen(num):
fib1 = 0
fib2 = 1
yield fib1
yield fib2
while fib2 < num:
fib2, fib1 = fib2 + fib1, fib2
yield fib2
[i for i in fib_gen(14)]
[0, 1, 1, 2, 3, 5, 8, 13, 21]
sum([i for i in fib_gen(4000000) if i%2 == 0])
4613732