Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions FibCode
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.HashMap;
import java.util.Map;

public class FibonacciCalc {
static private Map < Integer, Integer > memoizeTable = new HashMap < > ();

// Fibonacci with Memoization
static public int fibMemoize(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}

if (memoizeTable.containsKey(n)) {
// getting value from the stored result(s)
return memoizeTable.get(n);
}

int result = fibMemoize(n - 1) + fibMemoize(n - 2);

// storing the result in cache
memoizeTable.put(n, result);

return result;
}

public static void main(String[] args) {
//FibonacciCalc fibo = new FibonacciCalc();

System.out.println("Fibonacci value for n = 6 --> " +
fibMemoize(6));
}
}