MCS-211 · Block 1 Unit 3

Complexity Analysis of Simple Algorithms

Section 3 of 8

Block 1 · Unit 3Complexity Analysis of Simple Algorithms9 min · Section 3 of 8

3.3Polynomial Evaluation and Horner's Rule

Covers IGNOU MCS-211 §3.4.2

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: 5x35x^3 (one term, a monomial), x+1x + 1 (two terms, a binomial) and 15x4+7x2+9x+715x^4 + 7x^2 + 9x + 7 are all polynomials.

In a program, a polynomial of degree nn is usually stored as an array a[0..n] of coefficients, with a[k] the coefficient of xkx^k. A sparse polynomial (few non-zero terms) may instead be stored as a list of (coefficient, exponent) pairs.

Brute force

Evaluate each term separately: akxka_k x^k computed as ak⋅x⋅x⋯xa_k \cdot x \cdot x \cdots x uses kk multiplications. The total is n+(n−1)+⋯+2+1=n(n+1)2=Θ(n2) multiplications.n + (n-1) + \dots + 2 + 1 = \frac{n(n+1)}{2} = \Theta(n^2) \text{ multiplications.} (Additions are the same in both methods: nn of them.)

Horner’s rule

Factor out xx repeatedly: P(x)=(⋯((anx+an−1)x+an−2)x+⋯+a1)x+a0.P(x) = \big(\cdots\big((a_n x + a_{n-1})x + a_{n-2}\big)x + \dots + a_1\big)x + a_0 . For example, 15x4+17x3−12x2+13x+16=(((15x+17)x−12)x+13)x+1615x^4 + 17x^3 - 12x^2 + 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 for
return p

The loop runs nn times with one multiplication and one addition each: exactly nn multiplications, Θ(n)\Theta(n).

Why it is correct. After the iteration with index ii, p=anxn−i+an−1xn−i−1+⋯+aip = a_n x^{n-i} + a_{n-1} x^{n-i-1} + \dots + a_i. This holds at the start (p=anp = a_n) and each iteration multiplies by xx and adds the next coefficient, so it stays true (a loop invariant, proved by induction). After i=0i = 0, p=anxn+⋯+a0=P(x)p = a_n x^n + \dots + a_0 = 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 multiplications
horner     : p(2) = 61 with 4 multiplications
horner     : 3x^2+5x+6 at x=3 is 48

For degree 4 the brute force uses 4⋅5/2=104 \cdot 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=0p = 0 and runs 5 iterations; that works too (the first iteration just gives 0⋅x+a4=a40 \cdot x + a_4 = a_4) but wastes one multiplication.

Key takeaways

  • A polynomial is a sum of one or more terms akxka_k x^k with non-negative integer exponents.
  • Brute force: n(n+1)2\frac{n(n+1)}{2} multiplications, Θ(n2)\Theta(n^2).
  • Horner: p←anp \leftarrow a_n; for i=n−1i = n-1 down to 0, p←px+aip \leftarrow p x + a_i; nn multiplications, Θ(n)\Theta(n).
  • The loop must run while i≥0i \ge 0 and must include zero coefficients.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many multiplications does Horner’s rule use for a polynomial of degree 7?

Show answer

Answer: C. 7

One multiplication per iteration, n = 7 iterations. 28 = 7 × 8 / 2 is the brute-force count.

Question 2 · multiple choice

Which of these is a polynomial?

Show answer

Answer: B. x+1x + 1

x + 1 has two terms with non-negative integer exponents. The number of terms does not matter; fractional or negative exponents and variable exponents are not allowed.

Question 3 · multiple choice

Evaluate 2x3−x+42x^3 - x + 4 at x=3x = 3 with Horner’s rule. What is the value?

Show answer

Answer: A. 55

Coefficients 2, 0, −1, 4: p = 2 → 2·3 + 0 = 6 → 6·3 − 1 = 17 → 17·3 + 4 = 55. Forgetting the 0 coefficient gives a different, wrong value.

Question 4 · multiple choice

How many multiplications does brute-force evaluation of a degree-10 polynomial use?

Show answer

Answer: C. 55

10 + 9 + … + 1 = 10 × 11 / 2 = 55.

Question 5 · multiple choice

In for (i = n-1; i <= 0; i--) with n=4n = 4, how many times does the body run?

Show answer

Answer: D. 0

i starts at 3, and 3 <= 0 is false, so the loop ends immediately. The correct test is i >= 0.

Question 6 · short answer

Rewrite 5x3+4x2−2x+15x^3 + 4x^2 - 2x + 1 in Horner form and evaluate it at x=2x = 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.

↑ ↓ move↵ openTab switch type