Skip to content

Evaluating a polynomial with nested multiplication

Question

What is an efficient method for evaluating p(x)=anxn+an−1xn−1+⋯+a0p(x)=a_nx^n+a_{n-1}x^{n-1}+\dots+a_0 at a given xx?

Short answer

Horner's scheme: nn multiplications and nn additions. Rewrite p(x)=(⋯((anx+an−1)x+an−2)x+⋯ )x+a0p(x)=(\cdots((a_nx+a_{n-1})x+a_{n-2})x+\cdots)x+a_0 and evaluate from the inside out, starting with b=anb=a_n and repeating b←bx+akb\leftarrow bx+a_k. Computing each power separately costs O(n2)O(n^2) multiplications; Horner is O(n)O(n) and also numerically well behaved.

Adapted from Zhou, A Practical Guide to Quantitative Finance Interviews, sec. 7.1 Algorithms, 'Horner's algorithm', p.172; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question