Integer roots: Difference between revisions

m
Added Easylang
(→‎{{header|Phix}}: bigatom -> mpfr)
m (Added Easylang)
 
(41 intermediate revisions by 21 users not shown)
Line 18:
Example: &nbsp; With &nbsp; N=2 &nbsp; and &nbsp; X=2&times;100<sup>2,000</sup> &nbsp; you would calculate a large integer consisting of the first &nbsp; 2,001 &nbsp; digits (in order) of the square root of two.
<br><br>
 
=={{header|11l}}==
{{trans|D}}
 
<syntaxhighlight lang="11l">F iRoot(BigInt b, Int n)
I b < 2 {R b}
V n1 = n - 1
V n2 = BigInt(n)
V n3 = BigInt(n1)
V c = BigInt(1)
V d = (n3 + b) I/ n2
V e = (n3 * d + b I/ d ^ n1) I/ n2
L c != d & c != e
c = d
d = e
e = (n3 * e + b I/ e ^ n1) I/ n2
I d < e {R d}
R e
 
print(‘3rd root of 8 = ’iRoot(8, 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))</syntaxhighlight>
 
{{out}}
<pre>
3rd root of 8 = 2
3rd root of 9 = 2
First 2001 digits of the square root of 2: 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
-- Find integer roots
-- J. Carter 2023 Jun
 
with Ada.Text_IO;
with System;
 
procedure Integer_Roots is
type Big is mod System.Max_Binary_Modulus;
 
function Root (N : in Positive; X : in Big) return Big With Pre => N > 1;
-- Returns the largest integer R such that R ** N <= X
-- Derived from Modula-2
 
function Root (N : in Positive; X : in Big) return Big is
N1 : constant Positive := N - 1;
N2 : constant Big := Big (N);
N3 : constant Big := N2 - 1;
 
C : Big := 1;
D : Big := (N3 + X) / N2;
E : Big := (N3 * D + X / D ** N1) / N2;
begin -- Root
if X <= 1 then
return X;
end if;
 
Converge : loop
exit Converge when C = D or C = E;
 
C := D;
D := E;
E := (N3 * D + X / E ** N1) / N2;
end loop Converge;
 
return (if D < E then D else E);
end Root;
 
Large : constant Big := 2 * 10 ** 38;
-- On 64-bit platforms, recent versions of GNAT provide 128-bit integers
-- 10 ** 38 is the largest power of 10 < 2 ** 128
begin -- Integer_Roots
Ada.Text_IO.Put_Line (Item => "Cube root of 8 =" & Root (3, 8)'Image);
Ada.Text_IO.Put_Line (Item => "Cube root of 9 =" & Root (3, 9)'Image);
Ada.Text_IO.Put_Line (Item => "Square root of" & Large'Image & " =" & Root (2, Large)'Image);
end Integer_Roots;
</syntaxhighlight>
 
{{out}}
<pre>
Cube root of 8 = 2
Cube root of 9 = 2
Square root of 200000000000000000000000000000000000000 = 14142135623730950488
</pre>
 
=={{header|Arturo}}==
 
{{trans|D}}
 
<syntaxhighlight lang="rebol">iroot: function [b n][
if b<2 -> return b
n1: n-1
n2: n
n3: n1
c: 1
d: (n3+b)/n2
e: ((n3*d) + b/d^n1)/n2
while [and? c<>d c<>e][
c: d
d: e
e: ((n3*e) + b/e^n1)/n2
]
if d<e -> return d
return e
]
print ["3rd root of 8:" iroot 8 3]
print ["3rd root of 9:" iroot 9 3]
print ["First 2001 digits of the square root of 2:" iroot (100^2000)*2 2]</syntaxhighlight>
 
{{out}}
 
<pre>3rd root of 8: 2
3rd root of 9: 2
First 2001 digits of the square root of 2: 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008</pre>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">function root(n, x)
for nr = floor(sqr(x)) to 1 step -1
if (nr ^ n) <= x then return nr
next nr
end function
 
print root(3, 8)
print root(3, 9)
print root(4, 167)
print root(2, 2e18)
end</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <math.h>
 
Line 57 ⟶ 193:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>3rd root of 8 = 2
3rd root of 9 = 2
2nd root of 2000000000000000000 = 1414213562</pre>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <math.h>
 
unsigned long long root(unsigned long long base, unsigned int n) {
if (base < 2) return base;
if (n == 0) return 1;
 
unsigned int n1 = n - 1;
unsigned long long n2 = n;
unsigned long long n3 = n1;
unsigned long long c = 1;
auto d = (n3 + base) / n2;
auto e = (n3 * d + base / pow(d, n1)) / n2;
 
while (c != d && c != e) {
c = d;
d = e;
e = (n3*e + base / pow(e, n1)) / n2;
}
 
if (d < e) return d;
return e;
}
 
int main() {
using namespace std;
 
cout << "3rd root of 8 = " << root(8, 3) << endl;
cout << "3rd root of 9 = " << root(9, 3) << endl;
 
unsigned long long b = 2e18;
cout << "2nd root of " << b << " = " << root(b, 2) << endl;
 
return 0;
}</lang>
{{out}}
<pre>3rd root of 8 = 2
Line 104 ⟶ 199:
2nd root of 2000000000000000000 = 1414213562</pre>
 
=={{header|C#|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Numerics;
 
Line 141 ⟶ 236:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>3rd integer root of 8 = 2
3rd integer root of 9 = 2
First 2001 digits of the sqaure root of 2: 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <math.h>
 
unsigned long long root(unsigned long long base, unsigned int n) {
if (base < 2) return base;
if (n == 0) return 1;
 
unsigned int n1 = n - 1;
unsigned long long n2 = n;
unsigned long long n3 = n1;
unsigned long long c = 1;
auto d = (n3 + base) / n2;
auto e = (n3 * d + base / pow(d, n1)) / n2;
 
while (c != d && c != e) {
c = d;
d = e;
e = (n3*e + base / pow(e, n1)) / n2;
}
 
if (d < e) return d;
return e;
}
 
int main() {
using namespace std;
 
cout << "3rd root of 8 = " << root(8, 3) << endl;
cout << "3rd root of 9 = " << root(9, 3) << endl;
 
unsigned long long b = 2e18;
cout << "2nd root of " << b << " = " << root(b, 2) << endl;
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>3rd root of 8 = 2
3rd root of 9 = 2
2nd root of 2000000000000000000 = 1414213562</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.bigint;
import std.stdio;
 
Line 178 ⟶ 314:
b = BigInt(100)^^2000*2;
writeln("First 2001 digits of the square root of 2: ", b.iRoot(2));
}</langsyntaxhighlight>
{{out}}
<pre>3rd root of 8 = 2
3rd root of 9 = 2
First 2001 digits of the square root of 2: 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
{{trans|C++}}
 
 
<syntaxhighlight lang="Delphi">
 
 
function IntRoot(Base: int64; N: cardinal): int64;
var N1: cardinal;
var N2,N3,C: int64;
var D,E: int64;
begin
if Base < 2 then Result:=Base
else if N = 0 then Result:=1
else
begin
N1:=N - 1;
N2:=N;
N3:=N1;
C:=1;
d:=round((N3 + Base) / N2);
e:=round((N3 * D + Base / Power(D, N1)) / N2);
while (C<>D) and (C<>E) do
begin
C:=D;
D:=E;
E:=Round((N3*E + Base / Power(E, N1)) / N2);
end;
if D < E then Result:=D
else Result:=E;
end;
end;
 
 
procedure ShowIntegerRoots(Memo: TMemo);
var Base: int64;
begin
Memo.Lines.Add('3rd integer root of 8 = '+IntToStr(IntRoot(8, 3)));
Memo.Lines.Add('3rd integer root of 9 = '+IntToStr(IntRoot(9, 3)));
Base:=2000000000000000000;
Memo.Lines.Add('sqaure root of 2 = '+IntToStr(IntRoot(Base, 2)));
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
3rd integer root of 8 = 2
3rd integer root of 9 = 2
sqaure root of 2 = 1414213562
 
Elapsed Time: 2.747 ms.
 
</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func root base n .
if base < 2
return base
.
if n = 0
return 1
.
n1 = n - 1
n2 = n
n3 = n1
c = 1
d = (n3 + base) div n2
e = (n3 * d + base div pow d n1) div n2
while c <> d and c <> e
c = d
d = e
e = (n3 * e + base div pow e n1) div n2
.
if (d < e)
return d
.
return e
.
print "3rd root of 8 = " & root 8 3
print "3rd root of 9 = " & root 9 3
b = 2e18
print "2nd root of " & b & " = " & root b 2
</syntaxhighlight>
{{out}}
<pre>
3rd root of 8 = 2
3rd root of 9 = 2
2nd root of 2e+18 = 1414213562
</pre>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Integer_roots do
def root(_, b) when b<2, do: b
def root(a, b) do
Line 211 ⟶ 443:
end
 
Integer_roots.task</langsyntaxhighlight>
 
{{out}}
Line 221 ⟶ 453:
</pre>
 
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
 
let iroot (base_ : bigint) n =
Line 253 ⟶ 485:
Console.WriteLine("First 2001 digits of the sqaure root of 2: {0}", (iroot b 2))
 
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>3rd integer root of 8 = 2
3rd integer root of 9 = 2
First 2001 digits of the sqaure root of 2: 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008</pre>
 
=={{header|Factor}}==
{{trans|Sidef}}
<syntaxhighlight lang="factor">USING: io kernel locals math math.functions math.order
prettyprint sequences ;
 
:: (root) ( a b -- n )
a 1 - 1 :> ( a1 c! )
[| x | a1 x * b x a1 ^ /i + a /i ] :> f
c f call :> d!
d f call :> e!
[ c { d e } member? ] [
d c! e d! e f call e!
] until
d e min ;
 
: root ( a b -- n ) dup 2 < [ nip ] [ (root) ] if ;
 
"First 2,001 digits of the square root of two:" print
2 100 2000 ^ 2 * root .</syntaxhighlight>
{{out}}
<pre>
First 2,001 digits of the square root of two:
14142135623730950488016887242096980[...]32952546758516447107578486024636008
</pre>
 
 
=={{header|FreeBASIC}}==
{{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
For nr As Uinteger = floor(Sqr(x)) To 1 Step -1
If (nr ^ n) <= x Then Return nr
Next nr
End Function
 
Print root(3, 8)
Print root(3, 9)
Print root(4, 167)
Print root(2, 2e18)
 
Sleep</syntaxhighlight>
{{out}}
<pre>2
2
3
1414213562</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn root( n as UInt64, x as UInt64 ) as double
double nr, result = 0
for nr = fn floor( sqr(x) ) to 1 step -1
if ( nr ^ n ) <= x then result = nr : exit fn
next
end fn = result
 
print @"3rd root of 8 = "; fn root( 3, 8 )
print @"3rd root of 9 = "; fn root( 3, 9 )
print @"4th root of 167 = "; fn root( 4, 167 )
print @"2nd root of 2e+018 = "; fn root( 2, 2e+018 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
3rd root of 8 = 2
3rd root of 9 = 2
4th root of 167 = 3
2nd root of 2e+018 = 1414213562
</pre>
 
 
 
=={{header|Go}}==
===int===
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 292 ⟶ 600:
r += Δr
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 300 ⟶ 608:
</pre>
===big.Int===
<langsyntaxhighlight lang="go">package main
 
import (
Line 337 ⟶ 645:
r.Add(r, &Δr)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 348 ⟶ 656:
=={{header|Haskell}}==
{{trans|Python}}
<langsyntaxhighlight lang="haskell">root :: Integer -> Integer -> Integer
root a b = findAns $ iterate (\x -> (a1 * x + b `div` (x ^ a1)) `div` a) 1
where
Line 360 ⟶ 668:
print $ root 3 8
print $ root 3 9
print $ root 2 (2 * 100 ^ 2000) -- first 2001 digits of the square root of 2</langsyntaxhighlight>
 
Or equivalently, in terms of an applicative expression:
 
<syntaxhighlight lang="haskell">integerRoot :: Integer -> Integer -> Integer
integerRoot n x =
go $ iterate ((`div` n) . ((+) . (pn *) <*> (x `div`) . (^ pn))) 1
where
pn = pred n
go (x:xs@(y:z:_))
| x == y || x == z = min y z
| otherwise = go xs
main :: IO ()
main = mapM_ (print . uncurry integerRoot) [(3, 8), (3, 9), (2, 2 * 100 ^ 2000)]</syntaxhighlight>
 
{{out}}
<pre>2
Line 370 ⟶ 693:
<code><.@%:</code> satisfies this task. Left argument is the task's N, right argument is the task's X:
 
'''Note:''' DependingIf onyou '''''N'''''are looking for a decimal expansion of an integer root, one must select the proper number of digits for N, that is, ''2000'', ''2001'', ''2002'', etc..., otherwise the result will be the digits of the nth root of 20, 2000, etc...<br/>
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. This constraint is independent of the task requirements, except in an illustrative sense, so will not be developed further, here.
 
<langsyntaxhighlight Jlang="j"> 9!:37]0 4096 0 222 NB. set display truncation sufficiently high for our results
 
2 <.@%: (2*10x^2*2000)
Line 382 ⟶ 705:
114869835499703500679862694677792758944385088909779750551371111849360320625351305681147311301150847391457571782825280872990018972855371267615994917020637676959403854539263226492033301322122190625130645468320078386350285806907949085127708283982797043969640382563667945344431106523789654147255972578315704103326302050272017414235255993151553782375173884359786924137881735354092890268530342009402133755822717151679559278360263800840317501093689917495888199116488588871447782240220513546797235647742625493141141704109917646404017146978939243424915943739448283626010758721504375406023613552985026793701507511351368254645700768390780390334017990233124030682358360249760098999315658413563173197024899154512108923313999675829872581317721346549115423634135836394159076400636688679216398175376716152621781331348
7 <.@%: (2*10x^2*2002)
1104089513673812337649505387623344721325326600780124165514532464142106322880380980716598289886302005146897159065579931253969214680430855796510648058388081961639198643922155838145512343974763395078906646859029211806139421440562835192195007740110439139292223389537903767320705032063903809884944457070845279252405827307254864679671836816589429995916822424590361601902611505690284386526869351720866524568004847701822070064334667580822044823960984514550922242408608825451442062850448298384317793721518676765230683406727811327252052334859250776811047221310365241746671294399050316</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">import java.math.BigInteger;
 
public class IntegerRoots {
Line 424 ⟶ 747:
System.out.println(iRoot(b, 2));
}
}</langsyntaxhighlight>
{{out}}
<pre>3rd integer root of 8 = 2
3rd integer root of 9 = 2
First 2001 digits of the square root of 2: 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008</pre>
 
=={{header|jq}}==
'''Adapted from [[#Julia|Julia]]'''
 
'''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);
 
# If $j is 0, then an error condition is raised;
# otherwise, assuming infinite-precision integer arithmetic,
# if the input and $j are integers, then the result will be an integer.
def idivide($j):
(. - (. % $j)) / $j ;
 
def iroot(a; b):
if b < 2 then b
else (a-1) as $a1
| {c: 1,
d: (($a1 + (b | idivide(1))) | idivide(a)) }
| .d as $d
| .e = ($a1 * $d + (b |idivide($d | power($a1))) | idivide(a))
| until( .d == .c or .c == .e;
.c = .d
| .d = .e
| .e as $e
| .e = ($a1 * .e + (b | idivide(($e | power($a1)))) | idivide(a)) )
| [.d, .e] | min
end ;
 
# The task:
"First 2,001 digits of the square root of two:",
iroot(2; 2 * (100 | power(2000)))</syntaxhighlight>
{{out}}
Exactly as for [[#Julia|Julia]].
 
 
=={{header|Julia}}==
{{works with|Julia|01.63}}
{{trans|Python}}
 
<langsyntaxhighlight lang="julia">function iroot(a, b)
if b < 2 && return b end
a1, c = a - 1, 1
d = (a1 * c + b ÷ (c ^ a1)) ÷ a
e = (a1 * d + b ÷ (d ^ a1)) ÷ a
while cd != dc != e
c, d, e = d, e, (a1 * e + b ÷ (e ^ a1)) ÷ a
end
 
return min(d, e)
end
 
println("First 2,001 digits of the square root of two:\n", iroot(2, 2 * big(100) ^ 2000))</langsyntaxhighlight>
 
{{out}}
Line 454 ⟶ 812:
=={{header|Kotlin}}==
{{trans|Python}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.math.BigInteger
Line 488 ⟶ 846:
println("First 2001 digits of the square root of 2:")
println(b.iRoot(2))
}</langsyntaxhighlight>
 
{{out}}
Line 502 ⟶ 860:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function root(base, n)
if base < 2 then return base end
if n == 0 then return 1 end
Line 528 ⟶ 886:
print("3rd root of 8 = " .. root(8, 3))
print("3rd root of 9 = " .. root(9, 3))
print("2nd root of " .. b .. " = " .. root(b, 2))</langsyntaxhighlight>
{{out}}
<pre>3rd root of 8 = 2
Line 535 ⟶ 893:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE IntegerRoot;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
Line 595 ⟶ 953:
 
ReadChar
END IntegerRoot.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
{{libheader|bignum}}
<syntaxhighlight lang="nim">import bignum
 
proc root(x: Int; n: int): Int =
if x < 2: return x
let n1 = (n - 1).culong
var c = newInt(1)
var d = (n1 + x) div n
var e = (n1 * d + x div d.pow(n1)) div n
while c != d and c != e:
c = d
d = e
e = (n1 * e + x div e.pow(n1)) div n
result = if d < e: d else: e
 
 
var x: Int
x = newInt(8)
echo "3rd integer root of 8 = ", x.root(3)
x = newInt(9)
echo "3rd integer root of 9 = ", x.root(3)
x = newInt(100).pow(2000) * newInt(2)
echo "First 2001 digits of the square root of 2:"
let s = $x.root(2)
for i in countup(0, s.high, 87): echo s.substr(i, i + 86)</syntaxhighlight>
 
{{out}}
<pre>3rd integer root of 8 = 2
3rd integer root of 9 = 2
First 2001 digits of the square root of 2:
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038
753432764157273501384623091229702492483605585073721264412149709993583141322266592750559
275579995050115278206057147010955997160597027453459686201472851741864088919860955232923
048430871432145083976260362799525140798968725339654633180882964062061525835239505474575
028775996172983557522033753185701135437460340849884716038689997069900481503054402779031
645424782306849293691862158057846311159666871301301561856898723723528850926486124949771
542183342042856860601468247207714358548741556570696776537202264854470158588016207584749
226572260020855844665214583988939443709265918003113882464681570826301005948587040031864
803421948972782906410450726368813137398552561173220402450912277002269411275736272804957
381089675040183698683684507257993647290607629969413804756548237289971803268024744206292
691248590521810044598421505911202494413417285314781058036033710773091828693147101711116
839165817268894197587165821521282295184884720896946338628915628827659526351405422676532
396946175112916024087155101351504553812875600526314680171274026539694702403005174953188
629256313851881634780015693691768818523786840522878376293892143006558695686859645951555
016447245098368960368873231143894155766510408839142923381132060524336294853170499157717
562285497414389991880217624309652065642118273167262575395947172559346372386322614827426
222086711558395999265211762526989175409881593486400834570851814722318142040704265090565
323333984364578657967965192672923998753666172159825788602633636178274959942194037777536
814262177387991945513972312740668983299898953867288228563786977496625199665835257761989
393228453447356947949629521688914854925389047558288345260965240965428893945386466257449
275563819644103169798330618520193793849400571563337205480685405758679996701213722394758
214263065851322174088323829472876173936474678374319600015921888073478576172522118674904
249773669292073110963697216089337086611567345853348332952546758516447107578486024636008</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">sqrtnint(8,3)
sqrtnint(9,3)
sqrtnint(2*100^2000,2)</langsyntaxhighlight>
{{out}}
<pre>%1 = 2
Line 608 ⟶ 1,022:
=={{header|Perl}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="perl">use bigint;
 
sub integer_root {
Line 624 ⟶ 1,038:
print integer_root( 3, 8), "\n";
print integer_root( 3, 9), "\n";
print integer_root( 2, 2 * 100 ** 2000), "\n";</langsyntaxhighlight>
{{out}}
<pre>2
Line 632 ⟶ 1,046:
===Using a module===
If using bigints, we can do this directly, which will be much faster than the method above:
<langsyntaxhighlight lang="perl">use bigint;
print 8->babs->broot(3),"\n";
print 9->babs->broot(3),"\n";
print +(2*100**2000)->babs->broot(2),"\n";</langsyntaxhighlight>
 
The <code>babs</code> calls are only necessary if the input might be non-negative.
 
Even faster, using a module:
<langsyntaxhighlight lang="perl">use bigint;
use ntheory "rootint";
print rootint(8,3),"\n";
print rootint(9,3),"\n";
print rootint(2*100**2000,2),"\n";</langsyntaxhighlight>
 
Both generate the same output as above.
 
=={{header|Perl 6}}==
{{trans|Python}}
<lang perl6>sub integer_root ( Int $p where * >= 2, Int $n --> Int ) {
my Int $d = $p - 1;
my $guess = 10**($n.chars div $p);
my $iterator = { ( $d * $^x + $n div ($^x ** $d) ) div $p };
my $endpoint = { $^x ** $p <= $n
and ($^x + 1) ** $p > $n };
min (+$guess, $iterator ... $endpoint)[*-1, *-2];
}
 
say integer_root( 2, 2 * 100 ** 2000 );</lang>
{{out}}
<pre>141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008
</pre>
 
=={{header|Phix}}==
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include mpfr.e
<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;">function</span> <span style="color: #000000;">integer_root</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
function integer_root(integer n, object A)
<span style="color: #000080;font-style:italic;">-- a must be integer or string
-- yields the nth root of A, adapted from https://en.wikipedia.org/wiki/Nth_root_algorithm
-- (were a an mpz resyou would have to invoke mpz_init_set(), =not mpz_init(1),
-- or better yet pass a as the second parameter of mpz_root() instead.)</span>
x = mpz_init(),
<span style="color: #004080;">mpz</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
delta = mpz_init()
<span style="color: #7060A8;">mpz_nthroot</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
A = mpz_init(A)
<span style="color: #008080;">return</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
while true do
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
mpz_set(x,A)
mpz_pow_ui(delta,res,n-1)
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
mpz_fdiv_q(x, x, delta)
<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;">"3rd root of 8 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">integer_root</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)})</span>
mpz_sub(delta,x,res)
<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;">"3rd root of 9 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">integer_root</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)})</span>
{} = mpz_fdiv_q_ui(delta, delta, n)
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">integer_root</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2"</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4000</span><span style="color: #0000FF;">))</span>
if mpz_cmp_si(delta,0)=0 then exit end if
<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 square 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>
mpz_add(res,res,delta)
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">integer_root</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2"</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6000</span><span style="color: #0000FF;">))</span>
end while
<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>
return mpz_get_str(res)
<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>
end function
<!--</syntaxhighlight>-->
printf(1,"3rd root of 8 = %s\n", {integer_root(3,8)})
printf(1,"3rd root of 9 = %s\n", {integer_root(3,9)})
string s = integer_root(2,"2"&repeat('0',4000))
printf(1,"First digits of the square root of 2: %s\n", {shorten(s)})
s = integer_root(3,"2"&repeat('0',6000))
printf(1,"First digits of the cube root of 2: %s\n", {shorten(s)})</lang>
{{out}}
<pre>
3rd root of 8 = 2
3rd root of 9 = 2
First digits of the square root of 2: 141421356237309504814142135623730950488...710757848602463600847107578486024636008 (20012,001 digits)
First digits of the cube root of 2: 125992104989487316412599210498948731647...254682835318304706122546828353183047061 (20012,001 digits)
"0.4s"
</pre>
While this finishes near-instantly on the desktop, it takes about 25s under pwa/p2js.
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def root(a, b):
if b < 2:
return b
Line 715 ⟶ 1,111:
print("First 2,001 digits of the square root of two:\n{}".format(
root(2, 2 * 100 ** 2000)
))</langsyntaxhighlight>
 
{{out}}
<pre>First 2,001 digits of the square root of two:
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008</pre>
 
=={{header|Quackery}}==
{{trans|Python}}
<syntaxhighlight lang="quackery"> [ stack ] is a-1 ( --> s )
[ stack ] is b ( --> s )
 
[ a-1 share tuck 2dup *
unrot **
b share swap / +
swap 1+ / ] is nextapprox ( n --> n )
[ over 2 < iff drop done
1 - a-1 put
b put
1
2 times [ dup nextapprox ]
[ dip [ 2dup = rot ]
tuck = rot or not while
dup nextapprox again ]
min
b release a-1 release ] is root ( n n --> n )
 
say "3rd root of 8 = " 8 3 root echo cr
say "3rd root of 9 = " 9 3 root echo cr
say "First 2001 digits of the square root of 2: "
2 100 2000 ** * 2 root echo cr</syntaxhighlight>
 
{{out}}
 
<pre>3rd root of 8 = 2
3rd root of 9 = 2
First 2001 digits of the square root of 2: 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008
</pre>
 
=={{header|Racket}}==
 
See [[#Scheme]], there&rsquo;s very little can be done to improve it.
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Python}}
<syntaxhighlight lang="raku" line>sub integer_root ( Int $p where * >= 2, Int $n --> Int ) {
my Int $d = $p - 1;
(
10**($n.chars div $p),
{ ( $d * $^x + $n div ($x ** $d) ) div $p } ...
-> $a, $, $c { $a == $c }
).tail(2).min;
}
 
say integer_root( 2, 2 * 100 ** 2000 );</syntaxhighlight>
{{out}}
<pre>141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008
</pre>
 
=={{header|REXX}}==
Line 732 ⟶ 1,178:
<br>multiply the guess ['''G'''] by unity, &nbsp; and no need to compute the guess to the 1<sup>st</sup> power, &nbsp; bypassing some trivial arithmetic).
===integer result only===
<langsyntaxhighlight 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*/
if num=='' | num=="," then num= 2 /*Not specified? Then use the default.*/
Line 754 ⟶ 1,200:
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
return left(g,p) /*return the Nth root of Z to invoker.*/</langsyntaxhighlight>
'''output''' &nbsp; when the defaults are being used:
<pre>
Line 777 ⟶ 1,223:
===true results===
<br>Negative and complex roots are supported. &nbsp; The expressed root may have a decimal point.
<langsyntaxhighlight 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*/
if num=='' | num=="," then num= 2 /*Not specified? Then use the default.*/
Line 811 ⟶ 1,257:
numeric digits od /*set numeric digits to the original.*/
if oy<0 then return (1/_)i /*Is the root negative? Use reciprocal*/
return (_/1)i /*return the Yth root of X to invoker.*/</langsyntaxhighlight>
'''output''' &nbsp; when the defaults are being used:
<pre>
Line 851 ⟶ 1,297:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Integer roots
 
Line 865 ⟶ 1,311:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 872 ⟶ 1,318:
3
</pre>
 
=={{header|RPL}}==
{{trans|Python}}
« DUP 1 -
→ x n n1
« '''IF''' x 2 < '''THEN''' x
'''ELSE'''
« n1 OVER * x 3 PICK n1 ^ / IP + n / IP »
→ func
« 1 func EVAL func EVAL
'''WHILE''' ROT DUP2 ≠ SWAP 4 PICK ≠ AND
'''REPEAT''' func EVAL '''END'''
MIN
»
'''END'''
» » '<span style="color:blue">IROOT</span>' STO <span style="color:grey"> @ ''( x n → root ) with root^n ≤ x</span>''
 
=={{header|Ruby}}==
{{trans|Python, zkl}}
<langsyntaxhighlight lang="ruby">def root(a,b)
return b if b<2
a1, c = a-1, 1
Line 887 ⟶ 1,349:
puts "First 2,001 digits of the square root of two:"
puts root(2, 2*100**2000)
</syntaxhighlight>
</lang>
{{out}}<pre>First 2,001 digits of the square root of two:
14142135623730950488016887242096(...)46758516447107578486024636008</pre>
 
=={{header|Rust}}==
The rug crate provides the functionality required for this task.
<syntaxhighlight lang="rust">// [dependencies]
// rug = "1.9"
 
fn shorten(s: &str, digits: usize) -> String {
if s.len() <= digits + 3 {
return String::from(s);
}
format!("{}...{}", &s[0..digits/2], &s[s.len()-digits/2..])
}
 
fn main() {
use rug::{ops::Pow, Integer};
 
let x = Integer::from(8);
let r = Integer::from(x.root_ref(3));
println!("Integer cube root of {}: {}", x, r);
 
let x = Integer::from(9);
let r = Integer::from(x.root_ref(3));
println!("Integer cube root of {}: {}", x, r);
 
let mut x = Integer::from(100).pow(2000);
x *= 2;
let s = Integer::from(x.root(2)).to_string();
println!("First {} digits of the square root of 2:\n{}", s.len(), shorten(&s, 70));
 
let mut x = Integer::from(100).pow(3000);
x *= 2;
let s = Integer::from(x.root(3)).to_string();
println!("First {} digits of the cube root of 2:\n{}", s.len(), shorten(&s, 70));
}</syntaxhighlight>
 
{{out}}
<pre>
Integer cube root of 8: 2
Integer cube root of 9: 2
First 2001 digits of the square root of 2:
14142135623730950488016887242096980...32952546758516447107578486024636008
First 2001 digits of the cube root of 2:
12599210498948731647672106072782283...28546452083111122546828353183047061
</pre>
 
=={{header|Scala}}==
===Functional solution, tail recursive, no immutables===
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
 
object IntegerRoots extends App {
Line 920 ⟶ 1,426:
}
 
}</langsyntaxhighlight>
{{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}}==
{{trans|Python}}
<langsyntaxhighlight lang="scheme">(define (root a b)
(define // quotient)
(define (y a a1 b c d e)
Line 940 ⟶ 1,446:
(display "First 2,001 digits of the cube root of two:\n")
(display (root 3 (* 2 (expt 1000 2000))))</langsyntaxhighlight>
 
{{out}}
Line 948 ⟶ 1,454:
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">func root(a, b) {
b < 2 && return(b)
var (a1, c) = (a-1, 1)
Line 961 ⟶ 1,467:
 
say "First 2,001 digits of the square root of two:"
say root(2, 2 * 100**2000)</langsyntaxhighlight>
{{out}}
<pre>
Line 972 ⟶ 1,478:
 
On the other hand, everything is very straightforward, no libraries necessary.
<langsyntaxhighlight lang="tcl">
proc root {this n} {
if {$this < 2} {return $this}
Line 992 ⟶ 1,498:
puts [root 9 3]
puts [root [expr 2* (100**2000)] 2]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,002 ⟶ 1,508:
=={{header|Visual Basic .NET}}==
{{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''<langsyntaxhighlight lang="vbnet">Imports System
Imports System.Numerics
Imports Microsoft.VisualBasic.Strings
Line 1,050 ⟶ 1,556:
 
End Module
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:64ex;overflow:scroll">
Line 1,065 ⟶ 1,571:
Integer 7th Root of 2, (actually 2 * 10 ^ 4004):
110408951367381233764950538762334472132532660078012416551453246414210632288038098071659828988630200514689715906557993125396921468043085579651064805838808196163919864392215583814551234397476339507890664685902921180613942144056283519219500774011043913929222338953790376732070503206390380988494445707084527925240582730725486467967183681658942999591682242459036160190261150569028438652686935172086652456800484770182207006433466758082204482396098451455092224240860882545144206285044829838431779372151867676523068340672781132725205233485925077681104722131036524174667129439905032</pre>
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">import "./big" for BigInt
 
// only use for integers less than 2^53
var intRoot = Fn.new { |x, n|
if (!(x is Num && x.isInteger && x >= 0)) {
Fiber.abort("First argument must be a non-negative integer.")
}
if (!(n is Num && x.isInteger && x >= 1)) {
Fiber.abort("Second argument must be a positive integer.")
}
return x.pow(1/n).floor
}
 
var a = [ [8, 3], [9, 3], [2e18, 2] ]
for (e in a) {
var x = e[0]
var n = e[1]
System.print("%(x) ^ (1/%(n)) = %(intRoot.call(x, n))")
}
 
System.print("\nFirst 2001 digits of the square root of 2:")
System.print((BigInt.two * BigInt.new(100).pow(2000)).isqrt)</syntaxhighlight>
 
{{out}}
<pre>
8 ^ (1/3) = 2
9 ^ (1/3) = 2
2e+18 ^ (1/2) = 1414213562
 
First 2001 digits of the square root of 2:
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func real IRoot(X, N);
real X, N;
return Floor(Pow(X, 1./N));
 
[Format(1, 0);
RlOut(0, IRoot(8., 3.)); CrLf(0);
RlOut(0, IRoot(9., 3.)); CrLf(0);
RlOut(0, IRoot(2e18, 2.)); CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
2
2
1414213562
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">sub root(n, x)
for nr = floor(sqr(x)) to 1 step -1
if (nr ^ n) <= x then return nr : fi
next nr
end sub
 
print root(3, 8)
print root(3, 9)
print root(4, 167)
end</syntaxhighlight>
 
 
=={{header|zkl}}==
{{trans|Python}}
Uses GNU GMP library
<langsyntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum");
fcn root(n,r){
f:='wrap(z){ (n/z.pow(r-1) + z*(r-1))/r or 1 }; //--> v or 1
Line 1,075 ⟶ 1,646:
while(c!=d and c!=e){ c,d,e=d,e,f(e) }
if(d<e) d else e
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">a:=BN(100).pow(2000)*2;
println("Does GMP agree: ",root(a,3)==a.root(3));</langsyntaxhighlight>
{{out}}
<pre>
1,981

edits