Skip to content

Commit 676f21f

Browse files
Update main.py
1 parent 78a3159 commit 676f21f

File tree

1 file changed

+1
-47
lines changed

1 file changed

+1
-47
lines changed

main.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
1-
2-
def nth_fibonacci(n):
3-
4-
# Base case: if n is 0 or 1, return n
5-
if n <= 1:
6-
return n
7-
8-
# Recursive case: sum of the two preceding Fibonacci numbers
9-
return nth_fibonacci(n - 1) + nth_fibonacci(n - 2)
10-
11-
n = 5
12-
result = nth_fibonacci(n)
13-
print(result)
14-
15-
161
# Function to calculate the nth Fibonacci number using memoization
172
def nth_fibonacci_util(n, memo):
18-
193
# Base case: if n is 0 or 1, return n
204
if n <= 1:
215
return n
@@ -30,46 +14,16 @@ def nth_fibonacci_util(n, memo):
3014

3115
return memo[n]
3216

33-
3417
# Wrapper function that handles both initialization
3518
# and Fibonacci calculation
3619
def nth_fibonacci(n):
37-
3820
# Create a memoization table and initialize with -1
3921
memo = [-1] * (n + 1)
4022

4123
# Call the utility function
4224
return nth_fibonacci_util(n, memo)
4325

44-
4526
if __name__ == "__main__":
4627
n = 5
4728
result = nth_fibonacci(n)
48-
print(result)
49-
50-
51-
52-
def nth_fibonacci(n):
53-
54-
# Handle the edge cases
55-
if n <= 1:
56-
return n
57-
58-
# Create a list to store Fibonacci numbers
59-
dp = [0] * (n + 1)
60-
61-
# Initialize the first two Fibonacci numbers
62-
dp[0] = 0
63-
dp[1] = 1
64-
65-
# Fill the list iteratively
66-
for i in range(2, n + 1):
67-
dp[i] = dp[i - 1] + dp[i - 2]
68-
69-
# Return the nth Fibonacci number
70-
return dp[n]
71-
72-
n = 5
73-
result = nth_fibonacci(n)
74-
print(result)
75-
29+
print(result) # Output: 5

0 commit comments

Comments
 (0)