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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Non Fibonacci Numbers
- GCD and Fibonacci Numbers
- Sum of Fibonacci Numbers
- Sum of squares of Fibonacci numbers
- Find the sum of first N odd Fibonacci numbers
- Sum of Fibonacci Numbers in a range
- The Magic of Fibonacci Numbers
- Prime numbers and Fibonacci
- Alternate Fibonacci Numbers
- Generalized Fibonacci Numbers
- Program for Fibonacci numbers
- C Program for Fibonacci numbers
- Find the GCD of N Fibonacci Numbers with given Indices
- Remove all the fibonacci numbers from the given array
- Sum of numbers in the Kth level of a Fibonacci triangle
- Large Fibonacci Numbers in Java
- Find two Fibonacci numbers whose sum can be represented as N
- Interesting facts about Fibonacci numbers
- Program to print first n Fibonacci Numbers | Set 1
- Sum of Fibonacci Numbers with alternate negatives