spark: add fibonacci calculator utility
This commit is contained in:
1 parent
893d5d46c9
commit
25b68eb767
1 file changed
+34
@@ -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 <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()
|
||||
Reference in new issue
Block a user