Skip to content

Commit f9d2314

Browse files
authored
Merge pull request #1 from Faarisc/patch-1
Updated Intro + Methodology + Iterative section
2 parents e7dbcba + c7ead22 commit f9d2314

File tree

1 file changed

+66
-4
lines changed
  • allhands/spring2025/weekeleven/teamthree

1 file changed

+66
-4
lines changed

allhands/spring2025/weekeleven/teamthree/index.qmd

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@ toc: true
1010

1111
# Introduction
1212

13-
TODO: Faaris to complete
13+
This investigation seeks to answer a deceptively simple question with far-reaching
14+
implications, which method: recursive, iterative, or memoized - is fastest to build a
15+
Fibonacci sequence?
16+
17+
To evaluate these different approaches, we put them to the test by employing them to
18+
generate a Fibonacci sequence. Using precise timing measurements, we systematically
19+
compare how long each approach takes to compute these sequences. This head-to-head
20+
performance testing allows us to reveal differences in efficiency and ultimately enables
21+
us to determine which approach delivers results the fastest.
1422

1523
## Motivation
1624

17-
TODO: Faaris to complete
25+
The motivation for this comparison stems from real-world programming challenges. While
26+
all three approaches can correctly generate Fibonacci sequences, their performance
27+
characteristics differ. Developers frequently encounter situations where choosing the
28+
wrong implementation leads to sluggish performance, unresponsive applications, or even
29+
complete system failures. By systematically comparing these methods, we provide actionable
30+
insights that can prevent such issues.
1831

19-
# Method
32+
To conduct a fair comparison of these approaches we implemented each approach as separate,
33+
testable units and subjected them to benchmarking. This mimiced the conditions under which
34+
professional developers evaluate competing solutions and allowed us to yield practical
35+
concrete data which can ultimately used to help inform implementation decisions.
2036

2137
## Approach
2238

@@ -108,7 +124,53 @@ complexity and memory usage when considering a memoized approach.
108124

109125
### Generate Fibonacci using an Iterative approach
110126

111-
TODO: Faaris- please explain your approach, including a code sample
127+
The iterative approach works by initializing the first two Fibonacci numbers (0 and 1) and then
128+
iteratively calculating each subsequent number in the sequence. For each iteration from 2 to n,
129+
the algorithm simply updates two variables containing the previous two Fibonacci values.
130+
131+
This method is the fastest because it follows the most efficient computational path - it performs
132+
each calculation exactly once without any redundant operations, and uses minimal memory by only
133+
tracking the last two numbers. This ultimately makes it a predictable loop that modern processors
134+
can optimize extremely well. All-in-all the simplicity of this approach, just adding two numbers and
135+
updating values in each iteration, allows it to be the fastest.
136+
137+
Below is the implementation of the Iterative approach used:
138+
139+
140+
```python
141+
def fibonacci_iterative(n: int) -> int:
142+
"""Generates the Fibonacci sequence up to the nth term using an iterative approach."""
143+
144+
# Handle negative inputs
145+
if n < 0:
146+
raise ValueError("Input must be a positive integer.")
147+
148+
# Base cases
149+
if n == 0:
150+
return 0
151+
if n == 1:
152+
return 1
153+
154+
# Initialize the first two Fibonacci numbers
155+
a, b = 0, 1
156+
157+
# Iterate from 2 to n
158+
for _ in range(2, n + 1):
159+
# Update the Fibonacci numbers
160+
a, b = b, a + b
161+
162+
# Return the nth Fibonacci number
163+
return b
164+
```
165+
166+
This code works by checking if you ask for the 0th Fibonacci number, it immediately says "0".
167+
If you ask for the 1st number in the fibonnaci sequence, it says "1", and If you ask for a
168+
negative number, it gives an error. For any nth term above 1 the code looks at the last
169+
two numbers it wrote down ('a' and 'b') and adds them together to get the next number. It then
170+
"slides" the numbers over and the old 'b' becomes the new 'a', and the new sum becomes the new 'b'.
171+
172+
This means that tt doesn't re-calculate anything (unlike the recursive method), nor does it need
173+
extra memory to store past results (unlike memoization).
112174

113175
### Run a Benchmark
114176

0 commit comments

Comments
 (0)