Java Program for n-th Fibonacci numbers

• Difficulty Level : Easy
• Last Updated : 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 */
Output:
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;          if (n > 0) {            f = 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 */
Output:
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
Output:
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;    }      /* 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 * M + F * M;        int y = F * M + F * M;        int z = F * M + F * M;        int w = F * M + F * M;          F = x;        F = y;        F = z;        F = 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 */
Output:
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;    }      static void multiply(int F[][], int M[][])    {        int x = F * M + F * M;        int y = F * M + F * M;        int z = F * M + F * M;        int w = F * M + F * M;          F = x;        F = y;        F = z;        F = 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 */
Output:
34

Please refer complete article on Program for Fibonacci numbers for more details!

My Personal Notes arrow_drop_up