Nth root: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) (Added Arturo implementation) |
|||
Line 2,524: | Line 2,524: | ||
return $x[1]; |
return $x[1]; |
||
}</lang> |
}</lang> |
||
=={{header|Picat}}== |
|||
<lang Picat>go => |
|||
L = [[2,2], |
|||
[34,5], |
|||
[34**5,5], |
|||
[7131.5**10], |
|||
[7,0.5], |
|||
[1024,10], |
|||
[5642, 125] |
|||
], |
|||
foreach([A,N] in L) |
|||
R = nthroot(A,N), |
|||
printf("nthroot(%8w,%8w) %20w (check: %w)\n",A,N,R,A**(1/N)) |
|||
end, |
|||
nl. |
|||
% |
|||
% x^n = a |
|||
% |
|||
% Given a and n, find x (to Precision) |
|||
% |
|||
nthroot(A,N) = nthroot(A,N,0.000001). |
|||
nthroot(A,N,Precision) = X1 => |
|||
NF = N * 1.0, % float version of N |
|||
X0 = A / NF, |
|||
X1 = 1.0, |
|||
do |
|||
X0 := X1, |
|||
X1 := (1.0 / NF)*((NF - 1.0)*X0 + (A / (X0 ** (NF - 1)))) |
|||
while( abs(X0-X1) > Precision).</lang> |
|||
{{out}} |
|||
<pre>nthroot( 2, 2) 1.414213562373095 (check: 1.414213562373095) |
|||
nthroot( 34, 5) 2.024397458499885 (check: 2.024397458499885) |
|||
nthroot(45435424, 5) 34.0 (check: 34.000000000000007) |
|||
nthroot( 7, 0.5) 48.999999999999993 (check: 49.0) |
|||
nthroot( 1024, 10) 2.0 (check: 2.0) |
|||
nthroot( 5642, 125) 1.071547591944767 (check: 1.071547591944767)</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |