In
number theory
Number theory (or arithmetic or higher arithmetic in older usage) is a branch of pure mathematics devoted primarily to the study of the integers and arithmetic function, integer-valued functions. German mathematician Carl Friedrich Gauss (1777â ...
, the integer square root (isqrt) of a
non-negative integer
In mathematics, the natural numbers are those numbers used for counting (as in "there are ''six'' coins on the table") and ordering (as in "this is the ''third'' largest city in the country").
Numbers used for counting are called ''cardinal n ...
''n'' is the non-negative integer ''m'' 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 ; in other words, a number whose ''square'' (the result of multiplying the number by itself, or  ⋅ ) is . For example, 4 and −4 are square roots of 16, because .
E ...
of ''n'',
:
For example,
Introductory remark
Let
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_\ldots b_0.a_1a_2\ldots
Here is the decimal separator, i ...
of)
run forever on each input
which is not a perfect square.
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.
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
.
// Print sqrt(y), without halting
void sqrtForever( unsigned int y )
The conclusion is that algorithms which compute isqrt() are computationally equivalent to algorithms which compute sqrt().
[see ']Methods of computing square roots
Methods of computing square roots are numerical analysis algorithms for approximating the principal, or non-negative, square root (usually denoted \sqrt, \sqrt /math>, or S^) of a real number. Arithmetically, it means given S, a procedure for fi ...
'.
Basic algorithms
The integer square root of a
non-negative integer
In mathematics, the natural numbers are those numbers used for counting (as in "there are ''six'' coins on the table") and ordering (as in "this is the ''third'' largest city in the country").
Numbers used for counting are called ''cardinal n ...
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
In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
A linear search runs in at ...
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