Evaluating a polynomial at a point is a common task (graphics curves, error-checking codes, numerical methods). The obvious method wastes many multiplications; Horner’s rule removes almost all of them. This page analyses both and corrects the study material’s loop.
Polynomials
A polynomial may have any number of terms: 5x3 (one term, a monomial), x+1 (two terms, a binomial) and 15x4+7x2+9x+7 are all polynomials.
In a program, a polynomial of degree n is usually stored as an array a[0..n] of coefficients, with a[k] the coefficient of xk. A sparse polynomial (few non-zero terms) may instead be stored as a list of (coefficient, exponent) pairs.
Brute force
Evaluate each term separately: akxk computed as ak⋅x⋅x⋯x uses k multiplications. The total is
n+(n−1)+⋯+2+1=2n(n+1)=Θ(n2) multiplications.
(Additions are the same in both methods: n of them.)
Horner’s rule
Factor out x repeatedly:
P(x)=(⋯((anx+an−1)x+an−2)x+⋯+a1)x+a0.
For example, 15x4+17x3−12x2+13x+16=(((15x+17)x−12)x+13)x+16.
Horner pseudocode
Horner(a[0..n], x)p ← a[n]for i ← n − 1 downto 0 do p ← p * x + a[i]end forreturn p
The loop runs n times with one multiplication and one addition each: exactly n multiplications, Θ(n).
Why it is correct. After the iteration with index i, p=anxn−i+an−1xn−i−1+⋯+ai. This holds at the start (p=an) and each iteration multiplies by x and adds the next coefficient, so it stays true (a loop invariant, proved by induction). After i=0, p=anxn+⋯+a0=P(x).
C · horner_bug.c
#include <stdio.h>int main(void){ long A[] = {7, -5, 0, 2, 3}; /* 3x^4 + 2x^3 - 5x + 7 */ int n = 4, i; long x = 2; long p = A[n]; for (i = n - 1; i <= 0; i--) /* loop test as printed */ p = p * x + A[i]; printf("printed loop returns %ld (correct value is 61)\n", p); return 0;}
Output
printed loop returns 3 (correct value is 61)
C · horner.c
#include <stdio.h>/* Coefficients a[0..n] of p(x) = a[n] x^n + ... + a[1] x + a[0]. *//* Brute force: each term a[k] * x * x * ... * x (k multiplications). */long brute_force(const long a[], int n, long x, int *mults){ long p = 0; *mults = 0; for (int k = 0; k <= n; k++) { long term = a[k]; for (int j = 0; j < k; j++) { term = term * x; (*mults)++; } p = p + term; } return p;}/* Horner's rule: p = (...((a[n] x + a[n-1]) x + a[n-2]) ...) x + a[0]. */long horner(const long a[], int n, long x, int *mults){ long p = a[n]; *mults = 0; for (int i = n - 1; i >= 0; i--) { /* i >= 0, not i <= 0 */ p = p * x + a[i]; (*mults)++; } return p;}int main(void){ long a[] = {7, -5, 0, 2, 3}; /* 3x^4 + 2x^3 + 0x^2 - 5x + 7 */ int n = 4, mb, mh; long x = 2; long pb = brute_force(a, n, x, &mb); long ph = horner(a, n, x, &mh); printf("brute force: p(2) = %ld with %d multiplications\n", pb, mb); printf("horner : p(2) = %ld with %d multiplications\n", ph, mh); long b[] = {6, 5, 3}; /* 3x^2 + 5x + 6 */ printf("horner : 3x^2+5x+6 at x=3 is %ld\n", horner(b, 2, 3, &mh)); return 0;}
Output
brute force: p(2) = 61 with 10 multiplicationshorner : p(2) = 61 with 4 multiplicationshorner : 3x^2+5x+6 at x=3 is 48
For degree 4 the brute force uses 4⋅5/2=10 multiplications and Horner uses 4. For degree 100 the numbers are 5050 and 100.
The study material’s answer starts with p=0 and runs 5 iterations; that works too (the first iteration just gives 0⋅x+a4=a4) but wastes one multiplication.
Key takeaways
A polynomial is a sum of one or more terms akxk with non-negative integer exponents.
Brute force: 2n(n+1) multiplications, Θ(n2).
Horner: p←an; for i=n−1 down to 0, p←px+ai; n multiplications, Θ(n).
The loop must run while i≥0 and must include zero coefficients.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Rewrite 5x3+4x2−2x+1 in Horner form and evaluate it at x=2, showing each iteration.
Show worked answer
Horner form: ((5x + 4)x − 2)x + 1. At x = 2: p = 5; p = 5·2 + 4 = 14; p = 14·2 − 2 = 26; p = 26·2 + 1 = 53. Check: 5(8) + 4(4) − 2(2) + 1 = 40 + 16 − 4 + 1 = 53. Three multiplications were used.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/complexity-analysis-of-simple-algorithms/polynomial-evaluation-and-horner-s-rule/ to read this page.