-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacci.java
60 lines (52 loc) · 1.8 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
48
49
50
51
52
53
54
55
56
57
58
59
60
package dynamic_programming;
public class Fibonacci {
public static void main(String[] args) {
// Example:
printTiming("Top-down", Fibonacci::fibonacciRecursive, 42); // 267914296
printTiming("Top-down DP", Fibonacci::fibonacciRecursiveMemoization, 42); // 267914296
printTiming("Bottom-up DP", Fibonacci::fibonacciIterative, 42); // 267914296
}
private static void printTiming(String funcName, java.util.function.Function<Integer, Integer> func, int n) {
System.out.println(funcName + ":");
long t1;
long t2;
int result;
t1 = System.nanoTime();
result = func.apply(n);
t2 = System.nanoTime();
System.out.println(result);
System.out.println("Took " + ((double) (t2 - t1) / 1_000_000) + " ms");
System.out.println();
}
public static int fibonacciRecursive(int n) {
if (n <= 2) {
return 1;
}
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
public static int fibonacciRecursiveMemoization(int n) {
return fibonacciRecursiveMemoization(n, new int[n]);
}
private static int fibonacciRecursiveMemoization(int n, int[] memo) {
if (n <= 2) {
return 1;
}
int next = memo[n - 1];
if (next == 0) {
next = fibonacciRecursiveMemoization(n - 1, memo) + fibonacciRecursiveMemoization(n - 2, memo);
memo[n - 1] = next;
}
return next;
}
public static int fibonacciIterative(int n) {
int[] table = new int[2];
table[0] = 1;
table[1] = 1;
for (int i = 2; i < n; ++i) {
int next = table[0] + table[1];
table[0] = table[1];
table[1] = next;
}
return table[1];
}
}