RosettaCodeData/Task/Nth-root/ANSI-BASIC/nth-root.basic

24 lines
764 B
Plaintext

100 REM Nth root
110 DECLARE EXTERNAL FUNCTION NthRoot
120 LET X = 144
130 PRINT "Finding the nth root of"; X; "to 6 decimal places"
140 PRINT " x n root x ^ (1 / n)"
150 PRINT "--------------------------------------"
160 FOR I = 1 TO 8
170 PRINT USING "### ": X;
180 PRINT USING "#### ": I;
190 PRINT USING "###.######": NthRoot(I, X, 1.000000E-07);
200 PRINT USING " ###.######": X ^ (1 / I)
210 NEXT I
220 END
230 EXTERNAL FUNCTION NthRoot(N, X, Precision)
240 REM Returns the Nth root of value X to stated Precision
250 LET X0 = X
260 LET X1 = X / N ! initial guess
270 DO WHILE ABS(X1 - X0) > Precision
280 LET X0 = X1
290 LET X1 = ((N - 1) * X1 + X / X1 ^ (N - 1)) / N
300 LOOP
310 LET NthRoot = X1
320 END FUNCTION