22 lines
470 B
Python
22 lines
470 B
Python
#!/usr/bin/env python3
|
|
"""Simple Fibonacci calculator."""
|
|
|
|
|
|
def fib(n: int) -> int:
|
|
"""Return the nth Fibonacci number."""
|
|
if n < 0:
|
|
raise ValueError("n must be non-negative")
|
|
if n == 0:
|
|
return 0
|
|
if n == 1:
|
|
return 1
|
|
a, b = 0, 1
|
|
for _ in range(2, n + 1):
|
|
a, b = b, a + b
|
|
return b
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
n = int(sys.argv[1]) if len(sys.argv) > 1 else 10
|
|
print(f"fib({n}) = {fib(n)}") |