Horner's rule for polynomial evaluation: Difference between revisions

{{header|Liberty BASIC}}
m (oops, wrong lang tag)
({{header|Liberty BASIC}})
Line 557:
 
show horner 3 [-19 7 -4 6] ; 128</lang>
 
=={{header|Liberty BASIC}}==
<lang lb>src$ = "Hello"
coefficients$ = "-19 7 -4 6" ' list coefficients of all x^0..x^n in order
x = 3
print horner(coefficients$, x) '128
 
print horner("4 3 2 1", 10) '1234
print horner("1 1 0 0 1", 2) '19
end
 
function horner(coefficients$, x)
accumulator = 0
'getting length of a list requires extra pass with WORD$.
'So we just started from high above
for index = 100 to 1 step -1
cft$ = word$(coefficients$, index)
if cft$<>"" then accumulator = ( accumulator * x ) + val(cft$)
next
horner = accumulator
end function
</lang>
 
=={{header|Lua}}==
<lang lua>function horners_rule( coeff, x )
Anonymous user