Nth root: Difference between revisions

→‎{{header|Raku}}: create an infix operator and use a sequence
m (→‎{{header|Bracmat}}: added {{trans|C}})
(→‎{{header|Raku}}: create an infix operator and use a sequence)
Line 3,260:
(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}}==
1,935

edits