Skip to content

Blog#

The Fibonacci sequence

In mathematics, the Fibonacci sequence is a sequence in which each number is the sum of the two preceding ones, starting with [0, 1].

Let's calculate the first ten values of the sequence with the init_fields step factory function to initialize the accumulator variable, using the step function to determine the next value in the sequence.

Fibonacci sequence

from mltraq import Run, create_experiment
from mltraq.steps.init_fields import init_fields


def step(run: Run):
    run.fields.F.append(run.fields.F[-1] + run.fields.F[-2])


print(
    create_experiment()
    .execute(init_fields(F=[0, 1]))
    .execute([step] * 8)
    .runs.first()
    .fields.F
)
Output
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]