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 [30]:
#define variables
y = 0
z = 1
total_sum = 0
#"whose values do not exceed four million"
while z <= 4000000:
    #redefine variables (Worked with Jack Porter on the following 3 lines)
    x = y
    y = z
    z += x
    #"even-valued terms"
    if z % 2 == 0:
        total_sum += z
print (total_sum)


4613732

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