48 lines
1001 B
Python
48 lines
1001 B
Python
#!/usr/bin/env python3
|
|
"""Fibonacci sequence calculator with colored output."""
|
|
|
|
# ANSI color codes
|
|
GREEN = "\033[32m"
|
|
CYAN = "\033[36m"
|
|
RED = "\033[31m"
|
|
RESET = "\033[0m"
|
|
|
|
|
|
def fibonacci(n: int) -> int:
|
|
"""Return the nth Fibonacci number (0-indexed).
|
|
|
|
fibonacci(0) = 0
|
|
fibonacci(1) = 1
|
|
fibonacci(2) = 1
|
|
fibonacci(3) = 2
|
|
...
|
|
"""
|
|
if n < 0:
|
|
raise ValueError(f"n must be non-negative, got {n}")
|
|
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
|
|
|
|
if len(sys.argv) != 2:
|
|
print(f"Usage: {sys.argv[0]} <n>")
|
|
sys.exit(1)
|
|
|
|
try:
|
|
n = int(sys.argv[1])
|
|
except ValueError:
|
|
print(f"{RED}Error: '{sys.argv[1]}' is not a valid integer{RESET}")
|
|
sys.exit(1)
|
|
|
|
result = fibonacci(n)
|
|
print(f"{CYAN}fibonacci({n}){RESET} {GREEN}={RESET} {GREEN}{result}{RESET}")
|