Nth root: Difference between revisions
Content deleted Content added
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 |
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) > |
while abs(result-x) > 1e-15: |
||
x = result |
x = result |
||
result = (1 |
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1))) |
||
echo |
echo nthRoot(34.0, 5) |
||
echo |
echo nthRoot(42.0, 10) |
||
echo |
echo nthRoot(5.0, 2)</lang> |
||
Output: |
Output: |
||
<pre>2. |
<pre>2.024397458499885 |
||
1.453198460282268 |
|||
1.4531984602822678e+00 |
|||
2. |
2.23606797749979</pre> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |