# Nth Even Fibonacci Number

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

**Examples :**

Input : n = 3 Output : 34 Input : n = 4 Output : 144 Input : n = 7 Output : 10946

The Fibonacci numbers are the numbers in the following integer sequence.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ….

where any number in sequence is given by:

Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1.

The even number Fibonacci sequence is, 0, 2, 8, 34, 144, 610, 2584…. We need to find n’th number in this sequence.

If we take a closer look at Fibonacci sequence, we can notice that **every third number in sequence is even** and the sequence of even numbers follow following recursive formula.

Recurrence for Even Fibonacci sequence is: EFn = 4EFn-1 + EFn-2 with seed values EF0 = 0 and EF1 = 2.EFnrepresents n'th term in Even Fibonacci sequence.

**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 Fibinacci 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 [Expending first term] = Fn-3 + Fn-4 + 2Fn-3 + Fn-4 = 3Fn-3 + 2Fn-4 [Expending 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

## C++

`// C++ code to find Even Fibonacci ` `//Series using normal Recursion ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `// Function which return ` `//nth even fibonnaci number ` `long` `int` `evenFib(` `int` `n) ` `{ ` ` ` `if` `(n < 1) ` ` ` `return` `n; ` ` ` `if` `(n == 1) ` ` ` `return` `2; ` ` ` ` ` `// calculation of ` ` ` `// Fn = 4*(Fn-1) + Fn-2 ` ` ` `return` `((4 * evenFib(n-1)) + ` ` ` `evenFib(n-2)); ` `} ` ` ` `// Driver Code ` `int` `main () ` `{ ` ` ` `int` `n = 7; ` ` ` `cout << evenFib(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to find Even Fibonacci ` `// Series using normal Recursion ` ` ` `class` `GFG{ ` ` ` `// Function which return ` `// nth even fibonnaci number ` `static` `long` `evenFib(` `int` `n) ` `{ ` ` ` `if` `(n < ` `1` `) ` ` ` `return` `n; ` ` ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `2` `; ` ` ` ` ` `// calculation of ` ` ` `// Fn = 4*(Fn-1) + Fn-2 ` ` ` `return` `((` `4` `* evenFib(n-` `1` `)) + ` ` ` `evenFib(n-` `2` `)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `7` `; ` ` ` `System.out.println(evenFib(n)); ` `} ` `} ` ` ` `// This code is contributed by ` `// Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find Even Fibonacci ` `# Series using normal Recursion ` ` ` `# Function which return ` `#nth even fibonnaci number ` `def` `evenFib(n) : ` ` ` `if` `(n < ` `1` `) : ` ` ` `return` `n ` ` ` `if` `(n ` `=` `=` `1` `) : ` ` ` `return` `2` ` ` ` ` `# calculation of ` ` ` `# Fn = 4*(Fn-1) + Fn-2 ` ` ` `return` `((` `4` `*` `evenFib(n` `-` `1` `)) ` `+` `evenFib(n` `-` `2` `)) ` ` ` ` ` `# Driver Code ` `n ` `=` `7` `print` `(evenFib(n)) ` ` ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# code to find Even Fibonacci ` `// Series using normal Recursion ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function which return ` `// nth even fibonnaci number ` `static` `long` `evenFib(` `int` `n) ` `{ ` ` ` `if` `(n < 1) ` ` ` `return` `n; ` ` ` ` ` `if` `(n == 1) ` ` ` `return` `2; ` ` ` ` ` `// calculation of Fn = 4*(Fn-1) + Fn-2 ` ` ` `return` `((4 * evenFib(n - 1)) + ` ` ` `evenFib(n - 2)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `n = 7; ` ` ` `Console.Write(evenFib(n)); ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to find Even Fibonacci ` `// Series using normal Recursion ` ` ` `// Function which return ` `// nth even fibonnaci number ` `function` `evenFib(` `$n` `) ` `{ ` ` ` `if` `(` `$n` `< 1) ` ` ` `return` `$n` `; ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `2; ` ` ` ` ` `// calculation of ` ` ` `// Fn = 4*(Fn-1) + Fn-2 ` ` ` `return` `((4 * evenFib(` `$n` `-1)) + ` ` ` `evenFib(` `$n` `-2)); ` `} ` ` ` `// Driver Code ` `$n` `= 7; ` `echo` `(evenFib(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output : **

10946

Time complexity of above implementation is exponential. We can do it in linear time using Dynamic Programming. We can also do it in O(Log n) time using the fact EF_{n} = F_{3n}. Note that we can find n’th Fibonacci number in O(Log n) time (Please see Methods 5 and 6 here).

This article is contributed by **Shivam Pradhan(anuj_charm)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.

## Recommended Posts:

- Check if a M-th fibonacci number divides N-th fibonacci number
- Number of ways to represent a number as sum of k fibonacci numbers
- Finding number of digits in n'th Fibonacci number
- Fibonacci number in an array
- Count the nodes whose sum with X is a Fibonacci number
- Python Program for n-th Fibonacci number
- n'th multiple of a number in Fibonacci Series
- How to check if a given number is Fibonacci number?
- Find nth Fibonacci number using Golden ratio
- An efficient way to check whether n-th Fibonacci number is multiple of 10
- Program to find last two digits of Nth Fibonacci number
- C/C++ Program for nth multiple of a number in Fibonacci Series
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Find Index of given fibonacci number in constant time
- G-Fact 18 | Finding nth Fibonacci Number using Golden Ratio