This is a sub-page of a longer article, The classical Chinese version of Horner’s method: Technical considerations.
This program extracts the positive root of a Wang Xiaotong cubic by the Chinese version of Horner’s Method. It is written in the Basic programming language and has been tested using the Chipmunk Basic interpreter. Basic is hardly to be recommended for serious programming, but it is simple enough that readers who have learned any programming language should be able to understand the program.
10 ' Initialization.
20 '
30 epsilon = 0.001 ' Desired precision of result.
40 root = 0
50 '
60 ' Subroutine to reduce the roots of the equation by the value of a proposed digit.
70 '
80 sub chu(dv)
90 a = a+dv
100 b = b+a*dv
110 c = c-b*dv
120 a = a+dv
130 b = b+a*dv
140 a = a+dv
150 end sub
160 '
170 ' Ask for the coefficients of the equation.
180 '
190 print "coefficients"
200 input a,b,c
210 '
220 ' Propose a digit.
230 '
240 cq = c^(1/3)
250 order = 10^floor(log10(cq)+1)
260 d = 1
270 '
280 ' Reduce the roots of the equation by the value of the proposed digit.
290 '
300 chu(d*order)
310 '
320 ' If the proposed digit was too large, undo the previous action, propose a
321 ' smaller digit and go back to try again.
330 '
340 if c < 0
350 chu(-d*order)
360 d = d-1
370 if d = 0
380 order = order/10
390 d = 9
400 endif
410 goto 300
420 endif
430 '
440 ' Add the value of the digit to the accumulated result.
450 '
460 root = root+d*order
470 '
480 ' If the desired precision has not been reached, continue proposing digits.
490 '
500 if c > epsilon then goto 240
510 '
520 ' Print the final result and exit.
530 '
540 print "root=", root
550 end