A linear recurrence of order two is fixed by two coefficients and two starting terms. Your program lists the first terms, which is the first thing to do before you look for a closed form.
What to build
A program that prints a(0), a(1), ..., a(n) for the recurrence a(k) = c1 a(k − 1) + c2 a(k − 2).
Requirements
- Read five whole numbers on one line: c1, c2, a(0), a(1) and n.
- Print the terms a(0) to a(n) on one line, separated by spaces.
Input
One line with c1, c2, a0, a1 and n.
Output
One line with n + 1 numbers.
Constraints
1 ≤ n ≤ 60; the coefficients and starting terms have absolute value at most 20.
Example
Input:
1 1 0 1 10
Output:
0 1 1 2 3 5 8 13 21 34 55
Why it works
A recurrence plus initial conditions gives each term from the two before it, so a loop that keeps the whole list reproduces the sequence. The classic examples are Fibonacci (1, 1, 0, 1), Lucas (1, 1, 2, 1) and Pell (2, 1, 0, 1).
Examples
1 1 0 1 10
0 1 1 2 3 5 8 13 21 34 55
5 -6 1 4 5
1 4 14 46 146 454
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Keep a list of terms; each new term is c1 * a[k-1] + c2 * a[k-2].
Hint 2
The first two terms are given, so for n = 1 print just a(0) and a(1).
Hint 3
Python integers have no limit, so large terms are fine.
Solution
Show one correct solution (try it yourself first)
c1, c2, a0, a1, n = map(int, input().split())
a = [a0, a1]
for k in range(2, n + 1):
a.append(c1 * a[k - 1] + c2 * a[k - 2])
print(*a[: n + 1])
▶ Open this solution in the compiler, then press Visualize to watch it run step by step