Jump to content

Nth root: Difference between revisions

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.
(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:
<lang nim>import math
 
proc nthrootnthRoot(a,: float; n: int): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 10e1e-15:
x = result
result = (1.0/n) * (((n-1)*x) + (a / pow(x, n-1)))
 
echo nthrootnthRoot(34.0, 5)
echo nthrootnthRoot(42.0, 10)
echo nthrootnthRoot(5.0, 2)</lang>
Output:
<pre>2.0243974584998852e+00024397458499885
1.453198460282268
1.4531984602822678e+00
2.2360679774997898e+0023606797749979</pre>
 
=={{header|Objeck}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.