Even-valued Fibonacci terms summation

Friday, August 1st, 2008
Advertisement

Subscribe.
Enter your email:

Problem #2 of Project Euler

Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.

Analysis
The first 10 terms of Fibonacci sequence are
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

We need to add the even numbers terms
2 + 8 + 34, …

Solution:

def sumEvenFibonacci( limit ):
    a, b = 1, 1
    sum = 0
    while b < limit:
        if b%2 == 0: sum += b
        a, b = b, a + b
    return sum

Usage:

print sumEvenFibonacci( 4000000 )

If you are interested in listing all the Fibonacci numbers less than a given limit, read this article.

If you are new here, you might want to subscribe to the RSS feed or newsletter.

Enter your email address:

Creates the exact copy of your hard disk and allows you to instantly restore the entire machine.
New Acronis True Image Home 2010 is the most reliable and easy in use backup solution. Now with online backup option!
15% Discount Code: FMAATIH2010

What else?

Like this article? Share it

 Digg  del.icio.us  TwitThis  Facebook  Reddit  StumbleUpon

One Response to “Even-valued Fibonacci terms summation”

  1. MakanMalaya says:

    takde update ke eh ? hehe :grin:

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>