Matrix Exponentiation for Recurrences
A technique for computing the n-th term of a linear recurrence — like the Fibonacci sequence — in logarithmic time by expressing the recurrence as a matrix and repeatedly squaring it instead of iterating n times.
A linear recurrence like the Fibonacci rule can be rewritten as a matrix multiplication: stacking consecutive terms into a vector, for a fixed matrix . Applying once advances the sequence by one step, so applying it times from the starting vector produces — but computing doesn't require separate multiplications. Repeated squaring computes in about matrix multiplications by writing in binary and combining powers of that double at each stage (), the same trick used for fast integer exponentiation.
For Fibonacci, . To find , instead of 10 sequential additions, compute , , by three squarings, then combine to get — about 4 matrix multiplications total rather than 10 scalar steps. The gap widens enormously for large : computing takes about 30 matrix multiplications by this method versus a billion additions by direct iteration.
The technique generalizes to any order- linear recurrence with constant coefficients, using a companion matrix, and is a standard tool whenever a coding interview or a simulation needs a huge recurrence term without simulating every intermediate step. It also composes naturally with modular arithmetic — since matrix multiplication only needs addition and multiplication, taking every entry modulo some large prime at each step keeps the numbers bounded, which is exactly what's needed when a problem asks for a huge Fibonacci-style term modulo .
Any linear recurrence can be advanced steps via for a fixed matrix , and repeated squaring computes in multiplications instead of sequential steps.
Practice in interviews
Further reading
- Cormen et al., Introduction to Algorithms, ch. 4