48 lines
1.0 KiB
Plaintext
48 lines
1.0 KiB
Plaintext
MODULE NthRoot;
|
|
FROM LongMath IMPORT
|
|
power;
|
|
FROM STextIO IMPORT
|
|
WriteString, WriteLn;
|
|
FROM SLongIO IMPORT
|
|
WriteFixed;
|
|
FROM SWholeIO IMPORT
|
|
WriteInt;
|
|
|
|
VAR
|
|
X: LONGREAL;
|
|
I: CARDINAL;
|
|
|
|
PROCEDURE Root(X: LONGREAL; N: CARDINAL; Precision: LONGREAL): LONGREAL;
|
|
(* Returns the Nth root of value X to stated Precision *)
|
|
VAR
|
|
X0, X1, NR: LONGREAL;
|
|
BEGIN
|
|
NR := FLOAT(N);
|
|
X0 := X;
|
|
X1 := X / NR; (* initial guess *)
|
|
WHILE ABS(X1 - X0) > Precision DO
|
|
X0 := X1;
|
|
X1 := ((NR - 1.0) * X1 + X / power(X1, NR - 1.0)) / NR
|
|
END;
|
|
RETURN X1
|
|
END Root;
|
|
|
|
BEGIN
|
|
X := 144.0;
|
|
WriteString("Finding the nth root of ");
|
|
WriteFixed(X, 1, 5);
|
|
WriteString(" to 6 decimal places");
|
|
WriteLn;
|
|
WriteString(" x n root x ^ (1 / n)");
|
|
WriteLn;
|
|
WriteString("----------------------------------------");
|
|
WriteLn;
|
|
FOR I := 1 TO 8 DO
|
|
WriteFixed(X, 1, 5);
|
|
WriteInt(I, 7);
|
|
WriteFixed(Root(X, I, 1.0E-07), 6, 14);
|
|
WriteFixed(power(X, 1. / FLOAT(I)), 6, 14);
|
|
WriteLn
|
|
END
|
|
END NthRoot.
|