Horner's rule for polynomial evaluation: Difference between revisions
Horner's rule for polynomial evaluation (view source)
Revision as of 18:30, 17 March 2024
, 3 months agoAdded Easylang
(Added Gambas) |
(Added Easylang) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 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,306 ⟶ 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,443 ⟶ 2,480:
Recursive:
<syntaxhighlight lang="ruby">func horner(coeff, x) {
(coeff.len > 0) \
: 0
}
say horner([-19, 7, -4, 6], 3)
=={{header|Smalltalk}}==
Line 2,608 ⟶ 2,646:
=={{header|Wren}}==
<syntaxhighlight lang="
var count = c.count
if (count == 0) return 0
|