Even Values in the Fibonacci Sequence

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , . . . 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence to the maximum of four million, find the sum of the even terms in the sequence to this limit.

Hint: Use of a knowledge of programming might be helpful.


The answer is 4613732.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Hunter Edwards
Oct 31, 2017

Here is my python Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import time

def mat_sum(n):
    mat = [1,2]
    n1,n2 = 1,2
    while n1+n2 < n:
        mat.append(n1+n2)
        n1,n2 = n2,n1+n2
    return sum(mat[1::3])

start = time.time()
for i in range(1000000): matsum = mat_sum(4000000)
elapsed = (time.time() - start)

print "result %s returned after %s seconds (1 million iterations)." % (matsum, elapsed)

The console output (with the time given for other reasons):

result 4613732 returned after 16.3734951019 seconds (1 million iterations).

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...