Integer square root explained
In number theory, the integer square root (isqrt) of a non-negative integer is the non-negative integer which is the greatest integer less than or equal to the square root of,
For example,
\operatorname{isqrt}(27)=\lfloor\sqrt{27}\rfloor=\lfloor5.19615242270663...\rfloor=5.
Introductory remark
Let
and
be non-negative integers.
Algorithms that compute (the decimal representation of)
run forever on each input
which is not a
perfect square.
[1] Algorithms that compute
do not run forever. They are nevertheless capable of computing
up to any desired accuracy
.
Choose any
and compute
.
For example (setting
):
Compare the results with
\sqrt{2}=1.41421356237309504880168872420969807856967187537694...
It appears that the multiplication of the input by
gives an accuracy of decimal digits.
[2] To compute the (entire) decimal representation of
, one can execute
an infinite number of times, increasing
by a factor
at each pass.
Assume that in the next program (
\operatorname{sqrtForever}
) the procedure
is already defined and - for the sake of the argument - that all variables can hold integers of unlimited magnitude.
Then
\operatorname{sqrtForever}(y)
will print the entire decimal representation of
.
[3] // Print sqrt(y), without haltingvoid sqrtForever(unsigned int y)
The conclusion is that algorithms which compute are computationally equivalent to algorithms which compute .
Basic algorithms
can be defined as
For example,
\operatorname{isqrt}(27)=\lfloor\sqrt{27}\rfloor=5
because
.
Algorithm using linear search
The following C programs are straightforward implementations.
Linear search using addition
In the program above (linear search, ascending) one can replace multiplication by addition, using the equivalence
// Integer square root// (linear search, ascending) using additionunsigned int isqrt(unsigned int y)
Algorithm using binary search
Linear search sequentially checks every value until it hits the smallest
where
.
A speed-up is achieved by using binary search instead. The following C-program is an implementation.
// Integer square root (using binary search)unsigned int isqrt(unsigned int y)
Numerical example
For example, if one computes
\operatorname{isqrt}(2000000)
using binary search, one obtains the
sequence
This computation takes 21 iteration steps, whereas linear search (ascending, starting from
) needs steps.
Algorithm using Newton's method
One way of calculating
and
is to use Heron's method, which is a special case of
Newton's method, to find a solution for the equation
, giving the iterative formula
converges quadratically to
as
.
Stopping criterion
One can prove that
is the largest possible number for which the stopping criterion
ensures
\lfloorxk+1\rfloor=\lfloor\sqrtn\rfloor
in the algorithm above.
In implementations which use number formats that cannot represent all rational numbers exactly (for example, floating point), a stopping constant less than 1 should be used to protect against round-off errors.
Domain of computation
Although
is
irrational for many
, the sequence
contains only rational terms when
is rational. Thus, with this method it is unnecessary to exit the
field of rational numbers in order to calculate
, a fact which has some theoretical advantages.
Using only integer division
For computing
for very large integers
n, one can use the quotient of
Euclidean division for both of the division operations. This has the advantage of only using integers for each intermediate value, thus making the use of
floating point representations of large numbers unnecessary. It is equivalent to using the iterative formula
By using the fact that
one can show that this will reach
within a finite number of iterations.
In the original version, one has
for
, and
for
. So in the integer version, one has
\lfloorxk\rfloor\ge\lfloor\sqrtn\rfloor
and
xk\ge\lfloorxk\rfloor>xk+1\ge\lfloorxk+1\rfloor
until the final solution
is reached. For the final solution
, one has
\lfloor\sqrtn\rfloor\le\lfloorxs\rfloor\le\sqrtn
and
\lfloorxs+1\rfloor\ge\lfloorxs\rfloor
, so the stopping criterion is
\lfloorxk+1\rfloor\ge\lfloorxk\rfloor
.
However,
is not necessarily a
fixed point of the above iterative formula. Indeed, it can be shown that
is a fixed point if and only if
is not a perfect square. If
is a perfect square, the sequence ends up in a period-two cycle between
and
instead of converging.
Example implementation in C
// Square root of integerunsigned int int_sqrt(unsigned int s)
Numerical example
For example, if one computes the integer square root of using the algorithm above, one obtains the sequenceIn total 13 iteration steps are needed. Although Heron's method converges quadratically close to the solution, less than one bit precision per iteration is gained at the beginning. This means that the choice of the initial estimate is critical for the performance of the algorithm.
When a fast computation for the integer part of the binary logarithm or for the bit-length is available (like e.g. std::bit_width
in C++20), one should better start at which is the least power of two bigger than
. In the example of the integer square root of,
,
, and the resulting sequence is
In this case only four iteration steps are needed.
Digit-by-digit algorithm
The traditional pen-and-paper algorithm for computing the square root
is based on working from higher digit places to lower, and as each new digit pick the largest that will still yield a square
. If stopping after the one's place, the result computed will be the integer square root.
Using bitwise operations
If working in base 2, the choice of digit is simplified to that between 0 (the "small candidate") and 1 (the "large candidate"), and digit manipulations can be expressed in terms of binary shift operations. With *
being multiplication, <<
being left shift, and >>
being logical right shift, a recursive algorithm to find the integer square root of any natural number is:
def integer_sqrt(n: int) -> int: assert n >= 0, "sqrt works for only non-negative inputs" if n < 2: return n
# Recursive call: small_cand = integer_sqrt(n >> 2) << 1 large_cand = small_cand + 1 if large_cand * large_cand > n: return small_cand else: return large_cand
- equivalently:
def integer_sqrt_iter(n: int) -> int: assert n >= 0, "sqrt works for only non-negative inputs" if n < 2: return n
# Find the shift amount. See also find first set, # shift = ceil(log2(n) * 0.5) * 2 = ceil(ffs(n) * 0.5) * 2 shift = 2 while (n >> shift) != 0: shift += 2
# Unroll the bit-setting loop. result = 0 while shift >= 0: result = result << 1 large_cand = (result + 1) # Same as result ^ 1 (xor), because the last bit is always 0. if large_cand * large_cand <= n >> shift: result = large_cand shift -= 2
return result
Traditional pen-and-paper presentations of the digit-by-digit algorithm include various optimizations not present in the code above, in particular the trick of pre-subtracting the square of the previous digits which makes a general multiplication step unnecessary. See for an example.[4]
Karatsuba square root algorithm
The Karatsuba square root algorithm is a fast algorithm for big-integers of "50 to 1,000,000 digits" if Burnikel-Ziegler Karatsuba division and Karatsuba multiplication are used.[5]
An example algorithm for 64-bit unsigned integers is below. The algorithm:
- Normalizes the input inside .
- Calls, which requires a normalized input.
- Calls with the most-significant half of the normalized input's bits, which will already be normalized as the most-significant bits remain the same.
- Continues on recursively until there's an algorithm that's faster when the number of bits is small enough.
- then takes the returned integer square root and remainder to produce the correct results for the given normalized .
- then denormalizes the result.
/// Performs a Karatsuba square root on a `u64`.pub fn u64_isqrt(mut n: u64) -> u64
/// Performs a Karatsuba square root on a normalized `u64`, returning the square/// root and remainder.fn u64_normalized_isqrt_rem(n: u64) -> (u64, u64)
In programming languages
Some programming languages dedicate an explicit operation to the integer square root calculation in addition to the general case or can be extended by libraries to this end.
Programming language | Example use | Version introduced |
---|
| BigInteger.sqrt(result, n); [6]
BigInteger.sqrtRem(result, remainder, n); | Unknown |
| (isqrt n) [7] | Unknown |
| Math.isqrt(n) [8] | 1.2 |
| n.sqrt [9] (BigInteger only) | 9 |
| isqrt(n) [10] | 0.3 |
| isqrt(n) [11] | Unknown |
| sqrtint(n) [12] | 1.35a[13] (as isqrt ) or before |
| math.isqrt(n) [14] | 3.8 |
| (integer-sqrt n) [15]
(integer-sqrt/remainder n) | Unknown |
| Integer.sqrt(n) [16] | 2.5.0 |
| isqrt(n) [17] | Unknown |
| (exact-integer-sqrt n) [18] | R6RS |
| isqrt($n) [19] | 8.5 |
| std.math.sqrt(n) [20] | Unknown | |
See also
External links
Notes and References
- [Square root#As decimal expansions|The square roots of the perfect squares (e.g., 0, 1, 4, 9, 16) are integers. In all other cases, the square roots of positive integers are irrational numbers.]
- It is no surprise that the repeated multiplication by is a feature in
- The fractional part of square roots of perfect squares is rendered as .
- Web site: Woo . C . Square root by abacus algorithm (archived) . June 1985 . https://web.archive.org/web/20120306040058/http://medialab.freaknet.org/martin/src/sqrt/sqrt.c . 2012-03-06 .
- Web site: Zimmermann . Paul . Karatsuba Square Root . 1999 . . 2006-05-24 . Research report #3805 . https://web.archive.org/web/20230511212802/https://inria.hal.science/inria-00072854v1/file/RR-3805.pdf . 2023-05-11 .
- Web site: BigInteger - Chapel Documentation 2.1 . Chapel Documentation - Chapel Documentation 2.1.
- Web site: CLHS: Function SQRT, ISQRT . Common Lisp HyperSpec (TM).
- Web site: Math - Crystal 1.13.2 . The Crystal Programming Language API docs.
- Web site: BigInteger (Java SE 21 & JDK 21) . JDK 21 Documentation.
- Web site: Mathematics - The Julia Language . Julia Documentation - The Julia Language.
- Web site: iroot- Maple Help . Help - Maplesoft.
- Web site: Catalogue of GP/PARI Functions: Arithmetic functions . PARI/GP Development Headquarters.
- Web site: Index of /archive/science/math/multiplePrecision/pari/ . PSG Digital Resources . https://web.archive.org/web/20241106111659/http://library.snls.org.sz/archive/science/math/multiplePrecision/pari/ . 2024-11-06.
- Web site: Mathematical functions . Python Standard Library documentation.
- Web site: 4.3.2 Generic Numerics . Racket Documentation.
- Web site: class Integer - RDoc Documentation . RDoc Documentation.
- Web site: Elements of the ring ℤ of integers - Standard Commutative Rings . SageMath Documentation.
- Web site: Revised7 Report on the Algorithmic Language Scheme . Scheme Standards.
- Web site: mathfunc manual page - Tcl Mathematical Functions . Tcl/Tk 8.6 Manual.
- Web site: std.math.sqrt.sqrt - Zig Documentation . Home ⚡ Zig Programming Language .