Open In App

Nth Even Fibonacci Number

Last Updated : 21 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a value n, find the n'th even Fibonacci Number.

Examples : 

Input n = 3
Output 34
Explanation The first 3 even Fibonacci numbers are 0, 2, 8, 34, 144, with the 3rd being 34.

Input n = 4
Output 144
Explanation The first 4 even Fibonacci numbers are 0, 2, 8, 34, 144, with the 4th being 144.

[Naive Approach] Check Every Fibonacci Number One by One

We generate all Fibonacci numbers and check every number one by one if it is ever or not

[Efficient Approach] Using Direct Formula - O(n) time and O(1) space

The even number Fibonacci sequence is, 0, 2, 8, 34, 144, 610, 2584.... From this sequence we can get idea that every third number in sequence is even and the sequence follows following recursive formula. 

Recurrence for Even Fibonacci sequence is:

EFn = 4EFn-1 + EFn-2

How does above formula work? 
Let us take a look original Fibonacci Formula and write it in the form of Fn-3 and Fn-6 because of the fact that every third Fibonacci number is even. 

Fn = Fn-1 + Fn-2 [Expanding both terms]

= Fn-2 + Fn-3 + Fn-3 + Fn-4

= Fn-2 + 2Fn-3 + Fn-4 [Expanding first term]

= Fn-3 + Fn-4 + 2Fn-3 + Fn-4

= 3Fn-3 + 2Fn-4 [Expanding one Fn-4]

= 3Fn-3 + Fn-4 + Fn-5 + Fn-6 [Combing Fn-4 and Fn-5]

= 4Fn-3 + Fn-6

Since every third Fibonacci Number is even, So if Fn is

even then Fn-3 is even and Fn-6 is also even. Let Fn be

xth even element and mark it as EFx.

If Fn is EFx, then Fn-3 is previous even number i.e. EFx-1

and Fn-6 is previous of EFx-1 i.e. EFx-2

So Fn = 4Fn-3 + Fn-6

which means,

EFx = 4EFx-1 + EFx-2

Below is a simple implementation of the idea

C++
#include <iostream>
using namespace std;

// Optimized function to calculate the nth
// even Fibonacci number
int nthEvenFibonacci(int n) {
    
    // Base case: the first even Fibonacci number is 2
    if (n == 1) return 2;

    // Start with the first two even Fibonacci numbers
    int prev = 0;  // F(0)
    int curr = 2;  // F(3)

    // We need to find the nth even Fibonacci number
    for (int i = 2; i <= n; i++) {
        
        // Next even Fibonacci number is 4 times
        // the previous even Fibonacci number plus 
        // the one before that
        int nextEvenFib = 4 * curr + prev;
        prev = curr;
        curr = nextEvenFib;
    }

    return curr;
}

int main() {
    int n = 2; 
    int result = nthEvenFibonacci(n); 
    cout << result << endl; 
    return 0;
}
Java
public class GfG {

    // Function to calculate the nth even Fibonacci
    // number using dynamic programming
    public static int nthEvenFibonacci(int n) {
        
        // Base case: the first even
        // Fibonacci number is 2
        if (n == 1) return 2;

        // Start with the first two Fibonacci 
        // numbers (even ones)
        int prev = 0;  // F(0)
        int curr = 2;  // F(3)

        // We need to find the nth even Fibonacci number
        for (int i = 2; i <= n; i++) {
            
            // Next even Fibonacci number is 4 
            // times the previous even Fibonacci 
            // number plus the one before that
            int nextEvenFib = 4 * curr + prev;
            prev = curr;
            curr = nextEvenFib;
        }

        return curr;
    }

    public static void main(String[] args) {
        int n = 2;
        int result = nthEvenFibonacci(n);
        System.out.println(result); 
    }
}
Python
# Function to calculate the nth even 
# Fibonacci number using dynamic programming
def nthEvenFibonacci(n):
    
    # Base case: the first even Fibonacci number is 2
    if n == 1:
        return 2
    
    # Start with the first two Fibonacci numbers (even ones)
    prev = 0  # F(0)
    curr = 2  # F(3)
    
    # We need to find the nth even Fibonacci number
    for i in range(2, n + 1):
        
        # Next even Fibonacci number is 4 times the 
        # previous even Fibonacci number plus the
        # one before that
        next_even_fib = 4 * curr + prev
        prev = curr
        curr = next_even_fib
    
    return curr

# Driver code
if __name__ == "__main__":
    n = 2  # Setting n to 2
    result = nthEvenFibonacci(n)  
    print(result) 
C#
using System;

class GfG
{
    // Function to calculate the nth even Fibonacci 
    // number using dynamic programming
    public int NthEvenFibonacci(int n)
    {
        // Base case: the first even Fibonacci number is 2
        if (n == 1)
            return 2;

        // Start with the first two Fibonacci numbers (even ones)
        int prev = 0;  // F(0)
        int curr = 2;  // F(3)

        // We need to find the nth even Fibonacci number
        for (int i = 2; i <= n; i++)
        {
            // Next even Fibonacci number is 4 times the 
            // previous even Fibonacci number plus the 
            // one before that
            int nextEvenFib = 4 * curr + prev;
            prev = curr;
            curr = nextEvenFib;
        }

        return curr;
    }

    static void Main()
    {
        GfG gfg = new GfG();
        int n = 2;
        int result = gfg.NthEvenFibonacci(n);
        Console.WriteLine(result);  // Output: The nth even Fibonacci number
    }
}
JavaScript
// Function to calculate the nth even Fibonacci number using dynamic programming
function nthEvenFibonacci(n) {
    // Base case: the first even Fibonacci number is 2
    if (n === 1) return 2;

    // Start with the first two Fibonacci numbers (even ones)
    let prev = 0;  // F(0)
    let curr = 2;  // F(3)

    // We need to find the nth even Fibonacci number
    for (let i = 2; i <= n; i++) {
        
        // Next even Fibonacci number is 4 times 
        // the previous even Fibonacci number plus 
        // the one before that
        let nextEvenFib = 4 * curr + prev;
        prev = curr;
        curr = nextEvenFib;
    }

    return curr;
}

// Example usage:
const n = 2;  // Setting n to 2
const result = nthEvenFibonacci(n);  
console.log(result);  

Output
8



Next Article
Article Tags :
Practice Tags :

Similar Reads