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]:
# YOUR CODE HERE

def fibo_even_sum(num):
    even_sum = 0
    # define the first two values ad Fibonacci sequance and call x and y
    x, y = 0, 1
    #loop through x and y addint the previous ters to gain the next term
    while y < num:
        x, y = y, x + y
        #Take turnd from loop disregard odd values the sum the even ones
        if y % 2 == 0:
            even_sum = even_sum + y
    #return the values from loop
    return even_sum

#print the total
print (fibo_even_sum(4000000))


4613732

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