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.



vardef mnthroot(expr n, A) =
  x0 := A / n;
  m := n - 1;
  forever:
    x1 := (m*x0 + A/(x0 ** m)) / n;
    exitif abs(x1 - x0) < abs(x0 * 0.0001);
    x0 := x1;
  endfor;
  x1
enddef;
 
primarydef n nthroot A = mnthroot(n, A) enddef;
 
show 5 nthroot 34;  % 2.0244
show 0.5 nthroot 7; % 49.00528
 
bye