49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate and verify first 20 Fibonacci numbers - stdlib only."""
|
|
|
|
def fibonacci(n: int) -> list[int]:
|
|
"""Generate first n Fibonacci numbers."""
|
|
if n <= 0:
|
|
return []
|
|
if n == 1:
|
|
return [0]
|
|
seq = [0, 1]
|
|
for _ in range(n - 2):
|
|
seq.append(seq[-1] + seq[-2])
|
|
return seq
|
|
|
|
def verify_fibonacci(seq: list[int]) -> bool:
|
|
"""Verify sequence is valid Fibonacci."""
|
|
if len(seq) < 2:
|
|
return True
|
|
if seq[0] != 0 or seq[1] != 1:
|
|
return False
|
|
for i in range(2, len(seq)):
|
|
if seq[i] != seq[i-1] + seq[i-2]:
|
|
return False
|
|
return True
|
|
|
|
def main():
|
|
numbers = fibonacci(20)
|
|
output_file = "/root/.hermes/wizard-outputs/spark/fibonacci_output.txt"
|
|
|
|
with open(output_file, "w") as f:
|
|
f.write("First 20 Fibonacci numbers:\n")
|
|
for i, n in enumerate(numbers, 1):
|
|
f.write(f"{i}: {n}\n")
|
|
|
|
is_valid = verify_fibonacci(numbers)
|
|
expected_sum = sum(numbers)
|
|
|
|
verification_file = "/root/.hermes/wizard-outputs/spark/verification.txt"
|
|
with open(verification_file, "w") as f:
|
|
f.write(f"Sequence valid: {is_valid}\n")
|
|
f.write(f"Sum of first 20: {expected_sum}\n")
|
|
f.write(f"Last number: {numbers[-1]}\n")
|
|
|
|
print(f"Generated {len(numbers)} Fibonacci numbers")
|
|
print(f"Sum: {expected_sum}")
|
|
print(f"Valid: {is_valid}")
|
|
|
|
if __name__ == "__main__":
|
|
main() |