Java Program for n-th Fibonacci numbers - GeeksforGeeks

01 Apr, 2019
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.
Method 1 ( Use recursion )
// Fibonacci Series using Recursionclass Fibonacci {    static int fib(int n)    {        if (n <= 1)            return n;        return fib(n - 1) + fib(n - 2);    }      public static void main(String args[])    {        int n = 9;        System.out.println(fib(n));    }}/* This code is contributed by Rajat Mishra */
34

Method 2 ( Use Dynamic Programming )
// Fibonacci Series using Dynamic Programming  class Fibonacci {    static int fib(int n)    {        /* Declare an array to store Fibonacci numbers. */        int f[] = new int[n + 1];        int i;          /* 0th and 1st number of the series are 0 and 1*/        f[0] = 0;          if (n > 0) {            f[1] = 1;              for (i = 2; i <= n; i++) {                /* Add the previous 2 numbers in the series             and store it */                f[i] = f[i - 1] + f[i - 2];            }        }          return f[n];    }      public static void main(String args[])    {        int n = 9;        System.out.println(fib(n));    }}/* This code is contributed by Rajat Mishraand improved by MichaelJoshuaRamos */
34

Method 3 ( Use Dynamic Programming with Space Optimization)
// Java program for Fibonacci Series using Space// Optimized Methodclass Fibonacci {    static int fib(int n)    {        int a = 0, b = 1, c;        if (n == 0)            return a;        for (int i = 2; i <= n; i++) {            c = a + b;            a = b;            b = c;        }        return b;    }      public static void main(String args[])    {        int n = 9;        System.out.println(fib(n));    }}  // This code is contributed by Mihir Joshi
34

Method 4 (Divide and Conquer)
class Fibonacci {    static int fib(int n)    {        int F[][] = new int[][] { { 1, 1 }, { 1, 0 } };        if (n == 0)            return 0;        power(F, n - 1);          return F[0][0];    }      /* Helper function that multiplies 2 matrices F and M of size 2*2, and     puts the multiplication result back to F[][] */    static void multiply(int F[][], int M[][])    {        int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];        int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];        int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];        int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];          F[0][0] = x;        F[0][1] = y;        F[1][0] = z;        F[1][1] = w;    }      /* Helper function that calculates F[][] raise to the power n and puts the    result in F[][]    Note that this function is designed only for fib() and won't work as general    power function */    static void power(int F[][], int n)    {        int i;        int M[][] = new int[][] { { 1, 1 }, { 1, 0 } };          // n - 1 times multiply the matrix to {{1, 0}, {0, 1}}        for (i = 2; i <= n; i++)            multiply(F, M);    }      /* Driver program to test above function */    public static void main(String args[])    {        int n = 9;        System.out.println(fib(n));    }}/* This code is contributed by Rajat Mishra */
34

Method 5 (Divide and Conquer)
// Fibonacci Series using  Optimized Methodclass Fibonacci {    /* function that returns nth Fibonacci number */    static int fib(int n)    {        int F[][] = new int[][] { { 1, 1 }, { 1, 0 } };        if (n == 0)            return 0;        power(F, n - 1);          return F[0][0];    }      static void multiply(int F[][], int M[][])    {        int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];        int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];        int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];        int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];          F[0][0] = x;        F[0][1] = y;        F[1][0] = z;        F[1][1] = w;    }      /* Optimized version of power() in method 4 */    static void power(int F[][], int n)    {        if (n == 0 || n == 1)            return;        int M[][] = new int[][] { { 1, 1 }, { 1, 0 } };          power(F, n / 2);        multiply(F, F);          if (n % 2 != 0)            multiply(F, M);    }      /* Driver program to test above function */    public static void main(String args[])    {        int n = 9;        System.out.println(fib(n));    }}/* This code is contributed by Rajat Mishra */
34

Please refer complete article on Program for Fibonacci numbers for more details!
Aniket Krishna
MichaelJoshuaRamos
Java Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Iterate HashMap in Java?
Factory method design pattern in Java
Iterate through List in Java
Java Program to Remove Duplicate Elements From the Array
Java program to count the occurrence of each character in a string using Hashmap
Iterate Over the Characters of a String in Java
How to Get Elements By Index from HashSet in Java?
Dijkstra's shortest path algorithm in Java using PriorityQueue
How to remove all white spaces from a String in Java?
Extends vs Implements in Java