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 [17]:
#defining the variable fibonacci_seq
fibonacci_seq = [0,1]
i = 1
#While loop!!!
while fibonacci_seq[i] < 4000000:
fibonacci_seq.append(fibonacci_seq[i]+fibonacci_seq[i-1])
i += 1
print(fibonacci_seq)
#This prints out a number larger than 4000000... I have to get rid of that
In [27]:
#Getting the even numbers!!!
even = []
for x in fibonacci_seq:
if x % 2 == 0:
even.append(x)
print (even)
#Sum of the even numbers!
SumFibo = sum(even)
print(SumFibo)
In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.