There are many methods to compute square roots. Below is a list and explanation of methods that have been discovered throughout the history of human mathematical study.
Exponential identity
Pocket calculators typically implement good routines to compute the exponential function and the natural logarithm, and then compute the square root of using the identity
- .
The same identity is exploited when computing square roots with logarithm tables or slide rules.
Babylonian method
A commonly used algorithm for approximating is known as the "Babylonian method" and can be derived from (but predates) Newton's method. It proceeds as follows:
- Start with an arbitrary positive start value (the closer to the root, the better).
- Replace by the average of and r/x.
- Repeat steps 2 and 3.
This is a quadratically convergent algorithm, which means that the number of correct digits of roughly doubles with each step. It can also be represented as:
- .
This algorithm works equally well in the p-adic numbers, but cannot be used to identify real square roots with p-adic square roots; it is easy, for example, to construct a sequence of rational numbers by this method which converges to in the reals, but to in the 2-adics.
Rough estimation
Many of the methods for calculating square roots , such as the Babylonian method, require an initial seed value. If the initial value is too far from the actual square root, the calculation will be slowed down. It is therefore useful to have a rough estimate, which may be very inaccurate but easy to calculate. One way of obtaining such an estimate for is calculating 3D, where D is the number of digits in r (if r < 1, D will be negative).
Bakhshali approximation
The Bakhshali square root approximation is a mathematical method for finding an approximation to a square root which was described in an ancient manuscript, written in ancient India sometime between the 200 BC and the 400 CE, known as the Bakhshali Manuscript because it was discovered in 1881 near the village of Bakhshali (or Bakhshalai) in the Yusufzai subdivision of the Peshawar district (now part of Pakistan). The manuscript was made out of the leaves of birch bark tree and was written in the Sarada script.
The Bakhshali square root approximation is just the simple approximation applied twice. This gives the same result as using the simple approximation
as an initial value to Newton's method, then doing just one step of the Newton's method. It is just a little slower to compute than Newton's.
Let
Let
When expanded, the equation becomes
Example
Find
- Using and
We can get to the same result faster if we do not use the expansion:
And we can save one multiplication if instead of using
this last line we compute the result directly by
Newton's formula, like this:
this always gives the exact same result.
An exact long-division-like algorithm
This method, while much slower than the Babylonian method, has the advantage that it is exact: if the given number has a square root whose decimal representation terminates, then the algorithm terminates and produces the correct square root after finitely many steps. It can thus be used to check whether a given integer is a square number.
Write the number in decimal and divide it into pairs of digits starting from the decimal
point. The numbers are laid out similar to the long division algorithm and the final square root will appear above the original number.
For each iteration:
- Bring down the most significant pair of digits not yet used and append them to any remainder. This is the current value referred to in steps 2 and 3.
- If denotes the part of the result found so far, determine the greatest digit that does not make exceed the current value. Place the new digit on the quotient line.
- Subtract from the current value to form a new remainder.
- If the remainder is zero and there are no more digits to bring down the algorithm has terminated. Otherwise continue with step 1.
Example: What is the square root of 152.2756?
1 2. 3 4
| 01 52.27 56 1 The digit 1 is a solution digit
x 01 1(20*0+1)=1 +1
00 52 22 The digit 2 is a solution digit
2x 00 44 2(20*1+2)=44 + 2
08 27 243 The digit 3 is a solution digit
24x 07 29 3(20*12+3)=729 + 3
98 56 2464 The digit 4 is a solution digit
246x 98 56 4(20*123+4)=9856 4
00 00 Algorithm terminates: answer is 12.34
Although demonstrated here for base 10 numbers, the procedure works for
any base, including base 2. In the description above, 20 means double the number base used, in the case of binary this would really be 100. The algorithm is in fact much easier to perform in base 2, as in every step only the two digits 0 and 1 have to be tested. Napier's bones used this algorithm. See also Shifting nth-root algorithm.
More accurate rough estimation
Similar to the rough estimation procedure above. This version is slightly more accurate because it also uses the additional information provided by the first digit of the number r. The steps are almost the same as the one for previous rough estimate. This estimate can then be improved by any iterative method.
Provided that r ≫ 1
Steps
- Take the integer part of the number r. Z = int(r)
- Count the number of digits in Z. Let D be the number of digits.
- Calculate the value of 3.16D. Note: 3.16 ≈ √10
- The estimate is the value obtained in step 3 multiplied by an adjustment factor based on the first (left most) digit of the number n:
E = ADJ * 3.16D
Table of ADJ
First Digit
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
ADJ
|
0.382
|
0.497
|
0.590
|
0.670
|
0.741
|
0.806
|
0.866
|
0.922
|
0.974
|
where the derivation of the adjustment is the average of the square root of first digit and the square root of the first digit incremented by one, all divided by √10.
The accuracy of this method is very dependant on the number's first digit. Due to logrithmic spacings, errors for numbers starting with a "1" can range up to 18 percent, while errors for numbers starting with a "9" are less than 3 percent.
Example
r
|
Z
|
D
|
3.16 ^ D
|
ADJ
|
Estimate (E)
|
Actual Value of √r
|
723.47
|
723
|
3
|
31.55
|
0.866
|
27.32
|
26.90
|
5396.37
|
5396
|
4
|
99.71
|
0.741
|
73.89
|
73.46
|
24956.41
|
24956
|
5
|
315.09
|
0.497
|
156.60
|
157.98
|
789345.464
|
789345
|
6
|
995.7
|
0.866
|
862.28
|
888.45
|
198764.389
|
198764
|
6
|
995.7
|
0.382
|
380.36
|
445.83
|
Inverse square root approximation
There is an algorithm for finding the inverse square root very quickly.
Because , if we know the value of the inverse square root, we can easily get the value for the square root.
Steps for inverse square root approximation
Step 1 Start the initial value an approximate value of the inverse square root which is accurate to 2 significant digits.
Step 2 Calculate the next iteration with the following algorithm.
- where
Step 3 Finally obtain the value of with
Because would converge to the value of the inverse square root of r.
Example
Find the
Step 1 Find initial value using Bakhshali square root approximation.
Step 2 Calculate the next iteration with the following algorithm.
- where
Step 3 Finally obtain the value of
Properties
The greatest weakness of this algorithm is that it is not stable. The initial value must be near the actual value for the algorithm to converge correctly. If the initial value is not close enough, the algorithm will diverge away from the actual value.
However when the algorithm converges, it converges quickly. 20 digits of accuracy can be obtained in 6 iterations.
Square roots using Newton iteration
Basic Newton iteration finds a single root of a function given a sufficiently precise approximation to the root. The nature of which root will be given based on an approximation is dependent on the Newton fractal. The basic iteration is given by:
- .
There are two widely used functions and used to find the square root of a number denoted by "z".
(See also: Newton's method, Integer square root.)
First method
The first method finds the square root of "z"
Note that both and are roots of the function . ie .
The first derivative of is
Thus iteration for is derived where:
and
|
|
|
|
|
|
|
.
|
So this simplifies to the Babylonian method described above.
Second method
The second method finds the reciprocal of the square root of "z".
- .
The two roots to are and .
The derivative of is .
Thus iteration for is derived where:
and
|
|
|
|
|
|
|
|
|
|
|
.
|
The square root of "z" can be found by multiplying the number just computed by "z", so this method does not require any division at all.
Example
Find the using both methods.
- Because we are looking for the square root of 7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Comparison
The iteration for involves a division which is more time consuming than a multiplication (usually by a factor of 3 or more) in computer integer arithmetic. The iteration for involves no division and is thus recommended for large integers z. However, the iteration always converges, whereas the convergence of the iteration is sensitive to the initial seed.
Pell's equation
Pell's equation yields a method for finding rational approximations of square roots of integers.
Finding square roots using mental arithmetic
Based on Pell's equation there is a method to calculate square roots of integers simply by subtracting odd numbers, adapted therefore for mental arithmetic.
Example: For we start with the following sequence:
- 27 - 1 = 26
- 26 - 3 = 23
- 23 - 5 = 18
- 18 - 7 = 11
- 11 - 9 = 2
Five steps have been taken and thus the integer part of the square root of 27 is 5. We do not proceed any further because the sixth subtraction would give a negative answer.
Now take the rest (2) and multiply it by 100 to get the starting number for the next step. Take the answer we have already got (5) and multiply it by 20, then add 1 to get the first odd number we subtract by. This gives us 2 × 100 = 200 as the starting number and 5 × 20 + 1 = 101 as the first odd number. Subtract the successive odd numbers, 103, 105, etc. until we get to a step where the next subtraction would result in a negative number. So,
- 200 - 101 = 99
and 99 is less than 103 so this is the place to stop and since we only took one step we get that the next digit is 1.
For the next step the starting number will be 99 × 100 = 9900
and the answer we have already got is 51 so the first odd number will be
51 × 20 + 1 = 1021
- 9900 - 1021 = 8879
- 8879 - 1023 = 7856
- 7856 - 1025 = 6831
- 6831 - 1027 = 5804
- 5804 - 1029 = 4775
- 4775 - 1031 = 3744
- 3744 - 1033 = 2711
- 2711 - 1035 = 1676
- 1676 - 1037 = 639
We took nine steps so the next digit is 9.
Continuing with this procedure, the next starting number will be 639 × 100 = 63900 and the first odd number will be 519 × 20 + 1 = 10381
63900 - 10381 = 53519
53519 - 10383 = 43136
43136 - 10385 = 32751
32751 - 10387 = 22364
22364 - 10389 = 11975
11975 - 10391 = 1584
We took six steps so the next digit is 6.
The result gives us 5.196 as an approximation of the square root of 27.
Approximating with a random method for arbitrarily large integers
The idea of this method for finding the integer part of the square root of is to choose a random integer in the range , and square it. If it is greater than and smaller than the right bound of the range, it becomes the new right bound. If the number squared is less than n and greater than the left bound of the range, it becomes the new left bound. In this way a computer can find a good approximation to the square root for any size number very quickly. Here is Maple code that does the job:
intsqrt := proc(z) local A,B,a,b,c,e,r;
A := 1;
B:= z;
e := z-1;
while (e>1) do
r := rand(A+1..B-1):
a := r(e);
if (a > A and a^2 < z) then
A := a;
end;
if (a < B and a^2 > z) then
B := a;
end;
e := B-A;
end;
printf("Integer part of sqrt is %d.\n", A);
end proc:
Finding square roots using basic arithmetic
If you have access to a calculator capable of addition, subtraction, multiplication and division then the following method can be used to find the square root of a number. Note that this algorithm does not always terminate when attempting to determine the next digit. For example: 2, 3 and 7 do not terminate on the first digit.
Find
The starting values are
Now find new values for each variable
We found a digit when the value of “” is equal to the value of “”
After a new digit has been found.
- New
- New
The step are shown in the table below
Find
|
and
|
|
|
|
|
|
Remarks
|
SOLN
|
n=0
|
|
|
|
|
|
|
n=1
|
|
|
|
|
STOP
|
|
and
|
n=0
|
|
|
|
|
|
|
n=1
|
|
|
|
|
|
|
n=2
|
|
|
|
|
STOP
|
|
and
|
n=0
|
|
|
|
|
STOP
|
|
and
|
n=0
|
|
|
|
|
|
|
n=1
|
|
|
|
|
STOP
|
|
and
|
n=0
|
|
|
|
|
|
|
n=1
|
|
|
|
|
STOP
|
|
|
Continued fraction methods
Quadratic irrationals, that is numbers involving square roots in the form (a + √b)/c, have periodic continued fractions. This makes them easy to calculate recursively given the period. For example, to calculate √2, we make use of the fact that √2 − 1 = [0; 2, 2, 2, 2, 2, ...], and use the recurrence relation
- an + 1 = 1/(2 + an) with a0 = 0
to obtain √2 − 1 to some specific precision specified through n levels of recurrence, and add 1 to the result to obtain √2.
Steps for finding continued fractions
Find using continued fractions
Let
thus
Step 0. We shall assume that r is not a perfect square. In other words:
Step 1. Find using some other method. The best method is using some other algorithm to determine the integer square root.
Step 2. Find the highest lower bound (L) and lowest upper bound (U) for where both (L) and (U) are integers.
Hence
Step 3. Write in terms of .
and
and
Step 4. Substitute with
after substitution
Since is less than one, we can determine from the numeric values of and because (ie. is an integer). Determine the numeric value of .
Step 5. Once we know the value of , we can rework the equation for
and
Step 6. Write in terms of .
and
Step 7. Substitute with
after substitution
Since is less than one, we can determine from the numeric values of and because (ie. is an integer). Determine the numeric value of .
Step 8. Once we know the value of , we can rework the equation for
and
Step 9. Repeat step 6 , 7 and 8 .
Approximations that depend on IEEE representation
On computers, a very rapid Newton's method based approximation to the square root can be obtained for floating point numbers when computers use an IEEE (or sufficiently similar) representation.
float fastsqrt(float val) {
int tmp = *(int *)&val;
tmp -= 1<<23; /* Remove IEEE bias from exponent (-2^23) */
/* tmp is now an appoximation to logbase2(val) */
tmp = tmp >> 1; /* divide by 2 */
tmp += 1<<23; /* restore the IEEE bias from the exponent (+2^23) */
return *(float *)&tmp;
}
In the above, the operations to add and remove the IEEE bias can be combined into a single operation. An additional adjustment can be made in the same operation to reduce the maximum relative error. So, the three operations, not including the cast, can be rewritten as:
tmp = (1<<23) + (tmp >> 1) - 1<<22 + m;
Where m is some magic number that corresponds to a calculation involving an initial guess used in the Newton's approximation.
A variant of the above routine can be used to compute the inverse square root instead, which has been attributed to the programmers of the game Doom. As an approximation, it produced a relative error of less than 4%, and in computer graphics it is a very efficient way to normalize a vector.
See also
External links