Integer roots: Difference between revisions

m
Added Easylang
No edit summary
m (Added Easylang)
 
(11 intermediate revisions by 7 users not shown)
Line 46:
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>
 
Line 263 ⟶ 319:
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}}==
Line 541 ⟶ 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.
 
<syntaxhighlight lang="j"> 9!:37]0 4096 0 222 NB. set display truncation sufficiently high for our results
Line 1,010 ⟶ 1,162:
10**($n.chars div $p),
{ ( $d * $^x + $n div ($x ** $d) ) div $p } ...
{-> $_**a, $p ≤, $nc <{ ($_+1)**a == $pc }
).tail(2).min;
}
 
Line 1,166 ⟶ 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}}==
Line 1,405 ⟶ 1,573:
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">import "./big" for BigInt
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|
// 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.")
Line 1,421 ⟶ 1,591:
var n = e[1]
System.print("%(x) ^ (1/%(n)) = %(intRoot.call(x, n))")
}
}</syntaxhighlight>
 
System.print("\nFirst 2001 digits of the square root of 2:")
System.print((BigInt.two * BigInt.new(100).pow(2000)).isqrt)</syntaxhighlight>
 
{{out}}
Line 1,428 ⟶ 1,601:
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}}==
1,981

edits