Nth root: Difference between revisions

Content deleted Content added
Hkdtam (talk | contribs)
added Rust programming solution
Updated to work with Nim 1.4: added missing parameter types. Replaced 10e-15 which is 10*10^(-15) with 1e-15 which is 10^(-15). Updated result.
Line 2,107: Line 2,107:
<lang nim>import math
<lang nim>import math


proc nthroot(a, n): float =
proc nthRoot(a: float; n: int): float =
var n = float(n)
var n = float(n)
result = a
result = a
var x = a / n
var x = a / n
while abs(result-x) > 10e-15:
while abs(result-x) > 1e-15:
x = result
x = result
result = (1.0/n) * (((n-1)*x) + (a / pow(x, n-1)))
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))


echo nthroot(34.0, 5)
echo nthRoot(34.0, 5)
echo nthroot(42.0, 10)
echo nthRoot(42.0, 10)
echo nthroot(5.0, 2)</lang>
echo nthRoot(5.0, 2)</lang>
Output:
Output:
<pre>2.0243974584998852e+00
<pre>2.024397458499885
1.453198460282268
1.4531984602822678e+00
2.2360679774997898e+00</pre>
2.23606797749979</pre>


=={{header|Objeck}}==
=={{header|Objeck}}==