You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: allhands/spring2025/weekeleven/teamthree/index.qmd
+38-1Lines changed: 38 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,44 @@ TODO: Faaris to complete
22
22
23
23
### Generate Fibonacci using a Recursive approach
24
24
25
-
TODO: Duru- please explain your approach, including a code sample
25
+
The recursive approach computes Fibonacci numbers by repeatedly calling the function itself. However, this method is highly inefficient because it recalculates the same values multiple times. For example, to compute fibonacci_recursive(5), the function computes fibonacci_recursive(2) three times:
26
+
```python
27
+
fibonacci_recursive(5)
28
+
├── fibonacci_recursive(4)
29
+
│ ├── fibonacci_recursive(3)
30
+
│ │ ├── fibonacci_recursive(2)
31
+
│ │ │ ├── fibonacci_recursive(1) → 1
32
+
│ │ │ ├── fibonacci_recursive(0) → 0
33
+
│ │ ├── fibonacci_recursive(1) → 1
34
+
│ ├── fibonacci_recursive(2)
35
+
│ ├── fibonacci_recursive(1) → 1
36
+
│ ├── fibonacci_recursive(0) → 0
37
+
├── fibonacci_recursive(3)
38
+
├── fibonacci_recursive(2)
39
+
│ ├── fibonacci_recursive(1) → 1
40
+
│ ├── fibonacci_recursive(0) → 0
41
+
├── fibonacci_recursive(1) → 1
42
+
```
43
+
As shown above, even for a small input like n = 5, the function makes redundant calculations, leading to an exponential growth in execution time.
44
+
```python
45
+
deffibonacci_recursive(n: int) -> int:
46
+
"""Generates the Fibonacci sequence up to the nth term using recursion without memoization."""
47
+
48
+
# Handle negative inputs
49
+
if n <0:
50
+
raiseValueError("Input must be a positive integer.")
It takes in integer as an input and outputs an integer which is the fibonacci number's answer. There are two base cases for this approach which the input integer, in this case "n", is equal to 0 and 1. Each function call branches into two recursive calls, forming a binary tree of depth n. This results in an exponential number of operations, making the approach extremely slow for large values of n. Therefore, the worst-time complexity is O(n^2)!
0 commit comments