How to resolve the algorithm Isqrt (integer square root) of X step by step in the Modula-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Isqrt (integer square root) of X step by step in the Modula-2 programming language

Table of Contents

Problem Statement

Sometimes a function is needed to find the integer square root of   X,   where   X   can be a real non─negative number. Often   X   is actually a non─negative integer. For the purposes of this task,   X   can be an integer or a real number,   but if it simplifies things in your computer programming language,   assume it's an integer.

One of the most common uses of   Isqrt   is in the division of an integer by all factors   (or primes)   up to the   √ X    of that integer,   either to find the factors of that integer,   or to determine primality.

An alternative method for finding the   Isqrt   of a number is to calculate:       floor( sqrt(X) )

If the hardware supports the computation of (real) square roots,   the above method might be a faster method for small numbers that don't have very many significant (decimal) digits. However, floating point arithmetic is limited in the number of   (binary or decimal)   digits that it can support.

For this task, the integer square root of a non─negative number will be computed using a version of   quadratic residue,   which has the advantage that no   floating point   calculations are used,   only integer arithmetic. Furthermore, the two divisions can be performed by bit shifting,   and the one multiplication can also be be performed by bit shifting or additions. The disadvantage is the limitation of the size of the largest integer that a particular computer programming language can support.

Pseudo─code of a procedure for finding the integer square root of   X       (all variables are integers): Another version for the (above)   1st   perform   is:

Integer square roots of some values:

Compute and show all output here   (on this page)   for:

You can show more numbers for the 2nd requirement if the displays fits on one screen on Rosetta Code. If your computer programming language only supports smaller integers,   show what you can.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Isqrt (integer square root) of X step by step in the Modula-2 programming language

Source code in the modula-2 programming language

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.


  

You may also check:How to resolve the algorithm Generic swap step by step in the Perl programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Sidef programming language
You may also check:How to resolve the algorithm CUSIP step by step in the Perl programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Harbour programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the C# programming language