Horner's rule for polynomial evaluation: Difference between revisions

Added Easylang
(Added various dialects BASIC (Applesoft BASIC, BASIC256, Chipmunk Basic, GW-BASIC, Minimal BASIC, MSX Basic, QBasic, Quite BASIC and Yabasic))
(Added Easylang)
 
(4 intermediate revisions by 4 users not shown)
Line 352:
<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}}===
Line 754 ⟶ 783:
<syntaxhighlight lang="e">? makeHornerPolynomial([-19, 7, -4, 6])(3)
# 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}}==
Line 2,277 ⟶ 2,322:
 
</syntaxhighlight>
 
=={{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}}==
Line 2,414 ⟶ 2,480:
Recursive:
<syntaxhighlight lang="ruby">func horner(coeff, x) {
(coeff.len > 0) \
&&? (coeff[0] + x*horner(coeff.ftlast(-1), x));
: 0
}
 
say horner([-19, 7, -4, 6], 3); # => 128</syntaxhighlight>
 
=={{header|Smalltalk}}==
Line 2,579 ⟶ 2,646:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var horner = Fn.new { |x, c|
var count = c.count
if (count == 0) return 0
2,056

edits