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.



 
print "First estimate is: ",        using( "#.###############",  NthRoot( 125, 5642, 0.001  ));
print "    ... and better is: ",    using( "#.###############",  NthRoot( 125, 5642, 0.00001))
print "125'th root of 5642 by LB's exponentiation operator is "; using( "#.###############", 5642^(1 /125))
 
print "27^(1 / 3)",                 using( "#.###############",  NthRoot(   3,   27, 0.00001))
print "2^(1 / 2)",                  using( "#.###############",  NthRoot(   2,    2, 0.00001))
print "1024^(1 /10)",               using( "#.###############",  NthRoot(  10, 1024, 0.00001))
 
wait
 
function NthRoot( n, A, p)
  x( 0) =A
  x( 1) =A /n
  while abs( x( 1) -x( 0)) >p
    x( 0) =x( 1)
    x( 1) =( ( n -1.0) *x( 1) +A /x( 1)^( n -1.0)) /n
  wend
  NthRoot =x( 1)
end function
 
end
 
 