tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
23
Task/Nth-root/Icon/nth-root.icon
Normal file
23
Task/Nth-root/Icon/nth-root.icon
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue