Let's use the four basic operations, addition +
, multiplication *
, subtraction -
and division /
(float, not integer). We'll number these operators from 1-4 in the order given above.
Stewie's sequence is defined as follows:
x = [x(1), x(2)] // Two initial numbers (one indexed)
x(3) = x(1) + x(2)
x(4) = x(2) * x(3)
x(5) = x(3) - x(4)
x(6) = x(4) / x(5)
x(7) = x(5) + x(6)
... and so on.
Challenge:
Take two non-negative integers (x(1), x(2)
), and one positive integer N
as input.
x(1)
and x(2)
will be the two first numbers of your sequence, and N
will be the length of the sequence you must output. (You can choose to have the list 0-based, in which case N
will be one less than the length).
- You can not assume that
x(2) >= x(1)
. N
will always be>2
if 1-based, (>1
if 0-based).- You do not have to handle division by zero errors.
- Note the 2nd test case. You will not get
0, 1
, andN=6
as input, since that will result in division by zero, but you must support0, 1
andN=5
. - Assume only valid input will be given.
- The input and output can be on any convenient format, but you must support at least 3 digits after the decimal points if the output is non-integer.
Test cases:
1 3
8
1, 3, 4, 12, -8, -1.5, -9.5, 14.25
0 1
5
0, 1, 1, 1, 0 // N=6 would give division by zero error. You don't need to handle that case.
1 0
9
1, 0, 1, 0, 1, 0, 1, 0, 1
6 3
25
6, 3, 9, 27, -18, -1.5, -19.5, 29.25, -48.75, -0.6, -49.35, 29.61, -78.96, -0.375, -79.335, 29.7506, -109.086, -0.272727, -109.358, 29.825, -139.183, -0.214286, -139.398, 29.8709, -169.269