30 lines
864 B
Python
30 lines
864 B
Python
#!/usr/bin/env python3
|
|
"""Calculate Fibonacci sequence and save to file."""
|
|
import os
|
|
|
|
def fibonacci(n):
|
|
"""Return first n Fibonacci numbers."""
|
|
if n <= 0:
|
|
return []
|
|
if n == 1:
|
|
return [0]
|
|
if n == 2:
|
|
return [0, 1]
|
|
fib = [0, 1]
|
|
for i in range(2, n):
|
|
fib.append(fib[-1] + fib[-2])
|
|
return fib
|
|
|
|
if __name__ == '__main__':
|
|
os.makedirs('/root/.hermes/wizard-outputs/spark', exist_ok=True)
|
|
# Calculate first 20 Fibonacci numbers
|
|
result = fibonacci(20)
|
|
|
|
# Write to output file
|
|
with open('/root/.hermes/wizard-outputs/spark/fibonacci.txt', 'w') as f:
|
|
f.write("First 20 Fibonacci numbers:\n")
|
|
for i, val in enumerate(result):
|
|
f.write(f"F({i}) = {val}\n")
|
|
|
|
print(f"Generated {len(result)} Fibonacci numbers")
|
|
print(f"Last number: F(19) = {result[-1]}") |