-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacci.java
47 lines (38 loc) · 1.01 KB
/
Fibonacci.java
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
46
47
//Computing the nth Fibonacci number using Dynamic Programming
public class Fibonacci {
private static long[] store;
//Using tabulation iterative method
//Preferred method as less stack levels are required
//Method becomes inaccurate for big numbers
//as Fib becomes > Long.MAX_VALUE
//Is accurate up to computing Fib(92)
public static long iterativeFib(int n) {
if(n <= 1){
return n;
}
long[] F = new long[n+1];
F[0] = 0l; F[1]= 1l;
for(int i = 2; i <= n; i++){
F[i] = F[i-1] + F[i-2];
}
return F[n];
}
//Using memoisation recursive method
public static long recursiveFib(int n){
if(n <= 1) {
return n;
}
if(store == null){
store = new long[n+1];
store[1] = 1;
}
if(store[n] != 0){
return store[n];
}
store[n] = recursiveFib(n-1) + recursiveFib(n-2);
return store[n];
}
public static void main(String[] args){
System.out.print(recursiveFib(90));
}
}