User:Klever: Difference between revisions

Horner's rule
(→‎VBA Examples: look-and-say sequence)
(Horner's rule)
Line 15:
 
In MS Office program (Word, Excel, Access...): open the Visual Basic window. Paste the code in a module. Execute it by typing a suitable command in the Immediate Window. Output will be directed to the Immediate Window unless stated otherwise...
 
==[[Horner's rule for polynomial evaluation]]==
 
Note: this function Horner gets its coefficients in a ParamArray which has no specified length. You must specify x before the arguments.
 
<lang>
Public Function Horner(x, ParamArray coeff())
Dim result As Double
Dim ncoeff As Integer
 
result = 0
ncoeff = UBound(coeff())
 
For i = ncoeff To 0 Step -1
result = (result * x) + coeff(i)
Next i
Horner = result
End Function
</lang>
 
Output:
<pre>
print Horner2(3, -19, 7, -4, 6)
128
</pre>
 
==[[Look-and-say sequence]]==
Anonymous user