How to resolve the algorithm Isqrt (integer square root) of X step by step in the MAD programming language
How to resolve the algorithm Isqrt (integer square root) of X step by step in the MAD 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 MAD programming language
Source code in the mad programming language
NORMAL MODE IS INTEGER
R INTEGER SQUARE ROOT OF X
INTERNAL FUNCTION(X)
ENTRY TO ISQRT.
Q = 1
FNDPW4 WHENEVER Q.LE.X
Q = Q * 4
TRANSFER TO FNDPW4
END OF CONDITIONAL
Z = X
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
R PRINT INTEGER SQUARE ROOTS OF 0..65
THROUGH SQ65, FOR N=0, 11, N.G.65
SQ65 PRINT FORMAT N11, ISQRT.(N), ISQRT.(N+1), ISQRT.(N+2),
0 ISQRT.(N+3), ISQRT.(N+4), ISQRT.(N+5), ISQRT.(N+6),
1 ISQRT.(N+7), ISQRT.(N+8), ISQRT.(N+9), ISQRT.(N+10)
VECTOR VALUES N11 = $11(I1,S1)*$
R MACHINE WORD SIZE ON IBM 704 IS 36 BITS
R PRINT UP TO AND INCLUDING ISQRT(7^12)
POW7 = 1
THROUGH SQ7P12, FOR I=0, 1, I.G.12
PRINT FORMAT SQ7, I, ISQRT.(POW7)
SQ7P12 POW7 = POW7 * 7
VECTOR VALUES SQ7 = $9HISQRT.(7^,I2,4H) = ,I6*$
END OF PROGRAM
You may also check:How to resolve the algorithm Repeat step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Generic swap step by step in the Jakt programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Fantom programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Suneido programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Maple programming language