34 lines
661 B
Python
34 lines
661 B
Python
#!/usr/bin/env python3
|
|
"""Fibonacci calculator - computes nth Fibonacci number."""
|
|
|
|
import sys
|
|
|
|
|
|
def fibonacci(n: int) -> int:
|
|
"""Return the nth Fibonacci number (0-indexed)."""
|
|
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
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: fib_calc.py <n>", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
n = int(sys.argv[1])
|
|
result = fibonacci(n)
|
|
print(f"fib({n}) = {result}")
|
|
sys.exit(0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |