diff --git a/wizard-work/spark/fib_calc_new.py b/wizard-work/spark/fib_calc_new.py new file mode 100644 index 0000000..e9dcd8e --- /dev/null +++ b/wizard-work/spark/fib_calc_new.py @@ -0,0 +1,34 @@ +#!/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 ", 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() \ No newline at end of file