-
Notifications
You must be signed in to change notification settings - Fork 0
/
FibonacciWithMemoization.java
35 lines (31 loc) · 1.07 KB
/
FibonacciWithMemoization.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
import java.util.Arrays;
/**
* Created by sivan on 10/17/18.
*/
public class FibonacciWithMemoization {
public static long fibArray[] = new long[8]; //shoud be n+1
public static long fibonacci(long n) {
long fibValue = 0;
System.out.println("fib n vaues "+n);
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else if (fibArray[(int) n] != 0) {
return fibArray[(int) n];
} else {
fibValue = fibonacci(n - 1) + fibonacci(n - 2);
fibArray[(int) n] = fibValue;
System.out.println("fib array "+ Arrays.toString(fibArray));
return fibValue;
}
}
public static void main(String args[]) {
fibArray[0] = 1;
fibArray[1] = 1;
long preTime = System.currentTimeMillis();
System.out.println("Value of 25th number in Fibonacci series->" + fibonacci(7));
long postTime = System.currentTimeMillis();
System.out.println("Time taken to compute in milliseconds->" + (postTime - preTime));
}
}