2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-26 06:48:04 +02:00
LaTeX-examples/presentations/Programmieren-Tutorium/Tutorium-10/Fibonacci.java
2013-11-05 19:39:03 +01:00

32 lines
874 B
Java

import java.util.HashMap;
import java.util.Map;
public class Fibonacci {
private final Map<Integer, Integer> functionValues;
public Fibonacci() {
functionValues = new HashMap<Integer, Integer>();
functionValues.put(0, 0);
functionValues.put(1, 1);
}
private int calculate(int x) {
return getFunctionValue(x - 1) + getFunctionValue(x - 2);
}
public int getFunctionValue(int x) {
if (x < 0) {
/* Exception werfen */
throw new IllegalArgumentException(
"Fibonacci is not defined for negative values");
}
if (functionValues.containsKey(x)) {
return functionValues.get(x);
} else {
int functionValue = calculate(x);
functionValues.put(x, functionValue);
return functionValue;
}
}
}