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 [32]:
# YOUR CODE HERE
fib=[0,1]
fibo=[]
next=0
add=0
for i in range(100000000):
    next=fib[i]+fib[i+1]
    if next>=4000000:
        break
    fib.append(next)
    i=i+1    
#print ('Fibonacci sequence under 4,000,000: %s' %fib)
for num in fib:
    if num%2 ==0:
        add=add+ num
        fibo.append(num)
#print ('Fibonacci sequence under 4,000,000 even numbers: only %s' %fibo)
print('Sum of even Fibonacci numbers under 4,000,000: %s' %add)


Sum of even Fibonacci numbers under 4,000,000: 4613732

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

In [ ]:


In [ ]: