Horner's rule for polynomial evaluation: Difference between revisions

m (syntax highlighting fixup automation)
Line 1,065:
console.log(horner([-19,7,-4,6],3)); // ==> 128
</syntaxhighlight>
 
=={{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}}==
2,442

edits