Java Program for n-th Fibonacci numbers
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 Recursion class 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 */ |
chevron_right
filter_none
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] = 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 Mishra and improved by MichaelJoshuaRamos */ |
chevron_right
filter_none
Output:
34
Method 3 ( Use Dynamic Programming with Space Optimization)
// Java program for Fibonacci Series using Space // Optimized Method class 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 |
chevron_right
filter_none
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[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 */ |
chevron_right
filter_none
Output:
34
Method 5 (Divide and Conquer)
// Fibonacci Series using Optimized Method class 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 */ |
chevron_right
filter_none
Output:
34
Please refer complete article on Program for Fibonacci numbers for more details!
Recommended Posts:
- LongStream.Builder build() in Java
- DoubleStream.Builder build() in Java
- Java 8 | Consumer Interface in Java with Examples
- MouseListener and MouseMotionListener in Java
- Java Program for Selection Sort
- Java Program for Bubble Sort
- Java Program for Insertion Sort
- Java Program for Heap Sort
- Java Program for Radix Sort
- Java Program for Counting Sort
- Java Program for ShellSort
- Java Program for Longest Common Subsequence
- Java Program for Binary Search (Recursive and Iterative)
- Java Program 0-1 Knapsack Problem
- Java Program for Min Cost Path



