RosettaCodeData/Task/Trabb-Pardo-Knuth-algorithm/ASIC/trabb-pardo-knuth-algorithm...

79 lines
1.3 KiB
Plaintext

REM Trabb Pardo-Knuth algorithm
REM Used "magic numbers" because of strict specification of the algorithm.
DIM S@(10)
PRINT "Enter 11 numbers."
FOR I = 0 TO 10
I1= I + 1
PRINT I1;
PRINT " => ";
INPUT TMP@
S@(I) = TMP@
NEXT I
PRINT
REM Reverse
HALFIMAX = 10 / 2
FOR I = 0 TO HALFIMAX
IREV = 10 - I
TMP@ = S@(I)
S@(I) = S@(IREV)
S@(IREV) = TMP@
NEXT I
REM Results
REM Leading spaces in printed numbers are removed
CLS
FOR I = 0 TO 10
PRINT "f(";
STMP$ = STR$(S@(I))
STMP$ = LTRIM$(STMP$)
PRINT STMP$;
PRINT ") = ";
N@ = S@(I)
GOSUB CALCF:
R@ = F@
IF R@ > 400 THEN
PRINT "overflow"
ELSE
STMP$ = STR$(R@)
STMP$ = LTRIM$(STMP$)
PRINT STMP$
ENDIF
NEXT I
END
CALCF:
REM Calculates f(N@)
REM Result in F@
X@ = ABS(N@)
GOSUB CALCSQRT:
F@ = 5.0 * N@
F@ = F@ * N@
F@ = F@ * N@
F@ = F@ + SQRT@
RETURN
CALCSQRT:
REM Calculates approximate (+- 0.00001) square root of X@ for X@ >= 0 (bisection method)
REM Result in SQRT@
A@ = 0.0
IF X@ >= 1.0 THEN
B@ = X@
ELSE
B@ = 1.0
ENDIF
L@ = B@ - A@
L@ = ABS(L@)
WHILE L@ > 0.00001
MIDDLE@ = A@ + B@
MIDDLE@ = MIDDLE@ / 2
MIDDLETO2@ = MIDDLE@ * MIDDLE@
IF MIDDLETO2@ < X@ THEN
A@ = MIDDLE@
ELSE
B@ = MIDDLE@
ENDIF
L@ = B@ - A@
L@ = ABS(L@)
WEND
SQRT@ = MIDDLE@
RETURN