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 firstname.lastname@example.org. 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.
- GCD and Fibonacci Numbers
- Sum of Fibonacci Numbers
- Non Fibonacci Numbers
- Sum of Fibonacci Numbers in a range
- Prime numbers and Fibonacci
- Alternate Fibonacci Numbers
- The Magic of Fibonacci Numbers
- Sum of squares of Fibonacci numbers
- Program for Fibonacci numbers
- C Program for Fibonacci numbers
- Find the sum of first N odd Fibonacci numbers
- Sum of Fibonacci Numbers with alternate negatives
- Find the GCD of N Fibonacci Numbers with given Indices
- Program to print first n Fibonacci Numbers | Set 1
- Interesting facts about Fibonacci numbers