RosettaCodeData/Task/Fibonacci-sequence/Python/fibonacci-sequence-15.py

22 lines
564 B
Python
Raw Permalink Normal View History

2026-02-01 16:33:20 -08:00
def fibs(n):
"""Fibonacci accumulation
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
An accumulation of the first n integers in the Fibonacci series. The accumulator is a
pair of the two preceding numbers.
"""
# Local import is more efficient.
from itertools import accumulate
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
# Note: Numbers generated in range(1, n) [or range(n-1)] call will not be used.
return [a for a, b in accumulate(
2025-02-27 18:35:13 -05:00
range(1, n),
2026-02-01 16:33:20 -08:00
lambda acc, _: (acc[1], sum(acc)),
initial = (0, 1)
)
]
2023-07-01 11:58:00 -04:00
# MAIN ---
if __name__ == '__main__':
2026-02-01 16:33:20 -08:00
print(f'First twenty: {fibs(20)}')