Nth root: Difference between revisions

4,635 bytes added ,  1 month ago
 
(8 intermediate revisions by 7 users not shown)
Line 1,219:
[https://mlochbaum.github.io/BQN/try.html#code=X3doaWxlXyDihpAge/CdlL3ijZ/wnZS+4oiY8J2UvV/wnZWjX/CdlL7iiJjwnZS94o2f8J2UvvCdlal9ClJvb3Qg4oaQIOKImgpSb290MSDihpAg4ouG4p+cw7fLnApSb290MiDihpAgewogIG4g8J2ViiBh4oC/cHJlYzoKICAx4oqRewogICAgcOKAv3g6CiAgICDin6gKICAgICAgeAogICAgICAoKHAgw5cgbiAtIDEpICsgYSDDtyBwIOKLhiBuIC0gMSkgw7cgbgogICAg4p+pCiAgfSBfd2hpbGVfIHsKICAgIHDigL94OgogICAgcHJlYyDiiaQgfCBwIC0geAogIH0g4p+oYSwg4oyKYcO3buKfqTsKICDwnZWoIPCdlYog8J2VqTog8J2VqCDwnZWKIPCdlanigL8xRcKvNQp9CgrigKJTaG93IDMgUm9vdCA1CuKAolNob3cgMyBSb290MSA1CuKAolNob3cgMyBSb290MiA1CuKAolNob3cgMyBSb290MiA14oC/MUXCrzE2Cgo= Try It!]
=={{header|Bracmat}}==
Until 2023, Bracmat doesdid not have floating point numbers as primitive type. Instead we havehad to use rational numbers. This code is not fast!
<syntaxhighlight lang="bracmat">( ( root
= n a d x0 x1 d2 rnd 10-d
Line 1,254:
2^(1/2)=1,4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727*10E0
5642^(1/125)=1,07154759194476751170*10E0</pre>
 
This is a floating point reimplementation of the C solution:
{{trans|C}}
<syntaxhighlight lang="bracmat"> "The body of Pow will be compiled twice: once as the the code hidden in the
UFP object called 'POW' (see below) and once as a local function of the code
hidden in the UFP object called 'Root' (also see below)."
& ( Pow
= (s.x) (s.e)
. 1:?r
& 0:?i
& whl
' ( !i:<!e
& !x*!r:?r
& 1+!i:?i
)
& !r
)
& "The next expression is a macro expression that expands $Pow to the body of
the Pow function defined above.
There is another local function, called 'even'."
&
' ( (s.n) (s.x)
. (Pow=$Pow)
& ( even
= (s.v)
. floor$(!v*1/2):?v/2
& subtract$(!v,2*!v/2)
)
& ( !x:0
| ( !n:<1
| !x:<0&even$!n:0
)
& divide$(0,0)
| 0x1p-52*10:?EPS
& 0x1p-52*-10:?EPS-
& 1:?r
& !n+-1:?n-1
& whl
' ( divide
$ (divide$(!x,Pow$(!r,!n-1))+-1*!r,!n)
: ?d
& !d+!r:?r
& (!d:~<!EPS|!d:~>!EPS-)
)
& !r
)
)
: (=?root)
& "Create two UFP objects, POW and ROOT. They are each others' inverse."
& new$(UFP,Pow):?POW
& new$(UFP,root):?Root
& 15:?n
& (POW..go)$("-3.14159",15):?x
& out$((Root..go)$(!n,!x));</syntaxhighlight>
 
Output:
 
<pre>-3.1415899999999999E+00</pre>
 
=={{header|C}}==
Line 1,581 ⟶ 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)}');
}</syntaxhighlight>
 
=={{header|Delphi}}==
Line 1,623 ⟶ 1,694:
 
<syntaxhighlight lang="text">
procfunc power x n . r .
r = 1
for i = 1 to n
r *= x
.
return r
.
procfunc nth_root x n . r .
r = 2
repeat
callp = power r (n - 1 p)
d = (x / p - r) / n
r += d
until abs d < 0.0001
.
return r
.
call power 3.1416 10 x
call nth_root x 10 r
numfmt 4 0
x = power 3.1416 10
print r
print nth_root x 10
</syntaxhighlight>
 
Line 2,297 ⟶ 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 = fn(.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,315 ⟶ 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 "function"
writeln .nthroot(10, 7131.5 ^ 10, 0.001)
writeln .nthroot(6, 64, 0.001)</syntaxhighlight>
Line 2,324 ⟶ 2,395:
2
 
function
calculation
7131.5
2
Line 2,610 ⟶ 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,201 ⟶ 3,300:
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>sub nth-root infix:<√>($n, $A,) $p=1e-9){
.tail given $A / $n, { (($n-1) * $_+ $A / ($_** ($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,723 ⟶ 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")
885

edits