In
number theory
Number theory is a branch of pure mathematics devoted primarily to the study of the integers and arithmetic functions. Number theorists study prime numbers as well as the properties of mathematical objects constructed from integers (for example ...
, the integer square root (isqrt) of a
non-negative integer
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positiv ...
is the non-negative integer which is the
greatest integer less than or equal to the
square root
In mathematics, a square root of a number is a number such that y^2 = x; in other words, a number whose ''square'' (the result of multiplying the number by itself, or y \cdot y) is . For example, 4 and −4 are square roots of 16 because 4 ...
of ,
For example,
Introductory remark
Let
and
be non-negative integers.
Algorithms that compute (the
decimal representation
A decimal representation of a non-negative real number is its expression as a sequence of symbols consisting of decimal digits traditionally written with a single separator:
r = b_k b_\cdots b_0.a_1a_2\cdots
Here is the decimal separator, ...
of)
run forever on each input
which is not a
perfect square.
[ 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.]
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
It appears that the multiplication of the input by
gives an accuracy of decimal digits.
[It is no surprise that the repeated multiplication by is a feature in ]
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 (
) the procedure
is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude.
Then
will print the entire decimal representation of
.
[The fractional part of square roots of perfect squares is rendered as .]
// Print sqrt(y), without halting
void sqrtForever(unsigned int y)
The conclusion is that algorithms which compute are computationally equivalent to
algorithms which compute .
Basic algorithms
The integer square root of a
non-negative integer
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positiv ...
can be defined as
For example,
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 addition
unsigned 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
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...
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
using binary search, one obtains the