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

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Isqrt (integer square root) of X step by step in the J 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 J programming language

Source code in the j programming language

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
)


  

You may also check:How to resolve the algorithm Word search step by step in the J programming language
You may also check:How to resolve the algorithm Call a foreign-language function step by step in the Rust programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Constrained genericity step by step in the Eiffel programming language