Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
47
Task/Nth-root/Modula-2/nth-root.mod2
Normal file
47
Task/Nth-root/Modula-2/nth-root.mod2
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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.
|
||||
Loading…
Add table
Add a link
Reference in a new issue