Skip to content

Commit ebd7678

Browse files
authored
Create fibonacci_memoization.py
1 parent 513070d commit ebd7678

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/python/fibonacci_memoization.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def fibonacci(n, memo={}):
2+
if n <= 1:
3+
return 1
4+
5+
# Check if the result is already memoized
6+
if n in memo:
7+
return memo[n]
8+
9+
# Calculate Fibonacci recursively and store the result in memoization
10+
result = fibonacci(n - 1, memo) + fibonacci(n - 2, memo)
11+
memo[n] = result
12+
13+
return result
14+
15+
n = 16
16+
print(f"Fib({n}): {fibonacci(n)}")

0 commit comments

Comments
 (0)