Horner's rule for polynomial evaluation: Difference between revisions

Line 824:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
'''Imperative''':
<lang julia>function horner(coefs, x)
sums = coefcoefs[end]
function horner(coef,x)
for k in length(coefs)-1:-1:1
sum = coef[end]
for k s = length(coef)-1:-1:1coefs[k] + x * s
sumend
sum = coef[k] + x*sum
return ends
end
sum
 
end</lang>
<lang julia>julia>@show horner([-19, 7, -4, 6], 3)</lang>
Output:
 
<lang julia>julia> horner([-19,7,-4,6], 3)
{{out}}
128</lang>
<pre>horner([-19, 7, -4, 6], 3) = 128</pre>
 
'''Functional''':
<lang julia>horner2(coefcoefs, x) = foldr((u, v) -> u + x * v, 0, coefcoefs)</lang>
 
Output:
<lang julia>julia>@show horner2([-19, 7, -4, 6], 3)</lang>
 
128</lang>
{{out}}
<pre>horner2([-19, 7, -4, 6], 3) = 128</pre>
 
=={{header|K}}==
Anonymous user