Bostan-Mori: The Trillionth Term Without a Trillion States
When a recurrence has order several thousand and the requested index is , matrix exponentiation is usually the wrong abstraction. The recurrence has a compact rational generating function. Bostan-Mori repeatedly halves the requested index while keeping the polynomial degree bounded.
Turn the recurrence into a fraction
Let for , with initial terms . Set
and take . Multiplying the full generating function by cancels every coefficient from degree onward. Consequently .
For the running example , , , we get , . The sequence starts . Our task is coefficient extraction, not state simulation.
Make the denominator even
Multiply numerator and denominator by :
The denominator is unchanged under , so its odd coefficients vanish. Write it as . Split the new numerator into even and odd parts:
For an even requested index , keep and ask for coefficient . For an odd index , keep instead. This is an exact identity; it does not discard contributions that could matter later.
Trace one iteration
For our fraction,
To find , take the even numerator, yielding , now at index two. One more even extraction gives at index one. Selecting the odd numerator on the next iteration gives constant 46. No intermediate prefix of length four was required by the algorithm.
Why degrees stay under control
If and , multiplication raises degrees to at most and . Extracting one parity divides them roughly in half, restoring the original bound. Each step costs , and there are steps. With NTT, the total is and working storage is linear in for the straightforward version.
The companion implementation exposes wsh::bostan_mori(P, Q, N). It accepts an unsigned 64-bit index, handles a zero numerator, and requires an invertible constant of . Coefficients must use the header’s modulus.
Choosing the method
For order two or three, tiny matrix exponentiation can be simpler and faster. For a single huge-index query with a large order, Bostan-Mori avoids dense matrices. If you need all coefficients through and is moderate, compute instead. If the recurrence is unknown, discovering it is a separate problem; Bostan-Mori does not infer a recurrence from arbitrary data.
Exercise. Let count ordered constructions from pieces of sizes two and five, with . Derive and find by hand before attempting modulo . Explain why a multiset interpretation would produce a different denominator.
Support & Share
If this article helped you, please share or support!





























































Conversation
Comments powered by Waline