Linear Recurrences & Matrix Powers
Characteristic roots, Fibonacci in closed form, and the n-th term in O(log n) steps.
has about decimal digits, yet its last digit is easy to find: it is 5. Linear recurrences have closed forms built from the roots of a polynomial, generating functions that are ratios of polynomials, and an -th term that takes arithmetic operations. And one innocent question about them, whether a term is ever zero, is still open.
What you’ll learn
- Solve a linear recurrence with constant coefficients from the roots of its characteristic polynomial, including repeated roots and a constant non-homogeneous term.
- Derive the generating function of a linear recurrence and recover Binet's formula by partial fractions.
- Compute the -th term with operations by matrix powers, Fibonacci numbers by fast doubling, and terms modulo for huge .
- Use the Kitamasa–Fiduccia method and Berlekamp–Massey, and state what is known about the Skolem problem.
Characteristic roots
Roots give solutions. Substituting and dividing by shows that is a solution exactly when :
Repeated roots. If is a root of multiplicity , it is also a root of the first derivatives of . Applying the operator up to times and setting shows that is a solution for each :
A basis. These sequences are linearly independent (a generalized Vandermonde argument), and the solution space has dimension , so they span it.
For the Fibonacci numbers, has roots , the golden ratio, and . Fitting , gives Binet's formula
Since for all , is the integer nearest to . That is exact mathematics but poor computing: in 64-bit floating point, rounding first gives a wrong answer at .
A repeated root brings a polynomial factor: has , so , and , give . A constant inhomogeneous term needs one particular solution: is solved by the constant , so , and the Tower of Hanoi's gives .
Think first The sequence starts with , . Use the roots of to find a closed form, and then .
The roots are 2 and 3, so ; the initial values give , , so and . The terms run , and the larger root, 3, controls the growth.
Generating functions
A generating function packs a sequence into a power series, , and turns the recurrence into algebra. For Fibonacci, multiply the recurrence by and sum over :
Every shifted series lines up with the original, and only the first terms are left over:
The denominator factors with the reciprocals of the characteristic roots, because and ; split into partial fractions, using :
Each fraction is a geometric series, and reading off the coefficient of gives Binet's formula:
Proof. Multiply: , with for . The coefficient of vanishes for every exactly when the recurrence holds, so is a polynomial of degree less than . The same bookkeeping turns sums, shifts and convolutions of sequences into products of rational functions, which is why generating functions dominate the analysis of counting problems.
The n-th term in O(log n) operations
Stack consecutive terms into a state vector . One step of the recurrence is a multiplication by the companion matrix, whose first row is and whose other rows shift the vector down: , so . Computing by repeated squaring takes matrix products, each with the schoolbook method: matrix exponentiation finds in arithmetic operations. For Fibonacci:
Square to get and compare the top-right entries, using :
The top-left entries give the odd index:
So the pair yields with three multiplications, and one addition advances to when the next bit of is 1. That is multiplications, with no matrices at all:
def fib_pair(n, m):
"""Return (F(n) mod m, F(n+1) mod m)."""
if n == 0:
return (0, 1 % m)
a, b = fib_pair(n // 2, m) # a = F(k), b = F(k+1), k = n // 2
c = a * ((2 * b - a) % m) % m # F(2k) = F(k) * (2F(k+1) - F(k))
d = (a * a + b * b) % m # F(2k+1) = F(k)^2 + F(k+1)^2
return (d, (c + d) % m) if n % 2 else (c, d)
print(fib_pair(10**18, 10)[0]) # 5
print(fib_pair(10**18, 10**9 + 7)[0]) # 209783453The recursion is only about calls deep for . Reducing modulo at every step keeps the numbers small.
Modulo , the pairs take at most values, and each pair determines its predecessor as well as its successor, so the sequence is purely periodic. Its period is the Pisano period : , , , and in general , with equality exactly when for some .
Use , the fact that leaves remainder 40 when divided by 60, and . What is the last digit of ?
Show the answer
5
The last digits repeat every 60 terms, so the last digit of is that of , which is 5. fib_pair(10**18, 10) agrees. For a modulus whose period you do not know, fast doubling needs no period at all.
Large k: polynomials instead of matrices
For an order- recurrence with in the hundreds, per matrix product hurts. By the Cayley–Hamilton theorem the companion matrix satisfies , so depends only on modulo . This gives the method contest programmers call Kitamasa's, and that Fiduccia (1985) analyzed with fast polynomial arithmetic:
Divide by : with . The shift operator , which maps to , satisfies , so the quotient part vanishes:
Compute by repeated squaring of polynomials, reducing after each product. Each step multiplies two polynomials of degree below :
Where does the recurrence come from? Often you only have numbers. The Berlekamp–Massey algorithm (Berlekamp, 1968; Massey, 1969, who recast it as finding the shortest linear feedback shift register) takes terms of a sequence over a field and returns the shortest linear recurrence that generates them, in operations. A recurrence of order is determined by consecutive terms.
That gives a standard contest pipeline for "count the objects of size , modulo a prime, for up to ": compute the first few hundred answers with a slow dynamic program, let Berlekamp–Massey find a recurrence, check it on extra terms, then evaluate the -th term with Kitamasa's method. It works whenever the answers satisfy a linear recurrence, which is the case for counts that a finite automaton or a fixed-width transfer matrix generates.
You suspect that a sequence modulo a prime satisfies a linear recurrence of order at most 50. How many consecutive terms must Berlekamp–Massey see to be sure of finding it?
- 50
- 51
- 100
- 2500
Show the answer
C. 100
terms suffice for order at most : if two sequences each satisfy a recurrence of order at most 50 and agree on their first 100 terms, their difference satisfies a recurrence of order at most 100 and starts with 100 zeros, so it is zero everywhere. With fewer terms, a shorter but wrong recurrence can fit the data. In practice, feed it more terms and verify the result on extra ones.
Does it ever hit zero? The Skolem problem
The progressions are easy to see in examples: with , vanishes at every even . They can be found effectively. The trouble is the finite exceptional set: the proof uses -adic analysis and gives no bound on how large an isolated zero can be. Whence the decision question.
Think first Why can't we decide the Skolem problem by computing terms until a zero appears?
That search finds a zero if there is one, so it semi-decides the "yes" answer. But if no zero exists it runs forever, and Skolem–Mahler–Lech gives no bound telling it when to stop: for that we would need an effective bound on the largest isolated zero. Decidability up to order 4 comes from exactly such bounds, proved with deep results on linear forms in logarithms.
Key takeaways
- An order- linear recurrence is solved by over the roots of its characteristic polynomial, with below the multiplicity of .
- Its generating function is with , and partial fractions give the closed form, as in and Binet's formula.
- The -th term takes operations by companion-matrix powers and by computing ; Fibonacci numbers need only fast doubling. Modulo the sequence is periodic, with period at most for Fibonacci.
- Berlekamp–Massey recovers a recurrence of order from terms. Whether a given recurrence ever hits zero (the Skolem problem) is decidable only up to order 4.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A recurrence has characteristic polynomial . What is the form of its general solution?
Show the answer
C.
A root of multiplicity 2 contributes and , and the simple root contributes : three independent solutions for an order-3 recurrence, matching its three initial values.
You need the -th term, modulo a prime, of a recurrence of order . Which method has the best arithmetic cost?
- Iterate the recurrence times
- Raise the companion matrix to the power
- Evaluate Binet's formula in floating point
- Compute by repeated squaring of polynomials
Show the answer
D. Compute by repeated squaring of polynomials
Kitamasa's method costs multiplications with schoolbook polynomial products, against for the matrix power. Iterating needs operations, and floating point cannot work modulo a prime at all.
Why is the Fibonacci sequence modulo purely periodic, that is, periodic from its very first term?
- Because there are at most pairs of consecutive residues, and each pair determines both the next pair and the previous one
- Because grows like
- Because must be prime
- Because Binet's formula is periodic
Show the answer
A. Because there are at most pairs of consecutive residues, and each pair determines both the next pair and the previous one
Some pair must repeat among the first , and since the map from a pair to the next is invertible (), the first repeated pair is itself. Hence ; the sharper bound needs number theory.
What is known about deciding whether an integer linear recurrence ever has a zero term?
- It is undecidable, by a reduction from the halting problem
- It is decidable up to order 4 and open from order 5
- It is decidable for every order, by the Skolem–Mahler–Lech theorem
- It is decidable only for order 2
Show the answer
B. It is decidable up to order 4 and open from order 5
Skolem–Mahler–Lech says the zeros form a finite set plus finitely many arithmetic progressions, but its proof gives no bound on the finite set. Effective bounds from linear forms in logarithms settle orders up to 4; order 5 and beyond remain open, with conditional algorithms for simple recurrences.
Further reading
- R. L. Graham, D. E. Knuth & O. Patashnik, Concrete Mathematics, 2nd ed. (Addison-Wesley, 1994), chapters 6 and 7.
- C. M. Fiduccia, "An efficient formula for linear recurrences," SIAM Journal on Computing 14(1) (1985), 106–112.
- J. L. Massey, "Shift-register synthesis and BCH decoding," IEEE Transactions on Information Theory 15(1) (1969), 122–127.
- G. Everest, A. van der Poorten, I. Shparlinski & T. Ward, Recurrence Sequences (American Mathematical Society, 2003).