RosettaCodeData/Task/Nth-root/Icon/nth-root.icon
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

23 lines
587 B
Text

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