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.



with javascript_semantics
function pow_(atom x, integer e)
    atom r = 1
    for i=1 to e do
        r *= x
    end for
    return r
end function
 
function nth_root(atom y, n)
    atom eps = 1e-15,   -- relative accuracy
         x = 1
    while 1 do
--      atom d = ( y / power(x,n-1) - x ) / n
        atom d = ( y / pow_(x,n-1) - x ) / n
        x += d
        atom e = eps*x   -- absolute accuracy       
        if d > -e and d < e then exit end if
    end while 
    return x
end function

procedure test(sequence yn)
    atom {y,n} = yn
    printf(1,"nth_root(%d,%d) = %.10g, builtin = %.10g\n",{y,n,nth_root(y,n),power(y,1/n)})
end procedure
papply({{1024,10},{27,3},{2,2},{5642,125},{4913,3},{8,3},{16,2},{16,4},{125,3},{1000000000,3},{1000000000,9}},test)
