Nth Even Fibonacci Number
I this post, we'll learn about how to calculate Nth Even Fibonacci Number.
The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation,
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 have to find nth number in this sequence.
The formula of even Fibonacci number = ((4*evenFib(n-1)) + evenFib(n-2));
The Below Code shows how to calculate Nth Even Fibonacci Number in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class NthEvenFibonacciNumber { public static void main(String[] args) { // Here we are passing nth Number int nthNumber = 3; //Calling the getEvenNumber Method And Printing the Nth Even Fibonacci Number number System.out.println(getEvenNumber(nthNumber)); } public static int getEvenNumber(int nthNumber) { if (nthNumber < 1) { return nthNumber; } else if (nthNumber == 1) { return 2; } else { //Formula to calculate Nth Even Fibonacci Number Fn = 4*(Fn-1) + Fn-2 return ((4 * getEvenNumber(nthNumber - 1)) + getEvenNumber(nthNumber - 2)); } } } |
Output :- 34
If You like my post please like & Follow the blog, And get upcoming interesting topics.