How to resolve the algorithm Isqrt (integer square root) of X step by step in the S-BASIC programming language
How to resolve the algorithm Isqrt (integer square root) of X step by step in the S-BASIC 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 S-BASIC programming language
Source code in the s-basic programming language
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
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
You may also check:How to resolve the algorithm Bitcoin/address validation step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the jq programming language
You may also check:How to resolve the algorithm Catmull–Clark subdivision surface step by step in the OCaml programming language
You may also check:How to resolve the algorithm Empty program step by step in the LaTeX programming language