Horner's rule for polynomial evaluation: Difference between revisions

m
alphabetized list; minor markup edits to rexx sample outputs
(→‎E: new example)
m (alphabetized list; minor markup edits to rexx sample outputs)
Line 624:
>>> horner( (-19, 7, -4, 6), 3)
128</lang>
 
=={{header|R}}==
<lang r>horner <- function(a, x) {
iv <- 0
for(i in length(a):1) {
iv <- iv * x + a[i]
}
iv
 
cat(horner(c(-19, 7, -4, 6), 3), "\n")</lang>
 
=={{header|REXX}}==
<lang rexx>/*REXX program using Horner's rule for polynomial evaulation. */
<lang rexx>
/*REXX program using Horner's rule for polynomial evaulation. */
 
arg 'X=' x poly /*get value of X and coefficients*/
Line 653 ⟶ 663:
end
 
say ' answer=' a</lang>
</lang>
Output when the following is used for input:
x=3 -19 7 -4 6
<pre style="height:12ex;overflow:scroll">
x= 3
degree= 3
equation= -19 +7 x^1 -4 x^2 +6 x^3
answer= 128
</pre>
 
Output when the following is used for input: <code>x=3 -19 7 -4 6</code>
=={{header|R}}==
x= 3
<lang r>horner <- function(a, x) {
iv <-degree= 03
equation= -19 +7 x^1 -4 x^2 +6 x^3
for(i in length(a):1) {
answer= 128
iv <- iv * x + a[i]
}
iv
 
cat(horner(c(-19, 7, -4, 6), 3), "\n")</lang>
 
=={{header|RLaB}}==
Line 698 ⟶ 694:
p horner([-19, 7, -4, 6], 3) # ==> 128</lang>
 
=={{header|Scala}}==
<lang scala>def horner(coeffs:List[Double], x:Double)=
coeffs.reverse.foldLeft(0.0){(a,c)=> a*x+c}
</lang>
<lang scala>val coeffs=List(-19.0, 7.0, -4.0, 6.0)
println(horner(coeffs, 3))
-> 128.0
</lang>
=={{header|Sather}}==
<lang sather>class MAIN is
Line 722 ⟶ 710:
end;
end;</lang>
 
=={{header|Scala}}==
<lang scala>def horner(coeffs:List[Double], x:Double)=
coeffs.reverse.foldLeft(0.0){(a,c)=> a*x+c}
</lang rexx>
<lang scala>val coeffs=List(-19.0, 7.0, -4.0, 6.0)
println(horner(coeffs, 3))
-> 128.0
</lang>
 
=={{header|Scheme}}==
1,150

edits