Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 0 and 1, the first 12 terms will be:
0, 1, 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.
In [3]:
n = 0
x = 0
y = 1 #To get the fibonacci sequence started
while x < 4000000 and y < 4000000: #"whose values do not exceed four million"
x = y + x #Basic function for generating the fibonacci sequence
y = y + x
if y % 2 == 0 and x % 2 == 0: #If they both are even, add them both to n
n = n + y
n = n + x
elif y % 2 == 0: #If only x is even, add x
n = n + y
elif x % 2 == 0: #If only y is even, add y
n = n + x
print(n)
#raise NotImplementedError()
In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.
In [ ]: