Even Fibonacci Numbers Sum - GeeksforGeeks

26 Mar, 2021
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).Examples :  
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.  
C++
Java
Python3
C#
PHP
Javascript
// Find the sum of all the even-valued terms in// the Fibonacci sequence which do not exceed// given limit.#include<iostream>using namespace std; // Returns sum of even Fibonacci numbers which are// less than or equal to given limit.int evenFibSum(int limit){    if (limit < 2)        return 0;     // Initialize first two even prime numbers    // and their sum    long long int ef1 = 0, ef2 = 2;    long long int sum = ef1 + ef2;     // calculating sum of even Fibonacci value    while (ef2 <= limit)    {        // get next even value of Fibonacci sequence        long long int ef3 = 4*ef2 + ef1;         // If we go beyond limit, we break loop        if (ef3 > limit)            break;         // Move to next even number and update sum        ef1 = ef2;        ef2 = ef3;        sum += ef2;    }     return sum;} // Driver codeint main(){    int limit = 400;    cout << evenFibSum(limit);    return 0;}
// Find the sum of all the even-valued terms in// the Fibonacci sequence which do not exceed// given limit.import java.io.*; class GFG{    // Returns sum of even Fibonacci numbers which are    // less than or equal to given limit.    static int evenFibSum(int limit)    {        if (limit < 2)            return 0;             // Initialize first two even prime numbers        // and their sum        long ef1 = 0, ef2 = 2;        long sum = ef1 + ef2;             // calculating sum of even Fibonacci value        while (ef2 <= limit)        {            // get next even value of Fibonacci sequence            long ef3 = 4 * ef2 + ef1;                 // If we go beyond limit, we break loop            if (ef3 > limit)                break;                 // Move to next even number and update sum            ef1 = ef2;            ef2 = ef3;            sum += ef2;        }             return(int) sum;    }         // Driver code    public static void main (String[] args)    {        int limit = 400;        System.out.println(evenFibSum(limit));             }} // This code is contributed by vt_m.
# Find the sum of all the even-valued# terms in the Fibonacci sequence which# do not exceed given limit. # Returns sum of even Fibonacci numbers which# are less than or equal to given limit.def evenFibSum(limit) :    if (limit < 2) :        return 0     # Initialize first two even prime numbers    # and their sum    ef1 = 0    ef2 = 2    sm= ef1 + ef2         # calculating sum of even Fibonacci value    while (ef2 <= limit) :         # get next even value of Fibonacci        # sequence        ef3 = 4 * ef2 + ef1         # If we go beyond limit, we break loop        if (ef3 > limit) :            break         # Move to next even number and update        # sum        ef1 = ef2        ef2 = ef3        sm = sm + ef2         return sm # Driver codelimit = 400print(evenFibSum(limit)) # This code is contributed by Nikita Tiwari.
// C# program to Find the sum of all// the even-valued terms in the// Fibonacci sequence which do not// exceed given limit.given limit.using System; class GFG {         // Returns sum of even Fibonacci    // numbers which are less than or    // equal to given limit.    static int evenFibSum(int limit)    {        if (limit < 2)            return 0;             // Initialize first two even        // prime numbers and their sum        long ef1 = 0, ef2 = 2;        long sum = ef1 + ef2;             // calculating sum of even        // Fibonacci value        while (ef2 <= limit)        {                         // get next even value of            // Fibonacci sequence            long ef3 = 4 * ef2 + ef1;                 // If we go beyond limit,            // we break loop            if (ef3 > limit)                break;                 // Move to next even number            // and update sum            ef1 = ef2;            ef2 = ef3;            sum += ef2;        }             return(int) sum;    }         // Driver code    public static void Main ()    {        int limit = 400;        Console.Write(evenFibSum(limit));             }} // This code is contributed by Nitin Mittal.
<?php// Find the sum of all the// even-valued terms in the// Fibonacci sequence which// do not exceed given limit. // Returns sum of even Fibonacci// numbers which are less than or// equal to given limit.function evenFibSum($limit){    if ($limit < 2)        return 0;     // Initialize first two even    // prime numbers and their sum    $ef1 = 0; $ef2 = 2;    $sum = $ef1 + $ef2;     // calculating sum of    // even Fibonacci value    while ($ef2 <= $limit)    {        // get next even value of        // Fibonacci sequence        $ef3 = 4 * $ef2 + $ef1;         // If we go beyond limit,        // we break loop        if ($ef3 > $limit)            break;         // Move to next even number        // and update sum        $ef1 = $ef2;        $ef2 = $ef3;        $sum += $ef2;    }     return $sum;} // Driver code$limit = 400;echo(evenFibSum($limit)); // This code is contributed by Ajit.?>
<script> // Javascript program to find the sum of all the even-valued terms in// the Fibonacci sequence which do not exceed// given limit.      // Returns sum of even Fibonacci numbers which are    // less than or equal to given limit.    function evenFibSum(limit)    {        if (limit < 2)            return 0;               // Initialize first two even prime numbers        // and their sum        let ef1 = 0, ef2 = 2;        let sum = ef1 + ef2;               // calculating sum of even Fibonacci value        while (ef2 <= limit)        {            // get next even value of Fibonacci sequence            let ef3 = 4 * ef2 + ef1;                   // If we go beyond limit, we break loop            if (ef3 > limit)                break;                   // Move to next even number and update sum            ef1 = ef2;            ef2 = ef3;            sum += ef2;        }               return sum;    }       // Function call             let limit = 400;        document.write(evenFibSum(limit));     </script>
Output :  
188
This article is contributed by Nishant_singh(pintu) . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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. 
nitin mittal
jit_t
SarthakMittal3
susmitakundugoaldanga
Fibonacci
Mathematical
Mathematical
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Print all possible combinations of r elements in a given array of size n
The Knight's tour problem | Backtracking-1
Operators in C / C++
Program for factorial of a number
Program to find sum of elements in a given array
Minimum number of jumps to reach end
Program to print prime numbers from 1 to N.