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:
1 2 3 4 5 6 7 | 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.
Related Articles:
Popular Articles:
- Backup folders with 7-zip command line
- Insert figures to LaTeX
- Create LaTeX table easily
- Add personal signature to Lotus Notes 7
- Windows XP Service Pack 3 Final release download
Spread/Promote this article.
Digg | Del.icio.us | Stumble | Y! MyWeb | Y! Buzz | Fave It! | RedditSubscribe for free.
Subscribe to Selinap.com feed right now!
Tags: Fibonacci, Project Euler

takde update ke eh ? hehe