From ee00df9f6655b787385eec4fe4834abfd04d53a3 Mon Sep 17 00:00:00 2001 From: oshada2002 <147910452+oshada2002@users.noreply.github.com> Date: Tue, 17 Oct 2023 13:34:17 +0530 Subject: [PATCH] Create FibCode --- FibCode | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 FibCode diff --git a/FibCode b/FibCode new file mode 100644 index 0000000..60de9f9 --- /dev/null +++ b/FibCode @@ -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)); + } +}