Inspired by a recent challenge involving Fibonacci numbers in which OEIS was mentioned, I would like to present a challenge of creating a function that generates a wide array of different linear integer sequences, depending on user input.
Specifically, the user should provide three inputs:
- the kernel (a representation of the recurrence relation). More about this below.
- the starting value of every 'seed' needed for the sequence, and
- the number of integers to generate and display.
For example, the Fibonacci sequence has two members in the kernel and needs two starting seeds (0, 1). Then \$F(n) = F(n-1) + F(n-2)\$.
The Padovan sequence has three members in its kernel and needs three seeds, (1,0,0). Then \$F(n) = F(n-2) + F(n-3)\$.
I am not going to mandate the format of the 'kernel' input, though the natural form would be, I think, a list of the same length of the list of seeds, composed of integers corresponding to the weight to give to each of integers in the previous row.
This means that the kernel of the Fibonacci sequence, which adds together the previous two numbers, can be represented as (1,1). By contrast, the kernel of the Padovan sequence, which ignores the first member of the previous row and adds the second and third terms, can be represented as (0,1,1).
Your code should be able to handle an arbitrary number of seeds and lists of arbitrary length. For each sequence, the number of members of the kernel and the length of the seed list should be the same positive integer. For the sake of this challenge, assume all members of the kernel and all seed values are integers (though I suspect that most of the responses to this challenge will work for all real numbers). Similarly assume the required sequence length will be a positive integer.
My examples below use (parentheses) to encapsulate lists, but use whatever notation is most natural to your programming language.
Examples!
For the Fibonacci sequence, your function should take three inputs like
(1, 1), (0, 1), 10
(that's kernel, seed, and sequence length), and generate
(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
For the Padovan sequence, input like
(0, 1, 1), (1, 0, 0), 20
should generate
(1, 0, 0, 1, 0, 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37)
The Perrin Sequence takes input like
(0, 1, 1), (3, 0, 2), 10
to produce
(3, 0, 2, 3, 2, 5, 5, 7, 10, 12)
For edge cases,
(1, 1), (0, 0), 5
should produce
(0,0,0,0,0)
and
(-1, 0, 1, 0), (0, -1, 0, 1), 20
should produce
(0, -1, 0, 1, -2, 2, -1, -1, 3, -4, 3, 0, -4, 7, -7, 3, 4, -11, 14, -10).
I suspect there are going to be some extremely concise responses to this.