September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
33
Task/Nth-root/Phix/nth-root.phix
Normal file
33
Task/Nth-root/Phix/nth-root.phix
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
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
|
||||
atom 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 {y,n,x,power(y,1/n)}
|
||||
end function
|
||||
|
||||
?nth_root(1024,10)
|
||||
?nth_root(27,3)
|
||||
?nth_root(2,2)
|
||||
?nth_root(5642,125)
|
||||
--?nth_root(7,0.5) -- needs power(), not pow_()
|
||||
?nth_root(4913,3)
|
||||
?nth_root(8,3)
|
||||
?nth_root(16,2)
|
||||
?nth_root(16,4)
|
||||
?nth_root(125,3)
|
||||
?nth_root(1000000000,3)
|
||||
?nth_root(1000000000,9)
|
||||
Loading…
Add table
Add a link
Reference in a new issue