How to resolve the algorithm Isqrt (integer square root) of X step by step in the Lua programming language
How to resolve the algorithm Isqrt (integer square root) of X step by step in the Lua 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 Lua programming language
Source code in the lua programming language
function isqrt(x)
local q = 1
local r = 0
while q <= x do
q = q << 2
end
while q > 1 do
q = q >> 2
local t = x - r - q
r = r >> 1
if t >= 0 then
x = t
r = r + q
end
end
return r
end
print("Integer square root for numbers 0 to 65:")
for n=0,65 do
io.write(isqrt(n) .. ' ')
end
print()
print()
print("Integer square roots of oddd powers of 7 from 1 to 21:")
print(" n | 7 ^ n | isqrt(7 ^ n)")
local p = 7
local n = 1
while n <= 21 do
print(string.format("%2d | %18d | %12d", n, p, isqrt(p)))
----------------------
n = n + 2
p = p * 49
end
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the OCaml programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Snake step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the Delphi programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Groovy programming language