A fast scheme for evaluating a polynomial such as:

 



−
19
+
7
x
−
4

x

2


+
6

x

3





{\displaystyle -19+7x-4x^{2}+6x^{3}\,}


when

 



x
=
3



{\displaystyle x=3\;}

.
is to arrange the computation as follows:

 



(
(
(
(
0
)
x
+
6
)
x
+
(
−
4
)
)
x
+
7
)
x
+
(
−
19
)



{\displaystyle ((((0)x+6)x+(-4))x+7)x+(-19)\;}


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

 
[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
 
 