Horner's rule for polynomial evaluation: Difference between revisions

Added 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) \
&&? (coeff[0] + x*horner(coeff.ftlast(-1), x));
: 0
}
 
say horner([-19, 7, -4, 6], 3); # => 128</syntaxhighlight>
 
=={{header|Smalltalk}}==
Line 2,608 ⟶ 2,646:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var horner = Fn.new { |x, c|
var count = c.count
if (count == 0) return 0
2,064

edits