Horner's rule for polynomial evaluation: Difference between revisions
Content added Content deleted
No edit summary |
(Added various dialects BASIC (Applesoft BASIC, BASIC256, Chipmunk Basic, GW-BASIC, Minimal BASIC, MSX Basic, QBasic, Quite BASIC and Yabasic)) |
||
Line 241: | Line 241: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
<syntaxhighlight lang="rebol">horner: function [coeffs, x][ |
<syntaxhighlight lang="rebol">horner: function [coeffs, x][ |
||
result: 0 |
result: 0 |
||
Line 250: | Line 249: | ||
print horner @[neg 19, 7, neg 4, 6] 3</syntaxhighlight> |
print horner @[neg 19, 7, neg 4, 6] 3</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>128</pre> |
<pre>128</pre> |
||
Line 294: | Line 291: | ||
128 |
128 |
||
</pre> |
</pre> |
||
=={{header|BASIC}}== |
|||
==={{header|Applesoft BASIC}}=== |
|||
{{works with|Chipmunk Basic}} |
|||
{{works with|GW-BASIC}} |
|||
<syntaxhighlight lang="qbasic">100 HOME : REM 100 CLS for Chipmunk Basic and GW-BASIC |
|||
100 CLS : REM 100 HOME for Applesoft BASIC |
|||
110 X = 3 |
|||
120 DIM COEFFS(3) |
|||
130 COEFFS(0) = -19 |
|||
140 COEFFS(1) = 7 |
|||
150 COEFFS(2) = -4 |
|||
160 COEFFS(3) = 6 |
|||
170 PRINT "Horner's algorithm for the polynomial " |
|||
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: "; |
|||
190 ACCUM = 0 |
|||
200 FOR I = 3 TO 0 STEP -1 |
|||
210 ACCUM = (ACCUM*X)+COEFFS(I) |
|||
220 NEXT I |
|||
230 PRINT ACCUM |
|||
240 END</syntaxhighlight> |
|||
==={{header|BASIC256}}=== |
|||
<syntaxhighlight lang="vb">x = 3 |
|||
dim coeficientes = {-19, 7, -4, 6} |
|||
print "Horner's algorithm for the polynomial "; |
|||
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: "; |
|||
print AlgoritmoHorner(coeficientes, x) |
|||
end |
|||
function AlgoritmoHorner(coeffs, x) |
|||
acumulador = 0 |
|||
for i = coeffs[?]-1 to 0 step -1 |
|||
acumulador = (acumulador * x) + coeffs[i] |
|||
next i |
|||
return acumulador |
|||
end function</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
==={{header|Chipmunk Basic}}=== |
|||
{{works with|Chipmunk Basic|3.6.4}} |
|||
{{works with|QBasic}} |
|||
<syntaxhighlight lang="qbasic">100 CLS |
|||
110 x = 3 |
|||
120 DIM coeffs(3) |
|||
130 coeffs(0) = -19 |
|||
140 coeffs(1) = 7 |
|||
150 coeffs(2) = -4 |
|||
160 coeffs(3) = 6 |
|||
170 PRINT "Horner's algorithm for the polynomial " |
|||
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: "; |
|||
190 accum = 0 |
|||
200 FOR i = UBOUND(coeffs,1) TO 0 STEP -1 |
|||
210 accum = (accum*x)+coeffs(i) |
|||
220 NEXT i |
|||
230 PRINT accum |
|||
240 END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Horner's algorithm for the polynomial |
|||
6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: 128</pre> |
|||
==={{header|GW-BASIC}}=== |
|||
{{works with|BASICA}} |
|||
{{works with|Chipmunk Basic}} |
|||
{{works with|PC-BASIC|any}} |
|||
{{works with|QBasic}} |
|||
<syntaxhighlight lang="qbasic">100 CLS : REM 100 HOME for Applesoft BASIC |
|||
110 X = 3 |
|||
120 DIM COEFFS(3) |
|||
130 COEFFS(0) = -19 |
|||
140 COEFFS(1) = 7 |
|||
150 COEFFS(2) = -4 |
|||
160 COEFFS(3) = 6 |
|||
170 PRINT "Horner's algorithm for the polynomial " |
|||
180 PRINT "6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: "; |
|||
190 ACCUM = 0 |
|||
200 FOR I = 3 TO 0 STEP -1 |
|||
210 ACCUM = (ACCUM*X)+COEFFS(I) |
|||
220 NEXT I |
|||
230 PRINT ACCUM |
|||
240 END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as Chipmunk Basic entry.</pre> |
|||
==={{header|Minimal BASIC}}=== |
|||
{{works with|QBasic}} |
|||
{{works with|QuickBasic}} |
|||
{{works with|Applesoft BASIC}} |
|||
{{works with|BASICA}} |
|||
{{works with|Chipmunk Basic}} |
|||
{{works with|GW-BASIC}} |
|||
{{works with|MSX BASIC}} |
|||
{{works with|Just BASIC}} |
|||
{{works with|Liberty BASIC}} |
|||
{{works with|Run BASIC}} |
|||
{{works with|Yabasic}} |
|||
<syntaxhighlight lang="qbasic">20 LET X = 3 |
|||
30 DIM C(3) |
|||
40 LET C(0) = -19 |
|||
50 LET C(1) = 7 |
|||
60 LET C(2) = -4 |
|||
70 LET C(3) = 6 |
|||
80 PRINT "HORNER'S ALGORITHM FOR THE POLYNOMIAL" |
|||
90 PRINT "6*X^3 - 4*X^2 + 7*X - 19 WHEN X = 3 : "; |
|||
100 LET A = 0 |
|||
110 FOR I = 3 TO 0 STEP -1 |
|||
120 LET A = (A*X)+C(I) |
|||
130 NEXT I |
|||
140 PRINT A |
|||
150 END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as Chipmunk Basic entry.</pre> |
|||
==={{header|MSX Basic}}=== |
|||
The [[#Minimal_BASIC|Minimal BASIC]] solution works without any changes. |
|||
==={{header|QBasic}}=== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
<syntaxhighlight lang="qbasic">FUNCTION Horner (coeffs(), x) |
|||
acumulador = 0 |
|||
FOR i = UBOUND(coeffs) TO LBOUND(coeffs) STEP -1 |
|||
acumulador = (acumulador * x) + coeffs(i) |
|||
NEXT i |
|||
Horner = acumulador |
|||
END FUNCTION |
|||
x = 3 |
|||
DIM coeffs(3) |
|||
DATA -19, 7, -4, 6 |
|||
FOR a = LBOUND(coeffs) TO UBOUND(coeffs) |
|||
READ coeffs(a) |
|||
NEXT a |
|||
PRINT "Horner's algorithm for the polynomial 6*x^3 - 4*x^2 + 7*x - 19 when x = 3 is: "; |
|||
PRINT Horner(coeffs(), x) |
|||
END</syntaxhighlight> |
|||
==={{header|Quite BASIC}}=== |
|||
The [[#Minimal_BASIC|Minimal BASIC]] solution works without any changes. |
|||
==={{header|Yabasic}}=== |
|||
<syntaxhighlight lang="vb">x = 3 |
|||
dim coeffs(4) |
|||
coeffs(0) = -19 |
|||
coeffs(1) = 7 |
|||
coeffs(2) = -4 |
|||
coeffs(3) = 6 |
|||
print "Horner's algorithm for the polynomial "; |
|||
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: "; |
|||
print AlgoritmoHorner(coeffs, x) |
|||
end |
|||
sub AlgoritmoHorner(coeffs, x) |
|||
local acumulador, i |
|||
acumulador = 0 |
|||
for i = arraysize(coeffs(),1) to 0 step -1 |
|||
acumulador = (acumulador * x) + coeffs(i) |
|||
next i |
|||
return acumulador |
|||
end sub</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as FreeBASIC entry.</pre> |
|||
=={{header|Batch File}}== |
=={{header|Batch File}}== |