Nth root: Difference between revisions

3,075 bytes added ,  1 month ago
m (→‎{{header|Bracmat}}: added {{trans|C}})
 
(5 intermediate revisions by 5 users not shown)
Line 1,639:
<pre>7131.5
2</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">import 'dart:math' show pow;
 
num nroot(num value, num degree) {
return pow(value, (1 / degree));
}
 
void main() {
int n = 15;
num x = pow(-3.14159, 15);
print('root($n, $x) = ${nroot(n, x)}');
say nth-root(3,8);}</syntaxhighlight>
 
=={{header|Delphi}}==
Line 2,356 ⟶ 2,369:
Langur has a root operator. Here, we show use of both the root operator and an nth root function.
 
{{works with|langur|0.8}}
{{trans|D}}
<syntaxhighlight lang="langur">writeln "operator"
writeln( (7131.5 ^ 10) ^/ 10 )
writeln 64 ^/ 6
writeln()
 
val .nthroot = ffn(.n, .A, .p) {
# To make the example from the D language work, we set a low maximum for the number of digits after a decimal point in division.
mode divMaxScale = 7
 
val .nthroot = f(.n, .A, .p) {
var .x = [.A, .A / .n]
while abs(.x[2]-.x[1]) > .p {
Line 2,374 ⟶ 2,383:
}
 
# To make the example from the D language work, we set a low maximum for the number of digits after a decimal point in division.
writeln "calculation"
mode divMaxScale = 7
 
writeln "calculationfunction"
writeln .nthroot(10, 7131.5 ^ 10, 0.001)
writeln .nthroot(6, 64, 0.001)</syntaxhighlight>
Line 2,383 ⟶ 2,395:
2
 
function
calculation
7131.5
2
Line 2,669 ⟶ 2,681:
1.453198460282268
2.23606797749979</pre>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
def "math root" [n] {$in ** (1 / $n)}
 
1..10 | each {|it|
1..10 | reduce --fold {index: $it} {|root acc|
$acc | insert $"root ($root)" ($it | math root $root | into string --decimals 4 )
}
}
</syntaxhighlight>
{{out}}
<pre>
╭──────┬───────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬─────────┬─────────┬──────────╮
│ # │ root 1 │ root 2 │ root 3 │ root 4 │ root 5 │ root 6 │ root 7 │ root 8 │ root 9 │ root 10 │
├──────┼───────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼─────────┼─────────┼──────────┤
│ 1 │ 1.0000 │ 1.0000 │ 1.0000 │ 1.0000 │ 1.0000 │ 1.0000 │ 1.0000 │ 1.0000 │ 1.0000 │ 1.0000 │
│ 2 │ 2.0000 │ 1.4142 │ 1.2599 │ 1.1892 │ 1.1487 │ 1.1225 │ 1.1041 │ 1.0905 │ 1.0801 │ 1.0718 │
│ 3 │ 3.0000 │ 1.7321 │ 1.4422 │ 1.3161 │ 1.2457 │ 1.2009 │ 1.1699 │ 1.1472 │ 1.1298 │ 1.1161 │
│ 4 │ 4.0000 │ 2.0000 │ 1.5874 │ 1.4142 │ 1.3195 │ 1.2599 │ 1.2190 │ 1.1892 │ 1.1665 │ 1.1487 │
│ 5 │ 5.0000 │ 2.2361 │ 1.7100 │ 1.4953 │ 1.3797 │ 1.3077 │ 1.2585 │ 1.2228 │ 1.1958 │ 1.1746 │
│ 6 │ 6.0000 │ 2.4495 │ 1.8171 │ 1.5651 │ 1.4310 │ 1.3480 │ 1.2917 │ 1.2510 │ 1.2203 │ 1.1962 │
│ 7 │ 7.0000 │ 2.6458 │ 1.9129 │ 1.6266 │ 1.4758 │ 1.3831 │ 1.3205 │ 1.2754 │ 1.2414 │ 1.2148 │
│ 8 │ 8.0000 │ 2.8284 │ 2.0000 │ 1.6818 │ 1.5157 │ 1.4142 │ 1.3459 │ 1.2968 │ 1.2599 │ 1.2311 │
│ 9 │ 9.0000 │ 3.0000 │ 2.0801 │ 1.7321 │ 1.5518 │ 1.4422 │ 1.3687 │ 1.3161 │ 1.2765 │ 1.2457 │
│ 10 │ 10.0000 │ 3.1623 │ 2.1544 │ 1.7783 │ 1.5849 │ 1.4678 │ 1.3895 │ 1.3335 │ 1.2915 │ 1.2589 │
╰──────┴───────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴─────────┴─────────┴──────────╯
</pre>
 
=={{header|Objeck}}==
Line 3,260 ⟶ 3,300:
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>sub nth-root infix:<√>($n, $A,) $p=1e-9){
.tail given $A my/ $x1n, ={ (($n-1) * $x0 _+ $A / ($x0 _** ($n-1))) / $n } ... * ≅ *;
{
my $x0 = $A / $n;
loop {
my $x1 = (($n-1) * $x0 + $A / ($x0 ** ($n-1))) / $n;
return $x1 if abs($x1-$x0) < abs($x0 * $p);
$x0 = $x1;
}
}
 
use Test;
say nth-root(3,8);</syntaxhighlight>
plan 9;
is-approx ($_√pi)**$_, pi for 2 .. 10;</syntaxhighlight>
 
=={{header|RATFOR}}==
Line 3,782 ⟶ 3,818:
=={{header|Wren}}==
{{trans|E}}
<syntaxhighlight lang="ecmascriptwren">var nthRoot = Fn.new { |x, n|
if (n < 2) Fiber.abort("n must be more than 1")
if (x <= 0) Fiber.abort("x must be positive")
890

edits