-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFibo.java
More file actions
56 lines (49 loc) · 1.49 KB
/
Fibo.java
File metadata and controls
56 lines (49 loc) · 1.49 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
46
47
48
49
50
51
52
53
54
55
56
public class Fibo {
static int call = 0;
static int call2 = 0;
static int fiboRec(int n) {
// Termination Case
if (n == 0 || n == 1) {
return n;
}
call++;
int secondTerm = fiboRec(n - 1); // Small Problem
int firstTerm = fiboRec(n - 2);
int result = firstTerm + secondTerm;
return result;
}
static int tabulation(int n) {
int cache[] = new int[n + 1];
cache[0] = 0; // first term
cache[1] = 1; // second term
for (int i = 2; i <= n; i++) {
cache[i] = cache[i - 1] + cache[i - 2];
}
return cache[n];
}
static int fiboMemo(int n, int[] cache) {
// Termination Case
if (n == 0 || n == 1) {
return n;
}
if (cache[n] != 0) {
return cache[n];
}
call2++;
int secondTerm = fiboMemo(n - 1, cache); // Small Problem
int firstTerm = fiboMemo(n - 2, cache);
int result = firstTerm + secondTerm;
// Store the Computed result
cache[n] = result;
return result;
}
public static void main(String[] args) {
// System.out.println(fiboRec(5));
// System.out.println("rec call " + call);
// int n = 5;
// int cache[] = new int[n + 1];
// System.out.println("Memo Call result " + (fiboMemo(n, cache)));
// System.out.println("Memo Call " + call2);
System.out.println(tabulation(8));
}
}