Horner's rule for polynomial evaluation: Difference between revisions
Horner's rule for polynomial evaluation (view source)
Revision as of 05:39, 4 February 2011
, 13 years agoalphabetized 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▼
equation= -19 +7 x^1 -4 x^2 +6 x^3▼
answer= 128▼
▲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) {
▲ 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 scala>val coeffs=List(-19.0, 7.0, -4.0, 6.0)▼
println(horner(coeffs, 3))▼
-> 128.0▼
=={{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 scala>val coeffs=List(-19.0, 7.0, -4.0, 6.0)
▲println(horner(coeffs, 3))
▲ -> 128.0
▲</lang>
=={{header|Scheme}}==
|