HOME

TheInfoList



OR:

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 , \operatorname(n) = \lfloor \sqrt n \rfloor. For example, \operatorname(27) = \lfloor \sqrt \rfloor = \lfloor 5.19615242270663 ... \rfloor = 5.


Introductory remark

Let y and k 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) \sqrt y run forever on each input y 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 \lfloor \sqrt y \rfloor do not run forever. They are nevertheless capable of computing \sqrt y up to any desired accuracy k. Choose any k and compute \lfloor \sqrt \rfloor. For example (setting y = 2): \begin & k = 0: \lfloor \sqrt \rfloor = \lfloor \sqrt \rfloor = 1 \\ & k = 1: \lfloor \sqrt \rfloor = \lfloor \sqrt \rfloor = 14 \\ & k = 2: \lfloor \sqrt \rfloor = \lfloor \sqrt \rfloor = 141 \\ & k = 3: \lfloor \sqrt \rfloor = \lfloor \sqrt \rfloor = 1414 \\ & \vdots \\ & k = 8: \lfloor \sqrt \rfloor = \lfloor \sqrt \rfloor = 141421356 \\ & \vdots \\ \end Compare the results with \sqrt = 1.41421356237309504880168872420969807856967187537694 ... It appears that the multiplication of the input by 100^k 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 \sqrt y, one can execute \operatorname(y) an infinite number of times, increasing y by a factor 100 at each pass. Assume that in the next program (\operatorname) the procedure \operatorname(y) is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude. Then \operatorname(y) will print the entire decimal representation of \sqrt y.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 ...
y can be defined as \lfloor \sqrt y \rfloor = x : x^2 \leq y <(x+1)^2, x \in \mathbb For example, \operatorname(27) = \lfloor \sqrt \rfloor = 5 because 6^2 > 27 \text 5^2 \ngtr 27.


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 (L+1)^2 = L^2 + 2L + 1 = L^2 + 1 + \sum_^L 2. // 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 x where x^2 > y. 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 \operatorname(2000000) using binary search, one obtains the ,R/math> sequence \begin & ,2000001\rightarrow ,1000000\rightarrow ,500000\rightarrow ,250000\rightarrow ,125000\rightarrow ,62500\rightarrow ,31250\rightarrow ,15625\\ & \rightarrow ,7812\rightarrow ,3906\rightarrow ,1953\rightarrow 76,1953\rightarrow 76,1464\rightarrow 220,1464\rightarrow 342,1464\rightarrow 403,1464\\ & \rightarrow 403,1433\rightarrow 403,1418\rightarrow 410,1418\rightarrow 414,1418\rightarrow 414,1416\rightarrow 414,1415\end This computation takes 21 iteration steps, whereas linear search (ascending, starting from 0) needs steps.


Algorithm using Newton's method

One way of calculating \sqrt and \operatorname(n) is to use Heron's method, which is a special case of
Newton's method In numerical analysis, the Newton–Raphson method, also known simply as Newton's method, named after Isaac Newton and Joseph Raphson, is a root-finding algorithm which produces successively better approximations to the roots (or zeroes) of a ...
, to find a solution for the equation x^2 - n = 0, giving the iterative formula x_ = \frac\!\left(x_k + \frac\right), \quad k \ge 0, \quad x_0 > 0. The
sequence In mathematics, a sequence is an enumerated collection of objects in which repetitions are allowed and order matters. Like a set, it contains members (also called ''elements'', or ''terms''). The number of elements (possibly infinite) is cal ...
\ converges quadratically to \sqrt as k\to\infty.


Stopping criterion

One can prove that c=1 is the largest possible number for which the stopping criterion , x_ - x_, < c ensures \lfloor x_ \rfloor=\lfloor \sqrt n \rfloor in the algorithm above. In implementations which use number formats that cannot represent all
rational number In mathematics, a rational number is a number that can be expressed as the quotient or fraction of two integers, a numerator and a non-zero denominator . For example, is a rational number, as is every integer (for example, The set of all ...
s exactly (for example, floating point), a stopping constant less than 1 should be used to protect against round-off errors.


Domain of computation

Although \sqrt is
irrational Irrationality is cognition, thinking, talking, or acting without rationality. Irrationality often has a negative connotation, as thinking and actions that are less useful or more illogical than other more rational alternatives. The concept of ...
for many n, the sequence \ contains only rational terms when x_0 is rational. Thus, with this method it is unnecessary to exit the field of rational numbers in order to calculate \operatorname(n), a fact which has some theoretical advantages.


Using only integer division

For computing \lfloor \sqrt n \rfloor for very large integers ''n'', one can use the quotient of
Euclidean division In arithmetic, Euclidean division – or division with remainder – is the process of dividing one integer (the dividend) by another (the divisor), in a way that produces an integer quotient and a natural number remainder strictly smaller than ...
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 x_ = \left\lfloor \frac\!\left(x_k + \left\lfloor \frac \right\rfloor \right) \right\rfloor, \quad k \ge 0, \quad x_0 > 0, \quad x_0 \in \mathbb. By using the fact that \left\lfloor \frac\!\left(x_k + \left\lfloor \frac \right\rfloor \right) \right\rfloor = \left\lfloor \frac\!\left(x_k + \frac \right) \right\rfloor, one can show that this will reach \lfloor \sqrt n \rfloor within a finite number of iterations. In the original version, one has x_k \ge \sqrt n for k \ge 1, and x_k > x_ for x_k > \sqrt n. So in the integer version, one has \lfloor x_k \rfloor \ge \lfloor\sqrt n\rfloor and x_k \ge \lfloor x_k \rfloor > x_ \ge \lfloor x_\rfloor until the final solution x_s is reached. For the final solution x_s, one has \lfloor \sqrt n\rfloor\le\lfloor x_s\rfloor \le \sqrt n and \lfloor x_ \rfloor \ge \lfloor x_s \rfloor, so the stopping criterion is \lfloor x_ \rfloor \ge \lfloor x_k \rfloor. However, \lfloor \sqrt n \rfloor is not necessarily a fixed point of the above iterative formula. Indeed, it can be shown that \lfloor \sqrt n \rfloor is a fixed point if and only if n + 1 is not a perfect square. If n + 1 is a perfect square, the sequence ends up in a period-two cycle between \lfloor \sqrt n \rfloor and \lfloor \sqrt n \rfloor + 1 instead of converging.


Example implementation in C

// Square root of integer unsigned 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 sequence \begin & 1000000 \rightarrow 500001 \rightarrow 250002 \rightarrow 125004 \rightarrow 62509 \rightarrow 31270 \rightarrow 15666 \rightarrow 7896 \\ & \rightarrow 4074 \rightarrow 2282 \rightarrow 1579 \rightarrow 1422 \rightarrow 1414 \rightarrow 1414 \end In 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 In mathematics, the binary logarithm () is the exponentiation, power to which the number must be exponentiation, raised to obtain the value . That is, for any real number , :x=\log_2 n \quad\Longleftrightarrow\quad 2^x=n. For example, th ...
or for the
bit-length Bit length or bit width is the number of binary digits, called bits, necessary to represent an unsigned integer as a binary number. Formally, the bit length of a natural number n \geq 0 is :\ell(n) = \lceil \log_2(n+1) \rceil where \log_2 is th ...
is available (like e.g. std::bit_width in
C++20 C20 or C-20 may refer to: Science and technology * Carbon-20 (C-20 or 20C), an isotope of carbon * C20, the smallest possible fullerene (a carbon molecule) * C20 (engineering), a mix of concrete that has a compressive strength of 20 newtons per squ ...
), one should better start at x_0 = 2^, which is the least
power of two A power of two is a number of the form where is an integer, that is, the result of exponentiation with number 2, two as the Base (exponentiation), base and integer  as the exponent. In the fast-growing hierarchy, is exactly equal to f_1^ ...
bigger than \sqrt n. In the example of the integer square root of , \lfloor \log_2 n \rfloor = 20, x_0 = 2^ = 2048, and the resulting sequence is 2048 \rightarrow 1512 \rightarrow 1417 \rightarrow 1414 \rightarrow 1414. In this case only four iteration steps are needed.


Digit-by-digit algorithm

The traditional pen-and-paper algorithm for computing the square root \sqrt is based on working from higher digit places to lower, and as each new digit pick the largest that will still yield a square \leq n. 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 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 positive in ...
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 In computer software and hardware, find first set (ffs) or find first one is a bit operation that, given an unsigned Word (computer architecture), machine word, designates the index or position of the least significant bit set to one in the word c ...
, # 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.


Karatsuba square root algorithm

The Karatsuba square root algorithm is a combination of two functions: a
public In public relations and communication science, publics are groups of individual people, and the public (a.k.a. the general public) is the totality of such groupings. This is a different concept to the sociology, sociological concept of the ''Öf ...
function, which returns the integer square root of the input, and a recursive private function, which does the majority of the work. The public function normalizes the actual input, passes the normalized input to the private function, denormalizes the result of the private function, and returns that. The private function takes a normalized input, divides the input bits in half, passes the most-significant half of the input recursively to the private function, and performs some integer operations on the output of that recursive call and the least-significant half of the input to get the normalized output, which it returns. For big-integers of "50 to 1,000,000 digits", Burnikel-Ziegler Karatsuba division and Karatsuba multiplication are recommended by the algorithm's creator. 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 language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s 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.


See also

* Methods of computing square roots


Notes


References


External links

* * * {{number theoretic algorithms Number theoretic algorithms Number theory Root-finding algorithms Articles with example C code Articles with example Python (programming language) code Articles with example Rust code