-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay28.java
More file actions
30 lines (21 loc) · 716 Bytes
/
Day28.java
File metadata and controls
30 lines (21 loc) · 716 Bytes
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
import java.util.HashMap;
import java.util.Map;
public class Day28 {
private static Map<Integer, Integer> memoizationMap = new HashMap<>();
private static int fibonacci(int n) {
if (n <= 1) {
return n;
}
if (memoizationMap.containsKey(n)) {
return memoizationMap.get(n);
}
int fibValue = fibonacci(n - 1) + fibonacci(n - 2);
memoizationMap.put(n, fibValue);
return fibValue;
}
public static void main(String[] args) {
int n = 10; // Change n to the desired Fibonacci sequence index
int result = fibonacci(n);
System.out.println("The " + n + "th Fibonacci number is: " + result);
}
}