Java CoE
First code challenge 2023
import java.util.HashMap;
import java.util.Map;
public class Fibonacci {
private static Map<Integer, Long> memoizationMap = new HashMap<>();
public static long calcFibo(int n) {
if (n <= 1) {
return n;
}
if (memoizationMap.containsKey(n)) {
return memoizationMap.get(n);
}
long fiboValue = calcFibo(n - 1) + calcFibo(n - 2);
memoizationMap.put(n, fiboValue);
return fiboValue;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSomething() {
int n = 6;
long start = System.currentTimeMillis();
Long fibo = Fibonacci.calcFibo(n);
long end = System.currentTimeMillis();
System.out.println("Fibonacci(" + n + ") = "+ fibo + " and takes " +
(end - start) + "ms");
}
}