-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciNumber.java
More file actions
45 lines (40 loc) · 1.06 KB
/
FibonacciNumber.java
File metadata and controls
45 lines (40 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package algorithm.DynamicProgramming;
/**
* The type Fibonacci number.
*/
public class FibonacciNumber {
/**
* Get fibonacci using dynamic programming int.
*
* @param n the n
* @return the int
*/
public static int getFibonacciUsingDynamicProgramming(int n){
int[] arr = new int[n];
arr[0]=1;arr[1]=1;
for(int i =2;i<n;i++){
arr[i] = arr[i-1]+arr[i-2];
}
return arr[n-1];
}
/**
* Gets fibonacci using recursion.
*
* @param i the
* @return the fibonacci using recursion
*/
public static int getFibonacciUsingRecursion(int i) {
if (i <= 1)
return i;
return getFibonacciUsingRecursion(i - 1) + getFibonacciUsingRecursion(i - 2);
}
/**
* Main.
*
* @param args the args
*/
public static void main(String[] args){
System.out.println("Fibonacci number of 6 is "+getFibonacciUsingDynamicProgramming(6));
System.out.println("Fibonacci number of 5 is "+getFibonacciUsingRecursion(5));
}
}