Isqrt (integer square root) of X: Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
m →‎{{header|Phix}}: added syntax colouring the hard way
PureFox (talk | contribs)
m →‎{{header|Wren}}: Minor tidy
 
(87 intermediate revisions by 26 users not shown)
Line 97:
:*   [[Square_Root_by_Hand|square root by hand]]
<br><br>
 
=={{header|11l}}==
{{trans|D}}
 
<syntaxhighlight lang="11l">F commatize(number, step = 3, sep = ‘,’)
V s = reversed(String(number))
String r = s[0]
L(i) 1 .< s.len
I i % step == 0
r ‘’= sep
r ‘’= s[i]
R reversed(r)
 
F isqrt(BigInt x)
assert(x >= 0)
 
V q = BigInt(1)
L q <= x
q *= 4
 
V z = x
V r = BigInt(0)
L q > 1
q I/= 4
V t = z - r - q
r I/= 2
I t >= 0
z = t
r += q
 
R r
 
print(‘The integer square root of integers from 0 to 65 are:’)
L(i) 66
print(isqrt(BigInt(i)), end' ‘ ’)
print()
 
print(‘The integer square roots of powers of 7 from 7^1 up to 7^73 are:’)
print(‘power 7 ^ power integer square root’)
print(‘----- --------------------------------------------------------------------------------- -----------------------------------------’)
V pow7 = BigInt(7)
V bi49 = BigInt(49)
L(i) (1..73).step(2)
print(‘#2 #84 #41’.format(i, commatize(pow7), commatize(isqrt(pow7))))
pow7 *= bi49</syntaxhighlight>
 
{{out}}
<pre>
The integer square root of integers from 0 to 65 are:
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
The integer square roots of powers of 7 from 7^1 up to 7^73 are:
power 7 ^ power integer square root
----- --------------------------------------------------------------------------------- -----------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|Ada}}==
{{works with|Ada 2022}}
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Numerics.Big_Numbers.Big_Integers;
with Ada.Strings.Fixed;
 
procedure Integer_Square_Root is
 
use Ada.Numerics.Big_Numbers.Big_Integers;
use Ada.Text_Io;
 
function Isqrt (X : Big_Integer) return Big_Integer is
Q : Big_Integer := 1;
Z, T, R : Big_Integer;
begin
while Q <= X loop
Q := Q * 4;
end loop;
Z := X;
R := 0;
while Q > 1 loop
Q := Q / 4;
T := Z - R - Q;
R := R / 2;
if T >= 0 then
Z := T;
R := R + Q;
end if;
end loop;
return R;
end Isqrt;
 
function Commatize (N : Big_Integer; Width : Positive) return String is
S : constant String := To_String (N, Width);
Image : String (1 .. Width + Width / 3) := (others => ' ');
Pos : Natural := Image'Last;
begin
for I in S'Range loop
Image (Pos) := S (S'Last - I + S'First);
exit when Image (Pos) = ' ';
Pos := Pos - 1;
if I mod 3 = 0 and S (S'Last - I + S'First - 1) /= ' ' then
Image (Pos) := ''';
Pos := Pos - 1;
end if;
end loop;
return Image;
end Commatize;
 
type Mode_Kind is (Tens, Ones, Spacer, Result);
begin
Put_Line ("Integer square roots of integers 0 .. 65:");
for Mode in Mode_Kind loop
for N in 0 .. 65 loop
case Mode is
when Tens => Put ((if N / 10 = 0
then " "
else Natural'Image (N / 10)));
when Ones => Put (Natural'Image (N mod 10));
when Spacer => Put ("--");
when Result => Put (To_String (Isqrt (To_Big_Integer (N))));
end case;
end loop;
New_Line;
end loop;
New_Line;
 
declare
package Integer_Io is new Ada.Text_Io.Integer_Io (Natural);
use Ada.Strings.Fixed;
N : Integer := 1;
P, R : Big_Integer;
begin
Put_Line ("| N|" & 80 * " " & "7**N|" & 30 * " " & "isqrt (7**N)|");
Put_Line (133 * "=");
loop
P := 7**N;
R := Isqrt (P);
Put ("|"); Integer_Io.Put (N, Width => 3);
Put ("|"); Put (Commatize (P, Width => 63));
Put ("|"); Put (Commatize (R, Width => 32));
Put ("|"); New_Line;
exit when N >= 73;
N := N + 2;
end loop;
Put_Line (133 * "=");
end;
 
end Integer_Square_Root;</syntaxhighlight>
{{out}}
<pre>
Integer square roots of integers 0 .. 65:
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
------------------------------------------------------------------------------------------------------------------------------------
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
| N| 7**N| isqrt (7**N)|
=====================================================================================================================================
| 1| 7| 2|
| 3| 343| 18|
| 5| 16'807| 129|
| 7| 823'543| 907|
| 9| 40'353'607| 6'352|
| 11| 1'977'326'743| 44'467|
| 13| 96'889'010'407| 311'269|
| 15| 4'747'561'509'943| 2'178'889|
| 17| 232'630'513'987'207| 15'252'229|
| 19| 11'398'895'185'373'143| 106'765'608|
| 21| 558'545'864'083'284'007| 747'359'260|
| 23| 27'368'747'340'080'916'343| 5'231'514'822|
| 25| 1'341'068'619'663'964'900'807| 36'620'603'758|
| 27| 65'712'362'363'534'280'139'543| 256'344'226'312|
| 29| 3'219'905'755'813'179'726'837'607| 1'794'409'584'184|
| 31| 157'775'382'034'845'806'615'042'743| 12'560'867'089'291|
| 33| 7'730'993'719'707'444'524'137'094'407| 87'926'069'625'040|
| 35| 378'818'692'265'664'781'682'717'625'943| 615'482'487'375'282|
| 37| 18'562'115'921'017'574'302'453'163'671'207| 4'308'377'411'626'977|
| 39| 909'543'680'129'861'140'820'205'019'889'143| 30'158'641'881'388'842|
| 41| 44'567'640'326'363'195'900'190'045'974'568'007| 211'110'493'169'721'897|
| 43| 2'183'814'375'991'796'599'109'312'252'753'832'343| 1'477'773'452'188'053'281|
| 45| 107'006'904'423'598'033'356'356'300'384'937'784'807| 10'344'414'165'316'372'973|
| 47| 5'243'338'316'756'303'634'461'458'718'861'951'455'543| 72'410'899'157'214'610'812|
| 49| 256'923'577'521'058'878'088'611'477'224'235'621'321'607| 506'876'294'100'502'275'687|
| 51| 12'589'255'298'531'885'026'341'962'383'987'545'444'758'743| 3'548'134'058'703'515'929'815|
| 53| 616'873'509'628'062'366'290'756'156'815'389'726'793'178'407| 24'836'938'410'924'611'508'707|
| 55| 30'226'801'971'775'055'948'247'051'683'954'096'612'865'741'943| 173'858'568'876'472'280'560'953|
| 57| 1'481'113'296'616'977'741'464'105'532'513'750'734'030'421'355'207| 1'217'009'982'135'305'963'926'677|
| 59| 72'574'551'534'231'909'331'741'171'093'173'785'967'490'646'405'143| 8'519'069'874'947'141'747'486'745|
| 61| 3'556'153'025'177'363'557'255'317'383'565'515'512'407'041'673'852'007| 59'633'489'124'629'992'232'407'216|
| 63| 174'251'498'233'690'814'305'510'551'794'710'260'107'945'042'018'748'343| 417'434'423'872'409'945'626'850'517|
| 65| 8'538'323'413'450'849'900'970'017'037'940'802'745'289'307'058'918'668'807| 2'922'040'967'106'869'619'387'953'625|
| 67| 418'377'847'259'091'645'147'530'834'859'099'334'519'176'045'887'014'771'543| 20'454'286'769'748'087'335'715'675'381|
| 69| 20'500'514'515'695'490'612'229'010'908'095'867'391'439'626'248'463'723'805'607| 143'180'007'388'236'611'350'009'727'669|
| 71| 1'004'525'211'269'079'039'999'221'534'496'697'502'180'541'686'174'722'466'474'743| 1'002'260'051'717'656'279'450'068'093'686|
| 73| 49'221'735'352'184'872'959'961'855'190'338'177'606'846'542'622'561'400'857'262'407| 7'015'820'362'023'593'956'150'476'655'802|
=====================================================================================================================================
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Implements the task pseudo-code.
<langsyntaxhighlight lang="algol68">BEGIN # Integer square roots #
PR precision 200 PR
# returns the integer square root of x; x must be >= 0 #
Line 166 ⟶ 396:
p7 *:= 49
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 210 ⟶ 440:
71| 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743| 1,002,260,051,717,656,279,450,068,093,686
73|49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407| 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|ALGOL W}}==
Algol W integers are restricted to signed 32-bit, so only the roots of the powers of 7 up to 7^9 are shown (7^11 will fit in 32-bits but the smallest power of 4 higher than 7^11 will overflow).
<syntaxhighlight lang="algolw">
begin % Integer square roots by quadratic residue %
% returns the integer square root of x - x must be >= 0 %
integer procedure iSqrt ( integer value x ) ;
if x < 0 then begin assert x >= 0; 0 end
else if x < 2 then x
else begin
% x is greater than 1 %
integer q, r, t, z;
% find a power of 4 that's greater than x %
q := 1;
while q <= x do q := q * 4;
% find the root %
z := x;
r := 0;
while q > 1 do begin
q := q div 4;
t := z - r - q;
r := r div 2;
if t >= 0 then begin
z := t;
r := r + q
end if_t_ge_0
end while_q_gt_1 ;
r
end isqrt;
% writes n in 14 character positions with separator commas %
procedure writeonWithCommas ( integer value n ) ;
begin
string(10) decDigits;
string(14) r;
integer v, cPos, dCount;
decDigits := "0123456789";
v := abs n;
r := " ";
r( 13 // 1 ) := decDigits( v rem 10 // 1 );
v := v div 10;
cPos := 12;
dCount := 1;
while cPos > 0 and v > 0 do begin
r( cPos // 1 ) := decDigits( v rem 10 // 1 );
v := v div 10;
cPos := cPos - 1;
dCount := dCount + 1;
if v not = 0 and dCount = 3 then begin
r( cPos // 1 ) := ",";
cPos := cPos - 1;
dCount := 0
end if_v_ne_0_and_dCount_eq_3
end for_cPos;
r( cPos // 1 ) := if n < 0 then "-" else " ";
writeon( s_w := 0, r )
end writeonWithCommas ;
begin % task test cases %
integer prevI, prevR, root, p7;
write( "Integer square roots of 0..65 (values the same as the previous one not shown):" );
write();
prevR := prevI := -1;
for i := 0 until 65 do begin
root := iSqrt( i );
if root not = prevR then begin
prevR := root;
prevI := i;
writeon( i_w := 1, s_w := 0, " ", i, ":", root )
end
else if prevI = i - 1 then writeon( "..." );
end for_i ;
write();
% integer square roots of odd powers of 7 %
write( "Integer square roots of 7^n, odd n" );
write( " n| 7^n| isqrt(7^n)" );
write( " -+--------------+--------------" );
p7 := 7;
for p := 1 step 2 until 9 do begin
write( i_w := 2, s_w := 0, p );
writeon( s_w := 0, "|" ); writeonWithCommas( p7 );
writeon( s_w := 0, "|" ); writeonWithCommas( iSqrt( p7 ) );
p7 := p7 * 49
end for_p
end task_test_cases
end.
</syntaxhighlight>
{{out}}
<pre>
Integer square roots of 0..65 (values the same as the previous one not shown):
0:0 1:1... 4:2... 9:3... 16:4... 25:5... 36:6... 49:7... 64:8...
 
Integer square roots of 7^n, odd n
n| 7^n| isqrt(7^n)
-+--------------+--------------
1| 7| 2
3| 343| 18
5| 16,807| 129
7| 823,543| 907
9| 40,353,607| 6,352
</pre>
 
=={{header|ALGOL-M}}==
The code presented here follows the task description. But be warned: there is a bug lurking in the algorithm as presented. The statement q := q * 4 in the first while loop will overflow the limits of ALGOL-M's integer data type (-16,383 to +16,383) for any value of x greater than 4095 and trigger an endless loop. The output has been put into columnar form to avoid what would otherwise be an ugly mess on a typical 80 column display.
<syntaxhighlight lang="algol">
BEGIN
 
COMMENT
RETURN INTEGER SQUARE ROOT OF N USING QUADRATIC RESIDUE
ALGORITHM. WARNING: THE FUNCTION WILL FAIL FOR X GREATER
THAN 4095;
INTEGER FUNCTION ISQRT(X);
INTEGER X;
BEGIN
INTEGER Q, R, Z, T;
Q := 1;
WHILE Q <= X DO
Q := Q * 4; % WARNING! OVERFLOW YIELDS 0 %
Z := X;
R := 0;
WHILE Q > 1 DO
BEGIN
Q := Q / 4;
T := Z - R - Q;
R := R / 2;
IF T >= 0 THEN
BEGIN
Z := T;
R := R + Q;
END;
END;
ISQRT := R;
END;
 
COMMENT - LET'S EXERCISE THE FUNCTION;
 
INTEGER I, COL;
WRITE("INTEGER SQUARE ROOT OF FIRST 65 NUMBERS:");
WRITE("");
COL := 1;
FOR I := 1 STEP 1 UNTIL 65 DO
BEGIN
WRITEON(ISQRT(I));
COL := COL + 1;
IF COL > 10 THEN
BEGIN
WRITE("");
COL := 1;
END;
END;
 
WRITE("");
WRITE(" N 7^N ISQRT");
WRITE("--------------------");
COMMENT - ODD POWERS OF 7 GREATER THAN 3 WILL CAUSE OVERFLOW;
FOR I := 1 STEP 2 UNTIL 3 DO
BEGIN
INTEGER POW7;
POW7 := 7**I;
WRITE(I, POW7, ISQRT(POW7));
END;
WRITE("THAT'S ALL. GOODBYE.");
 
END
</syntaxhighlight>
An alternative to the quadratic residue approach will allow calculation of the integer square root for the full range of signed integer values supported by ALGOL-M. (The output is identical.)
<syntaxhighlight lang="algol">
% RETURN INTEGER SQUARE ROOT OF N %
INTEGER FUNCTION ISQRT(N);
INTEGER N;
BEGIN
INTEGER R1, R2;
R1 := N;
R2 := 1;
WHILE R1 > R2 DO
BEGIN
R1 := (R1+R2) / 2;
R2 := N / R1;
END;
ISQRT := R1;
END;
</syntaxhighlight>
{{out}}
<pre>
INTEGER SQUARE ROOT OF FIRST 65 NUMBERS:
1 1 1 2 2 2 2 2 3 3
3 3 3 3 3 4 4 4 4 4
4 4 4 4 5 5 5 5 5 5
5 5 5 5 5 6 6 6 6 6
6 6 6 6 6 6 6 6 7 7
7 7 7 7 7 7 7 7 7 7
7 7 7 8 8
 
N 7^N ISQRT
--------------------
1 7 2
3 343 18
THAT'S ALL. GOODBYE.
</pre>
 
Line 215 ⟶ 642:
The odd-powers-of-7 part of the task is limited by the precision of AppleScript reals.
 
<langsyntaxhighlight lang="applescript">on isqrt(x)
set q to 1
repeat until (q > x)
Line 269 ⟶ 696:
end doTask
 
doTask()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Isqrts of integers from 0 to 65:
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
Isqrts of odd powers of 7 from 1 to 17:
Line 283 ⟶ 710:
7^13 (96,889,010,407): 311,269
7^15 (4,747,561,509,943): 2,178,889
7^17 (232,630,513,987,207): 15,252,229"</langsyntaxhighlight>
 
=={{header|APL}}==
 
{{Incorrect|APL| <br><br> The method used by this APL program is '''not''' the method that this task requires, &nbsp; namely: <br><br> quadratic residue. <br><br>}}
 
Works in [[Dyalog APL]]
<syntaxhighlight lang="apl"> i←{x←⍵
<lang APL>i←⌊.5*⍨⊢</lang>
q←(×∘4)⍣{⍺>x}⊢1
⊃{ r z q←⍵
q←⌊q÷4
t←(z-r)-q
r←⌊r÷2
z←z t[1+t≥0]
r←r+q×t≥0
r z q
}⍣{ r z q←⍺
q≤1
}⊢0 x q
}</syntaxhighlight>
{{output}}
<pre>
Line 315 ⟶ 751:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">commatize: function [x][
reverse join.with:"," map split.every: 3 split reverse to :string x => join
]
Line 340 ⟶ 776:
loop range 1 .step: 2 72 'n ->
print [n "\t" commatize isqrt 7^n]</langsyntaxhighlight>
 
{{out}}
Line 381 ⟶ 817:
69 143,180,007,388,236,611,350,009,727,669
71 1,002,260,051,717,656,279,450,068,093,686</pre>
 
=={{header|ATS}}==
 
Big integers are achieved via the GNU Multiple Precision interface, which enforces proper memory management, without the need for a garbage collector. I felt that, in ATS, using GMP would be almost as simple as writing isqrt only for machine-native integers. One has to try it, to see the advantage of using a compiler that tells you when you have left out an initialization or a "free".
 
One might note the construction of a "comma'd numeral" by consing of a linked list. This method will work in any Lisp, ML, etc., also. Here the lists are of type "list_vt" and so enforce proper memory management, even in absence of a garbage collector.
 
 
<syntaxhighlight lang="ats">(*
 
Compile with "myatscc isqrt.dats", thus obtaining an executable called
"isqrt".
 
##myatsccdef=\
patscc -O2 \
-I"${PATSHOME}/contrib/atscntrb" \
-IATS "${PATSHOME}/contrib/atscntrb" \
-D_GNU_SOURCE -DATS_MEMALLOC_LIBC \
-o $fname($1) $1 -lgmp
 
*)
 
#include "share/atspre_staload.hats"
 
(* An interface to GNU Multiple Precision. The type system will help
ensure that you do "mpz_clear" on whatever you allocate. *)
staload "atscntrb-hx-libgmp/SATS/gmp.sats"
 
(* As of this writing, gmp.dats is empty, but it does no harm to
staload it. *)
staload _ = "atscntrb-hx-libgmp/DATS/gmp.dats"
 
fn
find_a_power_of_4_greater_than_x
(x : &mpz, (* Input. *)
q : &mpz? >> mpz) (* Output. *)
: void =
let
fun
loop (x : &mpz, q : &mpz) : void =
if 0 <= mpz_cmp (x, q) then
begin
mpz_mul (q, 4u);
loop (x, q)
end
in
mpz_init_set (q, 1u);
loop (x, q)
end
 
fn
isqrt_and_remainder
(x : &mpz, (* Input. *)
r : &mpz? >> mpz, (* Output: square root. *)
z : &mpz? >> mpz) (* Output: remainder. *)
: void =
let
fun
loop (q : &mpz, z : &mpz, r : &mpz, t : &mpz) : void =
if 0 < mpz_cmp (q, 1u) then
begin
mpz_tdiv_q (q, 4u);
mpz_set_mpz (t, z);
mpz_sub (t, r);
mpz_sub (t, q);
mpz_tdiv_q (r, 2u);
if 0 <= mpz_cmp (t, 0u) then
begin
mpz_set_mpz (z, t);
mpz_add (r, q);
end;
loop (q, z, r, t);
end
 
var q : mpz
var t : mpz
in
find_a_power_of_4_greater_than_x (x, q);
mpz_init_set (z, x);
mpz_init_set (r, 0u);
mpz_init (t);
 
loop (q, z, r, t);
 
mpz_clear (q);
mpz_clear (t);
end
 
fn
isqrt (x : &mpz, (* Input. *)
r : &mpz? >> mpz) (* Output: square root. *)
: void =
let
var z : mpz
in
isqrt_and_remainder (x, r, z);
mpz_clear (z);
end
 
fn
print_n_spaces (n : uint) : void =
let
var i : [i : nat] uint i
in
for (i := 0u; i < n; i := succ i)
print! (" ")
end
 
fn
print_with_commas (n : &mpz,
num_columns : uint) : void =
let
fun
make_list (q : &mpz,
r : &mpz,
lst : List0_vt char,
i : uint) : List_vt char =
if mpz_cmp (q, 0u) = 0 then
lst
else
let
val _ = mpz_tdiv_qr (q, r, 10u)
val ones_place = mpz_get_int (r)
val digit = int2char0 (ones_place + char2i '0')
in
if i = 3u then
let
val lst = list_vt_cons (',', lst)
val lst = list_vt_cons (digit, lst)
in
make_list (q, r, lst, 1u)
end
else
let
val lst = list_vt_cons (digit, lst)
in
make_list (q, r, lst, succ i)
end
end
 
var q : mpz
var r : mpz
 
val _ = mpz_init_set (q, n)
val _ = mpz_init (r)
val char_lst = make_list (q, r, list_vt_nil (), 0u)
val _ = mpz_clear (q)
val _ = mpz_clear (r)
 
fun
print_and_consume_lst (char_lst : List0_vt char) : void =
case+ char_lst of
| ~ list_vt_nil () => ()
| ~ list_vt_cons (head, tail) =>
begin
print! (head);
print_and_consume_lst (tail);
end
 
prval _ = lemma_list_vt_param (char_lst)
val len = i2u (list_vt_length (char_lst))
in
assertloc (len <= num_columns);
print_n_spaces (num_columns - len);
print_and_consume_lst (char_lst)
end
 
fn
do_the_roots_of_0_to_65 () : void =
let
var i : mpz
in
mpz_init_set (i, 0u);
while (mpz_cmp (i, 65u) <= 0)
let
var r : mpz
in
isqrt (i, r);
fprint (stdout_ref, r);
print! (" ");
mpz_add (i, 1u);
mpz_clear (r);
end;
mpz_clear (i);
end
 
fn
do_the_roots_of_odd_powers_of_7 () : void =
let
var seven : mpz
var seven_raised_i : mpz
var i_mpz : mpz
var i : [i : pos] uint i
in
mpz_init_set (seven, 7u);
mpz_init (seven_raised_i);
mpz_init (i_mpz);
for (i := 1u; i <= 73u; i := succ (succ i))
let
var r : mpz
in
mpz_pow_uint (seven_raised_i, seven, i);
isqrt (seven_raised_i, r);
mpz_set_uint (i_mpz, i);
print_with_commas (i_mpz, 2u);
print! (" ");
print_with_commas (seven_raised_i, 84u);
print! (" ");
print_with_commas (r, 43u);
print! ("\n");
mpz_clear (r);
end;
mpz_clear (seven);
mpz_clear (seven_raised_i);
mpz_clear (i_mpz);
end
 
implement
main0 () =
begin
print! ("isqrt(i) for 0 <= i <= 65:\n\n");
do_the_roots_of_0_to_65 ();
print! ("\n\n\n");
print! ("isqrt(7**i) for 1 <= i <= 73, i odd:\n\n");
print! (" i 7**i sqrt(7**i)\n");
print! ("-----------------------------------------------------------------------------------------------------------------------------------\n");
do_the_roots_of_odd_powers_of_7 ();
end</syntaxhighlight>
 
{{out}}
<pre>$ myatscc isqrt.dats
$ ./isqrt
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i sqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">print "Integer square root of first 65 numbers:"
for n = 1 to 65
print ljust(isqrt(n),3);
next n
print : print
print "Integer square root of odd powers of 7"
print " n 7^n isqrt"
print "-"*36
for n = 1 to 21 step 2
pow7 = int(7 ^ n)
print rjust(n,3);rjust(pow7,20);rjust(isqrt(pow7),12)
next n
end
 
function isqrt(x)
q = 1
while q <= x
q *= 4
end while
r = 0
while q > 1
q /= 4
t = x - r - q
r /= 2
if t >= 0 then
x = t
r += q
end if
end while
return int(r)
end function</syntaxhighlight>
 
=={{header|C}}==
{{trans|C++}}
Up to 64-bit limits with no big int library.
<langsyntaxhighlight lang="c">#include <stdint.h>
#include <stdio.h>
 
Line 422 ⟶ 1,169:
printf("%2d | %18lld | %12lld\n", n, p, isqrt(p));
}
}</langsyntaxhighlight>
{{out}}
<pre>Integer square root for numbers 0 to 65:
Line 443 ⟶ 1,190:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <sstream>
Line 504 ⟶ 1,251:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
<pre style="font-size: 11px">
<pre>
Integer square root for numbers 0 to 65:
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
Line 555 ⟶ 1,302:
=={{header|C#|CSharp}}==
{{libheader|System.Numerics}}
<langsyntaxhighlight lang="csharp">using System;
using static System.Console;
using BI = System.Numerics.BigInteger;
Line 580 ⟶ 1,327:
BI p = 7; for (int n = 1; n <= max; n += 2, p *= 49)
WriteLine (s, n, p, isqrt(p)); }
}</langsyntaxhighlight>
{{Out}}
<pre>Integer square root for numbers 0 to 65:
Line 629 ⟶ 1,376:
73 | 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 | 7,015,820,362,023,593,956,150,476,655,802
</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">% This program uses the 'bigint' cluster from PCLU's 'misc.lib'
 
% Integer square root of a bigint
isqrt = proc (x: bigint) returns (bigint)
% Initialize a couple of bigints we will reuse
own zero: bigint := bigint$i2bi(0)
own one: bigint := bigint$i2bi(1)
own two: bigint := bigint$i2bi(2)
own four: bigint := bigint$i2bi(4)
q: bigint := one
while q <= x do q := q * four end
t: bigint
z: bigint := x
r: bigint := zero
while q>one do
q := q / four
t := z - r - q
r := r / two
if t >= zero then
z := t
r := r + q
end
end
return(r)
end isqrt
 
% Format a bigint using commas
fmt = proc (x: bigint) returns (string)
own zero: bigint := bigint$i2bi(0)
own ten: bigint := bigint$i2bi(10)
if x=zero then return("0") end
out: array[char] := array[char]$[]
ds: int := 0
while x>zero do
array[char]$addl(out, char$i2c(bigint$bi2i(x // ten) + 48))
x := x / ten
ds := ds + 1
if x~=zero cand ds//3=0 then
array[char]$addl(out, ',')
end
end
return(string$ac2s(out))
end fmt
start_up = proc ()
po: stream := stream$primary_output()
% print square roots from 0..65
stream$putl(po, "isqrt of 0..65:")
for i: int in int$from_to(0, 65) do
stream$puts(po, fmt(isqrt(bigint$i2bi(i))) || " ")
end
% print square roots of odd powers
stream$putl(po, "\n\nisqrt of odd powers of 7:")
seven: bigint := bigint$i2bi(7)
for p: int in int$from_to_by(1, 73, 2) do
stream$puts(po, "isqrt(7^")
stream$putright(po, int$unparse(p), 2)
stream$puts(po, ") = ")
stream$putright(po, fmt(isqrt(seven ** bigint$i2bi(p))), 41)
stream$putl(po, "")
end
end start_up</syntaxhighlight>
{{out}}
<pre>isqrt of 0..65:
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
isqrt of odd powers of 7:
isqrt(7^ 1) = 2
isqrt(7^ 3) = 18
isqrt(7^ 5) = 129
isqrt(7^ 7) = 907
isqrt(7^ 9) = 6,352
isqrt(7^11) = 44,467
isqrt(7^13) = 311,269
isqrt(7^15) = 2,178,889
isqrt(7^17) = 15,252,229
isqrt(7^19) = 106,765,608
isqrt(7^21) = 747,359,260
isqrt(7^23) = 5,231,514,822
isqrt(7^25) = 36,620,603,758
isqrt(7^27) = 256,344,226,312
isqrt(7^29) = 1,794,409,584,184
isqrt(7^31) = 12,560,867,089,291
isqrt(7^33) = 87,926,069,625,040
isqrt(7^35) = 615,482,487,375,282
isqrt(7^37) = 4,308,377,411,626,977
isqrt(7^39) = 30,158,641,881,388,842
isqrt(7^41) = 211,110,493,169,721,897
isqrt(7^43) = 1,477,773,452,188,053,281
isqrt(7^45) = 10,344,414,165,316,372,973
isqrt(7^47) = 72,410,899,157,214,610,812
isqrt(7^49) = 506,876,294,100,502,275,687
isqrt(7^51) = 3,548,134,058,703,515,929,815
isqrt(7^53) = 24,836,938,410,924,611,508,707
isqrt(7^55) = 173,858,568,876,472,280,560,953
isqrt(7^57) = 1,217,009,982,135,305,963,926,677
isqrt(7^59) = 8,519,069,874,947,141,747,486,745
isqrt(7^61) = 59,633,489,124,629,992,232,407,216
isqrt(7^63) = 417,434,423,872,409,945,626,850,517
isqrt(7^65) = 2,922,040,967,106,869,619,387,953,625
isqrt(7^67) = 20,454,286,769,748,087,335,715,675,381
isqrt(7^69) = 143,180,007,388,236,611,350,009,727,669
isqrt(7^71) = 1,002,260,051,717,656,279,450,068,093,686
isqrt(7^73) = 7,015,820,362,023,593,956,150,476,655,802</pre>
 
 
=={{header|COBOL}}==
 
The COBOL compiler used here is limited to 18-digit math, meaning 7^19 is the largest odd power of 7 that can be calculated.
 
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. I-SQRT.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 QUAD-RET-VARS.
03 X PIC 9(18).
03 Q PIC 9(18).
03 Z PIC 9(18).
03 T PIC S9(18).
03 R PIC 9(18).
 
01 TO-65-VARS.
03 ISQRT-N PIC 99.
03 DISP-LN PIC X(22) VALUE SPACES.
03 DISP-FMT PIC Z9.
03 PTR PIC 99 VALUE 1.
 
01 BIG-SQRT-VARS.
03 POW-7 PIC 9(18) VALUE 7.
03 POW-N PIC 99 VALUE 1.
03 POW-N-OUT PIC Z9.
03 POW-7-OUT PIC Z(10).
 
PROCEDURE DIVISION.
BEGIN.
PERFORM SQRTS-TO-65.
PERFORM BIG-SQRTS.
STOP RUN.
 
SQRTS-TO-65.
PERFORM DISP-SMALL-SQRT
VARYING ISQRT-N FROM 0 BY 1
UNTIL ISQRT-N IS GREATER THAN 65.
 
DISP-SMALL-SQRT.
MOVE ISQRT-N TO X.
PERFORM ISQRT.
MOVE R TO DISP-FMT.
STRING DISP-FMT DELIMITED BY SIZE INTO DISP-LN
WITH POINTER PTR.
IF PTR IS GREATER THAN 22,
DISPLAY DISP-LN,
MOVE 1 TO PTR.
 
BIG-SQRTS.
PERFORM BIG-SQRT 10 TIMES.
BIG-SQRT.
MOVE POW-7 TO X.
PERFORM ISQRT.
MOVE POW-N TO POW-N-OUT.
MOVE R TO POW-7-OUT.
DISPLAY "ISQRT(7^" POW-N-OUT ") = " POW-7-OUT.
ADD 2 TO POW-N.
MULTIPLY 49 BY POW-7.
 
ISQRT.
MOVE 1 TO Q.
PERFORM MUL-Q-BY-4 UNTIL Q IS GREATER THAN X.
MOVE X TO Z.
MOVE ZERO TO R.
PERFORM ISQRT-STEP UNTIL Q IS NOT GREATER THAN 1.
 
MUL-Q-BY-4.
MULTIPLY 4 BY Q.
 
ISQRT-STEP.
DIVIDE 4 INTO Q.
COMPUTE T = Z - R - Q.
DIVIDE 2 INTO R.
IF T IS NOT LESS THAN ZERO,
MOVE T TO Z,
ADD Q TO R.</syntaxhighlight>
{{out}}
<pre> 0 1 1 1 2 2 2 2 2 3 3
3 3 3 3 3 4 4 4 4 4 4
4 4 4 5 5 5 5 5 5 5 5
5 5 5 6 6 6 6 6 6 6 6
6 6 6 6 6 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 8 8
ISQRT(7^ 1) = 2
ISQRT(7^ 3) = 18
ISQRT(7^ 5) = 129
ISQRT(7^ 7) = 907
ISQRT(7^ 9) = 6352
ISQRT(7^11) = 44467
ISQRT(7^13) = 311269
ISQRT(7^15) = 2178889
ISQRT(7^17) = 15252229
ISQRT(7^19) = 106765608</pre>
 
 
=={{header|Common Lisp}}==
{{trans|Scheme}}
 
 
The program is wrapped in a [https://roswell.github.io/ Roswell script]. On a POSIX system with Roswell installed, you can simply run the script.
 
The code is an "imperative" translation of the "functional" Scheme.
 
Side notes:
 
* Straight translations from Scheme to Common Lisp run the risk of running properly with some CL compilers but not others, due to the prevalence of tail calls in Scheme. Common Lisp does not require proper tail calls, although CL compilers do optimize such calls, to varying degrees depending on the compiler.
 
* The simple program here would not require tail call optimization at all; the depth of recursion would be too small. I felt it better to write the CL in "imperative" style nonetheless.
 
* Not all Scheme compilers make all tail calls proper, either, at least by default. This, however, is nonstandard behavior.
 
 
<syntaxhighlight lang="lisp">#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q -- $0 "$@"
|#
(progn ;;init forms
(ros:ensure-asdf)
#+quicklisp(ql:quickload '() :silent t)
)
 
(defpackage :ros.script.isqrt.3860764029
(:use :cl))
(in-package :ros.script.isqrt.3860764029)
 
;;
;; The Rosetta Code integer square root task, in Common Lisp.
;;
;; I translate the tail recursions of the Scheme as regular loops in
;; Common Lisp, although CL compilers most often can optimize tail
;; recursions of the kind. They are not required to, however.
;;
;; As a result, the CL is actually closer to the task's pseudocode
;; than is the Scheme.
;;
;; (The Scheme, by the way, could have been written much as follows,
;; using "set!" where the CL has "setf", and with other such
;; "linguistic" changes.)
;;
 
(defun find-a-power-of-4-greater-than-x (x)
(let ((q 1))
(loop until (< x q)
do (setf q (* 4 q)))
q))
 
(defun isqrt+remainder (x)
(let ((q (find-a-power-of-4-greater-than-x x))
(z x)
(r 0))
(loop until (= q 1)
do (progn (setf q (/ q 4))
(let ((z1 (- z r q)))
(setf r (/ r 2))
(when (<= 0 z1)
(setf z z1)
(setf r (+ r q))))))
(values r z)))
 
(defun rosetta_code_isqrt (x)
(nth-value 0 (isqrt+remainder x)))
 
(defun main (&rest argv)
(declare (ignorable argv))
(format t "isqrt(i) for ~D <= i <= ~D:~2%" 0 65)
(loop for i from 0 to 64
do (format t "~D " (isqrt i)))
(format t "~D~3%" (isqrt 65))
(format t "isqrt(7**i) for ~D <= i <= ~D, i odd:~2%" 1 73)
(format t "~2@A ~84@A ~43@A~%" "i" "7**i" "sqrt(7**i)")
(format t "~A~%" (make-string 131 :initial-element #\-))
(loop for i from 1 to 73 by 2
for 7**i = (expt 7 i)
for root = (rosetta_code_isqrt 7**i)
do (format t "~2D ~84:D ~43:D~%" i 7**i root)))
 
;;; vim: set ft=lisp lisp:</syntaxhighlight>
 
{{out}}
<pre>$ sh isqrt.ros
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i sqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Integer square root
Line 682 ⟶ 1,773:
print_nl();
x := x + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 704 ⟶ 1,795:
isqrt(7^10) = 16807
isqrt(7^11) = 44467</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">alert "integer square root of first 65 numbers:"
 
for n = 1 to 65
 
let x = n
gosub isqrt
print r
 
next n
 
alert "integer square root of odd powers of 7"
cls
cursor 1, 1
 
for n = 1 to 21 step 2
 
let x = 7 ^ n
gosub isqrt
print "isqrt of 7 ^ ", n, " = ", r
 
next n
 
end
 
sub isqrt
 
let q = 1
 
do
 
if q <= x then
 
let q = q * 4
 
endif
 
wait
 
loop q <= x
 
let r = 0
 
do
 
if q > 1 then
 
let q = q / 4
let t = x - r - q
let r = r / 2
 
if t >= 0 then
 
let x = t
let r = r + q
 
endif
 
endif
 
loop q > 1
 
let r = int(r)
 
return</syntaxhighlight>
{{out| Output}}<pre>integer square root of first 65 numbers:
1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
integer square root of odd powers of 7:
isqrt of 7 ^ 1 = 2
isqrt of 7 ^ 3 = 18
isqrt of 7 ^ 5 = 129
isqrt of 7 ^ 7 = 907
isqrt of 7 ^ 9 = 6352</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.bigint;
import std.conv;
import std.exception;
Line 786 ⟶ 1,952:
pow7 *= bi49;
}
}</langsyntaxhighlight>
{{out}}
<pre>The integer square root of integers from 0 to 65 are:
Line 833 ⟶ 1,999:
=={{header|Delphi}}==
See [[#Pascal]].
 
=={{header|Draco}}==
Because all the intermediate values have to fit in a signed 32-bit integer, the largest power of 7
for which the square root can be calculated is 7^10.
<syntaxhighlight lang="draco">/* Integer square root using quadratic residue method */
proc nonrec isqrt(ulong x) ulong:
ulong q, z, r;
long t;
q := 1;
while q <= x do q := q << 2 od;
z := x;
r := 0;
while q > 1 do
q := q >> 2;
t := z - r - q;
r := r >> 1;
if t >= 0 then
z := t;
r := r + q
fi
od;
r
corp
 
proc nonrec main() void:
byte x;
ulong pow7;
/* print isqrt(0) ... isqrt(65) */
for x from 0 upto 65 do
write(isqrt(x):2);
if x % 11 = 10 then writeln() fi
od;
/* print isqrt(7^0) thru isqrt(7^10) */
pow7 := 1;
for x from 0 upto 10 do
writeln("isqrt(7^", x:2, ") = ", isqrt(pow7):5);
pow7 := pow7 * 7
od
corp</syntaxhighlight>
{{out}}
<pre> 0 1 1 1 2 2 2 2 2 3 3
3 3 3 3 3 4 4 4 4 4 4
4 4 4 5 5 5 5 5 5 5 5
5 5 5 6 6 6 6 6 6 6 6
6 6 6 6 6 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 8 8
isqrt(7^ 0) = 1
isqrt(7^ 1) = 2
isqrt(7^ 2) = 7
isqrt(7^ 3) = 18
isqrt(7^ 4) = 49
isqrt(7^ 5) = 129
isqrt(7^ 6) = 343
isqrt(7^ 7) = 907
isqrt(7^ 8) = 2401
isqrt(7^ 9) = 6352
isqrt(7^10) = 16807</pre>
 
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight lang=easylang>
func isqrt x .
q = 1
while q <= x
q *= 4
.
while q > 1
q = q div 4
t = x - r - q
r = r div 2
if t >= 0
x = t
r = r + q
.
.
return r
.
print "Integer square roots from 0 to 65:"
for n = 0 to 65
write isqrt n & " "
.
print ""
print ""
print "Integer square roots of 7^n"
p = 7
n = 1
while n <= 21
print n & " " & isqrt p
n = n + 2
p = p * 49
.
</syntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Find Integer Floor sqrt of a Large Integer. Nigel Galloway: July 17th., 2020
let Isqrt n=let rec fN i g l=match(l>0I,i-g-l) with
Line 844 ⟶ 2,108:
[0I..65I]|>Seq.iter(Isqrt>>string>>printf "%s "); printfn "\n"
let fN n=7I**n in [1..2..73]|>Seq.iter(fN>>Isqrt>>printfn "%a" (fun n g -> n.Write("{0:#,#}", g)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 887 ⟶ 2,151:
7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|Factor}}==
The <code>isqrt</code> word is a straightforward translation of the pseudocode from the task description using lexical variables.
{{works with|Factor|0.99 2020-07-03}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel locals math math.functions
math.ranges prettyprint sequences tools.memory.private ;
 
Line 916 ⟶ 2,181:
"x" "7^x" "isqrt(7^x)" align
"-" "---" "----------" align
1 73 2 <range> [ show ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 962 ⟶ 2,227:
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|Fish}}==
A function (isolated stack; remove the 1[ and ] instructions to create "inline" version) that calculates and outputs the square root of the top of the stack/input number.
 
Código sacado de https://esolangs.org/wiki/Fish
<syntaxhighlight lang="fish">1[:>:r:@@:@,\;
]~$\!?={:,2+/n</syntaxhighlight>
 
=={{header|Forth}}==
Only handles odd powers of 7 up to 7^21.
<syntaxhighlight lang="forth">
<lang Forth>
: d., ( n -- ) \ write double precision int, commatized.
tuck dabs
Line 1,007 ⟶ 2,279:
 
task1 cr task2 bye
</syntaxhighlight>
</lang>
This version of the core word does not require locals.
<syntaxhighlight lang="text">: sqrt-rem ( n -- sqrt rem)
>r 0 1 begin dup r@ > 0= while 4 * repeat
begin \ find a power of 4 greater than TORS
dup 1 > \ compute while greater than unity
while
2/ 2/ swap over over + negate r@ + \ integer divide by 4
dup 0< if drop 2/ else r> drop >r 2/ over + then swap
repeat drop r> ( sqrt rem)
;
 
: isqrt-mod sqrt-rem swap ;</syntaxhighlight>
{{Out}}
<pre>
Line 1,026 ⟶ 2,310:
21 747,359,260
</pre>
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">MODULE INTEGER_SQUARE_ROOT
IMPLICIT NONE
 
Line 1,062 ⟶ 2,347:
T = 0
DO WHILE (Q .LTLE. NUM)
Q = Q * 4
END DO
Line 1,123 ⟶ 2,408:
 
END PROGRAM ISQRT_DEMO</langsyntaxhighlight>
<pre>
Integer square root for numbers 0 to 65
Line 1,154 ⟶ 2,439:
43 | 2,183,814,375,991,796,599,109,312,252,753,832,343 | 1,477,773,452,188,053,281
</pre>
 
=={{header|FreeBASIC}}==
Odd powers up to 7^21 are shown; more would require an arbitrary precision library that would just add bloat without being illustrative.
<langsyntaxhighlight lang="freebasic">
function isqrt( byval x as ulongint ) as ulongint
dim as ulongint q = 1, r
Line 1,196 ⟶ 2,482:
ns = str(isqrt(7^i))
print i, commatize(ns)
next i</langsyntaxhighlight>
{{out}}
<pre>0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
Line 1,210 ⟶ 2,496:
19 106,765,608
21 747,359,260</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn IntSqrt( x as SInt64 ) as SInt64
SInt64 q = 1, z = x, r = 0, t
do
q = q * 4
until ( q > x )
while( q > 1 )
q = q / 4 : t = z - r - q : r = r / 2
if ( t > -1 ) then z = t : r = r + q
wend
end fn = r
 
SInt64 p
NSInteger n
CFNumberRef tempNum
CFStringRef tempStr
 
NSLog( @"Integer square root for numbers 0 to 65:" )
 
for n = 0 to 65
NSLog( @"%lld \b", fn IntSqrt( n ) )
next
NSLog( @"\n" )
 
NSLog( @"Integer square roots of odd powers of 7 from 1 to 21:" )
NSLog( @" n | 7 ^ n | fn IntSqrt(7 ^ n)" )
p = 7
for n = 1 to 21 step 2
tempNum = fn NumberWithLongLong( fn IntSqrt(p) )
tempStr = fn NumberDescriptionWithLocale( tempNum, fn LocaleCurrent )
NSLog( @"%2d | %18lld | %12s", n, p, fn StringUTF8String( tempStr ) )
p = p * 49
next
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Integer square root for numbers 0 to 65:
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
Integer square roots of odd powers of 7 from 1 to 21:
n | 7 ^ n | fn IntSqrt(7 ^ n)
1 | 7 | 2
3 | 343 | 18
5 | 16807 | 129
7 | 823543 | 907
9 | 40353607 | 6,352
11 | 1977326743 | 44,467
13 | 96889010407 | 311,269
15 | 4747561509943 | 2,178,889
17 | 232630513987207 | 15,252,229
19 | 11398895185373143 | 106,765,608
21 | 558545864083284007 | 747,359,260
</pre>
 
 
=={{header|Go}}==
Go's big.Int type already has a built-in integer square root function but, as the point of this task appears to be to compute it using a particular algorithm, we re-code it from the pseudo-code given in the task description.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,272 ⟶ 2,619:
pow7.Mul(pow7, bi49)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,321 ⟶ 2,668:
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|Haskell}}==
 
<syntaxhighlight lang="haskell">import Data.Bits
 
isqrt :: Integer -> Integer
isqrt n = go n 0 (q `shiftR` 2)
where
q = head $ dropWhile (< n) $ iterate (`shiftL` 2) 1
go z r 0 = r
go z r q = let t = z - r - q
in if t >= 0
then go t (r `shiftR` 1 + q) (q `shiftR` 2)
else go z (r `shiftR` 1) (q `shiftR` 2)
 
main = do
print $ isqrt <$> [1..65]
mapM_ print $ zip [1,3..73] (isqrt <$> iterate (49 *) 7)</syntaxhighlight>
 
<pre>*Main> main
[0,1,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8]
(1,2)
(3,18)
(5,129)
(7,907)
(9,6352)
(11,44467)
(13,311269)
(15,2178889)
(17,15252229)
(19,106765608)
(21,747359260)
(23,5231514822)
(25,36620603758)
(27,256344226312)
(29,1794409584184)
(31,12560867089291)
(33,87926069625040)
(35,615482487375282)
(37,4308377411626977)
(39,30158641881388842)
(41,211110493169721897)
(43,1477773452188053281)
(45,10344414165316372973)
(47,72410899157214610812)
(49,506876294100502275687)
(51,3548134058703515929815)
(53,24836938410924611508707)
(55,173858568876472280560953)
(57,1217009982135305963926677)
(59,8519069874947141747486745)
(61,59633489124629992232407216)
(63,417434423872409945626850517)
(65,2922040967106869619387953625)
(67,20454286769748087335715675381)
(69,143180007388236611350009727669)
(71,1002260051717656279450068093686)
(73,7015820362023593956150476655802)</pre>
 
=={{header|Icon}}==
 
<syntaxhighlight lang="icon">link numbers # For the "commas" procedure.
link printf
 
procedure main ()
write ("isqrt(i) for 0 <= i <= 65:")
write ()
roots_of_0_to_65()
write ()
write ()
write ("isqrt(7**i) for 1 <= i <= 73, i odd:")
write ()
printf ("%2s %84s %43s\n", "i", "7**i", "sqrt(7**i)")
write (repl("-", 131))
roots_of_odd_powers_of_7()
end
 
procedure roots_of_0_to_65 ()
local i
 
every i := 0 to 64 do writes (isqrt(i), " ")
write (isqrt(65))
end
 
procedure roots_of_odd_powers_of_7 ()
local i, power_of_7, root
 
every i := 1 to 73 by 2 do {
power_of_7 := 7^i
root := isqrt(power_of_7)
printf ("%2d %84s %43s\n", i, commas(power_of_7), commas(root))
}
end
 
procedure isqrt (x)
local q, z, r, t
 
q := find_a_power_of_4_greater_than_x (x)
z := x
r := 0
while 1 < q do {
q := ishift(q, -2)
t := z - r - q
r := ishift(r, -1)
if 0 <= t then {
z := t
r +:= q
}
}
return r
end
 
procedure find_a_power_of_4_greater_than_x (x)
local q
 
q := 1
while q <= x do q := ishift(q, 2)
return q
end</syntaxhighlight>
 
{{out}}
<pre>$ icont -s -u -o isqrt isqrt-in-Icon.icn && ./isqrt
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i sqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
 
 
=={{header|J}}==
Three implementations given. The floating point method is best for small square roots, Newton's method is fastest for extended integers. isqrt adapted from the page preamble. Note that the "floating point method" is exact when arbitrary precision integers or rational numbers are used.
<syntaxhighlight lang="j">
isqrt_float=: <.@:%:
isqrt_newton=: 9&$: :(x:@:<.@:-:@:(] + x:@:<.@:%)^:_&>~&:x:)
 
 
align=: (|.~ i.&' ')"1
comma=: (' ' -.~ [: }: [: , [: (|.) _3 (',' ,~ |.)\ |.)@":&>
While=: {{ u^:(0-.@:-:v)^:_ }}
 
isqrt=: 3 :0&>
y =. x: y
NB. q is a power of 4 that's greater than y. Append 0 0 under binary representation
q =. y (,&0 0x&.:#:@:])While>: 1x
z =. y NB. set z to the value of y.
r =. 0x NB. initialize r to zero.
while. 1 < q do. NB. perform while q > unity.
q =. _2&}.&.:#: q NB. integer divide by 4 (-2 drop under binary representation)
t =. (z - r) - q NB. compute value of t.
r =. }:&.:#: r NB. integer divide by two. (curtail under binary representation)
if. 0 <: t do.
z =. t NB. set z to value of t
r =. r + q NB. compute new value of r
end.
end.
NB. r is now the isqrt(y). (most recent value computed)
NB. Sidenote: Also, Z is now the remainder after square root
NB. ie. r^2 + z = y, so if z = 0 then x is a perfect square
NB. r , z
)
</syntaxhighlight>
 
The first line here shows that the simplest approach (the "floating point square root") is treated specially by J.
 
<pre> <.@%: 1000000000000000000000000000000000000000000000000x
1000000000000000000000000
 
(,. isqrt_float) 7x ^ 20 21x
79792266297612001 282475249
558545864083284007 747359260
(,. isqrt_newton) 7x ^ 20 21x
79792266297612001 282475249
558545864083284007 747359260
 
 
align comma (,. isqrt) 7 ^&x: 1 2 p. i. 37
7
2
 
343
18
 
16,807
129
 
823,543
907
 
40,353,607
6,352
 
1,977,326,743
44,467
 
96,889,010,407
311,269
 
4,747,561,509,943
2,178,889
 
232,630,513,987,207
15,252,229
 
11,398,895,185,373,143
106,765,608
 
558,545,864,083,284,007
747,359,260
 
27,368,747,340,080,916,343
5,231,514,822
 
1,341,068,619,663,964,900,807
36,620,603,758
 
65,712,362,363,534,280,139,543
256,344,226,312
 
3,219,905,755,813,179,726,837,607
1,794,409,584,184
 
157,775,382,034,845,806,615,042,743
12,560,867,089,291
 
7,730,993,719,707,444,524,137,094,407
87,926,069,625,040
 
378,818,692,265,664,781,682,717,625,943
615,482,487,375,282
 
18,562,115,921,017,574,302,453,163,671,207
4,308,377,411,626,977
 
909,543,680,129,861,140,820,205,019,889,143
30,158,641,881,388,842
 
44,567,640,326,363,195,900,190,045,974,568,007
211,110,493,169,721,897
 
2,183,814,375,991,796,599,109,312,252,753,832,343
1,477,773,452,188,053,281
 
107,006,904,423,598,033,356,356,300,384,937,784,807
10,344,414,165,316,372,973
 
5,243,338,316,756,303,634,461,458,718,861,951,455,543
72,410,899,157,214,610,812
 
256,923,577,521,058,878,088,611,477,224,235,621,321,607
506,876,294,100,502,275,687
 
12,589,255,298,531,885,026,341,962,383,987,545,444,758,743
3,548,134,058,703,515,929,815
 
616,873,509,628,062,366,290,756,156,815,389,726,793,178,407
24,836,938,410,924,611,508,707
 
30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943
173,858,568,876,472,280,560,953
 
1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207
1,217,009,982,135,305,963,926,677
 
72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143
8,519,069,874,947,141,747,486,745
 
3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007
59,633,489,124,629,992,232,407,216
 
174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343
417,434,423,872,409,945,626,850,517
 
8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807
2,922,040,967,106,869,619,387,953,625
 
418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543
20,454,286,769,748,087,335,715,675,381
 
20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607
143,180,007,388,236,611,350,009,727,669
 
1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743
1,002,260,051,717,656,279,450,068,093,686
 
49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407
7,015,820,362,023,593,956,150,476,655,802
 
NB. isqrt_float is exact for large integers
align comma (,.isqrt_float),7^73x
49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407
7,015,820,362,023,593,956,150,476,655,802
 
 
NB. Newton's method result matches isqrt
(isqrt_newton -: isqrt)7 ^&x: 1 2 p. i. 37
1
NB. An order of magnitude faster and one tenth the space, in j
 
timespacex 'isqrt_newton 7 ^&x: 1 2 p. i. 37'
0.038085 39552
timespacex 'isqrt 7 ^&x: 1 2 p. i. 37'
0.367744 319712
 
NB. but not as fast as isqrt_float, nor as space efficient
timespacex 'isqrt_float 7 ^&x: 1 2 p. i. 37'
0.0005145 152192</pre>
 
Note that isqrt_float (<code><.@%:</code>) is, mechanically, a different operation from floating point square root followed by floor. This difference is probably best thought of as a type issue. For example, <code><.%:7^73x</code> (without the <code>@</code>) produces a result which has the comma delimited integer representation of <tt>7,015,820,362,023,59<i>4,877,495,225,090</i></tt> rather than the <tt>7,015,820,362,023,59<b>3,956,150,476,655,802</b></tt> which we would get from integer square root.
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.math.BigInteger;
 
public class Isqrt {
Line 1,368 ⟶ 3,067:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The integer square root of integers from 0 to 65 are:
Line 1,416 ⟶ 3,115:
{{trans|Julia}}
The following program takes advantage of the support for unbounded-precision
integer arithmetic provided by gojq, the Go implementation of jq, but it can also be run, with different numerical results, using the C implementation.<langsyntaxhighlight lang="jq"># For gojq
def idivide($j):
. as $i
Line 1,452 ⟶ 3,151:
(range( 1;74;2) as $i
| (7 | power($i)) as $p
| "\($i|lpad(2)) \($p|lpad(84)) \($p | isqrt | lpad(43))" )</langsyntaxhighlight>
{{out}}
Invocation: gojq -ncr -f isqrt.jq
Line 1,502 ⟶ 3,201:
{{trans|Go}}
Julia also has a built in isqrt() function which works on integer types, but the function integer_sqrt is shown for the task.
<langsyntaxhighlight lang="julia">using Formatting
 
function integer_sqrt(x)
Line 1,534 ⟶ 3,233:
lpad(format(integer_sqrt(pow7^i), commas=true), 43))
end
</langsyntaxhighlight>{{out}}
<pre>
The integer square roots of integers from 0 to 65 are:
Line 1,584 ⟶ 3,283:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
 
fun isqrt(x: BigInteger): BigInteger {
Line 1,626 ⟶ 3,325:
pow7 *= bi49
}
}</langsyntaxhighlight>
{{out}}
<pre>The integer square root of integers from 0 to 65 are:
Line 1,673 ⟶ 3,372:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function isqrt(x)
local q = 1
local r = 0
Line 1,707 ⟶ 3,406:
n = n + 2
p = p * 49
end</langsyntaxhighlight>
{{out}}
<pre>Integer square root for numbers 0 to 65:
Line 1,726 ⟶ 3,425:
21 | 558545864083284007 | 747359260</pre>
 
=={{header|MADM2000 Interpreter}}==
Using various types up to 7^35
 
<syntaxhighlight lang="m2000 interpreter">
{{incorrect|MAD| <br><br> The algorithm used is not the one that is mandated to be used by the task's requirements: <br><br> finding the integer square root by using a &nbsp;<u>quadratic residue</u>&nbsp; method &nbsp; (as shown my the pseudo-code). <br><br>}}
module integer_square_root (f=-2) {
function IntSqrt(x as long long) {
long long q=1, z=x, t, r
do q*=4&& : until (q>x)
while q>1&&
q|div 4&&:t=z-r-q:r|div 2&&
if t>-1&& then z=t:r+= q
end while
=r
}
long i
print #f, "The integer square root of integers from 0 to 65 are:"
for i=0 to 65
print #f, IntSqrt(i)+" ";
next
print #f
print #f, "Using Long Long Type"
print #f, "The integer square roots of powers of 7 from 7^1 up to 7^21 are:"
for i=1 to 21 step 2 {
print #f, "IntSqrt(7^"+i+")="+(IntSqrt(7&&^i))+" of 7^"+i+" ("+(7&&^I)+")"
}
print #f
function IntSqrt(x as decimal) {
decimal q=1, z=x, t, r
do q*=4 : until (q>x)
while q>1
q/=4:t=z-r-q:r/=2
if t>-1 then z=t:r+= q
end while
=r
}
print #f, "Using Decimal Type"
print #f, "The integer square roots of powers of 7 from 7^23 up to 7^33 are:"
decimal j,p
for i=23 to 33 step 2 {
p=1:for j=1 to i:p*=7@:next
print #f, "IntSqrt(7^"+i+")="+(IntSqrt(p))+" of 7^"+i+" ("+p+")"
}
print #f
function IntSqrt(x as double) {
double q=1, z=x, t, r
do q*=4 : until (q>x)
while q>1
q/=4:t=z-r-q:r/=2
if t>-1 then z=t:r+= q
end while
=r
}
print #f, "Using Double Type"
print #f, "The integer square roots of powers of 7 from 7^19 up to 7^35 are:"
for i=19 to 35 step 2 {
print #f, "IntSqrt(7^"+i+")="+(IntSqrt(7^i))+" of 7^"+i+" ("+(7^i)+")"
}
print #f
}
open "" for output as #f // f = -2 now, direct output to screen
integer_square_root
close #f
open "out.txt" for output as #f
integer_square_root f
close #f
win "notepad", dir$+"out.txt"
</syntaxhighlight>
{{out}}
<pre>
The integer square root of integers from 0 to 65 are:
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
Using Long Long Type
The integer square roots of powers of 7 from 7^1 up to 7^21 are:
IntSqrt(7^1)=2 of 7^1 (7)
IntSqrt(7^3)=18 of 7^3 (343)
IntSqrt(7^5)=129 of 7^5 (16807)
IntSqrt(7^7)=907 of 7^7 (823543)
IntSqrt(7^9)=6352 of 7^9 (40353607)
IntSqrt(7^11)=44467 of 7^11 (1977326743)
IntSqrt(7^13)=311269 of 7^13 (96889010407)
IntSqrt(7^15)=2178889 of 7^15 (4747561509943)
IntSqrt(7^17)=15252229 of 7^17 (232630513987207)
IntSqrt(7^19)=106765608 of 7^19 (11398895185373143)
IntSqrt(7^21)=747359260 of 7^21 (558545864083284007)
 
Using Decimal Type
<lang MAD> NORMAL MODE IS INTEGER
The integer square roots of powers of 7 from 7^23 up to 7^33 are:
IntSqrt(7^23)=5231514822 of 7^23 (27368747340080916343)
IntSqrt(7^25)=36620603758 of 7^25 (1341068619663964900807)
IntSqrt(7^27)=256344226312 of 7^27 (65712362363534280139543)
IntSqrt(7^29)=1794409584184 of 7^29 (3219905755813179726837607)
IntSqrt(7^31)=12560867089291 of 7^31 (157775382034845806615042743)
IntSqrt(7^33)=87926069625040 of 7^33 (7730993719707444524137094407)
 
Using Double Type
The integer square roots of powers of 7 from 7^19 up to 7^35 are:
IntSqrt(7^19)=106765608 of 7^19 (1.13988951853731E+16)
IntSqrt(7^21)=747359260 of 7^21 (5.58545864083284E+17)
IntSqrt(7^23)=5231514822 of 7^23 (2.73687473400809E+19)
IntSqrt(7^25)=36620603758 of 7^25 (1.34106861966396E+21)
IntSqrt(7^27)=256344226312 of 7^27 (6.57123623635343E+22)
IntSqrt(7^29)=1794409584184 of 7^29 (3.21990575581318E+24)
IntSqrt(7^31)=12560867089291 of 7^31 (1.57775382034846E+26)
IntSqrt(7^33)=87926069625040 of 7^33 (7.73099371970744E+27)
IntSqrt(7^35)=615482487375282 of 7^35 (3.78818692265665E+29)
 
 
</pre>
 
=={{header|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
R INTEGER SQUARE ROOT OF X
Line 1,736 ⟶ 3,543:
INTERNAL FUNCTION(X)
ENTRY TO ISQRT.
X0Q = X/21
FNDPW4 WHENEVER X0Q.ELE.0, FUNCTION RETURN X
STEP X1 Q = (X0Q +* X/X0)/24
WHENEVER X1.GE.X0, FUNCTION RETURN X0TRANSFER TO FNDPW4
X0END =OF X1CONDITIONAL
TRANSFERZ TO= STEPX
R = 0
FNDRT WHENEVER Q.G.1
Q = Q / 4
T = Z - R - Q
R = R / 2
WHENEVER T.GE.0
Z = T
R = R + Q
END OF CONDITIONAL
TRANSFER TO FNDRT
END OF CONDITIONAL
FUNCTION RETURN R
END OF FUNCTION
Line 1,761 ⟶ 3,580:
VECTOR VALUES SQ7 = $9HISQRT.(7^,I2,4H) = ,I6*$
 
END OF PROGRAM </langsyntaxhighlight>
 
{{out}}
Line 1,786 ⟶ 3,605:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ISqrt]
ISqrt[x_Integer?NonNegative] := Module[{q = 1, z, r, t},
While[q <= x,
Line 1,805 ⟶ 3,624:
]
ISqrt /@ Range[65]
Column[ISqrt /@ (7^Range[1, 73])]</langsyntaxhighlight>
{{out}}
<pre>{1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8}
Line 1,881 ⟶ 3,700:
2651730845859653471779023381601
7015820362023593956150476655802</pre>
 
=={{header|Maxima}}==
 
<syntaxhighlight lang="maxima">/* -*- Maxima -*- */
 
/*
 
The Rosetta Code integer square root task, in Maxima.
 
I have not tried to make the output conform quite to the task
description, because Maxima is not a general purpose programming
language. Perhaps someone else will care to do it.
 
I *do* check that the Rosetta Code routine gives the same results as
the built-in function.
 
*/
 
/* pow4gtx -- find a power of 4 greater than x. */
pow4gtx (x) := block (
[q],
q : 1, while q <= x do q : bit_lsh (q, 2),
q
) $
 
/* rosetta_code_isqrt -- find the integer square root. */
rosetta_code_isqrt (x) := block (
[q, z, r, t],
q : pow4gtx (x),
z : x,
r : 0,
while 1 < q do (
q : bit_rsh (q, 2),
t : z - r - q,
r : bit_rsh (r, 1),
if 0 <= t then (
z : t,
r : r + q
)
),
r
) $
 
for i : 0 thru 65 do (
display (rosetta_code_isqrt (i),
is (equal (rosetta_code_isqrt (i), isqrt (i))))
) $
for i : 1 thru 73 step 2 do (
display (7**i, rosetta_code_isqrt (7**i),
is (equal (rosetta_code_isqrt (7**i), isqrt (7**i))))
) $</syntaxhighlight>
 
{{out}}
<pre style="height:30em;overflow:auto">$ maxima -q -b isqrt.mac
(%i1) batch("isqrt.mac")
 
read and interpret /home/trashman/src/chemoelectric/rosettacode-contributions/isqrt.mac
(%i2) pow4gtx(x):=block([q],q:1,while q <= x do q:bit_lsh(q,2),q)
(%i3) rosetta_code_isqrt(x):=block([q,z,r,t],q:pow4gtx(x),z:x,r:0,
while 1 < q do
(q:bit_rsh(q,2),t:z-r-q,r:bit_rsh(r,1),
if 0 <= t then (z:t,r:r+q)),r)
(%i4) for i from 0 thru 65 do
display(rosetta_code_isqrt(i),
is(equal(rosetta_code_isqrt(i),isqrt(i))))
rosetta_code_isqrt(0) = 0
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(1) = 1
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(2) = 1
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(3) = 1
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(4) = 2
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(5) = 2
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(6) = 2
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(7) = 2
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(8) = 2
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(9) = 3
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(10) = 3
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(11) = 3
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(12) = 3
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(13) = 3
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(14) = 3
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(15) = 3
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(16) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(17) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(18) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(19) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(20) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(21) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(22) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(23) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(24) = 4
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(25) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(26) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(27) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(28) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(29) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(30) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(31) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(32) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(33) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(34) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(35) = 5
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(36) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(37) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(38) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(39) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(40) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(41) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(42) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(43) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(44) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(45) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(46) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(47) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(48) = 6
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(49) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(50) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(51) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(52) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(53) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(54) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(55) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(56) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(57) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(58) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(59) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(60) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(61) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(62) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(63) = 7
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(64) = 8
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
rosetta_code_isqrt(65) = 8
 
is(equal(rosetta_code_isqrt(i), isqrt(i))) = true
 
(%i5) for i step 2 thru 73 do
display(7^i,rosetta_code_isqrt(7^i),
is(equal(rosetta_code_isqrt(7^i),isqrt(7^i))))
1
7 = 7
 
rosetta_code_isqrt(7) = 2
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
3
7 = 343
 
rosetta_code_isqrt(343) = 18
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
5
7 = 16807
 
rosetta_code_isqrt(16807) = 129
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
7
7 = 823543
 
rosetta_code_isqrt(823543) = 907
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
9
7 = 40353607
 
rosetta_code_isqrt(40353607) = 6352
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
11
7 = 1977326743
 
rosetta_code_isqrt(1977326743) = 44467
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
13
7 = 96889010407
 
rosetta_code_isqrt(96889010407) = 311269
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
15
7 = 4747561509943
 
rosetta_code_isqrt(4747561509943) = 2178889
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
17
7 = 232630513987207
 
rosetta_code_isqrt(232630513987207) = 15252229
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
19
7 = 11398895185373143
 
rosetta_code_isqrt(11398895185373143) = 106765608
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
21
7 = 558545864083284007
 
rosetta_code_isqrt(558545864083284007) = 747359260
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
23
7 = 27368747340080916343
 
rosetta_code_isqrt(27368747340080916343) = 5231514822
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
25
7 = 1341068619663964900807
 
rosetta_code_isqrt(1341068619663964900807) = 36620603758
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
27
7 = 65712362363534280139543
 
rosetta_code_isqrt(65712362363534280139543) = 256344226312
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
29
7 = 3219905755813179726837607
 
rosetta_code_isqrt(3219905755813179726837607) = 1794409584184
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
31
7 = 157775382034845806615042743
 
rosetta_code_isqrt(157775382034845806615042743) = 12560867089291
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
33
7 = 7730993719707444524137094407
 
rosetta_code_isqrt(7730993719707444524137094407) = 87926069625040
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
35
7 = 378818692265664781682717625943
 
rosetta_code_isqrt(378818692265664781682717625943) = 615482487375282
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
37
7 = 18562115921017574302453163671207
 
rosetta_code_isqrt(18562115921017574302453163671207) = 4308377411626977
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
39
7 = 909543680129861140820205019889143
 
rosetta_code_isqrt(909543680129861140820205019889143) = 30158641881388842
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
41
7 = 44567640326363195900190045974568007
 
rosetta_code_isqrt(44567640326363195900190045974568007) = 211110493169721897
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
43
7 = 2183814375991796599109312252753832343
 
rosetta_code_isqrt(2183814375991796599109312252753832343) = 1477773452188053281
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
45
7 = 107006904423598033356356300384937784807
 
rosetta_code_isqrt(107006904423598033356356300384937784807) =
10344414165316372973
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
47
7 = 5243338316756303634461458718861951455543
 
rosetta_code_isqrt(5243338316756303634461458718861951455543) =
72410899157214610812
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
49
7 = 256923577521058878088611477224235621321607
 
rosetta_code_isqrt(256923577521058878088611477224235621321607) =
506876294100502275687
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
51
7 = 12589255298531885026341962383987545444758743
 
rosetta_code_isqrt(12589255298531885026341962383987545444758743) =
3548134058703515929815
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
53
7 = 616873509628062366290756156815389726793178407
 
rosetta_code_isqrt(616873509628062366290756156815389726793178407) =
24836938410924611508707
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
55
7 = 30226801971775055948247051683954096612865741943
 
rosetta_code_isqrt(30226801971775055948247051683954096612865741943) =
173858568876472280560953
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
57
7 = 1481113296616977741464105532513750734030421355207
 
rosetta_code_isqrt(1481113296616977741464105532513750734030421355207) =
1217009982135305963926677
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
59
7 = 72574551534231909331741171093173785967490646405143
 
rosetta_code_isqrt(72574551534231909331741171093173785967490646405143) =
8519069874947141747486745
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
61
7 = 3556153025177363557255317383565515512407041673852007
 
rosetta_code_isqrt(3556153025177363557255317383565515512407041673852007) =
59633489124629992232407216
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
63
7 = 174251498233690814305510551794710260107945042018748343
 
rosetta_code_isqrt(174251498233690814305510551794710260107945042018748343) =
417434423872409945626850517
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
65
7 = 8538323413450849900970017037940802745289307058918668807
 
rosetta_code_isqrt(8538323413450849900970017037940802745289307058918668807) =
2922040967106869619387953625
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
67
7 = 418377847259091645147530834859099334519176045887014771543
 
rosetta_code_isqrt(418377847259091645147530834859099334519176045887014771543
) = 20454286769748087335715675381
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
69
7 = 20500514515695490612229010908095867391439626248463723805607
 
rosetta_code_isqrt(20500514515695490612229010908095867391439626248463723805607
) = 143180007388236611350009727669
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
71
7 = 1004525211269079039999221534496697502180541686174722466474743
 
rosetta_code_isqrt(10045252112690790399992215344966975021805416861747224664747\
43) = 1002260051717656279450068093686
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
73
7 = 49221735352184872959961855190338177606846542622561400857262407
 
rosetta_code_isqrt(49221735352184872959961855190338177606846542622561400857262\
407) = 7015820362023593956150476655802
 
i i
is(equal(rosetta_code_isqrt(7 ), isqrt(7 ))) = true
 
(%o6) /home/trashman/src/chemoelectric/rosettacode-contributions/isqrt.mac</pre>
 
=={{header|Mercury}}==
{{trans|Prolog}}
{{works with|Mercury|20.06.1}}
 
 
<syntaxhighlight lang="mercury">:- module isqrt_in_mercury.
 
:- interface.
:- import_module io.
:- pred main(io, io).
:- mode main(di, uo) is det.
 
:- implementation.
:- import_module char.
:- import_module exception.
:- import_module int.
:- import_module integer. % Integers of arbitrary size.
:- import_module list.
:- import_module string.
 
:- func four = integer.
four = integer(4).
 
:- func seven = integer.
seven = integer(7).
 
%% Find a power of 4 greater than X.
:- func pow4gtx(integer) = integer.
pow4gtx(X) = Q :- pow4gtx_(X, one, Q).
 
%% The tail recursion for pow4gtx.
:- pred pow4gtx_(integer, integer, integer).
:- mode pow4gtx_(in, in, out) is det.
pow4gtx_(X, A, Q) :- if (X < A) then (Q = A)
else (A1 = A * four,
pow4gtx_(X, A1, Q)).
 
%% Integer square root function.
:- func isqrt(integer) = integer.
isqrt(X) = Root :- isqrt(X, Root, _).
 
%% Integer square root and remainder.
:- pred isqrt(integer, integer, integer).
:- mode isqrt(in, out, out) is det.
isqrt(X, Root, Remainder) :-
Q = pow4gtx(X),
isqrt_(X, Q, zero, X, Root, Remainder).
 
%% The tail recursion for isqrt.
:- pred isqrt_(integer, integer, integer, integer, integer, integer).
:- mode isqrt_(in, in, in, in, out, out) is det.
isqrt_(X, Q, R0, Z0, R, Z) :-
if (X < zero) then throw("isqrt of a negative integer")
else if (Q = one) then (R = R0, Z = Z0)
else (Q1 = Q // four,
T = Z0 - R0 - Q1,
(if (T >= zero)
then (R1 = (R0 // two) + Q1,
isqrt_(X, Q1, R1, T, R, Z))
else (R1 is R0 // two,
isqrt_(X, Q1, R1, Z0, R, Z)))).
 
%% Insert a character, every three digits, into (what presumably is)
%% an integer numeral. (The name "commas" is not very good.)
:- func commas(string, char) = string.
commas(S, Comma) = T :-
RCL = to_rev_char_list(S),
commas_(RCL, Comma, 0, [], CL),
T = from_char_list(CL).
 
%% The tail recursion for commas.
:- pred commas_(list(char), char, int, list(char), list(char)).
:- mode commas_(in, in, in, in, out) is det.
commas_([], _, _, L, CL) :- L = CL.
commas_([C | Tail], Comma, I, L, CL) :-
if (I = 3) then commas_([C | Tail], Comma, 0, [Comma | L], CL)
else (I1 = I + 1,
commas_(Tail, Comma, I1, [C | L], CL)).
 
:- pred roots_m_to_n(integer, integer, io, io).
:- mode roots_m_to_n(in, in, di, uo) is det.
roots_m_to_n(M, N, !IO) :-
if (N < M) then true
else (write_string(to_string(isqrt(M)), !IO),
(if (M \= N) then write_string(" ", !IO)
else true),
M1 = M + one,
roots_m_to_n(M1, N, !IO)).
 
:- pred roots_of_odd_powers_of_7(integer, integer, io, io).
:- mode roots_of_odd_powers_of_7(in, in, di, uo) is det.
roots_of_odd_powers_of_7(M, N, !IO) :-
if (N < M) then true
else (Pow7 = pow(seven, M),
Isqrt = isqrt(Pow7),
format("%2s %84s %43s",
[s(commas(to_string(M), (','))),
s(commas(to_string(Pow7), (','))),
s(commas(to_string(Isqrt), (',')))],
!IO),
nl(!IO),
M1 = M + two,
roots_of_odd_powers_of_7(M1, N, !IO)).
 
main(!IO) :-
write_string("isqrt(i) for 0 <= i <= 65:", !IO),
nl(!IO), nl(!IO),
roots_m_to_n(zero, integer(65), !IO),
nl(!IO), nl(!IO), nl(!IO),
write_string("isqrt(7**i) for 1 <= i <= 73, i odd:", !IO),
nl(!IO), nl(!IO),
format("%2s %84s %43s", [s("i"), s("7**i"), s("isqrt(7**i)")], !IO),
nl(!IO),
write_string(duplicate_char(('-'), 131), !IO),
nl(!IO),
roots_of_odd_powers_of_7(one, integer(73), !IO).
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Instructions for GNU Emacs--
%%% local variables:
%%% mode: mercury
%%% prolog-indent-width: 2
%%% end:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</syntaxhighlight>
 
{{out}}
<pre>$ mmc --warn-non-tail-recursion=self-and-mutual -o isqrt isqrt_in_mercury.m && ./isqrt
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i isqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
=={{header|Modula-2}}==
{{works with|TopSpeed (JPI) Modula-2 under DOSBox-X}}
Uses the algorithm in the task description, with two modifications:
(1) As noted in the ALGOL-M solution, the original algorithm can lead to integer overflow when trying to find a power of 4 greater than the argument X. The modified algorithm avoids overflow.
(2) In the original algorithm, the variable t can be negative. In the modified algorithm, all variables remain non-negative, and can therefore be declared as unsigned integers if desired.
 
TopSpeed Modula-2 supports no integers larger than unsigned 32-bit, which means that the second part of the task stops at 7^11. There seems to be no option to insert commas into long numbers as requested.
<syntaxhighlight lang="modula2">
MODULE IntSqrt;
 
IMPORT IO;
 
(* Procedure to find integer square root of a 32-bit unsigned integer. *)
PROCEDURE Isqrt( X : LONGCARD) : LONGCARD;
VAR
Xdiv4, q, r, s, z : LONGCARD;
BEGIN
Xdiv4 := X DIV 4;
q := 1;
WHILE q <= Xdiv4 DO q := 4*q; END;
z := X;
r := 0;
REPEAT
s := q + r;
r := r DIV 2;
IF z >= s THEN
DEC(z, s);
INC(r, q);
END;
q := q DIV 4;
UNTIL q = 0;
RETURN r;
END Isqrt;
 
(* Main program *)
CONST (* constants for Part 1 of the task *)
Max_n = 65;
NrPerLine = 22;
VAR
n : LONGCARD;
arr_n, arr_i : ARRAY[0..NrPerLine - 1] OF LONGCARD; (* for display *)
j, k : INTEGER;
BEGIN
(* Part 1 *)
k := 0; (* index into arrays *)
FOR n := 0 TO Max_n DO
arr_n[k] := n;
arr_i[k] := Isqrt(n);
INC(k);
IF (k = NrPerLine) OR (n = Max_n) THEN
IO.WrStr( 'Number: ');
FOR j := 0 TO k - 1 DO IO.WrLngCard(arr_n[j], 3); END;
IO.WrLn();
IO.WrStr( 'Isqrt: ');
FOR j := 0 TO k - 1 DO IO.WrLngCard(arr_i[j], 3); END;
IO.WrLn();
k := 0;
END;
END;
IO.WrLn();
 
(* Part 2 *)
IO.WrStr( 'Isqrt of odd powers of 7'); IO.WrLn();
n := 7;
FOR k := 1 TO 11 BY 2 DO
IF k > 1 THEN n := n*49; END;
IO.WrInt( k, 2); IO.WrStr( ' --> ');
IO.WrLngCard( n, 10); IO.WrStr(' --> ');
IO.WrLngCard( Isqrt(n), 5); IO.WrLn();
END;
END IntSqrt.
</syntaxhighlight>
{{out}}
<pre>
Number: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Isqrt: 0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4
Number: 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
Isqrt: 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6
Number: 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
Isqrt: 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
Isqrt of odd powers of 7
1 --> 7 --> 2
3 --> 343 --> 18
5 --> 16807 --> 129
7 --> 823543 --> 907
9 --> 40353607 --> 6352
11 --> 1977326743 --> 44467
</pre>
 
=={{header|Nim}}==
{{libheader|bignum}}
This Nim implementation provides an <code>isqrt</code> function for signed integers and for big integers.
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
import bignum
 
Line 1,927 ⟶ 4,654:
for n in countup(1, 73, 2):
echo &"{n:>2} {insertSep($x, ','):>82} {insertSep($isqrt(x), ','):>41}"
x *= 49</langsyntaxhighlight>
 
{{out}}
Line 1,973 ⟶ 4,700:
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
=={{header|ObjectIcon}}==
{{trans|Icon}}
 
 
The only changes needed to make the Icon version work in Object Icon were to "import io" and to import the IPL modules in a different way. ("write" and "writes" are supported by the io module, presumably for compatibility with Icon and to ease migration of the IPL.)
 
 
<syntaxhighlight lang="objecticon"># -*- ObjectIcon -*-
 
import io
import ipl.numbers # For the "commas" procedure.
import ipl.printf
 
procedure main ()
write ("isqrt(i) for 0 <= i <= 65:")
write ()
roots_of_0_to_65()
write ()
write ()
write ("isqrt(7**i) for 1 <= i <= 73, i odd:")
write ()
printf ("%2s %84s %43s\n", "i", "7**i", "sqrt(7**i)")
write (repl("-", 131))
roots_of_odd_powers_of_7()
end
 
procedure roots_of_0_to_65 ()
local i
 
every i := 0 to 64 do writes (isqrt(i), " ")
write (isqrt(65))
end
 
procedure roots_of_odd_powers_of_7 ()
local i, power_of_7, root
 
every i := 1 to 73 by 2 do {
power_of_7 := 7^i
root := isqrt(power_of_7)
printf ("%2d %84s %43s\n", i, commas(power_of_7), commas(root))
}
end
 
procedure isqrt (x)
local q, z, r, t
 
q := find_a_power_of_4_greater_than_x (x)
z := x
r := 0
while 1 < q do {
q := ishift(q, -2)
t := z - r - q
r := ishift(r, -1)
if 0 <= t then {
z := t
r +:= q
}
}
return r
end
 
procedure find_a_power_of_4_greater_than_x (x)
local q
 
q := 1
while q <= x do q := ishift(q, 2)
return q
end</syntaxhighlight>
 
{{out}}
<pre>$ oit -s -o isqrt isqrt-in-OI.icn && ./isqrt
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i sqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
 
 
=={{header|OCaml}}==
{{trans|Scheme}}
{{libheader|Zarith}}
 
 
<syntaxhighlight lang="ocaml">(* The Rosetta Code integer square root task, in OCaml, using Zarith
for large integers.
 
Compile with, for example:
 
ocamlfind ocamlc -package zarith -linkpkg -o isqrt isqrt.ml
 
Translated from the Scheme. *)
 
let find_a_power_of_4_greater_than_x x =
let open Z in
let rec loop q =
if x < q then q else loop (q lsl 2)
in
loop one
 
let isqrt x =
let open Z in
let rec loop q z r =
if q = one then
r
else
let q = q asr 2 in
let t = z - r - q in
let r = r asr 1 in
if t < zero then
loop q z r
else
loop q t (r + q)
in
let q0 = find_a_power_of_4_greater_than_x x in
let z0 = x in
let r0 = zero in
loop q0 z0 r0
 
let insert_separators s sep =
let rec loop revchars i newchars =
match revchars with
| [] -> newchars
| revchars when i = 3 -> loop revchars 0 (sep :: newchars)
| c :: tail -> loop tail (i + 1) (c :: newchars)
in
let revchars = List.rev (List.of_seq (String.to_seq s)) in
String.of_seq (List.to_seq (loop revchars 0 []))
 
let commas s = insert_separators s ','
 
let main () =
Printf.printf "isqrt(i) for 0 <= i <= 65:\n\n";
for i = 0 to 64 do
Printf.printf "%s " Z.(to_string (isqrt (of_int i)))
done;
Printf.printf "%s\n" Z.(to_string (isqrt (of_int 65)));
Printf.printf "\n\n";
Printf.printf "isqrt(7**i) for 1 <= i <= 73, i odd:\n\n";
Printf.printf "%2s %84s %43s\n" "i" "7**i" "isqrt(7**i)";
for i = 1 to 131 do Printf.printf "-" done;
Printf.printf "\n";
for j = 0 to 36 do
let i = j + j + 1 in
let power = Z.(of_int 7 ** i) in
let root = isqrt power in
Printf.printf "%2d %84s %43s\n"
i (commas (Z.to_string power)) (commas (Z.to_string root))
done
;;
 
main ()</syntaxhighlight>
 
{{out}}
<pre>$ ocamlfind ocamlc -package zarith -linkpkg -o isqrt isqrt.ml && ./isqrt
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i isqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
 
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(print "Integer square roots of 0..65")
(for-each (lambda (x)
Line 1,990 ⟶ 4,962:
(iota 73 1)))
(print)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,074 ⟶ 5,046:
{{libheader|Velthuis.BigIntegers}}[https://github.com/rvelthuis/DelphiBigNumbers]
{{trans|C++}}
<syntaxhighlight lang="pascal">
<lang Pascal>
//************************************************//
// //
Line 2,171 ⟶ 5,143:
CloseFile(f);
end.
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
{{trans|Julia}}
<langsyntaxhighlight Perllang="perl"># 20201029 added Perl programming solution
 
use strict;
Line 2,217 ⟶ 5,189:
printf("%44s", $decf->format( integer_sqrt(7**$i) ) ) ;
print "\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,266 ⟶ 5,238:
=={{header|Phix}}==
See also [[Integer_roots#Phix]] for a simpler and shorter example using the mpz_root() routine, or better yet just use mpz_root() directly (that is, rather than the isqrt() below).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 2,311 ⟶ 5,283:
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pow7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pow7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
Perfect squares are denoted with an asterisk.
<pre style="font-size: 11px">
<pre>
The integer square roots of integers from 0 to 65 are:
0* 1* 1 1 2* 2 2 2 2 3* 3 3 3 3 3 3 4* 4 4 4 4 4 4 4 4 5* 5 5 5 5 5 5 5 5 5 5 6* 6 6 6 6 6 6 6 6 6 6 6 6 7* 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8* 8
Line 2,380 ⟶ 5,352:
</pre>
<small>(Note that pre-0.8.2 the "(NNN digits)" count includes commas)</small>
 
=={{header|Prolog}}==
{{works with|SWI-Prolog|8.5.9}}
 
 
<syntaxhighlight lang="prolog">%%% -*- Prolog -*-
%%%
%%% The Rosetta Code integer square root task, for SWI Prolog.
%%%
 
%% pow4gtx/2 -- Find a power of 4 greater than X.
pow4gtx(X, Q) :- pow4gtx(X, 1, Q), !.
pow4gtx(X, A, Q) :- X < A, Q is A.
pow4gtx(X, A, Q) :- A1 is A * 4,
pow4gtx(X, A1, Q).
 
%% isqrt/2 -- Find integer square root.
%% isqrt/3 -- Find integer square root and remainder.
isqrt(X, R) :- isqrt(X, R, _).
isqrt(X, R, Z) :- pow4gtx(X, Q),
isqrt(X, Q, 0, X, R, Z).
isqrt(_, 1, R0, Z0, R, Z) :- R is R0,
Z is Z0.
isqrt(X, Q, R0, Z0, R, Z) :- Q1 is Q // 4,
T is Z0 - R0 - Q1,
(T >= 0
-> R1 is (R0 // 2) + Q1,
isqrt(X, Q1, R1, T, R, Z)
; R1 is R0 // 2,
isqrt(X, Q1, R1, Z0, R, Z)).
 
roots(N) :- roots(0, N).
roots(I, N) :- isqrt(I, R),
write(R),
(I =:= N; write(" ")),
I1 is I + 1,
(N < I1, !; roots(I1, N)).
 
rootspow7(N) :- rootspow7(1, N).
rootspow7(I, N) :- Pow7 is 7**I,
isqrt(Pow7, R),
format("~t~D~2|~t~D~87|~t~D~131|~n",
[I, Pow7, R]),
I1 is I + 2,
(N < I1, !; rootspow7(I1, N)).
 
main :-
format("isqrt(i) for 0 <= i <= 65:~2n"),
roots(65),
format("~3n"),
format("isqrt(7**i) for 1 <= i <= 73, i odd:~2n"),
format("~t~s~2|~t~s~87|~t~s~131|~n",
["i", "7**i", "isqrt(7**i)"]),
format("-----------------------------------------------------------------------------------------------------------------------------------~n"),
rootspow7(73),
halt.
 
:- initialization(main).
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Instructions for GNU Emacs--
%%% local variables:
%%% mode: prolog
%%% prolog-indent-width: 2
%%% end:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</syntaxhighlight>
 
{{out}}
<pre>$ swipl isqrt.pl
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i isqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
=={{header|Python}}==
{{works with|Python|2.7}}
<langsyntaxhighlight lang="python">def isqrt ( x ):
q = 1
while q <= x :
Line 2,396 ⟶ 5,483:
 
print ' '.join( '%d'%isqrt( n ) for n in xrange( 66 ))
print '\n'.join( '{0:114,} = isqrt( 7^{1:3} )'.format( isqrt( 7**n ),n ) for n in range( 1,204,2 ))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,506 ⟶ 5,593:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ dup size 3 / times
[ char , swap
i 1+ -3 * stuff ]
Line 2,531 ⟶ 5,618:
rot over + ]
again ]
nip swap ] is sqrt + ( n --> n n )
 
( sqrt+ returns the integer square root and remainder )
( i.e. isqrt+ of 28 is 5 remainder 3 as (5^2)+3 = 28 )
( To make it task compliant change the last line to )
( "nip nip ] is sqrt + ( n --> n )" )
 
66 times [ i^ sqrt+ drop echo sp ] cr cr
73 times
[ 7 i^ 1+ ** sqrt+ drop
number$ +commas 41 justify
echo$ cr
2 step ]</langsyntaxhighlight>
 
'''Output:'''
Line 2,587 ⟶ 5,674:
7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
#lang racket
 
;; Integer Square Root (using Quadratic Residue)
(define (isqrt x)
(define q-init ; power of 4 greater than x
(let loop ([acc 1])
(if (<= acc x) (loop (* acc 4)) acc)))
 
(define-values (z r q)
(let loop ([z x] [r 0] [q q-init])
(if (<= q 1)
(values z r q)
(let* ([q (/ q 4)]
[t (- z r q)]
[r (/ r 2)])
(if (>= t 0)
(loop t (+ r q) q)
(loop z r q))))))
 
r)
 
(define (format-with-commas str #:chunk-size [size 3])
(define len (string-length str))
(define len-mod (modulo len size))
(define chunks
(for/list ([i (in-range len-mod len size)])
(substring str i (+ i size))))
(string-join (if (= len-mod 0)
chunks
(cons (substring str 0 len-mod) chunks))
","))
 
(displayln "Isqrt of integers (0 -> 65):")
(for ([i 66])
(printf "~a " (isqrt i)))
 
(displayln "\n\nIsqrt of odd powers of 7 (7 -> 7^73):")
(for/fold ([num 7]) ([i (in-range 1 74 2)])
(printf "Isqrt(7^~a) = ~a\n"
i
(format-with-commas (number->string (isqrt num))))
(* num 49))
 
</syntaxhighlight>
{{out}}
<pre>
Isqrt of integers (0 -> 65):
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
Isqrt of odd powers of 7 (7 -> 7^73):
Isqrt(7^1) = 2
Isqrt(7^3) = 18
Isqrt(7^5) = 129
Isqrt(7^7) = 907
Isqrt(7^9) = 6,352
Isqrt(7^11) = 44,467
Isqrt(7^13) = 311,269
Isqrt(7^15) = 2,178,889
Isqrt(7^17) = 15,252,229
Isqrt(7^19) = 106,765,608
Isqrt(7^21) = 747,359,260
Isqrt(7^23) = 5,231,514,822
Isqrt(7^25) = 36,620,603,758
Isqrt(7^27) = 256,344,226,312
Isqrt(7^29) = 1,794,409,584,184
Isqrt(7^31) = 12,560,867,089,291
Isqrt(7^33) = 87,926,069,625,040
Isqrt(7^35) = 615,482,487,375,282
Isqrt(7^37) = 4,308,377,411,626,977
Isqrt(7^39) = 30,158,641,881,388,842
Isqrt(7^41) = 211,110,493,169,721,897
Isqrt(7^43) = 1,477,773,452,188,053,281
Isqrt(7^45) = 10,344,414,165,316,372,973
Isqrt(7^47) = 72,410,899,157,214,610,812
Isqrt(7^49) = 506,876,294,100,502,275,687
Isqrt(7^51) = 3,548,134,058,703,515,929,815
Isqrt(7^53) = 24,836,938,410,924,611,508,707
Isqrt(7^55) = 173,858,568,876,472,280,560,953
Isqrt(7^57) = 1,217,009,982,135,305,963,926,677
Isqrt(7^59) = 8,519,069,874,947,141,747,486,745
Isqrt(7^61) = 59,633,489,124,629,992,232,407,216
Isqrt(7^63) = 417,434,423,872,409,945,626,850,517
Isqrt(7^65) = 2,922,040,967,106,869,619,387,953,625
Isqrt(7^67) = 20,454,286,769,748,087,335,715,675,381
Isqrt(7^69) = 143,180,007,388,236,611,350,009,727,669
Isqrt(7^71) = 1,002,260,051,717,656,279,450,068,093,686
Isqrt(7^73) = 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
 
=={{header|Raku}}==
Line 2,594 ⟶ 5,774:
 
Quadratic residue algorithm follows:
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers;
<lang perl6>sub isqrt ( \x ) { my ( $X, $q, $r, $t ) = x, 1, 0 ;
 
sub isqrt ( \x ) { my ( $X, $q, $r, $t ) = x, 1, 0 ;
$q +<= 2 while $q ≤ $X ;
while $q > 1 {
Line 2,605 ⟶ 5,787:
say (^66)».&{ isqrt $_ }.Str ;
 
(1, 3…73)».&{ "7**$_: " ~ comma(isqrt 7**$_) }».say</langsyntaxhighlight>
{{out}}
<pre>0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
7**1: 2
7**3: 18
7**5: 129
7**7: 907
7**9: 63526,352
7**11: 4446744,467
7**13: 311269311,269
7**15: 21788892,178,889
7**17: 1525222915,252,229
7**19: 106765608106,765,608
7**21: 747359260747,359,260
7**23: 52315148225,231,514,822
7**25: 3662060375836,620,603,758
7**27: 256344226312256,344,226,312
7**29: 17944095841841,794,409,584,184
7**31: 1256086708929112,560,867,089,291
7**33: 8792606962504087,926,069,625,040
7**35: 615482487375282615,482,487,375,282
7**37: 43083774116269774,308,377,411,626,977
7**39: 3015864188138884230,158,641,881,388,842
7**41: 211110493169721897211,110,493,169,721,897
7**43: 14777734521880532811,477,773,452,188,053,281
7**45: 1034441416531637297310,344,414,165,316,372,973
7**47: 7241089915721461081272,410,899,157,214,610,812
7**49: 506876294100502275687506,876,294,100,502,275,687
7**51: 3,548,134,058,703,515,929,815
7**51: 3548134058703515929815
7**53: 24,836,938,410,924,611,508,707
7**53: 24836938410924611508707
7**55: 173,858,568,876,472,280,560,953
7**55: 173858568876472280560953
7**57: 1,217,009,982,135,305,963,926,677
7**57: 1217009982135305963926677
7**59: 8,519,069,874,947,141,747,486,745
7**59: 8519069874947141747486745
7**61: 59,633,489,124,629,992,232,407,216
7**61: 59633489124629992232407216
7**63: 417,434,423,872,409,945,626,850,517
7**63: 417434423872409945626850517
7**65: 2,922,040,967,106,869,619,387,953,625
7**65: 2922040967106869619387953625
7**67: 20,454,286,769,748,087,335,715,675,381
7**67: 20454286769748087335715675381
7**69: 143,180,007,388,236,611,350,009,727,669
7**69: 143180007388236611350009727669
7**71: 1,002,260,051,717,656,279,450,068,093,686
7**71: 1002260051717656279450068093686
7**73: 7,015,820,362,023,593,956,150,476,655,802</pre>
7**73: 7015820362023593956150476655802
</pre>
 
=={{header|REXX}}==
A fair amount of code was included so that the output aligns correctly.
<langsyntaxhighlight lang="rexx">/*REXX program computes and displays the Isqrt (integer square root) of some integers.*/
numeric digits 200 /*insure 'nuff decimal digs for results*/
parse arg range power base . /*obtain optional arguments from the CL*/
Line 2,694 ⟶ 5,875:
end
end /*while*/
return r /*return the integer square root of X. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,740 ⟶ 5,921:
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802
</pre>
=={{header|RPL}}==
Because RPL can only handle unsigned integers, a light change has been made in the proposed algorithm,
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
#1
'''WHILE''' DUP2 ≥ '''REPEAT'''
SL SL '''END'''
#0
'''WHILE''' OVER #1 > '''REPEAT'''
SWAP SR SR SWAP
DUP2 +
SWAP SR SWAP
'''IF''' 4 PICK SWAP DUP2 ≥ '''THEN'''
- 4 ROLL DROP ROT ROT
OVER +
'''ELSE''' DROP2 '''END'''
'''END''' ROT ROT DROP2
´'''ISQRT'''’ STO
|
'''ISQRT''' ''( #n -- #sqrt(n) )''
q ◄── 1
perform while q <= x
q ◄── q * 4
z ◄── x
r ◄── 0
perform while q > 1
q ◄── q ÷ 4
u ◄── r + q
r ◄── r ÷ 2
if z >= u then do
z ◄── z - u
r ◄── r + q
else remove u and copy of z from stack
end perform, clean stack
|}
{{in}}
<pre>
≪ { } 0 65 FOR n n R→B ISQRT B→R + NEXT ≫ EVAL
≪ {} #7 1 11 START DUP ISQRT ROT SWAP + SWAP 49 * NEXT DROP ≫ EVAL
</pre>
{{out}}
<pre>
2: { 0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 }
1: { # 2d # 18d # 129d # 907d # 6352d # 44467d # 311269d # 2178889d # 15252229d # 106765608d # 747359260d }
</pre>
 
=={{header|Ruby}}==
Ruby already has [https://ruby-doc.org/core-2.7.0/Integer.html#method-c-sqrt Integer.sqrt], which results in the integer square root of a positive integer. It can be re-implemented as follows:
<syntaxhighlight lang="ruby">module Commatize
<lang ruby>class Integer
refine Integer do
def commatize
def commatize
self.to_s.gsub( /(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/, "\\1,\\2")
self.to_s.gsub( /(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/, "\\1,\\2")
end
end
end
 
using Commatize
def isqrt(x)
q, r = 1, 0
Line 2,764 ⟶ 6,003:
puts isqrt(7**n).commatize
end
</syntaxhighlight>
</lang>
{{out}}
<pre>0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
Line 2,807 ⟶ 6,046:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use num::BigUint;
use num::CheckedSub;
Line 2,859 ⟶ 6,098:
});
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,904 ⟶ 6,143:
</pre>
 
=={{header|Seed7}}==
 
=={{header|S-BASIC}}==
{{incorrect|Seed7|<br><br>This Rosetta Code task is to use a &nbsp; ''quadratic residue'' &nbsp; algorithm for finding the integer square root.<br><br>The pseudo-code is shown in the task's preamble which does ''not'' use the language's built-in sqrt() function.<br><br>Please use the required pseudo-code as shown in the task's preamble.<br><br>}}
This follows the algorithm given in the task description. The q = q * 4 computation, however, will result in overflow (and an endless loop!) for large values of x.
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="basic">
comment
return integer square root of n using quadratic
residue algorithm. WARNING: the function will fail
for x > 16,383.
end
function isqrt(x = integer) = integer
var q, r, t = integer
q = 1
while q <= x do
q = q * 4 rem overflow may occur here!
r = 0
while q > 1 do
begin
q = q / 4
t = x - r - q
r = r / 2
if t >= 0 then
begin
x = t
r = r + q
end
end
end = r
 
rem - Exercise the function
 
var n, pow7 = integer
print "Integer square root of first 65 numbers"
for n=1 to 65
print using "#####";isqrt(n);
next n
print
print "Integer square root of odd powers of 7"
print " n 7^n isqrt"
print "------------------"
for n=1 to 3 step 2
pow7 = 7^n
print using "### #### ####";n; pow7; isqrt(pow7)
next n
 
end
</syntaxhighlight>
An alternate version of isqrt() that can handle the full range of S-BASIC integer values (well, almost: it will fail for 32,767) looks like this.
<syntaxhighlight lang="basic">
function isqrt(x = integer) = integer
var x0, x1 = integer
x1 = x
repeat
begin
x0 = x1
x1 = (x0 + x / x0) / 2
end
until x1 >= x0
end = x0
</syntaxhighlight>
{{out}}
The output for 7^5 will be shown only if the alternate version of the function is used.
<pre>
Integer square root of first 65 numbers
1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4
4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5
5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8
8
Integer square root of odd powers of 7
n 7^n isqrt
------------------
1 7 2
3 343 18
5 16807 129
</pre>
 
=={{header|Scheme}}==
{{works with|CHICKEN|5.3.0}}
{{libheader|r7rs}}
{{libheader|format}}
 
 
Adapting this to any given R7RS Scheme is probably mainly a matter of changing how output is done.
 
 
<syntaxhighlight lang="scheme">(import (scheme base))
(import (scheme write))
(import (format)) ;; Common Lisp formatting for CHICKEN Scheme.
 
(define (find-a-power-of-4-greater-than-x x)
(let loop ((q 1))
(if (< x q)
q
(loop (* 4 q)))))
 
(define (isqrt+remainder x)
(let loop ((q (find-a-power-of-4-greater-than-x x))
(z x)
(r 0))
(if (= q 1)
(values r z)
(let* ((q (truncate-quotient q 4))
(t (- z r q))
(r (truncate-quotient r 2)))
(if (negative? t)
(loop q z r)
(loop q t (+ r q)))))))
 
(define (isqrt x)
(let-values (((q r) (isqrt+remainder x)))
q))
 
(format #t "isqrt(i) for ~D <= i <= ~D:~2%" 0 65)
(do ((i 0 (+ i 1)))
((= i 65))
(format #t "~D " (isqrt i)))
(format #t "~D~3%" (isqrt 65))
 
(format #t "isqrt(7**i) for ~D <= i <= ~D, i odd:~2%" 1 73)
(format #t "~2@A ~84@A ~43@A~%" "i" "7**i" "sqrt(7**i)")
(format #t "~A~%" (make-string 131 #\-))
(do ((i 1 (+ i 2)))
((= i 75))
(let ((7**i (expt 7 i)))
(format #t "~2D ~84:D ~43:D~%" i 7**i (isqrt 7**i))))</syntaxhighlight>
 
{{out}}
<pre>$ csc -O3 -R r7rs isqrt.scm && ./isqrt
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i sqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program isqrt;
loop for i in [1..65] do
putchar(lpad(str isqrt(i), 5));
if i mod 13=0 then print(); end if;
end loop;
 
print();
loop for p in [1, 3..73] do
sqrtp := isqrt(7 ** p);
print("sqrt(7^" + lpad(str p,2) + ") = " + lpad(str sqrtp, 32));
end loop;
 
proc isqrt(x);
q := 1;
loop while q<=x do
q *:= 4;
end loop;
z := x;
r := 0;
loop while q>1 do
q div:= 4;
t := z-r-q;
r div:= 2;
if t>=0 then
z := t;
r +:= q;
end if;
end loop;
return r;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre> 1 1 1 2 2 2 2 2 3 3 3 3 3
3 3 4 4 4 4 4 4 4 4 4 5 5
5 5 5 5 5 5 5 5 5 6 6 6 6
6 6 6 6 6 6 6 6 6 7 7 7 7
7 7 7 7 7 7 7 7 7 7 7 8 8
 
sqrt(7^ 1) = 2
sqrt(7^ 3) = 18
sqrt(7^ 5) = 129
sqrt(7^ 7) = 907
sqrt(7^ 9) = 6352
sqrt(7^11) = 44467
sqrt(7^13) = 311269
sqrt(7^15) = 2178889
sqrt(7^17) = 15252229
sqrt(7^19) = 106765608
sqrt(7^21) = 747359260
sqrt(7^23) = 5231514822
sqrt(7^25) = 36620603758
sqrt(7^27) = 256344226312
sqrt(7^29) = 1794409584184
sqrt(7^31) = 12560867089291
sqrt(7^33) = 87926069625040
sqrt(7^35) = 615482487375282
sqrt(7^37) = 4308377411626977
sqrt(7^39) = 30158641881388842
sqrt(7^41) = 211110493169721897
sqrt(7^43) = 1477773452188053281
sqrt(7^45) = 10344414165316372973
sqrt(7^47) = 72410899157214610812
sqrt(7^49) = 506876294100502275687
sqrt(7^51) = 3548134058703515929815
sqrt(7^53) = 24836938410924611508707
sqrt(7^55) = 173858568876472280560953
sqrt(7^57) = 1217009982135305963926677
sqrt(7^59) = 8519069874947141747486745
sqrt(7^61) = 59633489124629992232407216
sqrt(7^63) = 417434423872409945626850517
sqrt(7^65) = 2922040967106869619387953625
sqrt(7^67) = 20454286769748087335715675381
sqrt(7^69) = 143180007388236611350009727669
sqrt(7^71) = 1002260051717656279450068093686
sqrt(7^73) = 7015820362023593956150476655802</pre>
 
=={{header|Seed7}}==
Seed7 has integer [https://seed7.sourceforge.net/libraries/integer.htm#sqrt(in_integer) sqrt()] and bigInteger [https://seed7.sourceforge.net/libraries/bigint.htm#sqrt(in_var_bigInteger) sqrt()] functions.
These functions could be used if an integer square root is needed.
But this task does not allow using the language's built-in sqrt() function.
Instead the ''quadratic residue'' algorithm for finding the integer square root must be used.
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
 
Line 2,920 ⟶ 6,413:
stri := stri[.. index] & "," & stri[succ(index) ..];
end for;
end func;
 
const func bigInteger: isqrt (in bigInteger: x) is func
result
var bigInteger: r is 0_;
local
var bigInteger: q is 1_;
var bigInteger: z is 0_;
var bigInteger: t is 0_;
begin
while q <= x do
q *:= 4_;
end while;
z := x;
while q > 1_ do
q := q mdiv 4_;
t := z - r - q;
r := r mdiv 2_;
if t >= 0_ then
z := t;
r +:= q;
end if;
end while;
end func;
 
Line 2,929 ⟶ 6,445:
writeln("The integer square roots of integers from 0 to 65 are:");
for number range 0 to 65 do
write(sqrtisqrt(bigInteger(number)) <& " ");
end for;
writeln("\n\nThe integer square roots of powers of 7 from 7**1 up to 7**73 are:");
Line 2,935 ⟶ 6,451:
writeln("----- --------------------------------------------------------------------------------- -----------------------------------------");
for number range 1 to 73 step 2 do
writeln(number lpad 2 <& commatize(pow7) lpad 85 <& commatize(sqrtisqrt(pow7)) lpad 42);
pow7 *:= pow7 * 49_;
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,989 ⟶ 6,505:
 
Built-in:
<langsyntaxhighlight lang="ruby">var n = 1234
say n.isqrt
say n.iroot(2)</langsyntaxhighlight>
 
Explicit implementation for the integer k-th root of n:
 
<langsyntaxhighlight lang="ruby">func rootint(n, k=2) {
return 0 if (n == 0)
var (s, v) = (n, k - 1)
Line 3,004 ⟶ 6,520:
}
s
}</langsyntaxhighlight>
 
Implementation of integer square root of n (using the quadratic residue algorithm):
<langsyntaxhighlight lang="ruby">func isqrt(x) { var (q, r) = (1, 0); while (q <= x) { q <<= 2 }
while (q > 1) { q >>= 2; var t = x-r+q; r >>= 1
if (t >= 0) { (x, r) = (t, r+q) } } r }
Line 3,014 ⟶ 6,530:
 
for n in (1..73 `by` 2) {
printf("isqrt(7^%-2d): %42s\n", n, isqrt(7**n).commify) }</langsyntaxhighlight>
{{out}}
<pre>0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
Line 3,056 ⟶ 6,572:
isqrt(7^73): 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|Standard ML}}==
{{trans|Scheme}}
{{trans|OCaml}}
{{works with|MLton}}
 
 
<syntaxhighlight lang="sml">(*
 
The Rosetta Code integer square root task, in Standard ML.
 
Compile with, for example:
 
mlton isqrt.sml
 
*)
 
val zero = IntInf.fromInt (0)
val one = IntInf.fromInt (1)
val seven = IntInf.fromInt (7)
val word1 = Word.fromInt (1)
val word2 = Word.fromInt (2)
 
fun
find_a_power_of_4_greater_than_x (x) =
let
fun
loop (q) =
if x < q then
q
else
loop (IntInf.<< (q, word2))
in
loop (one)
end;
 
fun
isqrt (x) =
let
fun
loop (q, z, r) =
if q = one then
r
else
let
val q = IntInf.~>> (q, word2)
val t = z - r - q
val r = IntInf.~>> (r, word1)
in
if t < zero then
loop (q, z, r)
else
loop (q, t, r + q)
end
in
loop (find_a_power_of_4_greater_than_x (x), x, zero)
end;
 
fun
insert_separators (s, sep) =
(* Insert separator characters (such as #",", #".", #" ") in a numeral
that is already in string form. *)
let
fun
loop (revchars, i, newchars) =
case (revchars, i) of
([], _) => newchars
| (revchars, 3) => loop (revchars, 0, sep :: newchars)
| (c :: tail, i) => loop (tail, i + 1, c :: newchars)
in
implode (loop (rev (explode s), 0, []))
end;
 
fun
commas (s) =
(* Insert commas in a numeral that is already in string form. *)
insert_separators (s, #",");
 
val pad_with_spaces = StringCvt.padLeft #" "
 
fun
main () =
let
val i = ref 0
in
print ("isqrt(i) for 0 <= i <= 65:\n\n");
 
i := 0;
while !i < 65 do (
print (IntInf.toString (isqrt (IntInf.fromInt (!i))));
print (" ");
i := !i + 1
);
print (IntInf.toString (isqrt (IntInf.fromInt (65))));
print ("\n\n\n");
 
print ("isqrt(7**i) for 1 <= i <= 73, i odd:\n\n");
print (pad_with_spaces 2 "i");
print (pad_with_spaces 85 "7**i");
print (pad_with_spaces 44 "sqrt(7**i)");
print ("\n");
 
i := 1;
while !i <= 131 do (
print ("-");
i := !i + 1
);
print ("\n");
 
i := 1;
while !i <= 73 do (
let
val pow7 = IntInf.pow (seven, !i)
val root = isqrt (pow7)
in
print (pad_with_spaces 2 (Int.toString (!i)));
print (pad_with_spaces 85 (commas (IntInf.toString pow7)));
print (pad_with_spaces 44 (commas (IntInf.toString root)));
print ("\n");
i := !i + 2
end
)
end;
 
main ();
 
(* local variables: *)
(* mode: sml *)
(* sml-indent-level: 2 *)
(* sml-indent-args: 2 *)
(* end: *)</syntaxhighlight>
 
{{out}}
<pre>$ mlton isqrt.sml && ./isqrt
isqrt(i) for 0 <= i <= 65:
 
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
 
isqrt(7**i) for 1 <= i <= 73, i odd:
 
i 7**i sqrt(7**i)
-----------------------------------------------------------------------------------------------------------------------------------
1 7 2
3 343 18
5 16,807 129
7 823,543 907
9 40,353,607 6,352
11 1,977,326,743 44,467
13 96,889,010,407 311,269
15 4,747,561,509,943 2,178,889
17 232,630,513,987,207 15,252,229
19 11,398,895,185,373,143 106,765,608
21 558,545,864,083,284,007 747,359,260
23 27,368,747,340,080,916,343 5,231,514,822
25 1,341,068,619,663,964,900,807 36,620,603,758
27 65,712,362,363,534,280,139,543 256,344,226,312
29 3,219,905,755,813,179,726,837,607 1,794,409,584,184
31 157,775,382,034,845,806,615,042,743 12,560,867,089,291
33 7,730,993,719,707,444,524,137,094,407 87,926,069,625,040
35 378,818,692,265,664,781,682,717,625,943 615,482,487,375,282
37 18,562,115,921,017,574,302,453,163,671,207 4,308,377,411,626,977
39 909,543,680,129,861,140,820,205,019,889,143 30,158,641,881,388,842
41 44,567,640,326,363,195,900,190,045,974,568,007 211,110,493,169,721,897
43 2,183,814,375,991,796,599,109,312,252,753,832,343 1,477,773,452,188,053,281
45 107,006,904,423,598,033,356,356,300,384,937,784,807 10,344,414,165,316,372,973
47 5,243,338,316,756,303,634,461,458,718,861,951,455,543 72,410,899,157,214,610,812
49 256,923,577,521,058,878,088,611,477,224,235,621,321,607 506,876,294,100,502,275,687
51 12,589,255,298,531,885,026,341,962,383,987,545,444,758,743 3,548,134,058,703,515,929,815
53 616,873,509,628,062,366,290,756,156,815,389,726,793,178,407 24,836,938,410,924,611,508,707
55 30,226,801,971,775,055,948,247,051,683,954,096,612,865,741,943 173,858,568,876,472,280,560,953
57 1,481,113,296,616,977,741,464,105,532,513,750,734,030,421,355,207 1,217,009,982,135,305,963,926,677
59 72,574,551,534,231,909,331,741,171,093,173,785,967,490,646,405,143 8,519,069,874,947,141,747,486,745
61 3,556,153,025,177,363,557,255,317,383,565,515,512,407,041,673,852,007 59,633,489,124,629,992,232,407,216
63 174,251,498,233,690,814,305,510,551,794,710,260,107,945,042,018,748,343 417,434,423,872,409,945,626,850,517
65 8,538,323,413,450,849,900,970,017,037,940,802,745,289,307,058,918,668,807 2,922,040,967,106,869,619,387,953,625
67 418,377,847,259,091,645,147,530,834,859,099,334,519,176,045,887,014,771,543 20,454,286,769,748,087,335,715,675,381
69 20,500,514,515,695,490,612,229,010,908,095,867,391,439,626,248,463,723,805,607 143,180,007,388,236,611,350,009,727,669
71 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 1,002,260,051,717,656,279,450,068,093,686
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802</pre>
 
 
=={{header|Swift}}==
{{trans|C++}}
Requires the attaswift BigInt package.
<langsyntaxhighlight lang="swift">import BigInt
 
func integerSquareRoot<T: BinaryInteger>(_ num: T) -> T {
Line 3,119 ⟶ 6,816:
print("\(pad(string: String(n), width: 2)) |\(power) |\(isqrt)")
p *= 49
}</langsyntaxhighlight>
 
{{out}}
<pre style="font-size: 11px">
<pre>
Integer square root for numbers 0 to 65:
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
Line 3,167 ⟶ 6,864:
73 | 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 | 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{Header|Tiny BASIC}}==
{{works with|TinyBasic}}
Tiny BASIC does not support string formatting or concatenation, and is limited to integer arithmetic on numbers no greater than 32,767. The isqrt of 0-65 and the first two odd powers of 7 are shown in column format. The algorithm itself (the interesting part) begins on line 100.
<syntaxhighlight lang Tiny BASIC="basic">10 LET X = 0
20 GOSUB 100
30 PRINT R
Line 3,196 ⟶ 6,895:
220 LET Z = T
230 LET R = R + Q
240 GOTO 170</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
{{Works with|Zsh}}
<syntaxhighlight lang="sh">function isqrt {
typeset -i x
for x; do
typeset -i q=1
while (( q <= x )); do
(( q <<= 2 ))
if (( q <= 0 )); then
return 1
fi
done
typeset -i z=x
typeset -i r=0
typeset -i t
while (( q > 1 )); do
(( q >>= 2 ))
(( t = z - r - q ))
(( r >>= 1 ))
if (( t >= 0 )); then
(( z = t ))
(( r = r + q ))
fi
done
printf '%d\n' "$r"
done
}
# demo
printf 'isqrt(n) for n from 0 to 65:\n'
for i in {1..4}; do
for n in {0..65}; do
case $i in
1)
(( tens=n/10 ))
if (( tens )); then
printf '%2d' "$tens"
else
printf ' '
fi
;;
2) printf '%2d' $(( n%10 ));;
3) printf -- '--';;
4) printf '%2d' "$(isqrt "$n")";;
esac
done
printf '\n'
done
printf '\n'
printf 'isqrt(7ⁿ) for odd n up to the limit of integer precision:\n'
printf '%2s|%27sⁿ|%14sⁿ)\n' "n" "7" "isqrt(7"
for (( i=0;i<48; ++i )); do printf '-'; done; printf '\n'
for (( p=1; p<=73 && (n=7**p) > 0; p+=2)); do
if r=$(isqrt $n); then
printf "%2d|%'28d|%'16d\n" "$p" "$n" "$r"
else
break
fi
done</syntaxhighlight>
 
{{Out}}
The powers-of-7 table is limited by the built-in precision; on my system, both bash and zsh use signed 64-bit integers with a max value of 7²² < 9223372036854775807 < 7²³.
Ksh uses signed 32-bit integers with a max value of 7¹¹ < 2147483647 < 7¹²; if I remove the <tt>typeset -i</tt> integer restriction, the code will work to a much larger power of 7, but at that point it's doing floating-point arithmetic, which is against the spirit of the task.
<pre>isqrt(n) for n from 0 to 65:
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
------------------------------------------------------------------------------------------------------------------------------------
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
 
isqrt(7ⁿ) for odd n up to the limit of integer precision:
n| 7ⁿ| isqrt(7ⁿ)
------------------------------------------------
1| 7| 2
3| 343| 18
5| 16,807| 129
7| 823,543| 907
9| 40,353,607| 6,352 # ksh stops here
11| 1,977,326,743| 44,467
13| 96,889,010,407| 311,269
15| 4,747,561,509,943| 2,178,889
17| 232,630,513,987,207| 15,252,229
19| 11,398,895,185,373,143| 106,765,608
21| 558,545,864,083,284,007| 747,359,260</pre>
 
=={{header|Visual Basic .NET}}==
{{libheader|System.Numerics}}{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System
Imports System.Console
Imports BI = System.Numerics.BigInteger
Line 3,233 ⟶ 7,019:
End While
End Sub
End Module</langsyntaxhighlight>
{{Out}}
<pre>Integer square root for numbers 0 to 65:
Line 3,281 ⟶ 7,067:
71 | 1,004,525,211,269,079,039,999,221,534,496,697,502,180,541,686,174,722,466,474,743 | 1,002,260,051,717,656,279,450,068,093,686
73 | 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 | 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|VTL-2}}==
The ISQRT routine starts at line 2000. As VTL-2 only has unsigned 16-bit arithmetic, only the roots of 7^1, 7^3 and 7^5 are shown as 7^7 is too large.
<syntaxhighlight lang="vtl2">
1000 X=0
1010 #=2000
1020 $=32
1030 ?=R
1040 X=X+1
1050 #=X=33=0*1070
1060 ?=""
1070 #=X<66*1010
1080 P=1
1090 X=7
1100 #=2000
1110 ?=""
1120 ?="Root 7^";
1130 ?=P
1140 ?="(";
1150 ?=X
1160 ?=") = ";
1170 ?=R
1180 X=X*49
1190 P=P+2
1200 #=P<6*1100
1210 #=9999
2000 A=!
2010 Q=1
2020 #=X>Q=0*2050
2030 Q=Q*4
2040 #=2020
2050 Z=X
2060 R=0
2070 #=Q<2*A
2080 Q=Q/4
2090 T=Z-R-Q
2100 I=Z<(R+Q)
2110 R=R/2
2120 #=I*2070
2130 Z=T
2140 R=R+Q
2150 #=2070
</syntaxhighlight>
{{out}}
<pre>
0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5
5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
Root 7^1(7) = 2
Root 7^3(343) = 18
Root 7^5(16807) = 129
</pre>
 
Line 3,286 ⟶ 7,123:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
import "./fmt" for Fmt
 
var isqrt = Fn.new { |x|
Line 3,323 ⟶ 7,160:
pow7 = pow7 * bi49
i = i + 2
}</langsyntaxhighlight>
 
{{out}}
Line 3,372 ⟶ 7,209:
73 49,221,735,352,184,872,959,961,855,190,338,177,606,846,542,622,561,400,857,262,407 7,015,820,362,023,593,956,150,476,655,802
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="freebasic">// Rosetta Code problem: https://rosettacode.org/wiki/Isqrt_(integer_square_root)_of_X
// by Jjuanhdez, 06/2022
 
print "Integer square root of first 65 numbers:"
for n = 1 to 65
print isqrt(n) using("##");
next n
print : print
print "Integer square root of odd powers of 7"
print " n | 7^n | isqrt "
print "----|--------------------|-----------"
for n = 1 to 21 step 2
pow7 = 7 ^ n
print n using("###"), " | ", left$(str$(pow7,"%20.1f"),18), " | ", left$(str$(isqrt(pow7),"%11.1f"),9)
next n
end
 
sub isqrt(x)
q = 1
while q <= x
q = q * 4
wend
r = 0
while q > 1
q = q / 4
t = x - r - q
r = r / 2
if t >= 0 then
x = t
r = r + q
end if
wend
return int(r)
end sub</syntaxhighlight>