Skip to content
Open
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions maths/fibonacci_iterative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def fibonacci_iterative(n: int) -> int:
"""
Return the n-th Fibonacci number using an iterative approach.

The function returns the Fibonacci number at index n where:
F(0) == 0, F(1) == 1, F(2) == 1, ...

Examples:
>>> fibonacci_iterative(0)
0
>>> fibonacci_iterative(1)
1
>>> fibonacci_iterative(7)
13
"""
if n < 0:
raise ValueError("Input must be a non-negative integer")

if n == 0:
return 0
a, b = 0, 1
for _ in range(1, n):
a, b = b, a + b
return b


if __name__ == "__main__":
# simple demonstration
print(fibonacci_iterative(7)) # expected 13