-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciGenerator.java
More file actions
40 lines (33 loc) · 1.16 KB
/
FibonacciGenerator.java
File metadata and controls
40 lines (33 loc) · 1.16 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
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
public class FibonacciGenerator {
private static final Map<Integer, BigInteger> memo = new HashMap<>();
static {
memo.put(0, BigInteger.ZERO);
memo.put(1, BigInteger.ONE);
}
public BigInteger getNumber(int n) {
if (!memo.containsKey(n)) {
memo.put(n, getNumber(n - 2).add(getNumber(n - 1)));
}
return memo.get(n);
}
/**
* This method utilizes Map.computeIfAbsent() but fails with ConcurrentModificationException for Java 9+
* In Java 9+ Map implementations, map should not be modified during computation
*/
public BigInteger getNumber8(int n) {
return memo.computeIfAbsent(n, k -> getNumber8(k - 2).add(getNumber8(k - 1)));
}
public List<BigInteger> getNumbers(int n) {
getNumber(n);
return memo.values().stream()
.limit(n + 1)
.collect(Collectors.toList());
}
public static void main(String[] args) {
FibonacciGenerator generator = new FibonacciGenerator();
System.out.println(generator.getNumbers(9));
}
}