Horner's Method for Polynomial Evaluation (original) (raw)

Last Updated : 23 Jul, 2025

Given a polynomial of the form cnxn + cn-1xn-1 + cn-2xn-2 + ... + c1x + c0 and a value of x, find the value of polynomial for a given value of x. Here cn, cn-1, .. are integers (may be negative) and n is a positive integer.
Input is in the form of an array say poly[] where poly[0] represents coefficient for xn and poly[1] represents coefficient for xn-1 and so on.
Examples:

// Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 Input: poly[] = {2, -6, 2, -1}, x = 3 Output: 5

// Evaluate value of 2x3 + 3x + 1 for x = 2 Input: poly[] = {2, 0, 3, 1}, x = 2 Output: 23

A naive way to evaluate a polynomial is to one by one evaluate all terms. First calculate xn, multiply the value with cn, repeat the same steps for other terms and return the sum. Time complexity of this approach is O(n2) if we use a simple loop for evaluation of xn. Time complexity can be improved to O(nLogn) if we use O(Logn) approach for evaluation of xn.
Horner's method can be used to evaluate polynomial in O(n) time. To understand the method, let us consider the example of 2x3 - 6x2 + 2x - 1. The polynomial can be evaluated as ((2x - 6)x + 2)x - 1. The idea is to initialize result as coefficient of xn which is 2 in this case, repeatedly multiply result with x and add next coefficient to result. Finally return result.
Following is implementation of Horner's Method.

Try It Yourselfredirect icon

C++ `

#include using namespace std;

// returns value of poly[0]x(n-1) + poly[1]x(n-2) + .. + poly[n-1] int horner(int poly[], int n, int x) { int result = poly[0]; // Initialize result

// Evaluate value of polynomial using Horner's method 
for (int i=1; i<n; i++) 
    result = result*x + poly[i]; 

return result; 

}

// Driver program to test above function. int main() { // Let us evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3 int poly[] = {2, -6, 2, -1}; int x = 3; int n = sizeof(poly)/sizeof(poly[0]); cout << "Value of polynomial is " << horner(poly, n, x); return 0; }

Java

// Java program for implementation of Horner Method // for Polynomial Evaluation import java.io.*;

class HornerPolynomial { // Function that returns value of poly[0]x(n-1) + // poly[1]x(n-2) + .. + poly[n-1] static int horner(int poly[], int n, int x) { // Initialize result int result = poly[0];

    // Evaluate value of polynomial using Horner's method
    for (int i=1; i<n; i++)
        result = result*x + poly[i];

    return result;
}

// Driver program
public static void main (String[] args) 
{
    // Let us evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3
    int[] poly = {2, -6, 2, -1};
    int x = 3;
    int n = poly.length;
    System.out.println("Value of polynomial is " 
                                    + horner(poly,n,x));
}

}

// Contributed by Pramod Kumar

Python3

Python program for

implementation of Horner Method

for Polynomial Evaluation

returns value of poly[0]x(n-1)

+ poly[1]x(n-2) + .. + poly[n-1]

def horner(poly, n, x):

# Initialize result
result = poly[0]  

# Evaluate value of polynomial
# using Horner's method
for i in range(1, n):

    result = result*x + poly[i]

return result

Driver program to

test above function.

Let us evaluate value of

2x3 - 6x2 + 2x - 1 for x = 3

poly = [2, -6, 2, -1] x = 3 n = len(poly)

print("Value of polynomial is " , horner(poly, n, x))

This code is contributed

by Anant Agarwal.

C#

// C# program for implementation of // Horner Method for Polynomial Evaluation. using System;

class GFG { // Function that returns value of poly[0]x(n-1) + // poly[1]x(n-2) + .. + poly[n-1] static int horner(int []poly, int n, int x) { // Initialize result int result = poly[0];

    // Evaluate value of polynomial
    // using Horner's method
    for (int i = 1; i < n; i++)
        result = result * x + poly[i];

    return result;
}

// Driver Code
public static void Main() 
{
    // Let us evaluate value of
    // 2x3 - 6x2 + 2x - 1 for x = 3
    int []poly = {2, -6, 2, -1};
    int x = 3;
    int n = poly.Length;
    Console.Write("Value of polynomial is " 
                        + horner(poly,n,x));
}

}

// This code Contributed by nitin mittal.

PHP

n,n, n,x) { // Initialize result result=result = result=poly[0]; // Evaluate value of polynomial // using Horner's method for ($i = 1; i<i < i<n; $i++) result=result = result=result * x+x + x+poly[$i]; return $result; } // Driver Code // Let us evaluate value of // 2x3 - 6x2 + 2x - 1 for x = 3 $poly = array(2, -6, 2, -1); x=3;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi><mi>o</mi><mi>f</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">n=sizeof(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.4306em;"></span><spanclass="mordmathnormal">n</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">zeo</span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mopen">(</span></span></span></span>poly)/sizeof(x = 3; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi><mi>o</mi><mi>f</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">n = sizeof(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">n</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">i</span><span class="mord mathnormal">zeo</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mopen">(</span></span></span></span>poly) / sizeof(x=3;<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi><mi>o</mi><mi>f</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">n=sizeof(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.4306em;"></span><spanclass="mordmathnormal">n</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">zeo</span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mopen">(</span></span></span></span>poly)/sizeof(poly[0]); echo "Value of polynomial is ". horner($poly, n,n, n,x); // This code is contributed by mits. ?>

JavaScript

`

Output:

Value of polynomial is 5

Time Complexity: O(n)

Auxiliary Space: O(1)