Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Fibonacci_series_with_classes #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions Fibonacci_series_with_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Fibonacci:
def __init__(self):
self.memo = {} # Dictionary to store calculated Fibonacci numbers for optimization

def calculate(self, n):
# Base cases
if n <= 0:
return 0
elif n == 1:
return 1

# Check if Fibonacci number is already calculated
if n in self.memo:
return self.memo[n]

# Calculate the Fibonacci number and store it in the dictionary (memoization)
self.memo[n] = self.calculate(n-1) + self.calculate(n-2)
return self.memo[n]

def print_series(self, count):
for i in range(count):
print(self.calculate(i), end=" ")

# Example usage
fib = Fibonacci()
fib.print_series(10) # Output: 0 1 1 2 3 5 8 13 21 34