Horner's rule for polynomial evaluation: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Arturo implementation)
(Added Easylang)
 
(17 intermediate revisions by 13 users not shown)
Line 24: Line 24:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F horner(coeffs, x)
<syntaxhighlight lang="11l">F horner(coeffs, x)
V acc = 0
V acc = 0
L(c) reversed(coeffs)
L(c) reversed(coeffs)
Line 30: Line 30:
R acc
R acc


print(horner([-19, 7, -4, 6], 3))</lang>
print(horner([-19, 7, -4, 6], 3))</syntaxhighlight>


{{out}}
{{out}}
Line 38: Line 38:


=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
<lang 360asm>* Horner's rule for polynomial evaluation - 07/10/2015
<syntaxhighlight lang="360asm">* Horner's rule for polynomial evaluation - 07/10/2015
HORNER CSECT
HORNER CSECT
USING HORNER,R15 set base register
USING HORNER,R15 set base register
Line 58: Line 58:
PG DS CL12 buffer
PG DS CL12 buffer
YREGS
YREGS
END HORNER</lang>
END HORNER</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 65: Line 65:


=={{header|ACL2}}==
=={{header|ACL2}}==
<lang Lisp>(defun horner (ps x)
<syntaxhighlight lang="lisp">(defun horner (ps x)
(if (endp ps)
(if (endp ps)
0
0
(+ (first ps)
(+ (first ps)
(* x (horner (rest ps) x)))))</lang>
(* x (horner (rest ps) x)))))</syntaxhighlight>


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>INT FUNC Horner(INT ARRAY coeffs INT count,x)
<syntaxhighlight lang="action!">INT FUNC Horner(INT ARRAY coeffs INT count,x)
INT v,i
INT v,i


Line 102: Line 102:
res=Horner(coeffs,4,x)
res=Horner(coeffs,4,x)
PrintF("=%I%E",res)
PrintF("=%I%E",res)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Horner's_rule_for_polynomial_evaluation.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Horner's_rule_for_polynomial_evaluation.png Screenshot from Atari 8-bit computer]
Line 111: Line 111:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Float_Text_IO; use Ada.Float_Text_IO;
<syntaxhighlight lang="ada">with Ada.Float_Text_IO; use Ada.Float_Text_IO;


procedure Horners_Rule is
procedure Horners_Rule is
Line 127: Line 127:
begin
begin
Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;</lang>
end Horners_Rule;</syntaxhighlight>
Output:
Output:
<pre>128.0</pre>
<pre>128.0</pre>


=={{header|Aime}}==
=={{header|Aime}}==
<lang aime>real
<syntaxhighlight lang="aime">real
horner(list coeffs, real x)
horner(list coeffs, real x)
{
{
Line 154: Line 154:


0;
0;
}</lang>
}</syntaxhighlight>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G}}
{{works with|ALGOL 68G}}
<lang algol68>PROC horner = ([]REAL c, REAL x)REAL :
<syntaxhighlight lang="algol68">PROC horner = ([]REAL c, REAL x)REAL :
(
(
REAL res := 0.0;
REAL res := 0.0;
Line 170: Line 170:
[4]REAL coeffs := (-19.0, 7.0, -4.0, 6.0);
[4]REAL coeffs := (-19.0, 7.0, -4.0, 6.0);
print( horner(coeffs, 3.0) )
print( horner(coeffs, 3.0) )
)</lang>
)</syntaxhighlight>


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% Horner's rule for polynominal evaluation %
% Horner's rule for polynominal evaluation %
% returns the value of the polynominal defined by coefficients, %
% returns the value of the polynominal defined by coefficients, %
Line 199: Line 199:
write( r_format := "A", r_w := 8, r_d := 2, Horner( coefficients, 4, 3 ) )
write( r_format := "A", r_w := 8, r_d := 2, Horner( coefficients, 4, 3 ) )
end test_cases
end test_cases
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 207: Line 207:
=={{header|APL}}==
=={{header|APL}}==
Works in [[Dyalog APL]]
Works in [[Dyalog APL]]
<lang APL>h←⊥∘⌽</lang>
<syntaxhighlight lang="apl">h←⊥∘⌽</syntaxhighlight>
{{output}}
{{output}}
<pre>
<pre>
Line 215: Line 215:


=={{header|ATS}}==
=={{header|ATS}}==
<lang ATS>#include
<syntaxhighlight lang="ats">#include
"share/atspre_staload.hats"
"share/atspre_staload.hats"


Line 238: Line 238:
in
in
println! (res)
println! (res)
end // end of [main0]</lang>
end // end of [main0]</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">horner: function [coeffs, x][

<lang rebol>horner: function [coeffs, x][
result: 0
result: 0
loop reverse coeffs 'c ->
loop reverse coeffs 'c ->
Line 249: Line 248:
]
]


print horner @[neg 19, 7, neg 4, 6] 3</lang>
print horner @[neg 19, 7, neg 4, 6] 3</syntaxhighlight>

{{out}}
{{out}}

<pre>128</pre>
<pre>128</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang autohotkey>Coefficients = -19, 7, -4, 6
<syntaxhighlight lang="autohotkey">Coefficients = -19, 7, -4, 6
x := 3
x := 3


Line 271: Line 268:
i := Co0 - A_Index + 1, Result := Result * x + Co%i%
i := Co0 - A_Index + 1, Result := Result * x + Co%i%
Return, Result
Return, Result
}</lang>
}</syntaxhighlight>
Message box shows:
Message box shows:
<pre>128</pre>
<pre>128</pre>


=={{header|AWK}}==
=={{header|AWK}}==
<lang awk>#!/usr/bin/awk -f
<syntaxhighlight lang="awk">#!/usr/bin/awk -f
function horner(x, A) {
function horner(x, A) {
acc = 0;
acc = 0;
Line 287: Line 284:
split(p,P);
split(p,P);
print horner(x,P);
print horner(x,P);
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 294: Line 291:
128
128
</pre>
</pre>

=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
<syntaxhighlight lang="qbasic">100 HOME : REM 100 CLS for Chipmunk Basic and GW-BASIC
100 CLS : REM 100 HOME for Applesoft BASIC
110 X = 3
120 DIM COEFFS(3)
130 COEFFS(0) = -19
140 COEFFS(1) = 7
150 COEFFS(2) = -4
160 COEFFS(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 ACCUM = 0
200 FOR I = 3 TO 0 STEP -1
210 ACCUM = (ACCUM*X)+COEFFS(I)
220 NEXT I
230 PRINT ACCUM
240 END</syntaxhighlight>

==={{header|BASIC256}}===
<syntaxhighlight lang="vb">x = 3
dim coeficientes = {-19, 7, -4, 6}
print "Horner's algorithm for the polynomial ";
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeficientes, x)
end

function AlgoritmoHorner(coeffs, x)
acumulador = 0
for i = coeffs[?]-1 to 0 step -1
acumulador = (acumulador * x) + coeffs[i]
next i
return acumulador
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>

==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 x = 3
120 DIM coeffs(3)
130 coeffs(0) = -19
140 coeffs(1) = 7
150 coeffs(2) = -4
160 coeffs(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 accum = 0
200 FOR i = UBOUND(coeffs,1) TO 0 STEP -1
210 accum = (accum*x)+coeffs(i)
220 NEXT i
230 PRINT accum
240 END</syntaxhighlight>
{{out}}
<pre>Horner's algorithm for the polynomial
6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: 128</pre>

==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public coeficientes As New Integer[4]

Public Function AlgoritmoHorner(coeficientes As Integer[], x As Integer) As Integer
coeficientes[0] = -19
coeficientes[1] = 7
coeficientes[2] = -4
coeficientes[3] = 6
Dim i As Integer, acumulador As Integer = 0

For i = coeficientes.Count - 1 To 0 Step -1
acumulador = (acumulador * x) + coeficientes[i]
Next
Return acumulador
End Function

Public Sub Main()
Dim x As Integer = 3
Print "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
Print AlgoritmoHorner(coeficientes, x)
End </syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>

==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS : REM 100 HOME for Applesoft BASIC
110 X = 3
120 DIM COEFFS(3)
130 COEFFS(0) = -19
140 COEFFS(1) = 7
150 COEFFS(2) = -4
160 COEFFS(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 ACCUM = 0
200 FOR I = 3 TO 0 STEP -1
210 ACCUM = (ACCUM*X)+COEFFS(I)
220 NEXT I
230 PRINT ACCUM
240 END</syntaxhighlight>
{{out}}
<pre>Same as Chipmunk Basic entry.</pre>

==={{header|Minimal BASIC}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
{{works with|Yabasic}}
<syntaxhighlight lang="qbasic">20 LET X = 3
30 DIM C(3)
40 LET C(0) = -19
50 LET C(1) = 7
60 LET C(2) = -4
70 LET C(3) = 6
80 PRINT "HORNER'S ALGORITHM FOR THE POLYNOMIAL"
90 PRINT "6*X^3 - 4*X^2 + 7*X - 19 WHEN X = 3 : ";
100 LET A = 0
110 FOR I = 3 TO 0 STEP -1
120 LET A = (A*X)+C(I)
130 NEXT I
140 PRINT A
150 END</syntaxhighlight>
{{out}}
<pre>Same as Chipmunk Basic entry.</pre>

==={{header|MSX Basic}}===
The [[#Minimal_BASIC|Minimal BASIC]] solution works without any changes.

==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FUNCTION Horner (coeffs(), x)
acumulador = 0
FOR i = UBOUND(coeffs) TO LBOUND(coeffs) STEP -1
acumulador = (acumulador * x) + coeffs(i)
NEXT i
Horner = acumulador
END FUNCTION

x = 3
DIM coeffs(3)
DATA -19, 7, -4, 6
FOR a = LBOUND(coeffs) TO UBOUND(coeffs)
READ coeffs(a)
NEXT a

PRINT "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
PRINT Horner(coeffs(), x)
END</syntaxhighlight>

==={{header|Quite BASIC}}===
The [[#Minimal_BASIC|Minimal BASIC]] solution works without any changes.

==={{header|Yabasic}}===
<syntaxhighlight lang="vb">x = 3
dim coeffs(4)
coeffs(0) = -19
coeffs(1) = 7
coeffs(2) = -4
coeffs(3) = 6
print "Horner's algorithm for the polynomial ";
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeffs, x)
end

sub AlgoritmoHorner(coeffs, x)
local acumulador, i
acumulador = 0
for i = arraysize(coeffs(),1) to 0 step -1
acumulador = (acumulador * x) + coeffs(i)
next i
return acumulador
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>


=={{header|Batch File}}==
=={{header|Batch File}}==
<lang dos>
<syntaxhighlight lang="dos">
@echo off
@echo off


Line 320: Line 511:
echo %return%
echo %return%
exit /b
exit /b
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 330: Line 521:


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
<lang bbcbasic> DIM coefficients(3)
<syntaxhighlight lang="bbcbasic"> DIM coefficients(3)
coefficients() = -19, 7, -4, 6
coefficients() = -19, 7, -4, 6
PRINT FNhorner(coefficients(), 3)
PRINT FNhorner(coefficients(), 3)
Line 340: Line 531:
v = v * x + coeffs(i%)
v = v * x + coeffs(i%)
NEXT
NEXT
= v</lang>
= v</syntaxhighlight>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang bracmat>( ( Horner
<syntaxhighlight lang="bracmat">( ( Horner
= accumulator coefficients x coeff
= accumulator coefficients x coeff
. !arg:(?coefficients.?x)
. !arg:(?coefficients.?x)
Line 354: Line 545:
)
)
& Horner$(-19 7 -4 6.3)
& Horner$(-19 7 -4 6.3)
);</lang>
);</syntaxhighlight>
Output:
Output:
<pre>128</pre>
<pre>128</pre>
Line 360: Line 551:
=={{header|C}}==
=={{header|C}}==
{{trans|Fortran}}
{{trans|Fortran}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


double horner(double *coeffs, int s, double x)
double horner(double *coeffs, int s, double x)
Line 381: Line 572:
printf("%5.1f\n", horner(coeffs, sizeof(coeffs)/sizeof(double), 3.0));
printf("%5.1f\n", horner(coeffs, sizeof(coeffs)/sizeof(double), 3.0));
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Linq;


Line 399: Line 590:
Console.WriteLine(Horner(new[] { -19.0, 7.0, -4.0, 6.0 }, 3.0));
Console.WriteLine(Horner(new[] { -19.0, 7.0, -4.0, 6.0 }, 3.0));
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>128</pre>
<pre>128</pre>
Line 406: Line 597:
The same C function works too, but another solution could be:
The same C function works too, but another solution could be:


<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <vector>


Line 426: Line 617:
cout << horner(v, 3.0) << endl;
cout << horner(v, 3.0) << endl;
return 0;
return 0;
}</lang>
}</syntaxhighlight>


Yet another solution, which is more idiomatic in C++ and works on any bidirectional sequence:
Yet another solution, which is more idiomatic in C++ and works on any bidirectional sequence:


<lang cpp>
<syntaxhighlight lang="cpp">
#include <iostream>
#include <iostream>


Line 447: Line 638:
std::cout << horner(c, c + 4, 3) << std::endl;
std::cout << horner(c, c + 4, 3) << std::endl;
}
}
</syntaxhighlight>
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>(defn horner [coeffs x]
<syntaxhighlight lang="clojure">(defn horner [coeffs x]
(reduce #(-> %1 (* x) (+ %2)) (reverse coeffs)))
(reduce #(-> %1 (* x) (+ %2)) (reverse coeffs)))


(println (horner [-19 7 -4 6] 3))</lang>
(println (horner [-19 7 -4 6] 3))</syntaxhighlight>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
<lang coffeescript>
<syntaxhighlight lang="coffeescript">
eval_poly = (x, coefficients) ->
eval_poly = (x, coefficients) ->
# coefficients are for ascending powers
# coefficients are for ascending powers
Line 466: Line 657:
console.log eval_poly 10, [4, 3, 2, 1] # 1234
console.log eval_poly 10, [4, 3, 2, 1] # 1234
console.log eval_poly 2, [1, 1, 0, 0, 1] # 19
console.log eval_poly 2, [1, 1, 0, 0, 1] # 19
</syntaxhighlight>
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun horner (coeffs x)
<syntaxhighlight lang="lisp">(defun horner (coeffs x)
(reduce #'(lambda (coef acc) (+ (* acc x) coef))
(reduce #'(lambda (coef acc) (+ (* acc x) coef))
coeffs :from-end t :initial-value 0))</lang>
coeffs :from-end t :initial-value 0))</syntaxhighlight>


Alternate version using LOOP. Coefficients are passed in a vector.
Alternate version using LOOP. Coefficients are passed in a vector.


<lang lisp>(defun horner (x a)
<syntaxhighlight lang="lisp">(defun horner (x a)
(loop :with y = 0
(loop :with y = 0
:for i :from (1- (length a)) :downto 0
:for i :from (1- (length a)) :downto 0
Line 481: Line 672:
:finally (return y)))
:finally (return y)))


(horner 1.414 #(-2 0 1))</lang>
(horner 1.414 #(-2 0 1))</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
The poly() function of the standard library std.math module uses Horner's rule:
The poly() function of the standard library std.math module uses Horner's rule:
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
void main() {
void main() {
import std.stdio, std.math;
import std.stdio, std.math;
Line 493: Line 684:
poly(x,pp).writeln;
poly(x,pp).writeln;
}
}
}</lang>
}</syntaxhighlight>
Basic implementation:
Basic implementation:
<lang d>import std.stdio, std.traits;
<syntaxhighlight lang="d">import std.stdio, std.traits;


CommonType!(U, V) horner(U, V)(U[] p, V x) pure nothrow @nogc {
CommonType!(U, V) horner(U, V)(U[] p, V x) pure nothrow @nogc {
Line 506: Line 697:
void main() {
void main() {
[-19, 7, -4, 6].horner(3.0).writeln;
[-19, 7, -4, 6].horner(3.0).writeln;
}</lang>
}</syntaxhighlight>
More functional style:
More functional style:
<lang d>import std.stdio, std.algorithm, std.range;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range;


auto horner(T, U)(in T[] p, in U x) pure nothrow @nogc {
auto horner(T, U)(in T[] p, in U x) pure nothrow @nogc {
Line 516: Line 707:
void main() {
void main() {
[-19, 7, -4, 6].horner(3.0).writeln;
[-19, 7, -4, 6].horner(3.0).writeln;
}</lang>
}</syntaxhighlight>


=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}


<syntaxhighlight lang="Delphi">
function EvaluatePolynomial(Args: array of double; X: double): double;
{Evaluate polynomial using Horner's rule }
var I: integer;
begin
Result:=0;
for I:=High(Args) downto 0 do
Result:=(Result * X ) + Args[I];
end;

function GetPolynomialStr(Args: array of double; VarName: string): string;
{Return a string display the polynomial in normal format}
{for example: 6.0 X^3 - 4.0 X^2 + 7.0 X - 19.0}
var I: integer;
begin
Result:='';
for I:=High(Args) downto 0 do
begin
if Args[I]>0 then
begin
if I<>High(Args) then Result:=Result+' + ';
end
else Result:=Result+' - ';
Result:=Result+FloatToStrF(Abs(Args[I]),ffFixed,18,1);
if I>0 then Result:=Result+' '+VarName;
if I>1 then Result:=Result+'^'+IntToStr(I);
end;
end;


procedure ShowHornerPoly(Memo: TMemo; Args: array of double; X: double);
{Evaluate polynomial, show formated polynomal and the result}
var R: double;
begin
R:=EvaluatePolynomial(Args,X);
Memo.Lines.Add(FloatToStrF(R,ffFixed, 18,1));
Memo.Lines.Add(GetPolynomialStr(Args,'X'));
end;


procedure DoHornerPoly(Memo: TMemo);
begin
ShowHornerPoly(Memo,[-19, 7, -4, 6],3)
end;


</syntaxhighlight>
{{out}}
<pre>
128.0
6.0 X^3 - 4.0 X^2 + 7.0 X - 19.0
</pre>


=={{header|E}}==
=={{header|E}}==


<lang e>def makeHornerPolynomial(coefficients :List) {
<syntaxhighlight lang="e">def makeHornerPolynomial(coefficients :List) {
def indexing := (0..!coefficients.size()).descending()
def indexing := (0..!coefficients.size()).descending()
return def hornerPolynomial(x) {
return def hornerPolynomial(x) {
Line 529: Line 779:
return acc
return acc
}
}
}</lang>
}</syntaxhighlight>


<lang e>? makeHornerPolynomial([-19, 7, -4, 6])(3)
<syntaxhighlight lang="e">? makeHornerPolynomial([-19, 7, -4, 6])(3)
# value: 128</lang>
# value: 128</syntaxhighlight>

=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func horner coeffs[] x .
for i = len coeffs[] downto 1
res = res * x + coeffs[i]
.
return res
.
print horner [ -19 7 -4 6 ] 3
</syntaxhighlight>
{{out}}
<pre>
128
</pre>


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
=== Functional version ===
=== Functional version ===
<lang lisp>
<syntaxhighlight lang="lisp">
(define (horner x poly)
(define (horner x poly)
(foldr (lambda (coeff acc) (+ coeff (* acc x))) 0 poly))
(foldr (lambda (coeff acc) (+ coeff (* acc x))) 0 poly))


(horner 3 '(-19 7 -4 6)) → 128
(horner 3 '(-19 7 -4 6)) → 128
</syntaxhighlight>
</lang>
=== Library ===
=== Library ===
<lang lisp>
<syntaxhighlight lang="lisp">
(lib 'math)
(lib 'math)
Lib: math.lib loaded.
Lib: math.lib loaded.
Line 550: Line 816:
(poly->string 'x P) → 6x^3 -4x^2 +7x -19
(poly->string 'x P) → 6x^3 -4x^2 +7x -19
(poly 3 P) → 128
(poly 3 P) → 128
</syntaxhighlight>
</lang>


=={{header|EDSAC order code}}==
=={{header|EDSAC order code}}==
<lang edsac>
<syntaxhighlight lang="edsac">
[Copyright <2021> <ERIK SARGSYAN>
[Copyright <2021> <ERIK SARGSYAN>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
Line 615: Line 881:
EZPF
EZPF
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 624: Line 890:
{{trans|C#}}
{{trans|C#}}
ELENA 5.0 :
ELENA 5.0 :
<lang elena>import extensions;
<syntaxhighlight lang="elena">import extensions;
import system'routines;
import system'routines;
Line 635: Line 901:
{
{
console.printLine(horner(new real[]{-19.0r, 7.0r, -4.0r, 6.0r}, 3.0r))
console.printLine(horner(new real[]{-19.0r, 7.0r, -4.0r, 6.0r}, 3.0r))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 642: Line 908:


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>horner = fn(list, x)-> List.foldr(list, 0, fn(c,acc)-> x*acc+c end) end
<syntaxhighlight lang="elixir">horner = fn(list, x)-> List.foldr(list, 0, fn(c,acc)-> x*acc+c end) end


IO.puts horner.([-19,7,-4,6], 3)</lang>
IO.puts horner.([-19,7,-4,6], 3)</syntaxhighlight>


{{out}}
{{out}}
Line 653: Line 919:
=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
{{trans|Common Lisp}}
{{trans|Common Lisp}}
<syntaxhighlight lang="lisp">(require 'cl-lib)
<lang Emacs Lisp>

(defun horner (coeffs x)
(defun horner (coeffs x)
(reduce #'(lambda (coef acc) (+ (* acc x) coef) )
(cl-reduce #'(lambda (coef acc) (+ (* acc x) coef))
coeffs :from-end t :initial-value 0) )
coeffs :from-end t :initial-value 0))


(horner '(-19 7 -4 6) 3)
(horner '(-19 7 -4 6) 3)</syntaxhighlight>

</lang>
{{out}}
<b>Output:</b>

<pre>
128
128
</pre>


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>
<syntaxhighlight lang="erlang">
horner(L,X) ->
horner(L,X) ->
lists:foldl(fun(C, Acc) -> X*Acc+C end,0, lists:reverse(L)).
lists:foldl(fun(C, Acc) -> X*Acc+C end,0, lists:reverse(L)).
t() ->
t() ->
horner([-19,7,-4,6], 3).
horner([-19,7,-4,6], 3).
</syntaxhighlight>
</lang>


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM HORNER
PROGRAM HORNER


Line 696: Line 962:
PRINT(RES)
PRINT(RES)
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>


=={{header|Euler Math Toolbox}}==
=={{header|Euler Math Toolbox}}==


<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function horner (x,v) ...
>function horner (x,v) ...
$ n=cols(v); res=v{n};
$ n=cols(v); res=v{n};
Line 719: Line 985:
3 2
3 2
6 x - 4 x + 7 x - 19
6 x - 4 x + 7 x - 19
</syntaxhighlight>
</lang>



=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
let horner l x =
let horner l x =
List.rev l |> List.fold ( fun acc c -> x*acc+c) 0
List.rev l |> List.fold ( fun acc c -> x*acc+c) 0


horner [-19;7;-4;6] 3
horner [-19;7;-4;6] 3
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>: horner ( coeff x -- res )
<syntaxhighlight lang="factor">: horner ( coeff x -- res )
[ <reversed> 0 ] dip '[ [ _ * ] dip + ] reduce ;</lang>
[ <reversed> 0 ] dip '[ [ _ * ] dip + ] reduce ;</syntaxhighlight>


( scratchpad ) { -19 7 -4 6 } 3 horner .
( scratchpad ) { -19 7 -4 6 } 3 horner .
Line 737: Line 1,004:


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>: fhorner ( coeffs len F: x -- F: val )
<syntaxhighlight lang="forth">: fhorner ( coeffs len F: x -- F: val )
0e
0e
floats bounds ?do
floats bounds ?do
Line 746: Line 1,013:
create coeffs 6e f, -4e f, 7e f, -19e f,
create coeffs 6e f, -4e f, 7e f, -19e f,


coeffs 4 3e fhorner f. \ 128.</lang>
coeffs 4 3e fhorner f. \ 128.</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
<lang fortran>program test_horner
<syntaxhighlight lang="fortran">program test_horner


implicit none
implicit none


write (*, '(f5.1)') horner ((/-19.0, 7.0, -4.0, 6.0/), 3.0)
write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)


contains
contains
Line 761: Line 1,028:


implicit none
implicit none
real, dimension (:), intent (in) :: coeffs
real, dimension (0:), intent (in) :: coeffs
real, intent (in) :: x
real, intent (in) :: x
real :: res
real :: res
integer :: i
integer :: i


res = 0.0
res = coeffs(ubound(coeffs,1))
do i = size (coeffs), 1, -1
do i = ubound(coeffs,1)-1, 0, -1
res = res * x + coeffs (i)
res = res * x + coeffs (i)
end do
end do
Line 773: Line 1,040:
end function horner
end function horner


end program test_horner</lang>
end program test_horner</syntaxhighlight>
Output:
Output:
<pre>128.0</pre>
<pre>128.0</pre>


=== Fortran 77 ===
=== Fortran 77 ===
<lang fortran> FUNCTION HORNER(N,A,X)
<syntaxhighlight lang="fortran"> FUNCTION HORNER(N,A,X)
IMPLICIT NONE
IMPLICIT NONE
INTEGER I,N
INTEGER I,N
Line 787: Line 1,054:
END DO
END DO
HORNER=Y
HORNER=Y
END</lang>
END</syntaxhighlight>


As a matter of fact, computing the derivative is not much more difficult (see [http://www.cs.berkeley.edu/~wkahan/Math128/Poly.pdf Roundoff in Polynomial Evaluation], W. Kahan, 1986). The following subroutine computes both polynomial value and derivative for argument x.
As a matter of fact, computing the derivative is not much more difficult (see [http://www.cs.berkeley.edu/~wkahan/Math128/Poly.pdf Roundoff in Polynomial Evaluation], W. Kahan, 1986). The following subroutine computes both polynomial value and derivative for argument x.


<lang fortran> SUBROUTINE HORNER2(N,A,X,Y,Z)
<syntaxhighlight lang="fortran"> SUBROUTINE HORNER2(N,A,X,Y,Z)
C COMPUTE POLYNOMIAL VALUE AND DERIVATIVE
C COMPUTE POLYNOMIAL VALUE AND DERIVATIVE
C SEE "ROUNDOFF IN POLYNOMIAL EVALUATION", W. KAHAN, 1986
C SEE "ROUNDOFF IN POLYNOMIAL EVALUATION", W. KAHAN, 1986
Line 805: Line 1,072:
10 Y = Y*X + A(I)
10 Y = Y*X + A(I)
END
END
</syntaxhighlight>
</lang>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
Function AlgoritmoHorner(coeffs() As Integer, x As Integer) As Integer
Function AlgoritmoHorner(coeffs() As Integer, x As Integer) As Integer
Dim As Integer i, acumulador = 0
Dim As Integer i, acumulador = 0
Line 822: Line 1,089:
Print AlgoritmoHorner(coeficientes(), x)
Print AlgoritmoHorner(coeficientes(), x)
End
End
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 830: Line 1,097:
=={{header|FunL}}==
=={{header|FunL}}==
{{trans|Haskell}}
{{trans|Haskell}}
<lang funl>import lists.foldr
<syntaxhighlight lang="funl">import lists.foldr


def horner( poly, x ) = foldr( \a, b -> a + b*x, 0, poly )
def horner( poly, x ) = foldr( \a, b -> a + b*x, 0, poly )


println( horner([-19, 7, -4, 6], 3) )</lang>
println( horner([-19, 7, -4, 6], 3) )</syntaxhighlight>


{{out}}
{{out}}
Line 840: Line 1,107:
<pre>
<pre>
128
128
</pre>

=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">include "NSLog.incl"

local fn horner( coeffs as CFArrayRef, x as NSInteger ) as double
CFArrayRef reversedCoeffs
CFNumberRef num
double accumulator = 0.0

// Reverse coeffs array
reversedCoeffs = fn EnumeratorAllObjects( fn ArrayReverseObjectEnumerator( coeffs ) )

// Iterate over CFNumberRefs in reversed array, convert to double values, calculate and add to accumulator
for num in reversedCoeffs
accumulator = ( accumulator * x ) + fn NumberDoubleValue( num )
next
end fn = accumulator

CFArrayRef coeffs

coeffs = @[@-19.0, @7.0, @-4.0, @6.0]
NSLog( @"%7.1f", fn horner( coeffs, 3 ) )

coeffs = @[@4.0, @3.0, @2.0, @1.0]
NSLog( @"%7.1f", fn horner( coeffs, 10 ) )

coeffs = @[@1, @1, @0, @0, @1]
NSLog( @"%7.1f", fn horner( coeffs, 2 ) )

coeffs = @[@1.2, @2.3, @3.4, @4.5, @5.6]
NSLog( @"%7.1f", fn horner( coeffs, 8 ) )

coeffs = @[@1, @0, @1, @1, @1, @0, @0, @1]
NSLog( @"%7.1f", fn horner( coeffs, 2 ) )

HandleEvents</syntaxhighlight>

{{out}}

<pre>
128.0
1234.0
19.0
25478.8
157.0
</pre>
</pre>


=={{header|GAP}}==
=={{header|GAP}}==
<lang gap># The idiomatic way to compute with polynomials
<syntaxhighlight lang="gap"># The idiomatic way to compute with polynomials


x := Indeterminate(Rationals, "x");
x := Indeterminate(Rationals, "x");
Line 874: Line 1,187:


Horner(u, 3);
Horner(u, 3);
# 128</lang>
# 128</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 890: Line 1,203:
func main() {
func main() {
fmt.Println(horner(3, []int64{-19, 7, -4, 6}))
fmt.Println(horner(3, []int64{-19, 7, -4, 6}))
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 898: Line 1,211:
=={{header|Groovy}}==
=={{header|Groovy}}==
Solution:
Solution:
<lang groovy>def hornersRule = { coeff, x -> coeff.reverse().inject(0) { accum, c -> (accum * x) + c } }</lang>
<syntaxhighlight lang="groovy">def hornersRule = { coeff, x -> coeff.reverse().inject(0) { accum, c -> (accum * x) + c } }</syntaxhighlight>


Test includes demonstration of [[currying]] to create polynomial functions of one variable from generic Horner's rule calculation. Also demonstrates constructing the derivative function for the given polynomial. And finally demonstrates in the Newton-Raphson method to find one of the polynomial's roots using the polynomial and derivative functions constructed earlier.
Test includes demonstration of [[currying]] to create polynomial functions of one variable from generic Horner's rule calculation. Also demonstrates constructing the derivative function for the given polynomial. And finally demonstrates in the Newton-Raphson method to find one of the polynomial's roots using the polynomial and derivative functions constructed earlier.
<lang groovy>def coefficients = [-19g, 7g, -4g, 6g]
<syntaxhighlight lang="groovy">def coefficients = [-19g, 7g, -4g, 6g]
println (["p coefficients":coefficients])
println (["p coefficients":coefficients])


Line 923: Line 1,236:


def root = newtonRaphson(3g, testPoly, testDeriv)
def root = newtonRaphson(3g, testPoly, testDeriv)
println ([root:root.toString()[0..5], "p(root)":testPoly(root).toString()[0..5], "p'(root)":testDeriv(root).toString()[0..5]])</lang>
println ([root:root.toString()[0..5], "p(root)":testPoly(root).toString()[0..5], "p'(root)":testDeriv(root).toString()[0..5]])</syntaxhighlight>


Output:
Output:
Line 935: Line 1,248:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>horner :: (Num a) => a -> [a] -> a
<syntaxhighlight lang="haskell">horner :: (Num a) => a -> [a] -> a
horner x = foldr (\a b -> a + b*x) 0
horner x = foldr (\a b -> a + b*x) 0


main = print $ horner 3 [-19, 7, -4, 6]</lang>
main = print $ horner 3 [-19, 7, -4, 6]</syntaxhighlight>


=={{header|HicEst}}==
=={{header|HicEst}}==
<lang HicEst>REAL :: x=3, coeffs(4)
<syntaxhighlight lang="hicest">REAL :: x=3, coeffs(4)
DATA coeffs/-19.0, 7.0, -4.0, 6.0/
DATA coeffs/-19.0, 7.0, -4.0, 6.0/


Line 952: Line 1,265:
Horner = x*Horner + c(i)
Horner = x*Horner + c(i)
ENDDO
ENDDO
END</lang>
END</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==


<syntaxhighlight lang="icon">
<lang Icon>
procedure poly_eval (x, coeffs)
procedure poly_eval (x, coeffs)
accumulator := 0
accumulator := 0
Line 967: Line 1,280:
write (poly_eval (3, [-19, 7, -4, 6]))
write (poly_eval (3, [-19, 7, -4, 6]))
end
end
</syntaxhighlight>
</lang>


=={{header|J}}==
=={{header|J}}==
'''Solution''':<lang j>
'''Solution''':<syntaxhighlight lang="j">
horner =: (#."0 _ |.)~ NB. Tacit
horner =: (#."0 _ |.)~ NB. Tacit
horner =: [: +`*/ [: }: ,@,. NB. Alternate tacit (equivalent)
horner =: [: +`*/ [: }: ,@,. NB. Alternate tacit (equivalent)
horner =: 4 : ' (+ *&y)/x' NB. Alternate explicit (equivalent)
horner =: 4 : ' (+ *&y)/x' NB. Alternate explicit (equivalent)
</syntaxhighlight>
</lang>
'''Example''':<lang j> _19 7 _4 6 horner 3
'''Example''':<syntaxhighlight lang="j"> _19 7 _4 6 horner 3
128</lang>
128</syntaxhighlight>
'''Note:'''<br>
'''Note:'''<br>
The primitive verb <code>p.</code> would normally be used to evaluate polynomials.
The primitive verb <code>p.</code> would normally be used to evaluate polynomials.
<lang j> _19 7 _4 6 p. 3
<syntaxhighlight lang="j"> _19 7 _4 6 p. 3
128</lang>
128</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
<lang java5>import java.util.ArrayList;
<syntaxhighlight lang="java5">import java.util.ArrayList;
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
Line 1,006: Line 1,319:
return accumulator;
return accumulator;
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>128.0</pre>
<pre>128.0</pre>
Line 1,014: Line 1,327:


{{trans|Haskell}}
{{trans|Haskell}}
<lang javascript>function horner(coeffs, x) {
<syntaxhighlight lang="javascript">function horner(coeffs, x) {
return coeffs.reduceRight( function(acc, coeff) { return(acc * x + coeff) }, 0);
return coeffs.reduceRight( function(acc, coeff) { return(acc * x + coeff) }, 0);
}
}
console.log(horner([-19,7,-4,6],3)); // ==> 128
console.log(horner([-19,7,-4,6],3)); // ==> 128
</syntaxhighlight>
</lang>

=={{header|jq}}==
<syntaxhighlight lang=jq>
# Input: an array of coefficients specifying the polynomial
# to be evaluated at $x, where .[0] is the constant
def horner($x):
. as $coefficients
| reduce range(length-1; -1; -1) as $i (0; . * $x + $coefficients[$i]);

# Example
[-19, 7, -4, 6] | horner(3)
</syntaxhighlight>

'''Invocation''': $JQ -n -f horner.jq

where $JQ is either jq or gojq

{{output}}
<pre>
128
</pre>


=={{header|Julia}}==
=={{header|Julia}}==
Line 1,024: Line 1,358:


'''Imperative''':
'''Imperative''':
<lang julia>function horner(coefs, x)
<syntaxhighlight lang="julia">function horner(coefs, x)
s = coefs[end] * one(x)
s = coefs[end] * one(x)
for k in length(coefs)-1:-1:1
for k in length(coefs)-1:-1:1
Line 1,032: Line 1,366:
end
end


@show horner([-19, 7, -4, 6], 3)</lang>
@show horner([-19, 7, -4, 6], 3)</syntaxhighlight>


{{out}}
{{out}}
Line 1,038: Line 1,372:


'''Functional''':
'''Functional''':
<lang julia>horner2(coefs, x) = foldr((u, v) -> u + x * v, coefs, init=zero(promote_type(typeof(x),eltype(coefs))))
<syntaxhighlight lang="julia">horner2(coefs, x) = foldr((u, v) -> u + x * v, coefs, init=zero(promote_type(typeof(x),eltype(coefs))))


@show horner2([-19, 7, -4, 6], 3)</lang>
@show horner2([-19, 7, -4, 6], 3)</syntaxhighlight>


{{out}}
{{out}}
Line 1,047: Line 1,381:
'''Note''':
'''Note''':
In Julia 1.4 or later one would normally use the built-in '''evalpoly''' function for this purpose:
In Julia 1.4 or later one would normally use the built-in '''evalpoly''' function for this purpose:
<lang julia>
<syntaxhighlight lang="julia">
@show evalpoly(3, [-19, 7, -4, 6]) </lang>
@show evalpoly(3, [-19, 7, -4, 6]) </syntaxhighlight>


{{out}}
{{out}}
Line 1,054: Line 1,388:


=={{header|K}}==
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
horner:{y _sv|x}
horner:{y _sv|x}
horner[-19 7 -4 6;3]
horner[-19 7 -4 6;3]
128
128
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


fun horner(coeffs: DoubleArray, x: Double): Double {
fun horner(coeffs: DoubleArray, x: Double): Double {
Line 1,072: Line 1,406:
val coeffs = doubleArrayOf(-19.0, 7.0, -4.0, 6.0)
val coeffs = doubleArrayOf(-19.0, 7.0, -4.0, 6.0)
println(horner(coeffs, 3.0))
println(horner(coeffs, 3.0))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,080: Line 1,414:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang="scheme">
{def horner
{def horner
{def horner.r
{def horner.r
Line 1,097: Line 1,431:
-> 2.220446049250313e-16 ~ 0
-> 2.220446049250313e-16 ~ 0


</syntaxhighlight>
</lang>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<lang lb>src$ = "Hello"
<syntaxhighlight lang="lb">src$ = "Hello"
coefficients$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
coefficients$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
x = 3
Line 1,119: Line 1,453:
horner = accumulator
horner = accumulator
end function
end function
</lang>
</syntaxhighlight>


=={{header|Logo}}==
=={{header|Logo}}==
<lang logo>to horner :x :coeffs
<syntaxhighlight lang="logo">to horner :x :coeffs
if empty? :coeffs [output 0]
if empty? :coeffs [output 0]
output (first :coeffs) + (:x * horner :x bf :coeffs)
output (first :coeffs) + (:x * horner :x bf :coeffs)
end
end


show horner 3 [-19 7 -4 6] ; 128</lang>
show horner 3 [-19 7 -4 6] ; 128</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>function horners_rule( coeff, x )
<syntaxhighlight lang="lua">function horners_rule( coeff, x )
local res = 0
local res = 0
for i = #coeff, 1, -1 do
for i = #coeff, 1, -1 do
Line 1,140: Line 1,474:
x = 3
x = 3
coefficients = { -19, 7, -4, 6 }
coefficients = { -19, 7, -4, 6 }
print( horners_rule( coefficients, x ) )</lang>
print( horners_rule( coefficients, x ) )</syntaxhighlight>


=={{header|Maple}}==
=={{header|Maple}}==
<syntaxhighlight lang="maple">
<lang Maple>
applyhorner:=(L::list,x)->foldl((s,t)->s*x+t,op(ListTools:-Reverse(L))):
applyhorner:=(L::list,x)->foldl((s,t)->s*x+t,op(ListTools:-Reverse(L))):


Line 1,149: Line 1,483:


applyhorner([-19,7,-4,6],3);
applyhorner([-19,7,-4,6],3);
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,158: Line 1,492:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>Horner[l_List, x_] := Fold[x #1 + #2 &, 0, l]
<syntaxhighlight lang="mathematica">Horner[l_List, x_] := Fold[x #1 + #2 &, 0, l]
Horner[{6, -4, 7, -19}, x]
Horner[{6, -4, 7, -19}, x]
-> -19 + x (7 + x (-4 + 6 x))
-> -19 + x (7 + x (-4 + 6 x))


-19 + x (7 + x (-4 + 6 x)) /. x -> 3
-19 + x (7 + x (-4 + 6 x)) /. x -> 3
-> 128</lang>
-> 128</syntaxhighlight>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
<lang MATLAB>function accumulator = hornersRule(x,coefficients)
<syntaxhighlight lang="matlab">function accumulator = hornersRule(x,coefficients)


accumulator = 0;
accumulator = 0;
Line 1,174: Line 1,508:
end
end
end</lang>
end</syntaxhighlight>
Output:
Output:
<lang MATLAB>>> hornersRule(3,[-19, 7, -4, 6])
<syntaxhighlight lang="matlab">>> hornersRule(3,[-19, 7, -4, 6])


ans =
ans =


128</lang>
128</syntaxhighlight>
Matlab also has a built-in function "polyval" which uses Horner's Method to evaluate polynomials. The list of coefficients is in descending order of power, where as to task spec specifies ascending order.
Matlab also has a built-in function "polyval" which uses Horner's Method to evaluate polynomials. The list of coefficients is in descending order of power, where as to task spec specifies ascending order.
<lang MATLAB>>> polyval(fliplr([-19, 7, -4, 6]),3)
<syntaxhighlight lang="matlab">>> polyval(fliplr([-19, 7, -4, 6]),3)


ans =
ans =


128</lang>
128</syntaxhighlight>


=={{header|Maxima}}==
=={{header|Maxima}}==
<lang maxima>/* Function horner already exists in Maxima, though it operates on expressions, not lists of coefficients */
<syntaxhighlight lang="maxima">/* Function horner already exists in Maxima, though it operates on expressions, not lists of coefficients */
horner(5*x^3+2*x+1);
horner(5*x^3+2*x+1);
x*(5*x^2+2)+1
x*(5*x^2+2)+1
Line 1,233: Line 1,567:


poleval([0, 0, 0, 0, 1], x);
poleval([0, 0, 0, 0, 1], x);
x^4</lang>
x^4</syntaxhighlight>


=={{header|Mercury}}==
=={{header|Mercury}}==
<lang mercury>
<syntaxhighlight lang="mercury">
:- module horner.
:- module horner.
:- interface.
:- interface.
Line 1,250: Line 1,584:


horner(X, Cs) = list.foldr((func(C, Acc) = Acc * X + C), Cs, 0).
horner(X, Cs) = list.foldr((func(C, Acc) = Acc * X + C), Cs, 0).
</syntaxhighlight>
</lang>


=={{header|МК-61/52}}==
=={{header|МК-61/52}}==
<lang>ИП0 1 + П0
<syntaxhighlight lang="text">ИП0 1 + П0
ИПE ИПD * КИП0 + ПE
ИПE ИПD * КИП0 + ПE
ИП0 1 - x=0 04
ИП0 1 - x=0 04
ИПE С/П</lang>
ИПE С/П</syntaxhighlight>


''Input:'' Р1:РС - coefficients, Р0 - number of the coefficients, РD - ''x''.
''Input:'' Р1:РС - coefficients, Р0 - number of the coefficients, РD - ''x''.


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE Horner;
<syntaxhighlight lang="modula2">MODULE Horner;
FROM RealStr IMPORT RealToStr;
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,289: Line 1,623:
WriteLn;
WriteLn;
ReadChar
ReadChar
END Horner.</lang>
END Horner.</syntaxhighlight>


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang netrexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
options replace format comments java crossref savelog symbols nobinary


Line 1,303: Line 1,637:
End
End
Say r
Say r
Say 6*x**3-4*x**2+7*x-19</lang>
Say 6*x**3-4*x**2+7*x-19</syntaxhighlight>
'''Output:'''
'''Output:'''
<pre>128
<pre>128
Line 1,309: Line 1,643:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim># You can also just use `reversed` proc from stdlib `algorithm` module
<syntaxhighlight lang="nim"># You can also just use `reversed` proc from stdlib `algorithm` module
iterator reversed[T](x: openArray[T]): T =
iterator reversed[T](x: openArray[T]): T =
for i in countdown(x.high, x.low):
for i in countdown(x.high, x.low):
Line 1,318: Line 1,652:
result = result * x + c
result = result * x + c
echo horner([-19, 7, -4, 6], 3)</lang>
echo horner([-19, 7, -4, 6], 3)</syntaxhighlight>


=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
{{works with|oo2c}}
{{works with|oo2c}}
<lang oberon2>
<syntaxhighlight lang="oberon2">
MODULE HornerRule;
MODULE HornerRule;
IMPORT
IMPORT
Line 1,351: Line 1,685:
Out.Int(Eval(coefs^,4,3),0);Out.Ln
Out.Int(Eval(coefs^,4,3),0);Out.Ln
END HornerRule.
END HornerRule.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,358: Line 1,692:


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<syntaxhighlight lang="objeck">
class Horner {
class Horner {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
Line 1,378: Line 1,712:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
{{works with|Mac OS X|10.6+}} Using blocks
{{works with|Mac OS X|10.6+}} Using blocks
<lang objc>#import <Foundation/Foundation.h>
<syntaxhighlight lang="objc">#import <Foundation/Foundation.h>


typedef double (^mfunc)(double, double);
typedef double (^mfunc)(double, double);
Line 1,423: Line 1,757:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml># let horner coeffs x =
<syntaxhighlight lang="ocaml"># let horner coeffs x =
List.fold_left (fun acc coef -> acc * x + coef) 0 (List.rev coeffs) ;;
List.fold_left (fun acc coef -> acc * x + coef) 0 (List.rev coeffs) ;;
val horner : int list -> int -> int = <fun>
val horner : int list -> int -> int = <fun>
Line 1,433: Line 1,767:
# let coeffs = [-19; 7; -4; 6] in
# let coeffs = [-19; 7; -4; 6] in
horner coeffs 3 ;;
horner coeffs 3 ;;
- : int = 128</lang>
- : int = 128</syntaxhighlight>
It's also possible to do fold_right instead of reversing and doing fold_left; but fold_right is not tail-recursive.
It's also possible to do fold_right instead of reversing and doing fold_left; but fold_right is not tail-recursive.


=={{header|Octave}}==
=={{header|Octave}}==
<lang octave>function r = horner(a, x)
<syntaxhighlight lang="octave">function r = horner(a, x)
r = 0.0;
r = 0.0;
for i = length(a):-1:1
for i = length(a):-1:1
Line 1,444: Line 1,778:
endfunction
endfunction


horner([-19, 7, -4, 6], 3)</lang>
horner([-19, 7, -4, 6], 3)</syntaxhighlight>


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang oorexx>/* Rexx ---------------------------------------------------------------
<syntaxhighlight lang="oorexx">/* Rexx ---------------------------------------------------------------
* 04.03.2014 Walter Pachl
* 04.03.2014 Walter Pachl
*--------------------------------------------------------------------*/
*--------------------------------------------------------------------*/
Line 1,458: Line 1,792:
End
End
Say r
Say r
Say 6*x**3-4*x**2+7*x-19</lang>
Say 6*x**3-4*x**2+7*x-19</syntaxhighlight>
'''Output:'''
'''Output:'''
<pre>128
<pre>128
Line 1,464: Line 1,798:


=={{header|Oz}}==
=={{header|Oz}}==
<lang oz>declare
<syntaxhighlight lang="oz">declare
fun {Horner Coeffs X}
fun {Horner Coeffs X}
{FoldL1 {Reverse Coeffs}
{FoldL1 {Reverse Coeffs}
Line 1,476: Line 1,810:
end
end
in
in
{Show {Horner [~19 7 ~4 6] 3}}</lang>
{Show {Horner [~19 7 ~4 6] 3}}</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Also note that Pari has a polynomial type. Evaluating these is as simple as <code>subst(P,variable(P),x)</code>.
Also note that Pari has a polynomial type. Evaluating these is as simple as <code>subst(P,variable(P),x)</code>.
<lang parigp>horner(v,x)={
<syntaxhighlight lang="parigp">horner(v,x)={
my(s=0);
my(s=0);
forstep(i=#v,1,-1,s=s*x+v[i]);
forstep(i=#v,1,-1,s=s*x+v[i]);
s
s
};</lang>
};</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>Program HornerDemo(output);
<syntaxhighlight lang="pascal">Program HornerDemo(output);


function horner(a: array of double; x: double): double;
function horner(a: array of double; x: double): double;
Line 1,504: Line 1,838:
write ('Horner calculated polynomial of 6*x^3 - 4*x^2 + 7*x - 19 for x = 3: ');
write ('Horner calculated polynomial of 6*x^3 - 4*x^2 + 7*x - 19 for x = 3: ');
writeln (horner (poly, 3.0):8:4);
writeln (horner (poly, 3.0):8:4);
end.</lang>
end.</syntaxhighlight>
Output:
Output:
<pre>Horner calculated polynomial of 6*x^3 - 4*x^2 + 7*x - 19 for x = 3: 128.0000
<pre>Horner calculated polynomial of 6*x^3 - 4*x^2 + 7*x - 19 for x = 3: 128.0000
Line 1,510: Line 1,844:


=={{header|Perl}}==
=={{header|Perl}}==
<lang Perl>use 5.10.0;
<syntaxhighlight lang="perl">use 5.10.0;
use strict;
use strict;
use warnings;
use warnings;
Line 1,523: Line 1,857:
my @coeff = (-19.0, 7, -4, 6);
my @coeff = (-19.0, 7, -4, 6);
my $x = 3;
my $x = 3;
say horner @coeff, $x;</lang>
say horner @coeff, $x;</syntaxhighlight>


===<!-- Perl -->Functional version===
===<!-- Perl -->Functional version===
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use List::Util qw(reduce);
use List::Util qw(reduce);


Line 1,536: Line 1,870:
my @coeff = (-19.0, 7, -4, 6);
my @coeff = (-19.0, 7, -4, 6);
my $x = 3;
my $x = 3;
print horner(\@coeff, $x), "\n";</lang>
print horner(\@coeff, $x), "\n";</syntaxhighlight>


===<!-- Perl -->Recursive version===
===<!-- Perl -->Recursive version===
<lang perl>sub horner {
<syntaxhighlight lang="perl">sub horner {
my ($coeff, $x) = @_;
my ($coeff, $x) = @_;
@$coeff and
@$coeff and
Line 1,545: Line 1,879:
}
}
print horner( [ -19, 7, -4, 6 ], 3 );</lang>
print horner( [ -19, 7, -4, 6 ], 3 );</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">horner</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">coeff</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">horner</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">coeff</span><span style="color: #0000FF;">)</span>
Line 1,559: Line 1,893:
<span style="color: #0000FF;">?</span><span style="color: #000000;">horner</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,{-</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">horner</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,{-</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,566: Line 1,900:


=={{header|PHP}}==
=={{header|PHP}}==
<lang php><?php
<syntaxhighlight lang="php"><?php
function horner($coeff, $x) {
function horner($coeff, $x) {
$result = 0;
$result = 0;
Line 1,577: Line 1,911:
$x = 3;
$x = 3;
echo horner($coeff, $x), "\n";
echo horner($coeff, $x), "\n";
?></lang>
?></syntaxhighlight>


===Functional version===
===Functional version===
{{works with|PHP|5.3+}}
{{works with|PHP|5.3+}}
<lang php><?php
<syntaxhighlight lang="php"><?php
function horner($coeff, $x) {
function horner($coeff, $x) {
return array_reduce(array_reverse($coeff), function ($a, $b) use ($x) { return $a * $x + $b; }, 0);
return array_reduce(array_reverse($coeff), function ($a, $b) use ($x) { return $a * $x + $b; }, 0);
Line 1,589: Line 1,923:
$x = 3;
$x = 3;
echo horner($coeff, $x), "\n";
echo horner($coeff, $x), "\n";
?></lang>
?></syntaxhighlight>

=={{header|Picat}}==
===Recursion===
<syntaxhighlight lang="picat">horner([],_X,0).
horner([H|T],X,V) :-
horner(T,X,V1),
V = V1 * X + H.</syntaxhighlight>

===Iterative===
<syntaxhighlight lang="picat">horner2(Coeff, X, V) =>
Acc = 0,
foreach(I in Coeff.length..-1..1)
Acc := Acc*X + Coeff[I]
end,
V = Acc.</syntaxhighlight>

===Functional approach===
<syntaxhighlight lang="picat">h3(X,A,B) = A+B*X.
horner3(Coeff, X) = fold($h3(X),0,Coeff.reverse()).</syntaxhighlight>

===Test===
<syntaxhighlight lang="picat">go =>
horner([-19, 7, -4, 6], 3, V),
println(V),
horner2([-19, 7, -4, 6], 3, V2),
println(V2),
V3 = horner3([-19, 7, -4, 6], 3),
println(V3),
nl.</syntaxhighlight>

{{out}}
<pre>128
128
128</pre>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de horner (Coeffs X)
<syntaxhighlight lang="picolisp">(de horner (Coeffs X)
(let Res 0
(let Res 0
(for C (reverse Coeffs)
(for C (reverse Coeffs)
(setq Res (+ C (* X Res))) ) ) )</lang>
(setq Res (+ C (* X Res))) ) ) )</syntaxhighlight>
<lang PicoLisp>: (horner (-19.0 7.0 -4.0 6.0) 3.0)
<syntaxhighlight lang="picolisp">: (horner (-19.0 7.0 -4.0 6.0) 3.0)
-> 128</lang>
-> 128</syntaxhighlight>


=={{header|PL/I}}==
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
declare (i, n) fixed binary, (x, value) float; /* 11 May 2010 */
declare (i, n) fixed binary, (x, value) float; /* 11 May 2010 */
get (x);
get (x);
Line 1,613: Line 1,983:
put (value);
put (value);
end;
end;
</syntaxhighlight>
</lang>


=={{header|Potion}}==
=={{header|Potion}}==
<lang potion>horner = (x, coef) :
<syntaxhighlight lang="potion">horner = (x, coef) :
result = 0
result = 0
coef reverse each (a) :
coef reverse each (a) :
Line 1,624: Line 1,994:
.
.


horner(3, (-19, 7, -4, 6)) print</lang>
horner(3, (-19, 7, -4, 6)) print</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
{{works with|PowerShell|4.0}}
{{works with|PowerShell|4.0}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function horner($coefficients, $x) {
function horner($coefficients, $x) {
$accumulator = 0
$accumulator = 0
Line 1,639: Line 2,009:
$x = 3
$x = 3
horner $coefficients $x
horner $coefficients $x
</syntaxhighlight>
</lang>
<b>Output:</b>
<b>Output:</b>
<pre>
<pre>
Line 1,647: Line 2,017:
=={{header|Prolog}}==
=={{header|Prolog}}==
Tested with SWI-Prolog. Works with other dialects.
Tested with SWI-Prolog. Works with other dialects.
<lang Prolog>horner([], _X, 0).
<syntaxhighlight lang="prolog">horner([], _X, 0).


horner([H|T], X, V) :-
horner([H|T], X, V) :-
horner(T, X, V1),
horner(T, X, V1),
V is V1 * X + H.
V is V1 * X + H.
</syntaxhighlight>
</lang>
Output :
Output :
<lang Prolog> ?- horner([-19, 7, -4, 6], 3, V).
<syntaxhighlight lang="prolog"> ?- horner([-19, 7, -4, 6], 3, V).
V = 128.</lang>
V = 128.</syntaxhighlight>


===Functional approach===
===Functional approach===
Works with SWI-Prolog and module lambda, written by <b>Ulrich Neumerkel</b> found there http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl
Works with SWI-Prolog and module lambda, written by <b>Ulrich Neumerkel</b> found there http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl
<lang Prolog>:- use_module(library(lambda)).
<syntaxhighlight lang="prolog">:- use_module(library(lambda)).




Line 1,671: Line 2,041:
f_horner(L, V, R) :-
f_horner(L, V, R) :-
foldr(\X^Y^Z^(Z is X * V + Y), 0, L, R).
foldr(\X^Y^Z^(Z is X * V + Y), 0, L, R).
</syntaxhighlight>
</lang>


===Functional syntax (Ciao)===
===Functional syntax (Ciao)===
Works with Ciao (https://github.com/ciao-lang/ciao) and the fsyntax package:
Works with Ciao (https://github.com/ciao-lang/ciao) and the fsyntax package:
<syntaxhighlight lang="prolog">
<lang Prolog>
:- module(_, [horner/3], [fsyntax, hiord]).
:- module(_, [horner/3], [fsyntax, hiord]).
:- use_module(library(hiordlib)).
:- use_module(library(hiordlib)).
horner(L, X) := ~foldr((''(H,V0,V) :- V is V0*X + H), L, 0).
horner(L, X) := ~foldr((''(H,V0,V) :- V is V0*X + H), L, 0).
</syntaxhighlight>
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure Horner(List Coefficients(), b)
<syntaxhighlight lang="purebasic">Procedure Horner(List Coefficients(), b)
Define result
Define result
ForEach Coefficients()
ForEach Coefficients()
Line 1,688: Line 2,058:
Next
Next
ProcedureReturn result
ProcedureReturn result
EndProcedure</lang>
EndProcedure</syntaxhighlight>


'''Implemented as
'''Implemented as
<lang PureBasic>NewList a()
<syntaxhighlight lang="purebasic">NewList a()
AddElement(a()): a()= 6
AddElement(a()): a()= 6
AddElement(a()): a()= -4
AddElement(a()): a()= -4
AddElement(a()): a()= 7
AddElement(a()): a()= 7
AddElement(a()): a()=-19
AddElement(a()): a()=-19
Debug Horner(a(),3)</lang>
Debug Horner(a(),3)</syntaxhighlight>
'''Outputs
'''Outputs
128
128


=={{header|Python}}==
=={{header|Python}}==
<lang python>>>> def horner(coeffs, x):
<syntaxhighlight lang="python">>>> def horner(coeffs, x):
acc = 0
acc = 0
for c in reversed(coeffs):
for c in reversed(coeffs):
Line 1,708: Line 2,078:


>>> horner( (-19, 7, -4, 6), 3)
>>> horner( (-19, 7, -4, 6), 3)
128</lang>
128</syntaxhighlight>


===Functional version===
===Functional version===
<lang python>>>> try: from functools import reduce
<syntaxhighlight lang="python">>>> try: from functools import reduce
except: pass
except: pass


Line 1,718: Line 2,088:


>>> horner( (-19, 7, -4, 6), 3)
>>> horner( (-19, 7, -4, 6), 3)
128</lang>
128</syntaxhighlight>


==={{libheader|NumPy}}===
==={{libheader|NumPy}}===
<lang python>>>> import numpy
<syntaxhighlight lang="python">>>> import numpy
>>> numpy.polynomial.polynomial.polyval(3, (-19, 7, -4, 6))
>>> numpy.polynomial.polynomial.polyval(3, (-19, 7, -4, 6))
128.0</lang>
128.0</syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
Procedural style:
Procedural style:
<lang r>horner <- function(a, x) {
<syntaxhighlight lang="r">horner <- function(a, x) {
y <- 0
y <- 0
for(c in rev(a)) {
for(c in rev(a)) {
Line 1,735: Line 2,105:
}
}


cat(horner(c(-19, 7, -4, 6), 3), "\n")</lang>
cat(horner(c(-19, 7, -4, 6), 3), "\n")</syntaxhighlight>
Functional style:
Functional style:
<lang r>horner <- function(x, v) {
<syntaxhighlight lang="r">horner <- function(x, v) {
Reduce(v, right=T, f=function(a, b) {
Reduce(v, right=T, f=function(a, b) {
b * x + a
b * x + a
})
})
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,752: Line 2,122:
Translated from Haskell
Translated from Haskell


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(define (horner x l)
(define (horner x l)
Line 1,759: Line 2,129:
(horner 3 '(-19 7 -4 6))
(horner 3 '(-19 7 -4 6))


</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>sub horner ( @coeffs, $x ) {
<syntaxhighlight lang="raku" line>sub horner ( @coeffs, $x ) {
@coeffs.reverse.reduce: { $^a * $x + $^b };
@coeffs.reverse.reduce: { $^a * $x + $^b };
}
}


say horner( [ -19, 7, -4, 6 ], 3 );</lang>
say horner( [ -19, 7, -4, 6 ], 3 );</syntaxhighlight>


A recursive version would spare us the need for reversing the list of coefficients. However, special care must be taken in order to write it, because the way Raku implements lists is not optimized for this kind of treatment. [[Lisp]]-style lists are, and fortunately it is possible to emulate them with [http://doc.raku.org/type/Pair Pairs] and the reduction meta-operator:
A recursive version would spare us the need for reversing the list of coefficients. However, special care must be taken in order to write it, because the way Raku implements lists is not optimized for this kind of treatment. [[Lisp]]-style lists are, and fortunately it is possible to emulate them with [http://doc.raku.org/type/Pair Pairs] and the reduction meta-operator:


<lang perl6>multi horner(Numeric $c, $) { $c }
<syntaxhighlight lang="raku" line>multi horner(Numeric $c, $) { $c }
multi horner(Pair $c, $x) {
multi horner(Pair $c, $x) {
$c.key + $x * horner( $c.value, $x )
$c.key + $x * horner( $c.value, $x )
}
}
say horner( [=>](-19, 7, -4, 6 ), 3 );</lang>
say horner( [=>](-19, 7, -4, 6 ), 3 );</syntaxhighlight>


We can also use the composition operator:
We can also use the composition operator:
<lang perl6>sub horner ( @coeffs, $x ) {
<syntaxhighlight lang="raku" line>sub horner ( @coeffs, $x ) {
([o] map { $_ + $x * * }, @coeffs)(0);
([o] map { $_ + $x * * }, @coeffs)(0);
}
}
say horner( [ -19, 7, -4, 6 ], 3 );</lang>
say horner( [ -19, 7, -4, 6 ], 3 );</syntaxhighlight>


{{out}}
{{out}}
Line 1,789: Line 2,159:


One advantage of using the composition operator is that it allows for the use of an infinite list of coefficients.
One advantage of using the composition operator is that it allows for the use of an infinite list of coefficients.
<lang perl6>sub horner ( @coeffs, $x ) {
<syntaxhighlight lang="raku" line>sub horner ( @coeffs, $x ) {
map { .(0) }, [\o] map { $_ + $x * * }, @coeffs;
map { .(0) }, [\o] map { $_ + $x * * }, @coeffs;
}
}
say horner( [ 1 X/ (1, |[\*] 1 .. *) ], i*pi )[20];
say horner( [ 1 X/ (1, |[\*] 1 .. *) ], i*pi )[20];
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>-0.999999999924349-5.28918515954219e-10i</pre>
<pre>-0.999999999924349-5.28918515954219e-10i</pre>


=={{header|Rascal}}==
=={{header|Rascal}}==
<lang rascal>import List;
<syntaxhighlight lang="rascal">import List;


public int horners_rule(list[int] coefficients, int x){
public int horners_rule(list[int] coefficients, int x){
Line 1,806: Line 2,176:
acc = acc * x + i;}
acc = acc * x + i;}
return acc;
return acc;
}</lang>
}</syntaxhighlight>
A neater and shorter solution using a reducer:
A neater and shorter solution using a reducer:
<lang rascal>public int horners_rule2(list[int] coefficients, int x) = (0 | it * x + c | c <- reverse(coefficients));</lang>
<syntaxhighlight lang="rascal">public int horners_rule2(list[int] coefficients, int x) = (0 | it * x + c | c <- reverse(coefficients));</syntaxhighlight>
Output:
Output:
<lang rascal>rascal>horners_rule([-19, 7, -4, 6], 3)
<syntaxhighlight lang="rascal">rascal>horners_rule([-19, 7, -4, 6], 3)
int: 128
int: 128


rascal>horners_rule2([-19, 7, -4, 6], 3)
rascal>horners_rule2([-19, 7, -4, 6], 3)
int: 128</lang>
int: 128</syntaxhighlight>


=={{header|REBOL}}==
=={{header|REBOL}}==


<lang rebol>REBOL []
<syntaxhighlight lang="rebol">REBOL []


horner: func [coeffs x] [
horner: func [coeffs x] [
Line 1,828: Line 2,198:
]
]


print horner [-19 7 -4 6] 3</lang>
print horner [-19 7 -4 6] 3</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
===version 1===
<lang rexx>/*REXX program demonstrates using Horner's rule for polynomial evaluation. */
<syntaxhighlight lang="rexx">/*REXX program demonstrates using Horner's rule for polynomial evaluation. */
numeric digits 30 /*use extra numeric precision. */
numeric digits 30 /*use extra numeric precision. */
parse arg x poly /*get value of X and the coefficients. */
parse arg x poly /*get value of X and the coefficients. */
Line 1,850: Line 2,220:
end /*j*/
end /*j*/
say /*display a blank line for readability.*/
say /*display a blank line for readability.*/
say ' answer = ' a /*stick a fork in it, we're all done. */</lang>
say ' answer = ' a /*stick a fork in it, we're all done. */</syntaxhighlight>
'''output''' &nbsp; when the following is used for input: &nbsp; <tt> 3 &nbsp; -19 &nbsp; 7 &nbsp; -4 &nbsp; 6 </tt>
'''output''' &nbsp; when the following is used for input: &nbsp; <tt> 3 &nbsp; -19 &nbsp; 7 &nbsp; -4 &nbsp; 6 </tt>
<pre>
<pre>
Line 1,861: Line 2,231:


===version 2===
===version 2===
<lang rexx>/* REXX ---------------------------------------------------------------
<syntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 27.07.2012 Walter Pachl
* 27.07.2012 Walter Pachl
* coefficients reversed to descending order of power
* coefficients reversed to descending order of power
Line 1,905: Line 2,275:
Say ' equation = ' equ
Say ' equation = ' equ
Say ' '
Say ' '
Say ' result = ' a</lang>
Say ' result = ' a</syntaxhighlight>
{{out}}
{{out}}
<pre> x = 3
<pre> x = 3
Line 1,914: Line 2,284:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
coefficients = [-19, 7, -4, 6]
coefficients = [-19, 7, -4, 6]
see "x = 3" + nl +
see "x = 3" + nl +
Line 1,927: Line 2,297:
next
next
return w
return w
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,943: Line 2,313:


This said, solution to the problem is
This said, solution to the problem is
<syntaxhighlight lang="rlab">
<lang RLaB>
>> a = [6, -4, 7, -19]
>> a = [6, -4, 7, -19]
6 -4 7 -19
6 -4 7 -19
Line 1,951: Line 2,321:
128
128


</syntaxhighlight>
</lang>

=={{header|RPL}}==
===Translation of the algorithm===
Following the pseudocode given [https://web.physics.utah.edu/~detar/lessons/c++/array/node2.html here] to the letter:
≪ OVER DUP SIZE GET → a x0 p
≪ a SIZE 1 - 1 '''FOR''' j
'a(j)+x0*p' EVAL 'p' STO -1 '''STEP'''
p
≫ ≫ ‘'''HORNR'''’ STO

===Idiomatic one-liner===
Reducing the loop to its simplest form: one memory call, one multiplication and one addition.
≪ → x0 ≪ LIST→ 2 SWAP '''START''' x0 * + '''NEXT''' ≫ ≫ ‘'''HORNR'''’ STO
{{in}}
<pre>
{ -19 7 -4 6 } 3 HORNR
</pre>
{{out}}
<pre>
1: 128
</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def horner(coeffs, x)
<syntaxhighlight lang="ruby">def horner(coeffs, x)
coeffs.reverse.inject(0) {|acc, coeff| acc * x + coeff}
coeffs.reverse.inject(0) {|acc, coeff| acc * x + coeff}
end
end
p horner([-19, 7, -4, 6], 3) # ==> 128</lang>
p horner([-19, 7, -4, 6], 3) # ==> 128</syntaxhighlight>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>coef$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
<syntaxhighlight lang="runbasic">coef$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
x = 3
print horner(coef$,x) '128
print horner(coef$,x) '128
Line 1,976: Line 2,367:
next
next
horner = accum
horner = accum
end function</lang>
end function</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn horner(v: &[f64], x: f64) -> f64 {
<syntaxhighlight lang="rust">fn horner(v: &[f64], x: f64) -> f64 {
v.iter().rev().fold(0.0, |acc, coeff| acc*x + coeff)
v.iter().rev().fold(0.0, |acc, coeff| acc*x + coeff)
}
}
Line 1,986: Line 2,377:
let v = [-19., 7., -4., 6.];
let v = [-19., 7., -4., 6.];
println!("result: {}", horner(&v, 3.0));
println!("result: {}", horner(&v, 3.0));
}</lang>
}</syntaxhighlight>


A generic version that works with any number type and much more. So much more, it's hard to imagine what that may be useful for.
A generic version that works with any number type and much more. So much more, it's hard to imagine what that may be useful for.
<lang rust>extern crate num; // 0.2.0
<syntaxhighlight lang="rust">extern crate num; // 0.2.0
use num::Zero;
use num::Zero;
use std::ops::{Add, Mul};
use std::ops::{Add, Mul};
Line 2,008: Line 2,399:
let output: f64 = horner(&v, 3.0);
let output: f64 = horner(&v, 3.0);
println!("result: {}", output);
println!("result: {}", output);
}</lang>
}</syntaxhighlight>


=={{header|Sather}}==
=={{header|Sather}}==
<lang sather>class MAIN is
<syntaxhighlight lang="sather">class MAIN is
action(s, e, x:FLT):FLT is
action(s, e, x:FLT):FLT is
Line 2,025: Line 2,416:
#OUT + horner(|-19.0, 7.0, -4.0, 6.0|, 3.0) + "\n";
#OUT + horner(|-19.0, 7.0, -4.0, 6.0|, 3.0) + "\n";
end;
end;
end;</lang>
end;</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>def horner(coeffs:List[Double], x:Double)=
<syntaxhighlight lang="scala">def horner(coeffs:List[Double], x:Double)=
coeffs.reverse.foldLeft(0.0){(a,c)=> a*x+c}
coeffs.reverse.foldLeft(0.0){(a,c)=> a*x+c}
</syntaxhighlight>
</lang>
<lang scala>val coeffs=List(-19.0, 7.0, -4.0, 6.0)
<syntaxhighlight lang="scala">val coeffs=List(-19.0, 7.0, -4.0, 6.0)
println(horner(coeffs, 3))
println(horner(coeffs, 3))
-> 128.0
-> 128.0
</syntaxhighlight>
</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
{{Works with|Scheme|R<math>^5</math>RS}}
{{Works with|Scheme|R<math>^5</math>RS}}
<lang scheme>(define (horner lst x)
<syntaxhighlight lang="scheme">(define (horner lst x)
(define (*horner lst x acc)
(define (*horner lst x acc)
(if (null? lst)
(if (null? lst)
Line 2,046: Line 2,437:


(display (horner (list -19 7 -4 6) 3))
(display (horner (list -19 7 -4 6) 3))
(newline)</lang>
(newline)</syntaxhighlight>
Output:
Output:
<lang>128</lang>
<syntaxhighlight lang="text">128</syntaxhighlight>


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "float.s7i";


Line 2,072: Line 2,463:
begin
begin
writeln(horner(coeffs, 3.0) digits 1);
writeln(horner(coeffs, 3.0) digits 1);
end func;</lang>
end func;</syntaxhighlight>


Output:
Output:
Line 2,081: Line 2,472:
=={{header|Sidef}}==
=={{header|Sidef}}==
Functional:
Functional:
<lang ruby>func horner(coeff, x) {
<syntaxhighlight lang="ruby">func horner(coeff, x) {
coeff.reverse.reduce { |a,b| a*x + b };
coeff.reverse.reduce { |a,b| a*x + b };
}
}


say horner([-19, 7, -4, 6], 3); # => 128</lang>
say horner([-19, 7, -4, 6], 3); # => 128</syntaxhighlight>


Recursive:
Recursive:
<lang ruby>func horner(coeff, x) {
<syntaxhighlight lang="ruby">func horner(coeff, x) {
coeff.len > 0
(coeff.len > 0) \
&& (coeff[0] + x*horner(coeff.ft(1), x));
? (coeff[0] + x*horner(coeff.last(-1), x))
: 0
}
}


say horner([-19, 7, -4, 6], 3); # => 128</lang>
say horner([-19, 7, -4, 6], 3) # => 128</syntaxhighlight>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
{{works with|GNU Smalltalk}}
<lang smalltalk>OrderedCollection extend [
<syntaxhighlight lang="smalltalk">OrderedCollection extend [
horner: aValue [
horner: aValue [
^ self reverse inject: 0 into: [:acc :c | acc * aValue + c].
^ self reverse inject: 0 into: [:acc :c | acc * aValue + c].
Line 2,103: Line 2,495:
].
].


(#(-19 7 -4 6) asOrderedCollection horner: 3) displayNl.</lang>
(#(-19 7 -4 6) asOrderedCollection horner: 3) displayNl.</syntaxhighlight>


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<lang sml>(* Assuming real type for coefficients and x *)
<syntaxhighlight lang="sml">(* Assuming real type for coefficients and x *)
fun horner coeffList x = foldr (fn (a, b) => a + b * x) (0.0) coeffList</lang>
fun horner coeffList x = foldr (fn (a, b) => a + b * x) (0.0) coeffList</syntaxhighlight>


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>func horner(coefs: [Double], x: Double) -> Double {
<syntaxhighlight lang="swift">func horner(coefs: [Double], x: Double) -> Double {
return reduce(lazy(coefs).reverse(), 0) { $0 * x + $1 }
return reduce(lazy(coefs).reverse(), 0) { $0 * x + $1 }
}
}


println(horner([-19, 7, -4, 6], 3))</lang>
println(horner([-19, 7, -4, 6], 3))</syntaxhighlight>
{{out}}
{{out}}
<pre>128.0</pre>
<pre>128.0</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>package require Tcl 8.5
<syntaxhighlight lang="tcl">package require Tcl 8.5
proc horner {coeffs x} {
proc horner {coeffs x} {
set y 0
set y 0
Line 2,126: Line 2,518:
}
}
return $y
return $y
}</lang>
}</syntaxhighlight>
Demonstrating:
Demonstrating:
<lang tcl>puts [horner {-19 7 -4 6} 3]</lang>
<syntaxhighlight lang="tcl">puts [horner {-19 7 -4 6} 3]</syntaxhighlight>
Output:
Output:
<pre>128</pre>
<pre>128</pre>
Line 2,136: Line 2,528:
Note: this function, "Horner", gets its coefficients as a ParamArray which has no specified length. This array collect all arguments after the first one(s). This means you must specify x first, then the coefficients.
Note: this function, "Horner", gets its coefficients as a ParamArray which has no specified length. This array collect all arguments after the first one(s). This means you must specify x first, then the coefficients.


<syntaxhighlight lang="vba">
<lang VBA>
Public Function Horner(x, ParamArray coeff())
Public Function Horner(x, ParamArray coeff())
Dim result As Double
Dim result As Double
Line 2,149: Line 2,541:
Horner = result
Horner = result
End Function
End Function
</syntaxhighlight>
</lang>


Output:
Output:
Line 2,158: Line 2,550:


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function horners_rule(coefficients,x)
Function horners_rule(coefficients,x)
accumulator = 0
accumulator = 0
Line 2,168: Line 2,560:


WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)
WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 2,175: Line 2,567:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Function Horner(coefficients As Double(), variable As Double) As Double
Function Horner(coefficients As Double(), variable As Double) As Double
Line 2,185: Line 2,577:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>128</pre>
<pre>128</pre>
Line 2,191: Line 2,583:
=={{header|Visual FoxPro}}==
=={{header|Visual FoxPro}}==
===Coefficients in ascending order.===
===Coefficients in ascending order.===
<lang vfp>
<syntaxhighlight lang="vfp">
LOCAL x As Double
LOCAL x As Double
LOCAL ARRAY aCoeffs[1]
LOCAL ARRAY aCoeffs[1]
Line 2,211: Line 2,603:
RETURN s
RETURN s
ENDFUNC
ENDFUNC
</syntaxhighlight>
</lang>


===Coefficients in descending order.===
===Coefficients in descending order.===
<lang vfp>
<syntaxhighlight lang="vfp">
LOCAL x As Double
LOCAL x As Double
LOCAL ARRAY aCoeffs[1]
LOCAL ARRAY aCoeffs[1]
Line 2,233: Line 2,625:
RETURN s
RETURN s
ENDFUNC
ENDFUNC
</syntaxhighlight>
</lang>

=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn horner(x i64, c []i64) i64 {
mut acc := i64(0)
for i := c.len - 1; i >= 0; i-- {
acc = acc*x + c[i]
}
return acc
}
fn main() {
println(horner(3, [i64(-19), 7, -4, 6]))
}</syntaxhighlight>

{{out}}
<pre>
128
</pre>


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var horner = Fn.new { |x, c|
<syntaxhighlight lang="wren">var horner = Fn.new { |x, c|
var count = c.count
var count = c.count
if (count == 0) return 0
if (count == 0) return 0
Line 2,242: Line 2,652:
}
}


System.print(horner.call(3, [-19, 7, -4, 6]))</lang>
System.print(horner.call(3, [-19, 7, -4, 6]))</syntaxhighlight>


{{out}}
{{out}}
Line 2,250: Line 2,660:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>code IntOut=11;
<syntaxhighlight lang="xpl0">code IntOut=11;


func Horner(X, N, C); \Return value of polynomial in X
func Horner(X, N, C); \Return value of polynomial in X
Line 2,261: Line 2,671:
];
];


IntOut(0, Horner(3, 4, [-19, 7, -4, 6]));</lang>
IntOut(0, Horner(3, 4, [-19, 7, -4, 6]));</syntaxhighlight>


Output:
Output:
Line 2,269: Line 2,679:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn horner(coeffs,x)
<syntaxhighlight lang="zkl">fcn horner(coeffs,x)
{ coeffs.reverse().reduce('wrap(a,coeff){ a*x + coeff },0.0) }</lang>
{ coeffs.reverse().reduce('wrap(a,coeff){ a*x + coeff },0.0) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>

Latest revision as of 18:30, 17 March 2024

Task
Horner's rule for polynomial evaluation
You are encouraged to solve this task according to the task description, using any language you may know.

A fast scheme for evaluating a polynomial such as:

when

.

is to arrange the computation as follows:

And compute the result from the innermost brackets outwards as in this pseudocode:

coefficients := [-19, 7, -4, 6] # list coefficients of all x^0..x^n in order
x := 3
accumulator := 0
for i in length(coefficients) downto 1 do
    # Assumes 1-based indexing for arrays
    accumulator := ( accumulator * x ) + coefficients[i]
done
# accumulator now has the answer

Task Description

Create a routine that takes a list of coefficients of a polynomial in order of increasing powers of x; together with a value of x to compute its value at, and return the value of the polynomial at that value using Horner's rule.

Cf. Formal power series

11l

Translation of: Python
F horner(coeffs, x)
   V acc = 0
   L(c) reversed(coeffs)
      acc = acc * x + c
   R acc

print(horner([-19, 7, -4, 6], 3))
Output:
128

360 Assembly

*        Horner's rule for polynomial evaluation - 07/10/2015
HORNER   CSECT
         USING  HORNER,R15         set base register
         SR     R5,R5              accumulator=0
         LA     R2,N               i=number_of_coeff
LOOP     M      R4,X               accumulator=accumulator*x
         LR     R1,R2              i
         SLA    R1,2               i*4
         L      R3,COEF-4(R1)      coef(i)
         AR     R5,R3              accumulator=accumulator+coef(i)
         BCT    R2,LOOP            i=i-1; loop n times
         XDECO  R5,PG              edit accumulator 
         XPRNT  PG,12              print buffer
         XR     R15,R15            set return code
         BR     R14                return to caller
COEF     DC     F'-19',F'7',F'-4',F'6'    <== input values
X        DC     F'3'                      <== input value
N        EQU    (X-COEF)/4         number of coefficients 
PG       DS     CL12               buffer
         YREGS
         END    HORNER
Output:
         128

ACL2

(defun horner (ps x)
   (if (endp ps)
       0
       (+ (first ps)
          (* x (horner (rest ps) x)))))

Action!

INT FUNC Horner(INT ARRAY coeffs INT count,x)
  INT v,i

  v=0 i=count-1
  WHILE i>=0
  DO
    v=v*x+coeffs(i)
    i==-1
  OD
RETURN (v)

PROC Main()
  INT ARRAY coeffs=[65517 7 65532 6]
  INT res,x=[3],i,count=[4]

  PrintF("x=%I%E",x)
  FOR i=0 TO count-1
  DO
    PrintI(coeffs(i))
    IF i=1 THEN
      Print("x")
    ELSEIF i>1 THEN
      PrintF("x^%I",i)
    FI
    IF i<count-1 AND coeffs(i+1)>=0 THEN
      Print("+")
    FI
  OD
  res=Horner(coeffs,4,x)
  PrintF("=%I%E",res)
RETURN
Output:

Screenshot from Atari 8-bit computer

x=3
-19+7x-4x^2+6x^3=128

Ada

with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Horners_Rule is
   type Coef is array(Positive range <>) of Float;

   function Horner(Coeffs: Coef; Val: Float) return Float is
      Res : Float := 0.0;
   begin
      for P in reverse Coeffs'Range loop
         Res := Res*Val + Coeffs(P);
      end loop;
      return Res;
   end Horner;

begin
   Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;

Output:

128.0

Aime

real
horner(list coeffs, real x)
{
    real c, z;

    z = 0;

    for (, c of coeffs) {
        z *= x;
        z += c;
    }

    z;
}


integer
main(void)
{
    o_(horner(list(-19r, 7.0, -4r, 6r), 3), "\n");

    0;
}

ALGOL 68

Works with: ALGOL 68G
PROC horner = ([]REAL c, REAL x)REAL :
(
  REAL res := 0.0;
  FOR i FROM UPB c BY -1 TO LWB c DO
    res := res * x + c[i]
  OD;
  res
);

main:(
  [4]REAL coeffs := (-19.0, 7.0, -4.0, 6.0);
  print( horner(coeffs, 3.0) )
)

ALGOL W

begin
    % Horner's rule for polynominal evaluation                             %
    % returns the value of the polynominal defined by coefficients,        %
    %         at the point x. The number of coefficients must be in ub     %
    %         the coefficients should be in order with x^0 first, x^n last %
    real procedure Horner( real    array coefficients ( * )
                         ; integer value ub
                         ; real    value x
                         ) ;
    begin
        real xValue;
        xValue := 0;
        for i := ub step -1 until 1 do xValue := ( xValue * x ) + coefficients( i );
        xValue
    end Horner ;
    % task test case                                                       %
    begin
        real array coefficients ( 1 :: 4 );
        integer    cPos;
        cPos := 1;
        for i := -19, 7, -4, 6 do begin
            coefficients( cPos ) := i;
            cPos                 := cPos + 1
        end for_i ;
        write( r_format := "A", r_w := 8, r_d := 2, Horner( coefficients, 4, 3 ) )
    end test_cases
end.
Output:
  128.00

APL

Works in Dyalog APL

h
Output:
       3 h ¯19 7 ¯4 6
128

ATS

#include
"share/atspre_staload.hats"

fun
horner
(
  x: int, cs: List int
) : int = let
//
implement
list_foldright$fopr<int><int> (a, b) = a + b * x
//
in
  list_foldright<int><int> (cs, 0)
end // end of [horner]

implement
main0 () = let
  val x = 3
  val cs = $list{int}(~19, 7, ~4, 6)
  val res = horner (x, cs)
in
  println! (res)
end // end of [main0]

Arturo

horner: function [coeffs, x][
    result: 0
    loop reverse coeffs 'c ->
        result: c + result * x
    return result
]

print horner @[neg 19, 7, neg 4, 6] 3
Output:
128

AutoHotkey

Coefficients = -19, 7, -4, 6
x := 3

MsgBox, % EvalPolynom(Coefficients, x)



;---------------------------------------------------------------------------
EvalPolynom(Coefficients, x) { ; using Horner's rule
;---------------------------------------------------------------------------
    StringSplit, Co, coefficients, `,, %A_Space%
    Result := 0
    Loop, % Co0
        i := Co0 - A_Index + 1, Result := Result * x + Co%i%
    Return, Result
}

Message box shows:

128

AWK

#!/usr/bin/awk -f
function horner(x, A) {
	acc = 0;	
	for (i = length(A); 0<i; i--) {
		acc = acc*x + A[i];
	}
	return acc;
}
BEGIN {
        split(p,P);
	print horner(x,P);
}
Output:
   awk  -v X=3 -v p="-19  7 -4  6" -f horner.awk
   128

BASIC

Applesoft BASIC

Works with: Chipmunk Basic
Works with: GW-BASIC
100 HOME : REM  100 CLS for Chipmunk Basic and GW-BASIC
100 CLS : REM  100 HOME for Applesoft BASIC
110 X = 3
120 DIM COEFFS(3)
130 COEFFS(0) = -19
140 COEFFS(1) = 7
150 COEFFS(2) = -4
160 COEFFS(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 ACCUM = 0
200 FOR I = 3 TO 0 STEP -1
210  ACCUM = (ACCUM*X)+COEFFS(I)
220 NEXT I
230 PRINT ACCUM
240 END

BASIC256

x = 3
dim coeficientes = {-19, 7, -4, 6}
print "Horner's algorithm for the polynomial ";
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeficientes, x)
end

function AlgoritmoHorner(coeffs, x)
	acumulador = 0
	for i = coeffs[?]-1 to 0 step -1
		acumulador = (acumulador * x) + coeffs[i]
	next i
	return acumulador
end function
Output:
Same as FreeBASIC entry.

Chipmunk Basic

Works with: Chipmunk Basic version 3.6.4
Works with: QBasic
100 CLS
110 x = 3
120 DIM coeffs(3)
130 coeffs(0) = -19
140 coeffs(1) = 7
150 coeffs(2) = -4
160 coeffs(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 accum = 0
200 FOR i = UBOUND(coeffs,1) TO 0 STEP -1
210  accum = (accum*x)+coeffs(i)
220 NEXT i
230 PRINT accum
240 END
Output:
Horner's algorithm for the polynomial
6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: 128

Gambas

Public coeficientes As New Integer[4] 

Public Function AlgoritmoHorner(coeficientes As Integer[], x As Integer) As Integer 
  
  coeficientes[0] = -19 
  coeficientes[1] = 7 
  coeficientes[2] = -4 
  coeficientes[3] = 6 
  Dim i As Integer, acumulador As Integer = 0 

  For i = coeficientes.Count - 1 To 0 Step -1  
    acumulador = (acumulador * x) + coeficientes[i] 
  Next
  Return acumulador 
  
End Function 

Public Sub Main() 
  
  Dim x As Integer = 3 
  
  Print "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3: "; 
  Print AlgoritmoHorner(coeficientes, x) 
  
End
Output:
Same as FreeBASIC entry.

GW-BASIC

Works with: BASICA
Works with: Chipmunk Basic
Works with: PC-BASIC version any
Works with: QBasic
100 CLS : REM  100 HOME for Applesoft BASIC
110 X = 3
120 DIM COEFFS(3)
130 COEFFS(0) = -19
140 COEFFS(1) = 7
150 COEFFS(2) = -4
160 COEFFS(3) = 6
170 PRINT "Horner's algorithm for the polynomial "
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
190 ACCUM = 0
200 FOR I = 3 TO 0 STEP -1
210  ACCUM = (ACCUM*X)+COEFFS(I)
220 NEXT I
230 PRINT ACCUM
240 END
Output:
Same as Chipmunk Basic entry.

Minimal BASIC

Works with: QBasic
Works with: QuickBasic
Works with: Applesoft BASIC
Works with: BASICA
Works with: Chipmunk Basic
Works with: GW-BASIC
Works with: MSX BASIC
Works with: Just BASIC
Works with: Liberty BASIC
Works with: Run BASIC
Works with: Yabasic
20 LET X = 3
30 DIM C(3)
40 LET C(0) = -19
50 LET C(1) = 7
60 LET C(2) = -4
70 LET C(3) = 6
80 PRINT "HORNER'S ALGORITHM FOR THE POLYNOMIAL"
90 PRINT "6*X^3 - 4*X^2 + 7*X - 19 WHEN X = 3 : ";
100 LET A = 0
110 FOR I = 3 TO 0 STEP -1
120  LET A = (A*X)+C(I)
130 NEXT I
140 PRINT A
150 END
Output:
Same as Chipmunk Basic entry.

MSX Basic

The Minimal BASIC solution works without any changes.

QBasic

Works with: QBasic version 1.1
Works with: QuickBasic version 4.5
FUNCTION Horner (coeffs(), x)
    acumulador = 0
    FOR i = UBOUND(coeffs) TO LBOUND(coeffs) STEP -1
        acumulador = (acumulador * x) + coeffs(i)
    NEXT i
    Horner = acumulador
END FUNCTION

x = 3
DIM coeffs(3)
DATA -19, 7, -4, 6
FOR a = LBOUND(coeffs) TO UBOUND(coeffs)
READ coeffs(a)
NEXT a

PRINT "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: ";
PRINT Horner(coeffs(), x)
END

Quite BASIC

The Minimal BASIC solution works without any changes.

Yabasic

x = 3
dim coeffs(4)
coeffs(0) = -19
coeffs(1) = 7
coeffs(2) = -4
coeffs(3) = 6
print "Horner's algorithm for the polynomial ";
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeffs, x)
end

sub AlgoritmoHorner(coeffs, x)
  local acumulador, i
  
  acumulador = 0
  for i = arraysize(coeffs(),1) to 0 step -1
    acumulador = (acumulador * x) + coeffs(i)
  next i
  return acumulador
end sub
Output:
Same as FreeBASIC entry.

Batch File

@echo off

call:horners a:-19 b:7 c:-4 d:6 x:3
call:horners x:3 a:-19 c:-4 d:6 b:7
pause>nul
exit /b

:horners
setlocal enabledelayedexpansion
set a=0
set b=0
set c=0
set d=0
set x=0

for %%i in (%*) do (
  for /f "tokens=1,2 delims=:" %%j in ("%%i") do (
    set %%j=%%k
  )
)
set /a return=((((0)*%x%+%d%)*%x%+(%c%))*%x%+%b%)*%x%+(%a%)
echo %return%
exit /b
Output:
>a:-19 b:7 c:-4 d:6 x:3
128
>x:3 a:-19 c:-4 d:6 b:7
128

BBC BASIC

      DIM coefficients(3)
      coefficients() = -19, 7, -4, 6
      PRINT FNhorner(coefficients(), 3)
      END
      
      DEF FNhorner(coeffs(), x)
      LOCAL i%, v
      FOR i% = DIM(coeffs(), 1) TO 0 STEP -1
        v = v * x + coeffs(i%)
      NEXT
      = v

Bracmat

( ( Horner
  =   accumulator coefficients x coeff
    .   !arg:(?coefficients.?x)
      & 0:?accumulator
      &   whl
        ' ( !coefficients:?coefficients #%@?coeff
          & !accumulator*!x+!coeff:?accumulator
          )
      & !accumulator
  )
& Horner$(-19 7 -4 6.3)
);

Output:

128

C

Translation of: Fortran
#include <stdio.h>

double horner(double *coeffs, int s, double x)
{
  int i;
  double res = 0.0;
  
  for(i=s-1; i >= 0; i--)
  {
    res = res * x + coeffs[i];
  }
  return res;
}


int main()
{
  double coeffs[] = { -19.0, 7.0, -4.0, 6.0 };
  
  printf("%5.1f\n", horner(coeffs, sizeof(coeffs)/sizeof(double), 3.0));
  return 0;
}

C#

using System;
using System.Linq;

class Program
{
    static double Horner(double[] coefficients, double variable)
    {
        return coefficients.Reverse().Aggregate(
                (accumulator, coefficient) => accumulator * variable + coefficient);
    }

    static void Main()
    {
        Console.WriteLine(Horner(new[] { -19.0, 7.0, -4.0, 6.0 }, 3.0));
    }
}

Output:

128

C++

The same C function works too, but another solution could be:

#include <iostream>
#include <vector>

using namespace std;

double horner(vector<double> v, double x)
{
  double s = 0;
  
  for( vector<double>::const_reverse_iterator i = v.rbegin(); i != v.rend(); i++ )
    s = s*x + *i;
  return s;
}

int main()
{
  double c[] = { -19, 7, -4, 6 };
  vector<double> v(c, c + sizeof(c)/sizeof(double));
  cout << horner(v, 3.0) << endl;
  return 0;
}

Yet another solution, which is more idiomatic in C++ and works on any bidirectional sequence:

#include <iostream>

template<typename BidirIter>
 double horner(BidirIter begin, BidirIter end, double x)
{
  double result = 0;
  while (end != begin)
    result = result*x + *--end;
  return result;
}

int main()
{
  double c[] = { -19, 7, -4, 6 };
  std::cout << horner(c, c + 4, 3) << std::endl;
}

Clojure

(defn horner [coeffs x]
  (reduce #(-> %1 (* x) (+ %2)) (reverse coeffs)))

(println (horner [-19 7 -4 6] 3))

CoffeeScript

eval_poly = (x, coefficients) ->
  # coefficients are for ascending powers
  return 0 if coefficients.length == 0
  ones_place = coefficients.shift()
  x * eval_poly(x, coefficients) + ones_place
  
console.log eval_poly 3, [-19, 7, -4, 6] # 128
console.log eval_poly 10, [4, 3, 2, 1] # 1234
console.log eval_poly 2, [1, 1, 0, 0, 1] # 19

Common Lisp

(defun horner (coeffs x)
  (reduce #'(lambda (coef acc) (+ (* acc x) coef))
	  coeffs :from-end t :initial-value 0))

Alternate version using LOOP. Coefficients are passed in a vector.

(defun horner (x a)
    (loop :with y = 0
          :for i :from (1- (length a)) :downto 0
          :do (setf y (+ (aref a i) (* y x)))
          :finally (return y)))

(horner 1.414 #(-2 0 1))

D

The poly() function of the standard library std.math module uses Horner's rule:

void main() {
  void main() {
    import std.stdio, std.math;
 double x = 3.0;
static real[] pp = [-19,7,-4,6];

    poly(x,pp).writeln;
}
}

Basic implementation:

import std.stdio, std.traits;

CommonType!(U, V) horner(U, V)(U[] p, V x) pure nothrow @nogc {
    typeof(return) accumulator = 0;
    foreach_reverse (c; p)
        accumulator = accumulator * x + c;
    return accumulator;
}

void main() {
    [-19, 7, -4, 6].horner(3.0).writeln;
}

More functional style:

import std.stdio, std.algorithm, std.range;

auto horner(T, U)(in T[] p, in U x) pure nothrow @nogc {
    return reduce!((a, b) => a * x + b)(U(0), p.retro);
}

void main() {
    [-19, 7, -4, 6].horner(3.0).writeln;
}


Delphi

Works with: Delphi version 6.0


function EvaluatePolynomial(Args: array of double; X: double): double;
{Evaluate polynomial using Horner's rule }
var I: integer;
begin
Result:=0;
for I:=High(Args) downto 0 do
    Result:=(Result * X ) + Args[I];
end;

function GetPolynomialStr(Args: array of double; VarName: string): string;
{Return a string display the polynomial in normal format}
{for example: 6.0 X^3 - 4.0 X^2 + 7.0 X - 19.0}
var I: integer;
begin
Result:='';
for I:=High(Args) downto 0 do
	begin
	if Args[I]>0 then
		begin
		if I<>High(Args) then Result:=Result+' + ';
		end
	else Result:=Result+' - ';
	Result:=Result+FloatToStrF(Abs(Args[I]),ffFixed,18,1);
	if I>0 then Result:=Result+' '+VarName;
	if I>1 then Result:=Result+'^'+IntToStr(I);
	end;
end;


procedure ShowHornerPoly(Memo: TMemo; Args: array of double; X: double);
{Evaluate polynomial, show formated polynomal and the result}
var R: double;
begin
R:=EvaluatePolynomial(Args,X);
Memo.Lines.Add(FloatToStrF(R,ffFixed, 18,1));
Memo.Lines.Add(GetPolynomialStr(Args,'X'));
end;


procedure DoHornerPoly(Memo: TMemo);
begin
ShowHornerPoly(Memo,[-19, 7, -4, 6],3)
end;
Output:
128.0
6.0 X^3 - 4.0 X^2 + 7.0 X - 19.0

E

def makeHornerPolynomial(coefficients :List) {
    def indexing := (0..!coefficients.size()).descending()
    return def hornerPolynomial(x) {
        var acc := 0
        for i in indexing {
            acc := acc * x + coefficients[i]
        }
        return acc
    }
}
? makeHornerPolynomial([-19, 7, -4, 6])(3)
# value: 128

EasyLang

Translation of: C
func horner coeffs[] x .
   for i = len coeffs[] downto 1
      res = res * x + coeffs[i]
   .
   return res
.
print horner [ -19 7 -4 6 ] 3
Output:
128

EchoLisp

Functional version

(define (horner x poly)
(foldr (lambda (coeff acc) (+ coeff (* acc x))) 0 poly))

(horner 3 '(-19 7 -4 6))  128

Library

(lib 'math)
Lib: math.lib loaded.

(define P '(-19 7 -4 6))
(poly->string 'x P)  6x^3 -4x^2 +7x -19 
(poly 3 P)  128

EDSAC order code

[Copyright <2021> <ERIK SARGSYAN>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.]
[Horner's rule for polynomial evaluation on EDSAC by Erik Sargsyan]
[EDSAC program, Initial Orders 2]


    T 56 K
    GK
[0] A 3 F
[1] T 9 F
[2] A 4 F [loading the address of the 1st array element into the accumulator]
[3] A 14 @ [add an instruction code with a zero address field]
[4] T 14 @ [writing the generated instruction, zeroing the accumulator]
[5 loop] A 1 F [load the counter of unprocessed array elements into the accumulator]
[6] S 21 @ [Subtract the constant = 1]
[7] E 10 @ [if (acc >= 0) goto 10 else end]
[8] T 300 F [zeroing the accumulator]
[9] E 0 F [breakpoint, end of program]
[10] T 1 F [update the counter value and reset the accumulator]
[11] V 2 F [multiplying the number in cell 2 by the number in the multiplier register]
[12] L 1024 F [shift the number in the accumulator 12 bits to the left]
[13] L 4 F [shift the number in the accumulator 4 bits to the left]
[14 r1] A 0 F [loading the value from cell 0 into the accumulator]
[15] T 2 F [writing this value to the working cell, zeroing the accumulator]
[16] A 21 @ [loading into accumulator constant value = 1]
[17] L 0 D [shift the number in the accumulator 1 bit to the left]
[18] A 14 @ [add the code of the instruction executed in the previous step]
[19] T 14 @ [write the generated instruction into memory]
[20] E 5 @ [repeat all operations; accumulator zeroed]
[21 const 1] P 0 D [1]

    GK
[0] H 10 @ [writing X0 to the multiplier register]
[1] A 11 @ [loading into the accumulator of the degree of the polynomial]
[2] T 1 F [writing the degree of a polynomial in cell 1]
[3] A 13 @ [loading the leading coefficient into the accumulator]
[4] T 2 F [writing the senior coefficient to the working cell 2]
[5] A 12 @ [loading the address of the 1st array element into the accumulator]
[6] T 4 F  [writing the address of the 1st element of the array]
[7] A 7 @ [\ call]
[8] G 56 F
[9] Z 0 F
[10] P 3 F [X0 is a fixed value of X, by which we calculate the value of the polynomial]
[11 power] P 2 F [polynomial degree times 2]
[12 addr] P 14 @ [address of the 1st element of the array]
[13] P 3 F [a4 = 6]
[14] P 8 D [a3 = 17]
[15] P 9 F [a2 = 18]
[16] P 12 D [a1 = 25]
[17] P 316 F [a0 = 632]
EZPF
Output:
Cell 2 will contain the number 12878

Elena

Translation of: C#

ELENA 5.0 :

import extensions;
import system'routines;
 
horner(coefficients,variable)
{
    ^ coefficients.clone().sequenceReverse().accumulate(new Real(),(accumulator,coefficient => accumulator * variable + coefficient))
}
 
public program()
{
    console.printLine(horner(new real[]{-19.0r, 7.0r, -4.0r, 6.0r}, 3.0r))
}
Output:
128.0

Elixir

horner = fn(list, x)-> List.foldr(list, 0, fn(c,acc)-> x*acc+c end) end

IO.puts horner.([-19,7,-4,6], 3)
Output:
128

Emacs Lisp

Translation of: Common Lisp
(require 'cl-lib)

(defun horner (coeffs x)
  (cl-reduce #'(lambda (coef acc) (+ (* acc x) coef))
	     coeffs :from-end t :initial-value 0))

(horner '(-19 7 -4 6) 3)
Output:
128

Erlang

horner(L,X) ->
  lists:foldl(fun(C, Acc) -> X*Acc+C end,0, lists:reverse(L)).
t() ->
  horner([-19,7,-4,6], 3).

ERRE

PROGRAM HORNER

!                        2   3
! polynomial is -19+7x-4x +6x
!

DIM C[3]

PROCEDURE HORNER(C[],X->RES)
  LOCAL I%,V
  FOR I%=UBOUND(C,1) TO 0 STEP -1 DO
     V=V*X+C[I%]
  END FOR
  RES=V
END PROCEDURE

BEGIN
  C[]=(-19,7,-4,6)
  HORNER(C[],3->RES)
  PRINT(RES)
END PROGRAM

Euler Math Toolbox

>function horner (x,v) ...
$  n=cols(v); res=v{n};
$  loop 1 to n-1; res=res*x+v{n-#}; end;
$  return res
$endfunction
>v=[-19,7,-4,6]
 [ -19  7  -4  6 ]
>horner(2,v) // test Horner
 27
>evalpoly(2,v) // built-in Horner
 27
>horner(I,v) // complex values
 -15+1i
>horner(1±0.05,v) // interval values
 ~-10.9,-9.11~
>function p(x) &= sum(@v[k]*x^(k-1),k,1,4) // Symbolic Polynomial
                            3      2
                         6 x  - 4 x  + 7 x - 19


F#

let horner l x =
    List.rev l |> List.fold ( fun acc c -> x*acc+c) 0

horner [-19;7;-4;6] 3

Factor

: horner ( coeff x -- res )
    [ <reversed> 0 ] dip '[ [ _ * ] dip + ] reduce ;
( scratchpad ) { -19 7 -4 6 } 3 horner .
128

Forth

: fhorner ( coeffs len F: x -- F: val )
  0e
  floats bounds ?do
    fover f*  i f@ f+
  1 floats +loop
  fswap fdrop ;

create coeffs 6e f, -4e f, 7e f, -19e f,

coeffs 4 3e fhorner f.    \ 128.

Fortran

Works with: Fortran version 90 and later
program test_horner

  implicit none

  write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)

contains

  function horner (coeffs, x) result (res)

    implicit none
    real, dimension (0:), intent (in) :: coeffs
    real, intent (in) :: x
    real :: res
    integer :: i

    res = coeffs(ubound(coeffs,1))
    do i = ubound(coeffs,1)-1, 0, -1
      res = res * x + coeffs (i)
    end do

  end function horner

end program test_horner

Output:

128.0

Fortran 77

      FUNCTION HORNER(N,A,X)
      IMPLICIT NONE
      INTEGER I,N
      DOUBLE PRECISION A(N),X,Y,HORNER
      Y = A(N)
      DO I = N - 1,1,-1
        Y = Y*X + A(I)
      END DO
      HORNER=Y
      END

As a matter of fact, computing the derivative is not much more difficult (see Roundoff in Polynomial Evaluation, W. Kahan, 1986). The following subroutine computes both polynomial value and derivative for argument x.

      SUBROUTINE HORNER2(N,A,X,Y,Z)
C COMPUTE POLYNOMIAL VALUE AND DERIVATIVE
C SEE "ROUNDOFF IN POLYNOMIAL EVALUATION", W. KAHAN, 1986
C POLY: A(1) + A(2)*X + ... + A(N)*X**(N-1)
C Y: VALUE, Z: DERIVATIVE
      IMPLICIT NONE
      INTEGER I,N
      DOUBLE PRECISION A(N),X,Y,Z
      Z = 0.0D0
      Y = A(N)
      DO 10 I = N - 1,1,-1
        Z = Z*X + Y
   10 Y = Y*X + A(I)
      END

FreeBASIC

Function AlgoritmoHorner(coeffs() As Integer, x As Integer) As Integer
    Dim As Integer  i, acumulador = 0
    For i = Ubound(coeffs, 1) To 0 Step -1
        acumulador = (acumulador * x) + coeffs(i)
    Next i
    Return acumulador
End Function

Dim As Integer x = 3
Dim As Integer coeficientes(3) = {-19, 7, -4, 6}
Print "Algoritmo de Horner para el polinomio 6*x^3 - 4*x^2 + 7*x - 19 para x = 3: ";
Print AlgoritmoHorner(coeficientes(), x)
End
Output:
Algoritmo de Horner para el polinomio 6*x^3 - 4*x^2 + 7*x - 19 para x = 3:  128

FunL

Translation of: Haskell
import lists.foldr

def horner( poly, x ) = foldr( \a, b -> a + b*x, 0, poly )

println( horner([-19, 7, -4, 6], 3) )
Output:
128

FutureBasic

include "NSLog.incl"

local fn horner( coeffs as CFArrayRef, x as NSInteger ) as double
CFArrayRef  reversedCoeffs
CFNumberRef num
double      accumulator = 0.0

// Reverse coeffs array
reversedCoeffs = fn EnumeratorAllObjects( fn ArrayReverseObjectEnumerator( coeffs ) )

// Iterate over CFNumberRefs in reversed array, convert to double values, calculate and add to accumulator
for num in reversedCoeffs
accumulator = ( accumulator * x ) + fn NumberDoubleValue( num )
next
end fn = accumulator

CFArrayRef coeffs

coeffs = @[@-19.0, @7.0, @-4.0, @6.0]
NSLog( @"%7.1f", fn horner( coeffs, 3 ) )

coeffs = @[@4.0, @3.0, @2.0, @1.0]
NSLog( @"%7.1f", fn horner( coeffs, 10 ) )

coeffs = @[@1, @1, @0, @0, @1]
NSLog( @"%7.1f", fn horner( coeffs, 2 ) )

coeffs = @[@1.2, @2.3, @3.4, @4.5, @5.6]
NSLog( @"%7.1f", fn horner( coeffs, 8 ) )

coeffs = @[@1, @0, @1, @1, @1, @0, @0, @1]
NSLog( @"%7.1f", fn horner( coeffs, 2 ) )

HandleEvents
Output:
  128.0
 1234.0
   19.0
25478.8
  157.0

GAP

# The idiomatic way to compute with polynomials

x := Indeterminate(Rationals, "x");

# This is a value in a polynomial ring, not a function
p := 6*x^3 - 4*x^2 + 7*x - 19;

Value(p, 3);
# 128

u := CoefficientsOfUnivariatePolynomial(p);
# [ -19, 7, -4, 6 ]

# One may also create the polynomial from coefficients
q := UnivariatePolynomial(Rationals, [-19, 7, -4, 6], x);
# 6*x^3-4*x^2+7*x-19

p = q;
# true

# Now a Horner implementation
Horner := function(coef, x)
	local v, c;
	v := 0;
	for c in Reversed(coef) do
		v := x*v + c;
	od;
	return v;
end;

Horner(u, 3);
# 128

Go

package main

import "fmt"

func horner(x int64, c []int64) (acc int64) {
    for i := len(c) - 1; i >= 0; i-- {
        acc = acc*x + c[i]
    }
    return
}

func main() {
    fmt.Println(horner(3, []int64{-19, 7, -4, 6}))
}

Output:

128

Groovy

Solution:

def hornersRule = { coeff, x -> coeff.reverse().inject(0) { accum, c -> (accum * x) + c } }

Test includes demonstration of currying to create polynomial functions of one variable from generic Horner's rule calculation. Also demonstrates constructing the derivative function for the given polynomial. And finally demonstrates in the Newton-Raphson method to find one of the polynomial's roots using the polynomial and derivative functions constructed earlier.

def coefficients = [-19g, 7g, -4g, 6g]
println (["p coefficients":coefficients])

def testPoly = hornersRule.curry(coefficients)
println (["p(3)":testPoly(3g)])
println (["p(0)":testPoly(0g)])

def derivativeCoefficients = { coeff -> (1..<(coeff.size())).collect { coeff[it] * it } }
println (["p' coefficients":derivativeCoefficients(coefficients)])

def testDeriv = hornersRule.curry(derivativeCoefficients(coefficients))
println (["p'(3)":testDeriv(3g)])
println (["p'(0)":testDeriv(0g)])

def newtonRaphson = { x, f, fPrime ->
    while (f(x).abs() > 0.0001) {
        x -= f(x)/fPrime(x)
    }
    x
}

def root = newtonRaphson(3g, testPoly, testDeriv)
println ([root:root.toString()[0..5], "p(root)":testPoly(root).toString()[0..5], "p'(root)":testDeriv(root).toString()[0..5]])

Output:

[p coefficients:[-19, 7, -4, 6]]
[p(3):128]
[p(0):-19]
[p' coefficients:[7, -8, 18]]
[p'(3):145]
[p'(0):7]
[root:1.4183, p(root):0.0000, p'(root):31.862]

Haskell

horner :: (Num a) => a -> [a] -> a
horner x = foldr (\a b -> a + b*x) 0

main = print $ horner 3 [-19, 7, -4, 6]

HicEst

REAL :: x=3, coeffs(4)
DATA    coeffs/-19.0, 7.0, -4.0, 6.0/

WRITE(Messagebox) Horner(coeffs, x) ! shows 128

FUNCTION Horner(c, x)
   DIMENSION c(1)
   Horner = 0
   DO i = LEN(c), 1, -1
      Horner = x*Horner + c(i)
   ENDDO
END

Icon and Unicon

procedure poly_eval (x, coeffs)
  accumulator := 0
  every index := *coeffs to 1 by -1 do 
    accumulator := accumulator * x + coeffs[index]
  return accumulator
end

procedure main ()
  write (poly_eval (3, [-19, 7, -4, 6]))
end

J

Solution:

   horner =:  (#."0 _ |.)~          NB. Tacit
   horner =:  [: +`*/ [: }: ,@,.    NB. Alternate tacit (equivalent)
   horner =:  4 :  '  (+ *&y)/x'    NB. Alternate explicit (equivalent)

Example:

   _19 7 _4 6 horner 3
128

Note:
The primitive verb p. would normally be used to evaluate polynomials.

   _19 7 _4 6 p. 3
128

Java

Works with: Java version 1.5+
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Horner {
    public static void main(String[] args){
        List<Double> coeffs = new ArrayList<Double>();
        coeffs.add(-19.0);
        coeffs.add(7.0);
        coeffs.add(-4.0);
        coeffs.add(6.0);
        System.out.println(polyEval(coeffs, 3));
    }

    public static double polyEval(List<Double> coefficients, double x) {
        Collections.reverse(coefficients);
        Double accumulator = coefficients.get(0);
        for (int i = 1; i < coefficients.size(); i++) {
            accumulator = (accumulator * x) + (Double) coefficients.get(i);
        }
        return accumulator;
    }
}

Output:

128.0

JavaScript

Works with: JavaScript version 1.8

which includes

Works with: Firefox version 3
Translation of: Haskell
function horner(coeffs, x) {
    return coeffs.reduceRight( function(acc, coeff) { return(acc * x + coeff) }, 0);
}
console.log(horner([-19,7,-4,6],3));  // ==> 128

jq

# Input: an array of coefficients specifying the polynomial 
# to be evaluated at $x, where .[0] is the constant
def horner($x):
  . as $coefficients
  | reduce range(length-1; -1; -1) as $i (0; . * $x + $coefficients[$i]);

# Example
[-19, 7, -4, 6] | horner(3)

Invocation: $JQ -n -f horner.jq

where $JQ is either jq or gojq

Output:
128

Julia

Works with: Julia version 1.x

Imperative:

function horner(coefs, x)
    s = coefs[end] * one(x)
    for k in length(coefs)-1:-1:1
        s = coefs[k] + x * s
    end
    return s
end

@show horner([-19, 7, -4, 6], 3)
Output:
horner([-19, 7, -4, 6], 3) = 128

Functional:

horner2(coefs, x) = foldr((u, v) -> u + x * v, coefs, init=zero(promote_type(typeof(x),eltype(coefs))))

@show horner2([-19, 7, -4, 6], 3)
Output:
horner2([-19, 7, -4, 6], 3) = 128

Note: In Julia 1.4 or later one would normally use the built-in evalpoly function for this purpose:

 
@show evalpoly(3, [-19, 7, -4, 6])
Output:
evalpoly(3, [-19, 7, -4, 6]) = 128

K

  horner:{y _sv|x}
  horner[-19 7 -4 6;3]
128

Kotlin

// version 1.1.2

fun horner(coeffs: DoubleArray, x: Double): Double {
    var sum = 0.0
    for (i in coeffs.size - 1 downTo 0) sum = sum * x + coeffs[i]
    return sum
}

fun main(args: Array<String>) {
    val coeffs = doubleArrayOf(-19.0, 7.0, -4.0, 6.0)
    println(horner(coeffs, 3.0))
}
Output:
128.0

Lambdatalk

{def horner
 {def horner.r
  {lambda {:p :x :r}
   {if {A.empty? :p}
    then :r
    else {horner.r {A.rest :p} :x {+ {A.first :p} {* :x :r}}}}}}
 {lambda {:p :x}
  {horner.r {A.reverse :p} :x 0}}}

{horner {A.new -19 7 -4 6} 3}
-> 128

{def φ {/ {+ 1 {sqrt 5}} 2}} = 1.618033988749895
{horner {A.new -1 -1 1} φ}
-> 2.220446049250313e-16 ~ 0

Liberty BASIC

src$ = "Hello"
coefficients$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
print horner(coefficients$, x)      '128

print horner("4  3  2  1", 10)      '1234
print horner("1  1  0  0  1", 2)    '19
end

function horner(coefficients$, x)
    accumulator = 0
    'getting length of a list requires extra pass with WORD$.
    'So we just started from high above
    for index = 100 to 1 step -1
        cft$ = word$(coefficients$, index)
        if cft$<>"" then accumulator = ( accumulator * x ) + val(cft$)
    next
    horner = accumulator
end function

to horner :x :coeffs
  if empty? :coeffs [output 0]
  output (first :coeffs) + (:x * horner :x bf :coeffs)
end

show horner 3 [-19 7 -4 6]   ; 128

Lua

function horners_rule( coeff, x )
    local res = 0    
    for i = #coeff, 1, -1 do
        res = res * x + coeff[i]
    end
    return res
end

x = 3
coefficients = { -19, 7, -4, 6 }
print( horners_rule( coefficients, x ) )

Maple

applyhorner:=(L::list,x)->foldl((s,t)->s*x+t,op(ListTools:-Reverse(L))):

applyhorner([-19,7,-4,6],x);

applyhorner([-19,7,-4,6],3);

Output:

                    ((6 x - 4) x + 7) x - 19

                              128

Mathematica / Wolfram Language

Horner[l_List, x_] := Fold[x #1 + #2 &, 0, l]
Horner[{6, -4, 7, -19}, x]
-> -19 + x (7 + x (-4 + 6 x))

-19 + x (7 + x (-4 + 6 x)) /. x -> 3
-> 128

MATLAB

function accumulator = hornersRule(x,coefficients)

    accumulator = 0;
    
    for i = (numel(coefficients):-1:1)
        accumulator = (accumulator * x) + coefficients(i);
    end
    
end

Output:

>> hornersRule(3,[-19, 7, -4, 6])

ans =

   128

Matlab also has a built-in function "polyval" which uses Horner's Method to evaluate polynomials. The list of coefficients is in descending order of power, where as to task spec specifies ascending order.

>> polyval(fliplr([-19, 7, -4, 6]),3)

ans =

   128

Maxima

/* Function horner already exists in Maxima, though it operates on expressions, not lists of coefficients */
horner(5*x^3+2*x+1);
x*(5*x^2+2)+1

/* Here is an implementation */
horner2(p, x) := block([n, y, i],
   n: length(p),
   y: p[n],
   for i: n - 1 step -1 thru 1 do y: y*x + p[i],
   y
)$

horner2([-19, 7, -4, 6], 3);
128

/* Another with rreduce */
horner3(p,x):=rreduce(lambda([a,y],x*y+a),p);
horner3([a,b,c,d,e,f],x);
x*(x*(x*(x*(f*x+e)+d)+c)+b)+a

/* Extension to compute also derivatives up to a specified order.
   See William Kahan, Roundoff in Polynomial Evaluation, 1986
   http://www.cs.berkeley.edu/~wkahan/Math128/Poly.pdf */

poleval(a, x, [m]) := block(
   [n: length(a), v, k: 1],
   if emptyp(m) then m: 1 else m: 1 + first(m),
   v: makelist(0, m),
   v[1]: a[n],
   for i from n - 1 thru 1 step -1 do (
      for j from m thru 2 step -1 do v[j]: v[j] * x + v[j - 1],
      v[1]: v[1] * x + a[i]
   ),
   for i from 2 thru m do (
      v[i]: v[i] * k,
      k: k * i
   ),
   if m = 1 then first(v) else v
)$

poleval([0, 0, 0, 0, 1], x, 4);
[x^4, 4 * x^3, 12 * x^2, 24 * x, 24]

poleval([0, 0, 0, 0, 1], x);
x^4

Mercury

:- module horner.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, list, string.

main(!IO) :-
    io.format("%i\n", [i(horner(3, [-19, 7, -4, 6]))], !IO).

:- func horner(int, list(int)) = int.

horner(X, Cs) = list.foldr((func(C, Acc) = Acc * X + C), Cs, 0).

МК-61/52

ИП0	1	+	П0
ИПE	ИПD	*	КИП0	+	ПE
ИП0	1	-	x=0	04
ИПE	С/П

Input: Р1:РС - coefficients, Р0 - number of the coefficients, РD - x.

Modula-2

MODULE Horner;
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;

PROCEDURE Horner(coeff : ARRAY OF REAL; x : REAL) : REAL;
VAR
    ans : REAL;
    i : CARDINAL;
BEGIN
    ans := 0.0;
    FOR i:=HIGH(coeff) TO 0 BY -1 DO
        ans := (ans * x) + coeff[i];
    END;
    RETURN ans
END Horner;

TYPE A = ARRAY[0..3] OF REAL;
VAR
    buf : ARRAY[0..63] OF CHAR;
    coeff : A;
    ans : REAL;
BEGIN
    coeff := A{-19.0, 7.0, -4.0, 6.0};
    ans := Horner(coeff, 3.0);
    RealToStr(ans, buf);
    WriteString(buf);
    WriteLn;
    ReadChar
END Horner.

NetRexx

/* NetRexx */
options replace format comments java crossref savelog symbols nobinary

c = [-19, 7, -4, 6] -- # list coefficients of all x^0..x^n in order
n=3
x=3
r=0
loop i=n to 0 by -1
  r=r*x+c[i]
  End
Say r
Say 6*x**3-4*x**2+7*x-19

Output:

128
128

Nim

# You can also just use `reversed` proc from stdlib `algorithm` module
iterator reversed[T](x: openArray[T]): T =
  for i in countdown(x.high, x.low):
    yield x[i]
 
proc horner[T](coeffs: openArray[T], x: T): int =
  for c in reversed(coeffs):
    result = result * x + c
 
echo horner([-19, 7, -4, 6], 3)

Oberon-2

Works with: oo2c
MODULE HornerRule;
IMPORT 
  Out;
 
TYPE
  Coefs = POINTER TO ARRAY OF LONGINT;
VAR
  coefs: Coefs;
  
PROCEDURE Eval(coefs: ARRAY OF LONGINT;size,x: LONGINT): LONGINT;
VAR
  i,acc: LONGINT;
BEGIN
  acc := 0;
  FOR i := LEN(coefs) - 1 TO 0 BY -1 DO
	acc := acc * x + coefs[i]
  END;
  RETURN acc
END Eval;

BEGIN
  NEW(coefs,4);
  coefs[0] := -19;
  coefs[1] := 7;
  coefs[2] := -4;
  coefs[3] := 6;
  Out.Int(Eval(coefs^,4,3),0);Out.Ln
END HornerRule.
Output:
128

Objeck

class Horner {
  function : Main(args : String[]) ~ Nil {
    coeffs := Collection.FloatVector->New();  
    coeffs->AddBack(-19.0);
    coeffs->AddBack(7.0);
    coeffs->AddBack(-4.0);
    coeffs->AddBack(6.0);
    PolyEval(coeffs, 3)->PrintLine();
  }
  
  function : PolyEval(coefficients : Collection.FloatVector , x : Float) ~ Float {
    accumulator := coefficients->Get(coefficients->Size() - 1);
    for(i := coefficients->Size() - 2; i > -1; i -= 1;) {
       accumulator := (accumulator * x) + coefficients->Get(i);
    };
    
    return accumulator;
  }
}

Objective-C

Works with: Mac OS X version 10.6+

Using blocks

#import <Foundation/Foundation.h>

typedef double (^mfunc)(double, double);

@interface NSArray (HornerRule)
- (double)horner: (double)x;
- (NSArray *)reversedArray;
- (double)injectDouble: (double)s with: (mfunc)op;
@end

@implementation NSArray (HornerRule)
- (NSArray *)reversedArray
{
  return [[self reverseObjectEnumerator] allObjects];
}


- (double)injectDouble: (double)s with: (mfunc)op
{
  double sum = s;
  for(NSNumber* el in self) {
    sum = op(sum, [el doubleValue]);
  }
  return sum;
}

- (double)horner: (double)x
{
  return [[self reversedArray] injectDouble: 0.0 with: ^(double s, double a) { return s * x + a; } ];
}
@end

int main()
{
  @autoreleasepool {

    NSArray *coeff = @[@-19.0, @7.0, @-4.0, @6.0];
    printf("%f\n", [coeff horner: 3.0]);

  }
  return 0;
}

OCaml

# let horner coeffs x =
    List.fold_left (fun acc coef -> acc * x + coef) 0 (List.rev coeffs) ;;
val horner : int list -> int -> int = <fun>

# let coeffs = [-19; 7; -4; 6] in
  horner coeffs 3 ;;
- : int = 128

It's also possible to do fold_right instead of reversing and doing fold_left; but fold_right is not tail-recursive.

Octave

function r = horner(a, x)
  r = 0.0;
  for i = length(a):-1:1
    r = r*x + a(i);
  endfor
endfunction

horner([-19, 7, -4, 6], 3)

ooRexx

/* Rexx ---------------------------------------------------------------
* 04.03.2014 Walter Pachl
*--------------------------------------------------------------------*/
c = .array~of(-19,7,-4,6) -- coefficients of all x^0..x^n in order
n=3
x=3
r=0
loop i=n+1 to 1 by -1
  r=r*x+c[i]
  End
Say r
Say 6*x**3-4*x**2+7*x-19

Output:

128
128

Oz

declare
  fun {Horner Coeffs X}
     {FoldL1 {Reverse Coeffs} 
      fun {$ Acc Coeff}
         Acc*X + Coeff
      end}
  end
  
  fun {FoldL1 X|Xr Fun}
     {FoldL Xr Fun X}
  end
in
  {Show {Horner [~19 7 ~4 6] 3}}

PARI/GP

Also note that Pari has a polynomial type. Evaluating these is as simple as subst(P,variable(P),x).

horner(v,x)={
  my(s=0);
  forstep(i=#v,1,-1,s=s*x+v[i]);
  s
};

Pascal

Program HornerDemo(output);

function horner(a: array of double; x: double): double;
  var
    i: integer;
  begin
    horner := a[high(a)];
    for i := high(a) - 1 downto low(a) do
      horner := horner * x + a[i];
  end;

const
  poly: array [1..4] of double = (-19.0, 7.0, -4.0, 6.0);

begin
  write ('Horner calculated polynomial of 6*x^3 - 4*x^2 + 7*x - 19 for x = 3: ');
  writeln (horner (poly, 3.0):8:4);
end.

Output:

Horner calculated polynomial of 6*x^3 - 4*x^2 + 7*x - 19 for x = 3: 128.0000

Perl

use 5.10.0;
use strict;
use warnings;
 
sub horner(\@$){
	my ($coef, $x) = @_;
	my $result = 0;
	$result = $result * $x + $_ for reverse @$coef;
	return $result;
}
 
my @coeff = (-19.0, 7, -4, 6);
my $x = 3;
say horner @coeff, $x;

Functional version

use strict;
use List::Util qw(reduce);

sub horner($$){
	my ($coeff_ref, $x) = @_;
	reduce { $a * $x + $b } reverse @$coeff_ref;
}

my @coeff = (-19.0, 7, -4, 6);
my $x = 3;
print horner(\@coeff, $x), "\n";

Recursive version

sub horner {
    my ($coeff, $x) = @_;
    @$coeff and
    $$coeff[0] + $x * horner( [@$coeff[1 .. $#$coeff]], $x ) 
}
 
print horner( [ -19, 7, -4, 6 ], 3 );

Phix

with javascript_semantics
function horner(atom x, sequence coeff)
    atom res = 0
    for i=length(coeff) to 1 by -1 do
        res = res*x + coeff[i]
    end for
    return res
end function
 
?horner(3,{-19, 7, -4, 6})
Output:
128

PHP

<?php
function horner($coeff, $x) {
    $result = 0;
    foreach (array_reverse($coeff) as $c)
        $result = $result * $x + $c;
    return $result;
}
 
$coeff = array(-19.0, 7, -4, 6);
$x = 3;
echo horner($coeff, $x), "\n";
?>

Functional version

Works with: PHP version 5.3+
<?php
function horner($coeff, $x) {
    return array_reduce(array_reverse($coeff), function ($a, $b) use ($x) { return $a * $x + $b; }, 0);
}

$coeff = array(-19.0, 7, -4, 6);
$x = 3;
echo horner($coeff, $x), "\n";
?>

Picat

Recursion

horner([],_X,0).
horner([H|T],X,V) :-
  horner(T,X,V1),
  V = V1 * X + H.

Iterative

horner2(Coeff, X, V) => 
  Acc = 0,
  foreach(I in Coeff.length..-1..1) 
     Acc := Acc*X + Coeff[I]
  end,
  V = Acc.

Functional approach

h3(X,A,B) = A+B*X.
horner3(Coeff, X) = fold($h3(X),0,Coeff.reverse()).

Test

go =>
  horner([-19, 7, -4, 6], 3, V),
  println(V),
  
  horner2([-19, 7, -4, 6], 3, V2),
  println(V2),
  
  V3 = horner3([-19, 7, -4, 6], 3),
  println(V3),
  nl.
Output:
128
128
128

PicoLisp

(de horner (Coeffs X)
   (let Res 0
      (for C (reverse Coeffs)
         (setq Res (+ C (* X Res))) ) ) )
: (horner (-19.0 7.0 -4.0 6.0) 3.0)
-> 128

PL/I

declare (i, n) fixed binary, (x, value) float; /* 11 May 2010 */
get (x);
get (n);
begin;
   declare a(0:n) float;
   get list (a);
   value = a(n);
   do i = n to 1 by -1;
      value = value*x + a(i-1);
   end;
   put (value);
end;

Potion

horner = (x, coef) :
   result = 0
   coef reverse each (a) :
      result = (result * x) + a
   .
   result
.

horner(3, (-19, 7, -4, 6)) print

PowerShell

Works with: PowerShell version 4.0
function horner($coefficients, $x) {
    $accumulator = 0
    foreach($i in ($coefficients.Count-1)..0){ 
        $accumulator = ( $accumulator * $x ) + $coefficients[$i]
    }
    $accumulator
}
$coefficients = @(-19, 7, -4, 6)
$x = 3
horner $coefficients $x

Output:

 
128

Prolog

Tested with SWI-Prolog. Works with other dialects.

horner([], _X, 0).

horner([H|T], X, V) :-
	horner(T, X, V1),
	V is V1 * X + H.

Output :

 ?- horner([-19, 7, -4, 6], 3, V).
V = 128.

Functional approach

Works with SWI-Prolog and module lambda, written by Ulrich Neumerkel found there http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl

:- use_module(library(lambda)).


% foldr(Pred, Init, List, R).
%
foldr(_Pred, Val, [], Val).
foldr(Pred, Val, [H | T], Res) :-
	foldr(Pred, Val, T, Res1),
	call(Pred, Res1, H, Res).

f_horner(L, V, R) :-
	foldr(\X^Y^Z^(Z is X * V + Y), 0, L, R).

Functional syntax (Ciao)

Works with Ciao (https://github.com/ciao-lang/ciao) and the fsyntax package:

:- module(_, [horner/3], [fsyntax, hiord]).
:- use_module(library(hiordlib)).
horner(L, X) := ~foldr((''(H,V0,V) :- V is V0*X + H), L, 0).

PureBasic

Procedure Horner(List Coefficients(), b)
  Define result
  ForEach Coefficients()
    result*b+Coefficients()
  Next
  ProcedureReturn result
EndProcedure

Implemented as

NewList a()
AddElement(a()): a()=  6
AddElement(a()): a()= -4
AddElement(a()): a()=  7
AddElement(a()): a()=-19
Debug Horner(a(),3)

Outputs

128

Python

>>> def horner(coeffs, x):
	acc = 0
	for c in reversed(coeffs):
		acc = acc * x + c
	return acc

>>> horner( (-19, 7, -4, 6), 3)
128

Functional version

>>> try: from functools import reduce
except: pass

>>> def horner(coeffs, x):
	return reduce(lambda acc, c: acc * x + c, reversed(coeffs), 0)

>>> horner( (-19, 7, -4, 6), 3)
128

Library: NumPy

>>> import numpy
>>> numpy.polynomial.polynomial.polyval(3, (-19, 7, -4, 6))
128.0

R

Procedural style:

horner <- function(a, x) {
  y <- 0
  for(c in rev(a)) {
    y <- y * x + c
  }
  y
}

cat(horner(c(-19, 7, -4, 6), 3), "\n")

Functional style:

horner <- function(x, v) {
  Reduce(v, right=T, f=function(a, b) {
    b * x + a
  })
}
Output:
> v <- c(-19, 7, -4, 6)
> horner(3, v)
[1] 128

Racket

Translated from Haskell

#lang racket
(define (horner x l) 
    (foldr (lambda (a b) (+ a (* b x))) 0 l))

(horner 3 '(-19 7 -4 6))

Raku

(formerly Perl 6)

sub horner ( @coeffs, $x ) {
    @coeffs.reverse.reduce: { $^a * $x + $^b };
}

say horner( [ -19, 7, -4, 6 ], 3 );

A recursive version would spare us the need for reversing the list of coefficients. However, special care must be taken in order to write it, because the way Raku implements lists is not optimized for this kind of treatment. Lisp-style lists are, and fortunately it is possible to emulate them with Pairs and the reduction meta-operator:

multi horner(Numeric $c, $) { $c }
multi horner(Pair $c, $x) {
    $c.key + $x * horner( $c.value, $x ) 
}
 
say horner( [=>](-19, 7, -4, 6 ), 3 );

We can also use the composition operator:

sub horner ( @coeffs, $x ) {
    ([o] map { $_ + $x * * }, @coeffs)(0);
}
 
say horner( [ -19, 7, -4, 6 ], 3 );
Output:
128

One advantage of using the composition operator is that it allows for the use of an infinite list of coefficients.

sub horner ( @coeffs, $x ) {
    map { .(0) }, [\o] map { $_ + $x * * }, @coeffs;
}
 
say horner( [ 1 X/ (1, |[\*] 1 .. *) ], i*pi )[20];
Output:
-0.999999999924349-5.28918515954219e-10i

Rascal

import List;

public int horners_rule(list[int] coefficients, int x){
	acc = 0;
	for( i <- reverse(coefficients)){
		acc = acc * x + i;}
	return acc;
}

A neater and shorter solution using a reducer:

public int horners_rule2(list[int] coefficients, int x) = (0 | it * x + c | c <- reverse(coefficients));

Output:

rascal>horners_rule([-19, 7, -4, 6], 3)
int: 128

rascal>horners_rule2([-19, 7, -4, 6], 3)
int: 128

REBOL

REBOL []

horner: func [coeffs x] [
    result: 0
    foreach i reverse coeffs [
        result: (result * x) + i
        ]
    return result
    ]

print horner [-19 7 -4 6] 3

REXX

version 1

/*REXX program  demonstrates using    Horner's rule    for   polynomial evaluation.     */
numeric digits 30                                /*use extra numeric precision.         */
parse  arg  x poly                               /*get value of X and the coefficients. */
$=                                               /*start with a clean slate equation.   */
       do deg=0  until  poly==''                 /*get the equation's coefficients.     */
       parse var poly c.deg poly;  c.deg=c.deg/1 /*get equation coefficient & normalize.*/
       if c.deg>=0  then c.deg= '+'c.deg         /*if ¬ negative, then prefix with a  + */
       $=$  c.deg                                /*concatenate it to the equation.      */
       if deg\==0 & c.deg\=0  then $=$'∙x^'deg   /*¬1st coefficient & ¬0?  Append X pow.*/
       $=$ '  '                                  /*insert some blanks, make it look nice*/
       end   /*deg*/
say '         x = '   x
say '    degree = '  deg
say '  equation = '   $
a=c.deg                                          /*A:  is the accumulator  (or answer). */
         do j=deg-1  by -1  for deg;   a=a*x+c.j /*apply Horner's rule to the equations.*/
         end   /*j*/
say                                              /*display a blank line for readability.*/
say '    answer = ' a                            /*stick a fork in it,  we're all done. */

output   when the following is used for input:   3   -19   7   -4   6

         x =  3
    degree =  3
  equation =   -19    +7∙x^1    -4∙x^2    +6∙x^3

    answer =  128

version 2

/* REXX ---------------------------------------------------------------
* 27.07.2012 Walter Pachl
*            coefficients reversed to descending order of power
*            I'm used to x**2+x-3
*            equation formatting prettified (coefficients 1 and 0)
*--------------------------------------------------------------------*/
  Numeric Digits 30                /* use extra numeric precision.   */
  Parse Arg x poly                 /* get value of x and coefficients*/
  rpoly=''
  Do p=0 To words(poly)-1
    rpoly=rpoly word(poly,words(poly)-p)
    End
  poly=rpoly
  equ=''                           /* start with equation clean slate*/
  deg=words(poly)-1
  pdeg=deg
  Do Until deg<0                   /* get the equation's coefficients*/
    Parse Var poly c.deg poly      /* in descending order of powers  */
    c.deg=c.deg+0                  /* normalize it                   */
    If c.deg>0 & deg<pdeg Then     /* positive and not first term    */
      prefix='+'                   /*  prefix a + sign.              */
    Else prefix=''
    Select
      When deg=0 Then term=c.deg
      When deg=1 Then
        If c.deg=1 Then term='x'
                   Else term=c.deg'*x'
      Otherwise
        If c.deg=1 Then term='x^'deg
                   Else term=c.deg'*x^'deg
      End
    If c.deg<>0 Then               /* build up the equation          */
      equ=equ||prefix||term
    deg=deg-1
    End
  a=c.pdeg
  Do p=pdeg To 1 By -1             /* apply Horner's rule.           */
    pm1=p-1
    a=a*x+c.pm1
    End
  Say '        x = ' x
  Say '   degree = ' pdeg
  Say ' equation = ' equ
  Say ' '
  Say '   result = ' a
Output:
        x =  3
   degree =  3
 equation =  6*x^3-4*x^2+7*x-19

   result =  128

Ring

coefficients = [-19, 7, -4, 6] 
see "x =  3" + nl +
"degree =  3" + nl +
"equation =  6*x^3-4*x^2+7*x-19" + nl +
"result = " + horner(coefficients, 3) + nl
 
func horner coeffs, x
w = 0
for n = len(coeffs) to 1 step -1
    w = w * x + coeffs[n]
next
return w

Output:

x =  3
degree =  3
equation =  6*x^3-4*x^2+7*x-19
result = 128

RLaB

RLaB implements horner's scheme for polynomial evaluation in its built-in function polyval. What is important is that RLaB stores the polynomials as row vectors starting from the highest power just as matlab and octave do.

This said, solution to the problem is

>> a = [6, -4, 7, -19]
           6            -4             7           -19
>> x=3
           3
>> polyval(x, a)
         128

RPL

Translation of the algorithm

Following the pseudocode given here to the letter:

≪ OVER DUP SIZE GET → a x0 p
  ≪  a SIZE 1 - 1 FOR j 
         'a(j)+x0*p' EVAL 'p' STO -1 STEP
      p
≫ ≫ ‘HORNR’ STO

Idiomatic one-liner

Reducing the loop to its simplest form: one memory call, one multiplication and one addition.

≪ → x0 ≪ LIST→ 2 SWAP START x0 * + NEXT ≫ ≫ ‘HORNR’ STO
Input:
{ -19 7 -4 6 } 3 HORNR
Output:
1: 128

Ruby

def horner(coeffs, x)
  coeffs.reverse.inject(0) {|acc, coeff| acc * x + coeff}
end
p horner([-19, 7, -4, 6], 3)  # ==> 128

Run BASIC

coef$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
print horner(coef$,x)                     '128
print horner("1.2 2.3 3.4 4.5 5.6", 8)    '25478.8
print horner("5 4 3 2 1", 10)             '12345
print horner("1 0 1 1 1 0 0 1", 2)        '157
end

function horner(coef$,x)
  while word$(coef$, i + 1) <> ""        
     i = i + 1                          ' count the num of values
  wend
  for j = i to 1 step -1
    accum = ( accum * x ) + val(word$(coef$, j))
  next
  horner = accum
end function

Rust

fn horner(v: &[f64], x: f64) -> f64 {
    v.iter().rev().fold(0.0, |acc, coeff| acc*x + coeff)
}

fn main() {
    let v = [-19., 7., -4., 6.];
    println!("result: {}", horner(&v, 3.0));
}

A generic version that works with any number type and much more. So much more, it's hard to imagine what that may be useful for.

extern crate num; // 0.2.0
use num::Zero;
use std::ops::{Add, Mul};

fn horner<Arr, Arg, Out>(v: &[Arr], x: Arg) -> Out
where
    Arr: Clone,
    Arg: Clone,
    Out: Zero + Mul<Arg, Output = Out> + Add<Arr, Output = Out>,
{
    v.iter()
        .rev()
        .fold(Zero::zero(), |acc, coeff| acc * x.clone() + coeff.clone())
}

fn main() {
    let v = [-19., 7., -4., 6.];
    let output: f64 = horner(&v, 3.0);
    println!("result: {}", output);
}

Sather

class MAIN is
  
  action(s, e, x:FLT):FLT is
    return s*x + e;
  end;

  horner(v:ARRAY{FLT}, x:FLT):FLT is
    rv ::= v.reverse;
    return rv.reduce(bind(action(_, _, x)));
  end;

  main is
    #OUT + horner(|-19.0, 7.0, -4.0, 6.0|, 3.0) + "\n";
  end;
end;

Scala

def horner(coeffs:List[Double], x:Double)=
   coeffs.reverse.foldLeft(0.0){(a,c)=> a*x+c}
val coeffs=List(-19.0, 7.0, -4.0, 6.0)
println(horner(coeffs, 3))
   -> 128.0

Scheme

Works with: Scheme version RRS
(define (horner lst x)
  (define (*horner lst x acc)
    (if (null? lst)
        acc
        (*horner (cdr lst) x (+ (* acc x) (car lst)))))
  (*horner (reverse lst) x 0))

(display (horner (list -19 7 -4 6) 3))
(newline)

Output:

128

Seed7

$ include "seed7_05.s7i";
  include "float.s7i";

const type: coeffType is array float;

const func float: horner (in coeffType: coeffs, in float: x) is func
  result
    var float: res is 0.0;
  local
    var integer: i is 0;
  begin
    for i range length(coeffs) downto 1 do
      res := res * x + coeffs[i];
    end for;
  end func;

const proc: main is func
  local
    const coeffType: coeffs is [] (-19.0, 7.0, -4.0, 6.0);
  begin
    writeln(horner(coeffs, 3.0) digits 1);
  end func;

Output:

128.0

Sidef

Functional:

func horner(coeff, x) {
    coeff.reverse.reduce { |a,b| a*x + b };
}

say horner([-19, 7, -4, 6], 3);   # => 128

Recursive:

func horner(coeff, x) {
    (coeff.len > 0) \
        ? (coeff[0] + x*horner(coeff.last(-1), x))
        : 0
}

say horner([-19, 7, -4, 6], 3)   # => 128

Smalltalk

Works with: GNU Smalltalk
OrderedCollection extend [
  horner: aValue [
    ^ self reverse inject: 0 into: [:acc :c | acc * aValue + c].
  ]
].

(#(-19 7 -4 6) asOrderedCollection horner: 3) displayNl.

Standard ML

(* Assuming real type for coefficients and x *)
fun horner coeffList x = foldr (fn (a, b) => a + b * x) (0.0) coeffList

Swift

func horner(coefs: [Double], x: Double) -> Double {
  return reduce(lazy(coefs).reverse(), 0) { $0 * x + $1 }
}

println(horner([-19, 7, -4, 6], 3))
Output:
128.0

Tcl

package require Tcl 8.5
proc horner {coeffs x} {
    set y 0
    foreach c [lreverse $coeffs] {
        set y [expr { $y*$x+$c }]
    }
    return $y
}

Demonstrating:

puts [horner {-19 7 -4 6} 3]

Output:

128

VBA

Note: this function, "Horner", gets its coefficients as a ParamArray which has no specified length. This array collect all arguments after the first one(s). This means you must specify x first, then the coefficients.

Public Function Horner(x, ParamArray coeff())
Dim result As Double
Dim ncoeff As Integer

result = 0
ncoeff = UBound(coeff())

For i = ncoeff To 0 Step -1
  result = (result * x) + coeff(i)
Next i
Horner = result
End Function

Output:

print Horner(3, -19, 7, -4, 6)
 128 

VBScript

Function horners_rule(coefficients,x)
	accumulator = 0
	For i = UBound(coefficients) To 0 Step -1
		accumulator = (accumulator * x) + coefficients(i)
	Next
	horners_rule = accumulator
End Function

WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)
Output:
128

Visual Basic .NET

Translation of: C#
Module Module1

    Function Horner(coefficients As Double(), variable As Double) As Double
        Return coefficients.Reverse().Aggregate(Function(acc, coeff) acc * variable + coeff)
    End Function

    Sub Main()
        Console.WriteLine(Horner({-19.0, 7.0, -4.0, 6.0}, 3.0))
    End Sub

End Module
Output:
128

Visual FoxPro

Coefficients in ascending order.

LOCAL x As Double
LOCAL ARRAY aCoeffs[1]
CLEAR
CREATE CURSOR coeffs (c1 I, c2 I, c3 I, c4 I)
INSERT INTO coeffs VALUES (-19,7,-4,6)
SCATTER TO aCoeffs
x = VAL(INPUTBOX("Value of x:", "Value"))
? EvalPoly(@aCoeffs, x)
USE IN coeffs

FUNCTION EvalPoly(c, x As Double) As Double
LOCAL s As Double, k As Integer, n As Integer
n = ALEN(c)
s = 0
FOR k = n TO 1 STEP -1
	s = s*x + c[k]
ENDFOR
RETURN s
ENDFUNC

Coefficients in descending order.

LOCAL x As Double
LOCAL ARRAY aCoeffs[1]
CLEAR
CREATE CURSOR tmp (c1 I, c2 I, c3 I, c4 I)
INSERT INTO tmp VALUES (6,-4,7,-19)
SCATTER TO aCoeffs
x = VAL(INPUTBOX("Value of x:", "Value"))
? EvalPolyDesc(@aCoeffs, x)
USE IN tmp

FUNCTION EvalPolyDesc(c, x As Double) As Double
LOCAL s As Double, e
s = 0
FOR EACH e IN c FOXOBJECT
	s = s*x + e
ENDFOR	
RETURN s
ENDFUNC

V (Vlang)

fn horner(x i64, c []i64) i64 {
    mut acc := i64(0)
    for i := c.len - 1; i >= 0; i-- {
        acc = acc*x + c[i]
    }
    return acc
}
 
fn main() {
    println(horner(3, [i64(-19), 7, -4, 6]))
}
Output:
128

Wren

var horner = Fn.new { |x, c|
    var count = c.count
    if (count == 0) return 0
    return (count-1..0).reduce(0) { |acc, index| acc*x + c[index] }
}

System.print(horner.call(3, [-19, 7, -4, 6]))
Output:
128

XPL0

code IntOut=11;

func Horner(X, N, C);   \Return value of polynomial in X
int X, N, C;            \variable, number of terms, coefficient list
int A, I;
[A:= 0;
for I:= N-1 downto 0 do
        A:= A*X + C(I);
return A;
];

IntOut(0, Horner(3, 4, [-19, 7, -4, 6]));

Output:

128

zkl

fcn horner(coeffs,x)
   { coeffs.reverse().reduce('wrap(a,coeff){ a*x + coeff },0.0) }
Output:
horner(T(-19,7,-4,6), 3).println();
128