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

Content added Content deleted
(Added Algol W)
Line 542: Line 542:


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
The approach here, while not the quadratic residue algorithm, works just fine. The output has been put into columnar form to avoid what would otherwise be an ugly mess on a typical 80 column display.
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">
<syntaxhighlight lang="algol">
BEGIN
BEGIN


COMMENT
% RETURN INTEGER SQUARE ROOT OF N %
RETURN INTEGER SQUARE ROOT OF N USING QUADRATIC RESIDUE
INTEGER FUNCTION ISQRT(N);
ALGORITHM. WARNING: THE FUNCTION WILL FAIL FOR X GREATER
INTEGER N;
THAN 4095;
INTEGER FUNCTION ISQRT(X);
INTEGER X;
BEGIN
BEGIN
INTEGER R1, R2;
INTEGER Q, R, Z, T;
R1 := N;
Q := 1;
R2 := 1;
WHILE Q <= X DO
Q := Q * 4; % WARNING! OVERFLOW YIELDS 0 %
WHILE R1 > R2 DO
Z := X;
R := 0;
WHILE Q > 1 DO
BEGIN
BEGIN
R1 := (R1+R2) / 2;
Q := Q / 4;
R2 := N / R1;
T := Z - R - Q;
R := R / 2;
IF T >= 0 THEN
BEGIN
Z := T;
R := R + Q;
END;
END;
END;
ISQRT := R1;
ISQRT := R;
END;
END;
</syntaxhighlight>


COMMENT - LET'S EXERCISE THE FUNCTION;
COMMENT - LET'S EXERCISE THE FUNCTION;
Line 592: Line 605:
END
END
</syntaxhighlight>
</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.)
But for those for whom only the quadratic residue algorithm will do, just substitute this for the ISQRT() function in the previous example. (The output is identical.) But be warned: there is a bug lurking in the algorithm as presented in the task description. 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.
<syntaxhighlight lang="algol">
<syntaxhighlight lang="algol">
% RETURN INTEGER SQUARE ROOT OF N %
COMMENT
INTEGER FUNCTION ISQRT(N);
RETURN INTEGER SQUARE ROOT OF N USING QUADRATIC RESIDUE
INTEGER N;
ALGORITHM. WARNING: THE FUNCTION WILL FAIL FOR X GREATER
THAN 4095;
INTEGER FUNCTION ISQRT(X);
INTEGER X;
BEGIN
BEGIN
INTEGER Q, R, Z, T;
INTEGER R1, R2;
Q := 1;
R1 := N;
WHILE Q <= X DO
R2 := 1;
WHILE R1 > R2 DO
Q := Q * 4; % WARNING! OVERFLOW YIELDS 0 %
Z := X;
R := 0;
WHILE Q > 1 DO
BEGIN
BEGIN
Q := Q / 4;
R1 := (R1+R2) / 2;
T := Z - R - Q;
R2 := N / R1;
R := R / 2;
IF T >= 0 THEN
BEGIN
Z := T;
R := R + Q;
END;
END;
END;
ISQRT := R;
ISQRT := R1;
END;
END;

</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>