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.



procedure main()
   showroot(125,3)
   showroot(27,3)
   showroot(1024,10)
   showroot(39.0625,4)
   showroot(7131.5^10,10)
end
 
procedure showroot(a,n)
   printf("%i-th root of %i = %i\n",n,a,root(a,n))
end
 
procedure root(a,n,p) #: finds the n-th root of the number a to precision p
   if n < 0 | type(n) !== "integer" then runerr(101,n)     
   if a < 0 then runerr(205,a)   
   /p := 1e-14                  # precision
   xn := a / real(n)            # initial guess
   while abs(a - xn^n) > p do       
      xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
   return xn
end
 
link printf