Task

Implement the algorithm to compute the principal   nth   root   





A

n





{\displaystyle {\sqrt[{n}]{A}}}

   of a positive real number   A,   as explained at the   Wikipedia page.



include c:\cxpl\stdlib;
 
func real NRoot(A, N);  \Return the Nth root of A
real A, N;
real X, X0, Y;
int  I;
[X:= 1.0;               \initial guess
repeat  X0:= X;
        Y:= 1.0;
        for I:= 1 to fix(N)-1 do Y:= Y*X0;
        X:= ((N-1.0)*X0 + A/Y) / N;
until   abs(X-X0) < 1.0E-15;            \(until X=X0 doesn't always work)
return X;
];
 
[Format(5, 15);
RlOut(0, NRoot( 2., 2.));  CrLf(0);
RlOut(0, Power( 2., 0.5)); CrLf(0);     \for comparison
RlOut(0, NRoot(27., 3.));  CrLf(0);
RlOut(0, NRoot(1024.,10.)); CrLf(0);
]