Re: Programming Challenge Message #17 Posted by Namir on 27 Sept 2010, 1:13 p.m., in response to message #16 by Olivier De Smet
Thanks!!! I was able to translate the 71B code into Excel VBA. Had to watch for those zigzagging GOTOs!!
Here is the VBA code:
Option Explicit
Option Base 0
' Version 1.1
Sub CalcCoeff()
Dim N As Integer, S As Double, P As Double
Dim I As Integer, J As Integer, K As Integer, M As Integer
Dim Root(99) As Double, Coeff(99) As Double, IK(99) As Integer
Dim bJump As Boolean, Row As Integer, Col As Integer
Dim aStr As String
N = 2
Do While Cells(N, 1) <> ""
Root(N - 1) = Cells(N, 1)
N = N + 1
Loop
N = N - 2 ' number of roots
Coeff(N) = 1
' display Coeff(N)
Cells(2, 2) = Coeff(N)
Range("B3:Z1000").Value = ""
Row = 3
' main loop
For K = 1 To N
Col = 3
S = 0
M = 1
IK(1) = 0
' Line 40
Do
J = IK(M) + 1
For I = M To K
IK(I) = J
J = J + 1
Next I
M = K
' Line 50
Do
P = 1
For I = 1 To K
P = P * Root(IK(I))
aStr = Cells(Row, Col)
If aStr = "" Then
Cells(Row, Col) = "r" & CStr(IK(I))
Else
Cells(Row, Col) = aStr & "*r" & CStr(IK(I))
End If
Next I
S = S + P
' L60
IK(M) = IK(M) + 1
' move column for Viete's terms to the right
If IK(M) <= N - K + M Then Col = Col + 1
Loop While IK(M) <= N - K + M
bJump = False
Do
M = M - 1
If M = 0 Then
bJump = True
Exit Do
End If
' move column for Viete's terms to the right
If IK(M) <> (N - K + M) Then Col = Col + 1
Loop While IK(M) = N - K + M
' jump out of outer loop?
If bJump Then Exit Do
Loop
' Line 200
Coeff(N - K) = S * ((-1) ^ K)
' show the coefficient value
Cells(Row, 2) = Coeff(N - K)
Row = Row + 1
Next K
End Sub
I replaced variables l, x() and a() with M, Root() and Coeff(), respectively. I also added the variable Row to display the polynomial coefficients in a descending power order. The program expects the roots to appear in column 1 starting with Row 2. The output polynomial coefficients appear in column 2, starting with row 2. Row 1 for columns 1 and 2 are reserved for labeling the columns. The output places the coefficients of the polynomial in a descending power order. Row 2 has 1 (the term for X^n), row 3 has the coefficient for X^(n-1), and so on. Row n+2 has the constant term.
The above code also shows the different terms in Viete's formula. These symbolic terms appear in columns C, D, and up. Each cell contains a set of root products. The variable Col controls which column each root product group appears.
Namir
Edited: 27 Sept 2010, 11:12 p.m.
|