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 [59]:
# YOUR CODE HERE
def FibbList(maximum):
x = 0
n = 1
fibblist = [0, 1]
while x <= maximum:
fibblist.append(fibblist[n] + fibblist[n-1])
n = n + 1
x = fibblist[n]
fibblist.pop()
return fibblist
In [60]:
def is_even(x):
if x%2 == 0:
return True
else:
return False
In [61]:
def Even_Fibb_Sum(maximum):
fibblist = FibbList(maximum)
total = 0
for i in fibblist:
if is_even(i):
total += i
return total
In [68]:
print(FibbList(4000000))
In [69]:
print(Even_Fibb_Sum(4000000))
In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.