Integer roots: Difference between revisions
Content added Content deleted
m (→{{header|F#}}: fix heading, as suggested on the Count examples/Full list/Tier 4 talk page) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22: | Line 22: | ||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="11l">F iRoot(BigInt b, Int n) |
||
I b < 2 {R b} |
I b < 2 {R b} |
||
V n1 = n - 1 |
V n1 = n - 1 |
||
Line 39: | Line 39: | ||
print(‘3rd root of 8 = ’iRoot(8, 3)) |
print(‘3rd root of 8 = ’iRoot(8, 3)) |
||
print(‘3rd root of 9 = ’iRoot(9, 3)) |
print(‘3rd root of 9 = ’iRoot(9, 3)) |
||
print(‘First 2001 digits of the square root of 2: ’iRoot(BigInt(100) ^ 2000 * 2, 2))</ |
print(‘First 2001 digits of the square root of 2: ’iRoot(BigInt(100) ^ 2000 * 2, 2))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 52: | Line 52: | ||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="rebol">iroot: function [b n][ |
||
if b<2 -> return b |
if b<2 -> return b |
||
Line 72: | Line 72: | ||
print ["3rd root of 8:" iroot 8 3] |
print ["3rd root of 8:" iroot 8 3] |
||
print ["3rd root of 9:" iroot 9 3] |
print ["3rd root of 9:" iroot 9 3] |
||
print ["First 2001 digits of the square root of 2:" iroot (100^2000)*2 2]</ |
print ["First 2001 digits of the square root of 2:" iroot (100^2000)*2 2]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 82: | Line 82: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="basic256">function root(n, x) |
||
for nr = floor(sqr(x)) to 1 step -1 |
for nr = floor(sqr(x)) to 1 step -1 |
||
if (nr ^ n) <= x then return nr |
if (nr ^ n) <= x then return nr |
||
Line 92: | Line 92: | ||
print root(4, 167) |
print root(4, 167) |
||
print root(2, 2e18) |
print root(2, 2e18) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 101: | Line 101: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 137: | Line 137: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3rd root of 8 = 2 |
<pre>3rd root of 8 = 2 |
||
Line 145: | Line 145: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Numerics; |
using System.Numerics; |
||
Line 180: | Line 180: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3rd integer root of 8 = 2 |
<pre>3rd integer root of 8 = 2 |
||
Line 187: | Line 187: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <math.h> |
#include <math.h> |
||
Line 221: | Line 221: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3rd root of 8 = 2 |
<pre>3rd root of 8 = 2 |
||
Line 229: | Line 229: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="d">import std.bigint; |
||
import std.stdio; |
import std.stdio; |
||
Line 258: | Line 258: | ||
b = BigInt(100)^^2000*2; |
b = BigInt(100)^^2000*2; |
||
writeln("First 2001 digits of the square root of 2: ", b.iRoot(2)); |
writeln("First 2001 digits of the square root of 2: ", b.iRoot(2)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3rd root of 8 = 2 |
<pre>3rd root of 8 = 2 |
||
Line 266: | Line 266: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Integer_roots do |
||
def root(_, b) when b<2, do: b |
def root(_, b) when b<2, do: b |
||
def root(a, b) do |
def root(a, b) do |
||
Line 291: | Line 291: | ||
end |
end |
||
Integer_roots.task</ |
Integer_roots.task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 303: | Line 303: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
let iroot (base_ : bigint) n = |
let iroot (base_ : bigint) n = |
||
Line 333: | Line 333: | ||
Console.WriteLine("First 2001 digits of the sqaure root of 2: {0}", (iroot b 2)) |
Console.WriteLine("First 2001 digits of the sqaure root of 2: {0}", (iroot b 2)) |
||
0 // return an integer exit code</ |
0 // return an integer exit code</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3rd integer root of 8 = 2 |
<pre>3rd integer root of 8 = 2 |
||
Line 341: | Line 341: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
< |
<syntaxhighlight lang="factor">USING: io kernel locals math math.functions math.order |
||
prettyprint sequences ; |
prettyprint sequences ; |
||
Line 357: | Line 357: | ||
"First 2,001 digits of the square root of two:" print |
"First 2,001 digits of the square root of two:" print |
||
2 100 2000 ^ 2 * root .</ |
2 100 2000 ^ 2 * root .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 367: | Line 367: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|Ring}} |
{{trans|Ring}} |
||
< |
<syntaxhighlight lang="freebasic">#define floor(x) ((x*2.0-0.5) Shr 1) |
||
Function root(n As Uinteger, x As Uinteger) As Uinteger |
Function root(n As Uinteger, x As Uinteger) As Uinteger |
||
Line 380: | Line 380: | ||
Print root(2, 2e18) |
Print root(2, 2e18) |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 390: | Line 390: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
===int=== |
===int=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 421: | Line 421: | ||
r += Δr |
r += Δr |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 429: | Line 429: | ||
</pre> |
</pre> |
||
===big.Int=== |
===big.Int=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 466: | Line 466: | ||
r.Add(r, &Δr) |
r.Add(r, &Δr) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 477: | Line 477: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="haskell">root :: Integer -> Integer -> Integer |
||
root a b = findAns $ iterate (\x -> (a1 * x + b `div` (x ^ a1)) `div` a) 1 |
root a b = findAns $ iterate (\x -> (a1 * x + b `div` (x ^ a1)) `div` a) 1 |
||
where |
where |
||
Line 489: | Line 489: | ||
print $ root 3 8 |
print $ root 3 8 |
||
print $ root 3 9 |
print $ root 3 9 |
||
print $ root 2 (2 * 100 ^ 2000) -- first 2001 digits of the square root of 2</ |
print $ root 2 (2 * 100 ^ 2000) -- first 2001 digits of the square root of 2</syntaxhighlight> |
||
Or equivalently, in terms of an applicative expression: |
Or equivalently, in terms of an applicative expression: |
||
< |
<syntaxhighlight lang="haskell">integerRoot :: Integer -> Integer -> Integer |
||
integerRoot n x = |
integerRoot n x = |
||
go $ iterate ((`div` n) . ((+) . (pn *) <*> (x `div`) . (^ pn))) 1 |
go $ iterate ((`div` n) . ((+) . (pn *) <*> (x `div`) . (^ pn))) 1 |
||
Line 503: | Line 503: | ||
main :: IO () |
main :: IO () |
||
main = mapM_ (print . uncurry integerRoot) [(3, 8), (3, 9), (2, 2 * 100 ^ 2000)]</ |
main = mapM_ (print . uncurry integerRoot) [(3, 8), (3, 9), (2, 2 * 100 ^ 2000)]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 517: | Line 517: | ||
For example, If you use "3 <.@%: (2*10x^2*200'''0''')" instead of "3 <.@%: (2*10x^2*200'''1''')", you will get an output starting with "271441761659490657151808946...", which are the first digits of the cube root of 20, not 2. |
For example, If you use "3 <.@%: (2*10x^2*200'''0''')" instead of "3 <.@%: (2*10x^2*200'''1''')", you will get an output starting with "271441761659490657151808946...", which are the first digits of the cube root of 20, not 2. |
||
< |
<syntaxhighlight lang="j"> 9!:37]0 4096 0 222 NB. set display truncation sufficiently high for our results |
||
2 <.@%: (2*10x^2*2000) |
2 <.@%: (2*10x^2*2000) |
||
Line 526: | Line 526: | ||
114869835499703500679862694677792758944385088909779750551371111849360320625351305681147311301150847391457571782825280872990018972855371267615994917020637676959403854539263226492033301322122190625130645468320078386350285806907949085127708283982797043969640382563667945344431106523789654147255972578315704103326302050272017414235255993151553782375173884359786924137881735354092890268530342009402133755822717151679559278360263800840317501093689917495888199116488588871447782240220513546797235647742625493141141704109917646404017146978939243424915943739448283626010758721504375406023613552985026793701507511351368254645700768390780390334017990233124030682358360249760098999315658413563173197024899154512108923313999675829872581317721346549115423634135836394159076400636688679216398175376716152621781331348 |
114869835499703500679862694677792758944385088909779750551371111849360320625351305681147311301150847391457571782825280872990018972855371267615994917020637676959403854539263226492033301322122190625130645468320078386350285806907949085127708283982797043969640382563667945344431106523789654147255972578315704103326302050272017414235255993151553782375173884359786924137881735354092890268530342009402133755822717151679559278360263800840317501093689917495888199116488588871447782240220513546797235647742625493141141704109917646404017146978939243424915943739448283626010758721504375406023613552985026793701507511351368254645700768390780390334017990233124030682358360249760098999315658413563173197024899154512108923313999675829872581317721346549115423634135836394159076400636688679216398175376716152621781331348 |
||
7 <.@%: (2*10x^2*2002) |
7 <.@%: (2*10x^2*2002) |
||
1104089513673812337649505387623344721325326600780124165514532464142106322880380980716598289886302005146897159065579931253969214680430855796510648058388081961639198643922155838145512343974763395078906646859029211806139421440562835192195007740110439139292223389537903767320705032063903809884944457070845279252405827307254864679671836816589429995916822424590361601902611505690284386526869351720866524568004847701822070064334667580822044823960984514550922242408608825451442062850448298384317793721518676765230683406727811327252052334859250776811047221310365241746671294399050316</ |
1104089513673812337649505387623344721325326600780124165514532464142106322880380980716598289886302005146897159065579931253969214680430855796510648058388081961639198643922155838145512343974763395078906646859029211806139421440562835192195007740110439139292223389537903767320705032063903809884944457070845279252405827307254864679671836816589429995916822424590361601902611505690284386526869351720866524568004847701822070064334667580822044823960984514550922242408608825451442062850448298384317793721518676765230683406727811327252052334859250776811047221310365241746671294399050316</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="java">import java.math.BigInteger; |
||
public class IntegerRoots { |
public class IntegerRoots { |
||
Line 568: | Line 568: | ||
System.out.println(iRoot(b, 2)); |
System.out.println(iRoot(b, 2)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3rd integer root of 8 = 2 |
<pre>3rd integer root of 8 = 2 |
||
Line 578: | Line 578: | ||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
< |
<syntaxhighlight lang="jq"># To take advantage of gojq's arbitrary-precision integer arithmetic: |
||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
||
Line 604: | Line 604: | ||
# The task: |
# The task: |
||
"First 2,001 digits of the square root of two:", |
"First 2,001 digits of the square root of two:", |
||
iroot(2; 2 * (100 | power(2000)))</ |
iroot(2; 2 * (100 | power(2000)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Exactly as for [[#Julia|Julia]]. |
Exactly as for [[#Julia|Julia]]. |
||
Line 613: | Line 613: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="julia">function iroot(a, b) |
||
b < 2 && return b |
b < 2 && return b |
||
a1, c = a - 1, 1 |
a1, c = a - 1, 1 |
||
Line 625: | Line 625: | ||
end |
end |
||
println("First 2,001 digits of the square root of two:\n", iroot(2, 2 * big(100) ^ 2000))</ |
println("First 2,001 digits of the square root of two:\n", iroot(2, 2 * big(100) ^ 2000))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 633: | Line 633: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.math.BigInteger |
import java.math.BigInteger |
||
Line 667: | Line 667: | ||
println("First 2001 digits of the square root of 2:") |
println("First 2001 digits of the square root of 2:") |
||
println(b.iRoot(2)) |
println(b.iRoot(2)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 681: | Line 681: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="lua">function root(base, n) |
||
if base < 2 then return base end |
if base < 2 then return base end |
||
if n == 0 then return 1 end |
if n == 0 then return 1 end |
||
Line 707: | Line 707: | ||
print("3rd root of 8 = " .. root(8, 3)) |
print("3rd root of 8 = " .. root(8, 3)) |
||
print("3rd root of 9 = " .. root(9, 3)) |
print("3rd root of 9 = " .. root(9, 3)) |
||
print("2nd root of " .. b .. " = " .. root(b, 2))</ |
print("2nd root of " .. b .. " = " .. root(b, 2))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3rd root of 8 = 2 |
<pre>3rd root of 8 = 2 |
||
Line 714: | Line 714: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">MODULE IntegerRoot; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
FROM Terminal IMPORT WriteString,ReadChar; |
FROM Terminal IMPORT WriteString,ReadChar; |
||
Line 774: | Line 774: | ||
ReadChar |
ReadChar |
||
END IntegerRoot.</ |
END IntegerRoot.</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|bignum}} |
{{libheader|bignum}} |
||
< |
<syntaxhighlight lang="nim">import bignum |
||
proc root(x: Int; n: int): Int = |
proc root(x: Int; n: int): Int = |
||
Line 802: | Line 802: | ||
echo "First 2001 digits of the square root of 2:" |
echo "First 2001 digits of the square root of 2:" |
||
let s = $x.root(2) |
let s = $x.root(2) |
||
for i in countup(0, s.high, 87): echo s.substr(i, i + 86)</ |
for i in countup(0, s.high, 87): echo s.substr(i, i + 86)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 833: | Line 833: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">sqrtnint(8,3) |
||
sqrtnint(9,3) |
sqrtnint(9,3) |
||
sqrtnint(2*100^2000,2)</ |
sqrtnint(2*100^2000,2)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = 2 |
<pre>%1 = 2 |
||
Line 843: | Line 843: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="perl">use bigint; |
||
sub integer_root { |
sub integer_root { |
||
Line 859: | Line 859: | ||
print integer_root( 3, 8), "\n"; |
print integer_root( 3, 8), "\n"; |
||
print integer_root( 3, 9), "\n"; |
print integer_root( 3, 9), "\n"; |
||
print integer_root( 2, 2 * 100 ** 2000), "\n";</ |
print integer_root( 2, 2 * 100 ** 2000), "\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 867: | Line 867: | ||
===Using a module=== |
===Using a module=== |
||
If using bigints, we can do this directly, which will be much faster than the method above: |
If using bigints, we can do this directly, which will be much faster than the method above: |
||
< |
<syntaxhighlight lang="perl">use bigint; |
||
print 8->babs->broot(3),"\n"; |
print 8->babs->broot(3),"\n"; |
||
print 9->babs->broot(3),"\n"; |
print 9->babs->broot(3),"\n"; |
||
print +(2*100**2000)->babs->broot(2),"\n";</ |
print +(2*100**2000)->babs->broot(2),"\n";</syntaxhighlight> |
||
The <code>babs</code> calls are only necessary if the input might be non-negative. |
The <code>babs</code> calls are only necessary if the input might be non-negative. |
||
Even faster, using a module: |
Even faster, using a module: |
||
< |
<syntaxhighlight lang="perl">use bigint; |
||
use ntheory "rootint"; |
use ntheory "rootint"; |
||
print rootint(8,3),"\n"; |
print rootint(8,3),"\n"; |
||
print rootint(9,3),"\n"; |
print rootint(9,3),"\n"; |
||
print rootint(2*100**2000,2),"\n";</ |
print rootint(2*100**2000,2),"\n";</syntaxhighlight> |
||
Both generate the same output as above. |
Both generate the same output as above. |
||
Line 885: | Line 885: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/mpfr}} |
{{libheader|Phix/mpfr}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 906: | Line 906: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First digits of the cube root of 2: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First digits of the cube root of 2: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 918: | Line 918: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">def root(a, b): |
||
if b < 2: |
if b < 2: |
||
return b |
return b |
||
Line 932: | Line 932: | ||
print("First 2,001 digits of the square root of two:\n{}".format( |
print("First 2,001 digits of the square root of two:\n{}".format( |
||
root(2, 2 * 100 ** 2000) |
root(2, 2 * 100 ** 2000) |
||
))</ |
))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 940: | Line 940: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="quackery"> [ stack ] is a-1 ( --> s ) |
||
[ stack ] is b ( --> s ) |
[ stack ] is b ( --> s ) |
||
Line 962: | Line 962: | ||
say "3rd root of 9 = " 9 3 root echo cr |
say "3rd root of 9 = " 9 3 root echo cr |
||
say "First 2001 digits of the square root of 2: " |
say "First 2001 digits of the square root of 2: " |
||
2 100 2000 ** * 2 root echo cr</ |
2 100 2000 ** * 2 root echo cr</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 978: | Line 978: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{trans|Python}} |
{{trans|Python}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub integer_root ( Int $p where * >= 2, Int $n --> Int ) { |
||
my Int $d = $p - 1; |
my Int $d = $p - 1; |
||
my $guess = 10**($n.chars div $p); |
my $guess = 10**($n.chars div $p); |
||
Line 987: | Line 987: | ||
} |
} |
||
say integer_root( 2, 2 * 100 ** 2000 );</ |
say integer_root( 2, 2 * 100 ** 2000 );</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008 |
<pre>141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008 |
||
Line 999: | Line 999: | ||
<br>multiply the guess ['''G'''] by unity, and no need to compute the guess to the 1<sup>st</sup> power, bypassing some trivial arithmetic). |
<br>multiply the guess ['''G'''] by unity, and no need to compute the guess to the 1<sup>st</sup> power, bypassing some trivial arithmetic). |
||
===integer result only=== |
===integer result only=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program calculates the Nth root of a number to a specified number of decimal digs*/ |
||
parse arg num root digs . /*obtain the optional arguments from CL*/ |
parse arg num root digs . /*obtain the optional arguments from CL*/ |
||
if num=='' | num=="," then num= 2 /*Not specified? Then use the default.*/ |
if num=='' | num=="," then num= 2 /*Not specified? Then use the default.*/ |
||
Line 1,021: | Line 1,021: | ||
if m==1 then do until old==g; old=g; g=(g + z % g ) % root; end |
if m==1 then do until old==g; old=g; g=(g + z % g ) % root; end |
||
else do until old==g; old=g; g=(g*m + z % (g**m) ) % root; end |
else do until old==g; old=g; g=(g*m + z % (g**m) ) % root; end |
||
return left(g,p) /*return the Nth root of Z to invoker.*/</ |
return left(g,p) /*return the Nth root of Z to invoker.*/</syntaxhighlight> |
||
'''output''' when the defaults are being used: |
'''output''' when the defaults are being used: |
||
<pre> |
<pre> |
||
Line 1,044: | Line 1,044: | ||
===true results=== |
===true results=== |
||
<br>Negative and complex roots are supported. The expressed root may have a decimal point. |
<br>Negative and complex roots are supported. The expressed root may have a decimal point. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program calculates the Nth root of a number to a specified number of decimal digs*/ |
||
parse arg num root digs . /*obtain the optional arguments from CL*/ |
parse arg num root digs . /*obtain the optional arguments from CL*/ |
||
if num=='' | num=="," then num= 2 /*Not specified? Then use the default.*/ |
if num=='' | num=="," then num= 2 /*Not specified? Then use the default.*/ |
||
Line 1,078: | Line 1,078: | ||
numeric digits od /*set numeric digits to the original.*/ |
numeric digits od /*set numeric digits to the original.*/ |
||
if oy<0 then return (1/_)i /*Is the root negative? Use reciprocal*/ |
if oy<0 then return (1/_)i /*Is the root negative? Use reciprocal*/ |
||
return (_/1)i /*return the Yth root of X to invoker.*/</ |
return (_/1)i /*return the Yth root of X to invoker.*/</syntaxhighlight> |
||
'''output''' when the defaults are being used: |
'''output''' when the defaults are being used: |
||
<pre> |
<pre> |
||
Line 1,118: | Line 1,118: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Integer roots |
# Project : Integer roots |
||
Line 1,132: | Line 1,132: | ||
ok |
ok |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,142: | Line 1,142: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Python, zkl}} |
{{trans|Python, zkl}} |
||
< |
<syntaxhighlight lang="ruby">def root(a,b) |
||
return b if b<2 |
return b if b<2 |
||
a1, c = a-1, 1 |
a1, c = a-1, 1 |
||
Line 1,154: | Line 1,154: | ||
puts "First 2,001 digits of the square root of two:" |
puts "First 2,001 digits of the square root of two:" |
||
puts root(2, 2*100**2000) |
puts root(2, 2*100**2000) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre>First 2,001 digits of the square root of two: |
{{out}}<pre>First 2,001 digits of the square root of two: |
||
14142135623730950488016887242096(...)46758516447107578486024636008</pre> |
14142135623730950488016887242096(...)46758516447107578486024636008</pre> |
||
Line 1,160: | Line 1,160: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
The rug crate provides the functionality required for this task. |
The rug crate provides the functionality required for this task. |
||
< |
<syntaxhighlight lang="rust">// [dependencies] |
||
// rug = "1.9" |
// rug = "1.9" |
||
Line 1,190: | Line 1,190: | ||
let s = Integer::from(x.root(3)).to_string(); |
let s = Integer::from(x.root(3)).to_string(); |
||
println!("First {} digits of the cube root of 2:\n{}", s.len(), shorten(&s, 70)); |
println!("First {} digits of the cube root of 2:\n{}", s.len(), shorten(&s, 70)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,204: | Line 1,204: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Functional solution, tail recursive, no immutables=== |
===Functional solution, tail recursive, no immutables=== |
||
< |
<syntaxhighlight lang="scala">import scala.annotation.tailrec |
||
object IntegerRoots extends App { |
object IntegerRoots extends App { |
||
Line 1,231: | Line 1,231: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/bVwlHfa/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/0T93IhLVRGiYfuKpW7DTUg Scastie (JVM)]. |
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/bVwlHfa/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/0T93IhLVRGiYfuKpW7DTUg Scastie (JVM)]. |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="scheme">(define (root a b) |
||
(define // quotient) |
(define // quotient) |
||
(define (y a a1 b c d e) |
(define (y a a1 b c d e) |
||
Line 1,251: | Line 1,251: | ||
(display "First 2,001 digits of the cube root of two:\n") |
(display "First 2,001 digits of the cube root of two:\n") |
||
(display (root 3 (* 2 (expt 1000 2000))))</ |
(display (root 3 (* 2 (expt 1000 2000))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,259: | Line 1,259: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">func root(a, b) { |
||
b < 2 && return(b) |
b < 2 && return(b) |
||
var (a1, c) = (a-1, 1) |
var (a1, c) = (a-1, 1) |
||
Line 1,272: | Line 1,272: | ||
say "First 2,001 digits of the square root of two:" |
say "First 2,001 digits of the square root of two:" |
||
say root(2, 2 * 100**2000)</ |
say root(2, 2 * 100**2000)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,283: | Line 1,283: | ||
On the other hand, everything is very straightforward, no libraries necessary. |
On the other hand, everything is very straightforward, no libraries necessary. |
||
< |
<syntaxhighlight lang="tcl"> |
||
proc root {this n} { |
proc root {this n} { |
||
if {$this < 2} {return $this} |
if {$this < 2} {return $this} |
||
Line 1,303: | Line 1,303: | ||
puts [root 9 3] |
puts [root 9 3] |
||
puts [root [expr 2* (100**2000)] 2] |
puts [root [expr 2* (100**2000)] 2] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,313: | Line 1,313: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{libheader|System.Numerics}} |
{{libheader|System.Numerics}} |
||
From the method described on [https://en.wikipedia.org/wiki/Nth_root_algorithm the Wikipedia page]. Included is an Integer Square Root function to compare results to the Integer Nth Square root function. One must choose the exponents carefully, otherwise one will obtain the digits of the nth root of 20, 200, 2000, etc..., instead of 2. For example, 4008 was chosen because it works for both ''n = 2'' and ''n = 3'', whereas 4004 was chosen for ''n = 7''< |
From the method described on [https://en.wikipedia.org/wiki/Nth_root_algorithm the Wikipedia page]. Included is an Integer Square Root function to compare results to the Integer Nth Square root function. One must choose the exponents carefully, otherwise one will obtain the digits of the nth root of 20, 200, 2000, etc..., instead of 2. For example, 4008 was chosen because it works for both ''n = 2'' and ''n = 3'', whereas 4004 was chosen for ''n = 7''<syntaxhighlight lang="vbnet">Imports System |
||
Imports System.Numerics |
Imports System.Numerics |
||
Imports Microsoft.VisualBasic.Strings |
Imports Microsoft.VisualBasic.Strings |
||
Line 1,361: | Line 1,361: | ||
End Module |
End Module |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="height:64ex;overflow:scroll"> |
<pre style="height:64ex;overflow:scroll"> |
||
Line 1,379: | Line 1,379: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Wren doesn't have arbitrary precision numerics and so can't do the third example in the task description. We therefore do the third C/C++ example instead. |
Wren doesn't have arbitrary precision numerics and so can't do the third example in the task description. We therefore do the third C/C++ example instead. |
||
< |
<syntaxhighlight lang="ecmascript">var intRoot = Fn.new { |x, n| |
||
if (!(x is Num && x.isInteger && x >= 0)) { |
if (!(x is Num && x.isInteger && x >= 0)) { |
||
Fiber.abort("First argument must be a non-negative integer.") |
Fiber.abort("First argument must be a non-negative integer.") |
||
Line 1,394: | Line 1,394: | ||
var n = e[1] |
var n = e[1] |
||
System.print("%(x) ^ (1/%(n)) = %(intRoot.call(x, n))") |
System.print("%(x) ^ (1/%(n)) = %(intRoot.call(x, n))") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,406: | Line 1,406: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="yabasic">sub root(n, x) |
||
for nr = floor(sqr(x)) to 1 step -1 |
for nr = floor(sqr(x)) to 1 step -1 |
||
if (nr ^ n) <= x then return nr : fi |
if (nr ^ n) <= x then return nr : fi |
||
Line 1,415: | Line 1,415: | ||
print root(3, 9) |
print root(3, 9) |
||
print root(4, 167) |
print root(4, 167) |
||
end</ |
end</syntaxhighlight> |
||
Line 1,421: | Line 1,421: | ||
{{trans|Python}} |
{{trans|Python}} |
||
Uses GNU GMP library |
Uses GNU GMP library |
||
< |
<syntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"); |
||
fcn root(n,r){ |
fcn root(n,r){ |
||
f:='wrap(z){ (n/z.pow(r-1) + z*(r-1))/r or 1 }; //--> v or 1 |
f:='wrap(z){ (n/z.pow(r-1) + z*(r-1))/r or 1 }; //--> v or 1 |
||
Line 1,427: | Line 1,427: | ||
while(c!=d and c!=e){ c,d,e=d,e,f(e) } |
while(c!=d and c!=e){ c,d,e=d,e,f(e) } |
||
if(d<e) d else e |
if(d<e) d else e |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">a:=BN(100).pow(2000)*2; |
||
println("Does GMP agree: ",root(a,3)==a.root(3));</ |
println("Does GMP agree: ",root(a,3)==a.root(3));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |