Given a limit, find the sum of all the even-valued terms in the Fibonacci sequence below given limit.
The first few terms of Fibonacci Numbers are, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ,… (Even numbers are highlighted).
Input : limit = 8 Output : 10 Explanation : 2 + 8 = 10 Input : limit = 400; Output : 188. Explanation : 2 + 8 + 34 + 144 = 188.
A simple solution is to iterate through all prime numbers while the next number is less than or equal to given limit. For every number, check if it is even. If the number is even, add it to the result.
An efficient solution is based on the below recursive formula for even Fibonacci Numbers
Recurrence for Even Fibonacci sequence is: EFn = 4EFn-1 + EFn-2 with seed values EF0 = 0 and EF1 = 2. EFn represents n'th term in Even Fibonacci sequence.
Refer this more details of above formula.
So while iterating over Fibonacci numbers, we only generate even Fibonacci numbers.
This article is contributed by Nishant_singh(pintu) . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Sum of Fibonacci Numbers
- GCD and Fibonacci Numbers
- Non Fibonacci Numbers
- The Magic of Fibonacci Numbers
- Find the sum of first N odd Fibonacci numbers
- C Program for Fibonacci numbers
- Prime numbers and Fibonacci
- Sum of Fibonacci Numbers in a range
- Alternate Fibonacci Numbers
- Program for Fibonacci numbers
- Sum of squares of Fibonacci numbers
- Interesting facts about Fibonacci numbers
- Find the GCD of N Fibonacci Numbers with given Indices
- Program to print first n Fibonacci Numbers | Set 1
- Large Fibonacci Numbers in Java