From c45c5c79c120f1f10d03d176b793fef9deb1887e Mon Sep 17 00:00:00 2001 From: "Lennie S." Date: Sat, 11 Apr 2026 22:36:58 +0000 Subject: [PATCH] Initial commit: fibonacci calculator --- fibonacci.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 fibonacci.py diff --git a/fibonacci.py b/fibonacci.py new file mode 100644 index 0000000..a9a4c09 --- /dev/null +++ b/fibonacci.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Fibonacci sequence calculator.""" + + +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]} ") + sys.exit(1) + + try: + n = int(sys.argv[1]) + except ValueError: + print(f"Error: '{sys.argv[1]}' is not a valid integer") + sys.exit(1) + + result = fibonacci(n) + print(f"fibonacci({n}) = {result}")