Project Euler: Problem 2

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 [1]:
n=0                    #this will be the fibonacci numbers
L=[0,1]                #list of the  numbers
s=0                    #the sum (eventually)
while n<4000000:       #limiting n
    n=L[-1]+(L[-2])    #to define which terms i want
    L.append(n)        #to put n in the list
    if n%2 == 0:       #to get the even numbers
        s += n         #sum them
print (s)              #print the sum


4613732

In [2]:
# This cell will be used for grading, leave it at the end of the notebook.