MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / fibonacci.py
| 1 | def fibonacci(n: int) -> int: |
| 2 | """Return the nth Fibonacci number using iterative approach. |
| 3 | |
| 4 | Args: |
| 5 | n: The position in the Fibonacci sequence (0-indexed). |
| 6 | |
| 7 | Returns: |
| 8 | The nth Fibonacci number where fibonacci(0) = 0, fibonacci(1) = 1. |
| 9 | |
| 10 | Raises: |
| 11 | ValueError: If n is negative. |
| 12 | """ |
| 13 | if n < 0: |
| 14 | raise ValueError("n must be non-negative") |
| 15 | a, b = 0, 1 |
| 16 | for _ in range(n): |
| 17 | a, b = b, a + b |
| 18 | return a |