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 [5]:
# Fibonacci numbers are found by adding the previous two Fibonacci numbers.
n=1
x=0 # holds value of the sum of even numbers, "n" and "y" hold the numbers
y=0
while n <= 4000000:
    if n % 2 == 0:   
        x += n      #taking value of n and adding it x
    y = n
    n = y + n
        
print(x)


4194302

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

In [ ]:


In [ ]:


In [ ]: