Square root algorithms compute the non-negative
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 a positive
real number
In mathematics, a real number is a number that can be used to measure a continuous one- dimensional quantity such as a duration or temperature. Here, ''continuous'' means that pairs of values can have arbitrarily small differences. Every re ...
.
Since all square roots of
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 ...
s, other than of
perfect squares, are
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 ...
,
square roots can usually only be computed to some finite precision: these
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
s typically construct a series of increasingly accurate
approximations.
Most square root computation methods are iterative: after choosing a suitable
initial estimate of
, an iterative refinement is performed until some termination criterion is met.
One refinement scheme is
Heron's method, 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 ...
.
If division is much more costly than multiplication, it may be preferable to compute the
inverse square root instead.
Other methods are available to compute the square root
digit by digit, or using
Taylor series
In mathematics, the Taylor series or Taylor expansion of a function is an infinite sum of terms that are expressed in terms of the function's derivatives at a single point. For most common functions, the function and the sum of its Taylor ser ...
.
Rational approximations of square roots may be calculated using
continued fraction expansions.
The method employed depends on the needed accuracy, and the available tools and computational power. The methods may be roughly classified as those suitable for mental calculation, those usually requiring at least paper and pencil, and those which are implemented as programs to be executed on a digital electronic computer or other computing device. Algorithms may take into account convergence (how many iterations are required to achieve a specified precision), computational complexity of individual operations (i.e. division) or iterations, and error propagation (the accuracy of the final result).
A few methods like paper-and-pencil synthetic division and series expansion, do not require a starting value. In some applications, an
integer square root is required, which is the square root rounded or truncated to the nearest integer (a modified procedure may be employed in this case).
History
Procedures for finding square roots (particularly the
square root of 2
The square root of 2 (approximately 1.4142) is the positive real number that, when multiplied by itself or squared, equals the number 2. It may be written as \sqrt or 2^. It is an algebraic number, and therefore not a transcendental number. Te ...
) have been known since at least the period of ancient Babylon in the 17th century BCE.
Babylonian mathematicians calculated the square root of 2 to three
sexagesimal
Sexagesimal, also known as base 60, is a numeral system with 60 (number), sixty as its radix, base. It originated with the ancient Sumerians in the 3rd millennium BC, was passed down to the ancient Babylonians, and is still used—in a modified fo ...
"digits" after the 1, but it is not known exactly how. They knew how to approximate a hypotenuse using
(giving for example
for the diagonal of a gate whose height is
rods and whose width is
rods) and they may have used a similar approach for finding the approximation of
Heron's method from first century Egypt was the first ascertainable algorithm for computing square root.
Modern analytic methods began to be developed after introduction of the
Arabic numeral
The ten Arabic numerals (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) are the most commonly used symbols for writing numbers. The term often also implies a positional notation number with a decimal base, in particular when contrasted with Roman numerals. ...
system to western Europe in the early Renaissance.
Today, nearly all computing devices have a fast and accurate square root function, either as a programming language construct, a compiler intrinsic or library function, or as a hardware operator, based on one of the described procedures.
Initial estimate
Many iterative square root algorithms require an initial
seed value. The seed must be a non-zero positive number; it should be between 1 and
, the number whose square root is desired, because the square root must be in that range. If the seed is far away from the root, the algorithm will require more iterations. If one initializes with
(or
), then approximately
iterations will be wasted just getting the order of magnitude of the root. It is therefore useful to have a rough estimate, which may have limited accuracy but is easy to calculate. In general, the better the initial estimate, the faster the convergence. For Newton's method, a seed somewhat larger than the root will converge slightly faster than a seed somewhat smaller than the root.
In general, an estimate is pursuant to an arbitrary interval known to contain the root (such as