Skip to content

Commit

Permalink
FEAT Implemented fibonacci sequence algorithm utilizing dynamic and…
Browse files Browse the repository at this point in the history
… programming memoization
  • Loading branch information
dev-xero committed May 20, 2023
1 parent dd1c362 commit 4d3fc9d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ def fib(n: int) -> int:

return Fibonacci.fib(n - 2) + Fibonacci.fib(n - 1)

@staticmethod
def fast_fib(n: int, cache: {int: int}) -> int:
"""Fibonacci sequence algorithm utilizing dynamic programing and memoization"""
if n == 0:
cache[n] = 0
return 0

if n == 1:
cache[n] = 1
return 1

cache[n] = cache[n - 2] + cache[n - 1]
return cache[n]


# ---------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -135,6 +149,8 @@ def main():

test_int_matrix = [[0] * 2 for _ in range(3)]

cache = {}

for i in range(3):
for j in range(2):
test_int_matrix[i][j] = generator.randint(0, 9)
Expand Down Expand Up @@ -163,6 +179,12 @@ def main():
print()
print(f"floor of lg(128): { lg(128) }")

print()
print("- Dynamic Programming and Memoization Fib")

for i in range(40):
print(Fibonacci.fast_fib(i, cache))

print()
print("- Recursive Fib")

Expand Down

0 comments on commit 4d3fc9d

Please sign in to comment.