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
| import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
public class FibMemoized {
public static void main(String[] args) {
System.out.println(fib(100));
}
private static Map<Integer, BigInteger> lookupTable = new
HashMap<>();
static {
lookupTable.put(0, BigInteger.ZERO);
lookupTable.put(1, BigInteger.ONE);
}
private static BigInteger fib(int n) {
if (lookupTable.containsKey(n)) {
return lookupTable.get(n);
} else {
BigInteger result = fib(n - 1).add(fib(n - 2));
lookupTable.put(n, result);
return result;
}
}
}
|