Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Square root algorithms

From Wikipedia, the free encyclopedia
(Redirected fromMethods of computing square roots)
Algorithms for calculating square roots
This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages)
This articlemay containoriginal research. Pleaseimprove it byverifying the claims made and addinginline citations. Statements consisting only of original research should be removed.(January 2012) (Learn how and when to remove this message)
This articlemay be too technical for most readers to understand. Pleasehelp improve it tomake it understandable to non-experts, without removing the technical details.(September 2012) (Learn how and when to remove this message)
(Learn how and when to remove this message)

Square root algorithms compute the non-negativesquare rootS{\displaystyle {\sqrt {S}}} of a positivereal numberS{\displaystyle S}.Since all square roots ofnatural numbers, other than ofperfect squares, areirrational,[1]square roots can usually only be computed to some finite precision: thesealgorithms typically construct a series of increasingly accurateapproximations.

Most square root computation methods are iterative: after choosing a suitableinitial estimate ofS{\displaystyle {\sqrt {S}}}, aniterative refinement is performed until some termination criterion is met.One refinement scheme isHeron's method, a special case ofNewton's method.If division is much more costly than multiplication, it may be preferable to compute theinverse square root instead.

Other methods are available to compute the square rootdigit by digit, or usingTaylor series.Rational approximations of square roots may be calculated usingcontinued 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, aninteger 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

[edit]

Procedures for finding square roots (particularly thesquare root of 2) 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 threesexagesimal "digits" after the 1, but it is not known exactly how. They knew how to approximate a hypotenuse usinga2+b2a+b22a{\displaystyle {\sqrt {a^{2}+b^{2}}}\approx a+{\frac {b^{2}}{2a}}}(giving for example4160+153600{\displaystyle {\frac {41}{60}}+{\frac {15}{3600}}} for the diagonal of a gate whose height is4060{\displaystyle {\frac {40}{60}}} rods and whose width is1060{\displaystyle {\frac {10}{60}}} rods) and they may have used a similar approach for finding the approximation of2.{\displaystyle {\sqrt {2}}.}[2]

Heron's method from first century Egypt was the first ascertainable algorithm for computing square root.[3]

Modern analytic methods began to be developed after introduction of theArabic numeral system to western Europe in the early Renaissance.[4]

Today, nearly all computing devices have a fast and accurate square root function, either as a programminglanguage construct, a compiler intrinsic or library function, or as a hardware operator, based on one of the described procedures.

Initial estimate

[edit]

Many iterative square root algorithms require an initialseed value. The seed must be a non-zero positive number; it should be between 1 andS{\displaystyle S}, 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 withx0=1{\displaystyle x_{0}=1} (orS{\displaystyle S}), then approximately12|log2S|{\displaystyle {\tfrac {1}{2}}\vert \log _{2}S\vert } 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[x0,S/x0]{\displaystyle [x_{0},S/x_{0}]}). The estimate is a specific value of a functional approximation tof(x)=x{\displaystyle f(x)={\sqrt {x}}} over the interval. Obtaining a better estimate involves either obtaining tighter bounds on the interval, or finding a better functional approximation tof(x){\displaystyle f(x)}. The latter usually means using a higher order polynomial in the approximation, though not all approximations are polynomial. Common methods of estimating include scalar, linear, hyperbolic and logarithmic. A decimal base is usually used for mental or paper-and-pencil estimating. A binary base is more suitable for computer estimates. In estimating, the exponent andmantissa are usually treated separately, as the number would be expressed in scientific notation.

Decimal estimates

[edit]

Typically the numberS{\displaystyle S} is expressed inscientific notation asa×102n{\displaystyle a\times 10^{2n}} where1a<100{\displaystyle 1\leq a<100} andn is an integer, and the range of possible square roots isa×10n{\displaystyle {\sqrt {a}}\times 10^{n}} where1a<10{\displaystyle 1\leq {\sqrt {a}}<10}.

Scalar estimates

[edit]

Scalar methods divide the range into intervals, and the estimate in each interval is represented by a single scalar number. If the range is considered as a single interval, the arithmetic mean (5.5) or geometric mean (103.16{\displaystyle {\sqrt {10}}\approx 3.16}) times10n{\displaystyle 10^{n}} are plausible estimates. The absolute and relative error for these will differ. In general, a single scalar will be very inaccurate. Better estimates divide the range into two or more intervals, but scalar estimates have inherently low accuracy.

For two intervals, divided geometrically, the square rootS=a×10n{\displaystyle {\sqrt {S}}={\sqrt {a}}\times 10^{n}} can be estimated as[Note 1]S{2×10nif a<10,6×10nif a10.{\displaystyle {\sqrt {S}}\approx {\begin{cases}2\times 10^{n}&{\text{if }}a<10,\\6\times 10^{n}&{\text{if }}a\geq 10.\end{cases}}}

This estimate has maximum absolute error of4×10n{\displaystyle 4\times 10^{n}} ata=100{\displaystyle a=100}, and maximum relative error of 100% ata=1{\displaystyle a=1}.

Example

ForS=125348{\displaystyle S=125348} factored as12.5348×104{\displaystyle 12.5348\times 10^{4}}, the estimate isS6102=600{\displaystyle {\sqrt {S}}\approx 6\cdot 10^{2}=600}.

125348=354.0{\displaystyle {\sqrt {125348}}=354.0}, an absolute error of 246 and relative error of almost 70%.

Linear estimates

[edit]

A better estimate, and the standard method used, is alinear approximation to the functiony=x2{\displaystyle y=x^{2}} over a small arc. If, as above, powers of the base are factored out of the numberS and the interval reduced to [1, 100], asecant line spanning the arc, or a tangent line somewhere along the arc may be used as the approximation, but a least-squares regression line intersecting the arc will be more accurate.

A least-squares regression line minimizes the average difference between the estimate and the value of the function. Its equation isy=8.7x10{\displaystyle y=8.7x-10}. Reordering,x=0.115y+1.15{\displaystyle x=0.115y+1.15}. Rounding the coefficients for ease of computation,S(a/10+1.2)10n{\displaystyle {\sqrt {S}}\approx (a/10+1.2)\cdot 10^{n}}

That is the best estimateon average that can be achieved with a single piece linear approximation of the functiony=x2{\displaystyle y=x^{2}} in the interval [1, 100]. It has a maximum absolute error of 1.2 ata=100, and maximum relative error of 30% atS=1 and 10.[Note 2]

To divide by 10, subtract one from the exponent ofa, or figuratively move the decimal point one digit to the left. For this formulation, any additive constant 1 plus a small increment will make a satisfactory estimate so remembering the exact number isn't a burden. The approximation (rounded or not) using a single line spanning the range [1, 100] is less than one significant digit of precision; the relative error is greater than 1/22, so less than 2 bits of information are provided. The accuracy is severely limited because the range is two orders of magnitude, quite large for this kind of estimation.

A much better estimate can be obtained by a piece-wise linear approximation: multiple line segments, each approximating some subarc of the original. The more line segments used, the better the approximation. The most common way is to use tangent lines; the critical choices are how to divide the arc and where to place the tangent points. An efficacious way to divide the arc fromy = 1 toy = 100 is geometrically: for two intervals, the bounds of the intervals are the square root of the bounds of the original interval, 1×100, i.e.[1,2100] and[2100,100]. For three intervals, the bounds are the cube roots of 100:[1,3100], [3100,(3100)2], and[(3100)2,100], etc. For two intervals,2100 = 10, a very convenient number. Tangent lines are easy to derive, and are located atx=110{\displaystyle x={\sqrt {1{\sqrt {10}}}}} andx=1010{\displaystyle x={\sqrt {10{\sqrt {10}}}}}. Their equations are:y=3.56x3.16{\displaystyle y=3.56x-3.16} andy=11.2x31.6{\displaystyle y=11.2x-31.6}. Inverting, the square roots are:x=0.28y+0.89{\displaystyle x=0.28y+0.89} andx=.089y+2.8{\displaystyle x=.089y+2.8}. Thus forS=a102n{\displaystyle S=a\cdot 10^{2n}}:S{(0.28a+0.89)10nif a<10,(.089a+2.8)10nif a10.{\displaystyle {\sqrt {S}}\approx {\begin{cases}(0.28a+0.89)\cdot 10^{n}&{\text{if }}a<10,\\(.089a+2.8)\cdot 10^{n}&{\text{if }}a\geq 10.\end{cases}}}

The maximum absolute errors occur at the high points of the intervals, ata=10 and 100, and are 0.54 and 1.7 respectively. The maximum relative errors are at the endpoints of the intervals, ata=1, 10 and 100, and are 17% in both cases. 17% or 0.17 is larger than 1/10, so the method yields less than a decimal digit of accuracy.

Hyperbolic estimates

[edit]

In some cases, hyperbolic estimates may be efficacious, because a hyperbola is also a convex curve and may lie along an arc ofy =x2 better than a line. Hyperbolic estimates are more computationally complex, because they necessarily require a floating division. A near-optimal hyperbolic approximation tox2 on the interval [1, 100] isy=190/(10x)20{\displaystyle y=190/(10-x)-20}. Transposing, the square root isx=10190/(y+20){\displaystyle x=10-190/(y+20)}. Thus forS=a102n{\displaystyle S=a\cdot 10^{2n}}:S(10190a+20)10n{\displaystyle {\sqrt {S}}\approx \left(10-{\frac {190}{a+20}}\right)\cdot 10^{n}}

The division need be accurate to only one decimal digit, because the estimate overall is only that accurate, and can be done mentally. This hyperbolic estimate is better on average than scalar or linear estimates. It has maximum absolute error of 1.58 ata = 100 and maximum relative error ata = 10, where the estimate of 3.67 is 16.0% higher than the root of 3.16. If instead one performed Newton-Raphson iterations beginning with an estimate of 10, it would take two iterations to get to 3.66, matching the hyperbolic estimate. For a more typical case like 75, the hyperbolic estimate of 8.00 is only 7.6% low, and 5 Newton-Raphson iterations starting at 75 would be required to obtain a more accurate result.

Arithmetic estimates

[edit]

A method analogous to piece-wise linear approximation but using only arithmetic instead of algebraic equations, uses the multiplication tables in reverse: the square root of a number between 1 and 100 is between 1 and 10, so if we know 25 is a perfect square (5 × 5), and 36 is a perfect square (6 × 6), then the square root of a number greater than or equal to 25 but less than 36, begins with a 5. Similarly for numbers between other squares. This method will yield a correct first digit, but it is not accurate to one digit: the first digit of the square root of 35 for example, is 5, but the square root of 35 is almost 6.

A better way is to the divide the range into intervals halfway between the squares. So any number between 25 and halfway to 36, which is 30.5, estimate 5; any number greater than 30.5 up to 36, estimate 6.[Note 3] The procedure only requires a little arithmetic to find a boundary number in the middle of two products from the multiplication table. Here is a reference table of those boundaries:

anearest squarek=a{\displaystyle k={\sqrt {a}}} est.
1
1 (= 12)1
2.5
4 (= 22)2
6.5
9 (= 32)3
12.5
16 (= 42)4
20.5
25 (= 52)5
30.5
36 (= 62)6
42.5
49 (= 72)7
56.5
64 (= 82)8
72.5
81 (= 92)9
90.5
100 (= 102)10
100

The final operation is to multiply the estimatek by the power of ten divided by 2, so forS=a102n{\displaystyle S=a\cdot 10^{2n}},Sk10n{\displaystyle {\sqrt {S}}\approx k\cdot 10^{n}}

The method implicitly yields one significant digit of accuracy, since it rounds to the best first digit.

The method can be extended 3 significant digits in most cases, by interpolating between the nearest squares bounding the operand. Ifk2a<(k+1)2{\displaystyle k^{2}\leq a<(k+1)^{2}}, thena{\displaystyle {\sqrt {a}}} is approximately k plus a fraction, the difference betweena andk2 divided by the difference between the two squares:ak+R{\displaystyle {\sqrt {a}}\approx k+R} whereR=(ak2)(k+1)2k2{\displaystyle R={\frac {(a-k^{2})}{(k+1)^{2}-k^{2}}}}

The final operation, as above, is to multiply the result by the power of ten divided by 2;S=a10n(k+R)10n{\displaystyle {\sqrt {S}}={\sqrt {a}}\cdot 10^{n}\approx (k+R)\cdot 10^{n}}

k is a decimal digit andR is a fraction that must be converted to decimal. It usually has only a single digit in the numerator, and one or two digits in the denominator, so the conversion to decimal can be done mentally.

Example

find the square root of 75.

75=751020{\displaystyle 75=75\cdot 10^{2\cdot 0}}, soa is 75 andn is 0. From the multiplication tables, the square root of the mantissa must be 8 pointsomething becausea is between 8×8 = 64 and 9×9 = 81, sok is 8;something is the decimal representation ofR. The fractionR = 75 −k2 = 11, the numerator, and81 −k2 = 17, the denominator. 11/17 is a little less than 12/18 = 2/3 = .67, so guess .66 (it's okay to guess here, the error is very small). The final estimate is8 + .66 = 8.66.

75 to three significant digits is 8.66, so the estimate is good to 3 significant digits. Not all such estimates using this method will be so accurate, but they will be close.

Binary estimates

[edit]

When working in thebinary numeral system (as computers do internally), by expressingS asa×22n{\displaystyle a\times 2^{2n}} where0.12a<102{\displaystyle 0.1_{2}\leq a<10_{2}}, the square rootS=a×2n{\displaystyle {\sqrt {S}}={\sqrt {a}}\times 2^{n}} can be estimated asS(0.485+0.485a)2n{\displaystyle {\sqrt {S}}\approx (0.485+0.485a)\cdot 2^{n}}

which is the least-squares regression line to 3 significant digit coefficients.a{\displaystyle {\sqrt {a}}} has maximum absolute error of 0.0408 ata=2{\displaystyle a=2}, and maximum relative error of 3.0% ata=1{\displaystyle a=1}. A computationally convenient rounded estimate (because the coefficients are powers of 2) is:

S(0.5+0.5a)2n{\displaystyle {\sqrt {S}}\approx (0.5+0.5a)\cdot 2^{n}}[Note 4]

which has maximum absolute error of 0.086 at 2 and maximum relative error of 6.1% ata = 0.5 anda = 2.0.

ForS=125348=111101001101001002=1.11101001101001002×216,{\displaystyle S=125348=1\;1110\;1001\;1010\;0100_{2}=1.1110\;1001\;1010\;0100_{2}\times 2^{16}\,,} the binary approximation givesS(0.5+0.5a)28=1.01110100110100102×1000000002=1.456×256=372.8.{\displaystyle {\sqrt {S}}\approx (0.5+0.5a)\cdot 2^{8}=1.0111\;0100\;1101\;0010_{2}\times 1\;0000\;0000_{2}=1.456\times 256=372.8.}125348=354.0{\displaystyle {\sqrt {125348}}=354.0}, so the estimate has an absolute error of 19 and relative error of 5.3%. The relative error is a little less than 1/24, so the estimate is good to 4+ bits.

An estimate fora good to 8 bits can be obtained by table lookup on the high 8 bits ofa, remembering that the high bit is implicit in most floating point representations, and the bottom bit of the 8 should be rounded. The table is 256 bytes of precomputed 8-bit square root values. For example, for the index 111011012 representing 1.851562510, the entry is 101011102 representing 1.35937510, the square root of 1.851562510 to 8 bit precision (2+ decimal digits).

Heron's method

[edit]
For the formula used to find the area of a triangle, seeHeron's formula.

The first explicitalgorithm for approximating S  {\displaystyle \ {\sqrt {S~}}\ } is known asHeron's method, after the first-centuryGreek mathematicianHero of Alexandria who described the method in hisAD 60 workMetrica.[3] This method is also called theBabylonian method (not to be confused with theBabylonian method for approximating hypotenuses), although there is no evidence that the method was known toBabylonians.

Given a positive real numberS{\displaystyle S}, letx0 > 0 be any positiveinitial estimate.Heron's method consists in iteratively computingxn+1=12(xn+Sxn),{\displaystyle x_{n+1}={\frac {1}{2}}\left(x_{n}+{\frac {S}{x_{n}}}\right),}until the desired accuracy is achieved.The sequence ( x0, x1, x2, x3,  ) {\displaystyle \ {\bigl (}\ x_{0},\ x_{1},\ x_{2},\ x_{3},\ \ldots \ {\bigr )}\ } defined by this equation converges to limnxn=S  .{\displaystyle \ \lim _{n\to \infty }x_{n}={\sqrt {S~}}~.}

This is equivalent to usingNewton's method to solvex2S=0{\displaystyle x^{2}-S=0}. This algorithm isquadratically convergent: the number of correct digits ofxn{\displaystyle x_{n}} roughly doubles with each iteration.[5]

Derivation

[edit]

The basic idea is that if x {\displaystyle \ x\ } is an overestimate to the square root of a positive real number S {\displaystyle \ S\ } then  S x {\displaystyle \ {\tfrac {\ S\ }{x}}\ } will be an underestimate, and vice versa, so the average of these two numbers may reasonably be expected to provide a better approximation. (Theformal proof of that assertion depends on theinequality of arithmetic and geometric means that shows this average is always an overestimate of the square root, as noted in the article onsquare roots, thus assuring convergence.)

More precisely, if x {\displaystyle \ x\ } is our initial guess of S  {\displaystyle \ {\sqrt {S~}}\ } and ε {\displaystyle \ \varepsilon \ } is the error in our estimate such that S=(x+ε)2 ,{\displaystyle \ S=\left(x+\varepsilon \right)^{2}\ ,} then we can expand the binomial as: ( x+ε )2=x2+2xε+ε2{\displaystyle \ {\bigl (}\ x+\varepsilon \ {\bigr )}^{2}=x^{2}+2x\varepsilon +\varepsilon ^{2}} and solve for the error term

ε= Sx2  2x+ε  Sx2 2x ,{\displaystyle \varepsilon ={\frac {\ S-x^{2}\ }{\ 2x+\varepsilon \ }}\approx {\frac {\ S-x^{2}\ }{2x}}\ ,} if we suppose that εx {\displaystyle \ \varepsilon \ll x~}

Therefore, we can compensate for the error and update our old estimate as x+ε  x+ Sx2 2x =  S+x2 2x =  S x +x 2  xrevised .{\displaystyle \ x+\varepsilon \ \approx \ x+{\frac {\ S-x^{2}\ }{2x}}\ =\ {\frac {\ S+x^{2}\ }{2x}}\ =\ {\frac {\ {\frac {S}{\ x\ }}+x\ }{2}}\ \equiv \ x_{\mathsf {revised}}~.} Since the computed error was not exact, this is not the actual answer, but becomes our new guess to use in the next round of correction. The process of updating is iterated until desired accuracy is obtained.

This algorithm works equally well in thep-adic numbers, but cannot be used to identify real square roots withp-adic square roots; one can, for example, construct a sequence of rational numbers by this method that converges to+3 in the reals, but to−3 in the 2-adics.

Implementation inPython

[edit]
fromdecimalimportDecimal,localcontext,getcontextNumberType=int|float|Decimaldefsqrt_Heron(s:NumberType,precision:int|None=None,guess:NumberType|None=None)->Decimal:"""    Compute sqrt(s) using the Heron-Newton method with arbitrary precision.    :param s: Non-negative number whose square root to compute.    :param precision: Number of significant digits.        Defaults to the current decimal context.        Minimum supported precision is 2.        (Precision = 1 is disallowed to avoid rounding anomalies.)    :param guess: Initial guess. Defaults to s / 2.    :return: Approximation of sqrt(s) rounded to the specified precision.    """ifs==0:returnDecimal(0)s=Decimal(s)ifs<0:raiseValueError("sqrt(s) is not defined for negative numbers.")ifprecisionisNone:precision=getcontext().prec# use current global context if not specified# Silently enforce minimum precisionifprecision<2:precision=2ifguessisNone:guess=Decimal(s/2)guard=25# temporary extra digits for internal stabilitymax_iter=10_000# Local context: isolate precision changeswithlocalcontext()asctx:ctx.prec=precision+guardguess=(guess+s/guess)/2for_inrange(max_iter):next_guess=(guess+s/guess)/2# Stop when improvement is small enoughifguess-next_guess<Decimal(f"1e-{precision}"):breakguess=next_guesselse:raiseArithmeticError(f"Heron method did not converge within{max_iter} iterations")# Round to target precision (getting rid of guard)ctx.prec=precisionreturn+next_guess

Example computation

[edit]

The following example demonstrates the execution of thesqrt_Heron function with various inputs

print(f"1){sqrt_Heron(125348,precision=7,guess=600)}")print(f"2){sqrt_Heron(Decimal('3.1415926535897932384626433832795028841971693993'))}")print(f"3){sqrt_Heron(2,1_000_157)}")print(f"4){sqrt_Heron(2,10_000_005,1.414)}")print(f"5){sqrt_Heron(2,100_000_000,1)}")

This produces the following output:

1) 354.04522) 1.7724538509055160272981674833) 1.4142135623730950488016887242 ... 2697320257318491414938800048567428924) 1.4142135623730950488016887242 ... ... 8724805080541235727278721315897142625) 1.4142135623730950488016887242 ... ... ... 023678977744844723443287604232894971

Ad 1)

The computation ofs{\displaystyle {\sqrt {s\,}}} fors=125348{\displaystyle s=125348} to seven significant figures takes the following course:x0=6102=600x1=12(x0+Sx0)=12(600456666666+1253486004566666)404.456666666x2=12(x1+Sx1)=12(404.456666666+125348404.456666666)357.186837334x3=12(x2+Sx2)=12(357.186837334+125348357.186837334)354.059011038x4=12(x3+Sx3)=12(354.059011038+125348354.059011038)354.045195124x5=12(x4+Sx4)=12(354.045195124+125348354.045195124)354.045194855{\displaystyle {\begin{alignedat}{5}x_{0}&=6\cdot 10^{2}=600\\x_{1}&={\frac {1}{2}}\left(x_{0}+{\frac {S}{x_{0}}}\right)&&={\frac {1}{2}}\left(600{\phantom {456666666}}+{\frac {125348}{600}}{\phantom {4566666}}\right)&&\approx 404.456666666\\x_{2}&={\frac {1}{2}}\left(x_{1}+{\frac {S}{x_{1}}}\right)&&={\frac {1}{2}}\left(404.456666666+{\frac {125348}{404.456666666}}\right)&&\approx 357.186837334\\x_{3}&={\frac {1}{2}}\left(x_{2}+{\frac {S}{x_{2}}}\right)&&={\frac {1}{2}}\left(357.186837334+{\frac {125348}{357.186837334}}\right)&&\approx 354.059011038\\x_{4}&={\frac {1}{2}}\left(x_{3}+{\frac {S}{x_{3}}}\right)&&={\frac {1}{2}}\left(354.059011038+{\frac {125348}{354.059011038}}\right)&&\approx 354.045195124\\x_{5}&={\frac {1}{2}}\left(x_{4}+{\frac {S}{x_{4}}}\right)&&={\frac {1}{2}}\left(354.045195124+{\frac {125348}{354.045195124}}\right)&&\approx 354.045194855\end{alignedat}}}

Therefore125348354.0452{\displaystyle {\sqrt {\,125348\,}}\approx 354.0452} to seven significant figures (rounded).

Ad 2) Computation (in 6 iteration steps) ofπ×1046×1046{\displaystyle {\sqrt {\left\lfloor \pi \times 10^{46}\right\rfloor \times 10^{-46}}}} to default precision.[Note 5]

Ad 3) Computation (in 22 iteration steps) of2{\displaystyle {\sqrt {2}}} to 1,000,157 digits.[6]

Ad 4) Computation (in 23 iteration steps) of2{\displaystyle {\sqrt {2}}} to 10,000,005 digits.[7]

Ad 5) Computation (in 28 iteration steps) of2{\displaystyle {\sqrt {2}}} to 100 million digits.[citation needed]

It appears that for reasonable initial guesses not many iterations are needed.

Notes

[edit]
Explanation of lines 41 and 46
[edit]

Heron's method has the following property:

xn<Sxn+1>Sxn=Sxn+1=xnxn>Sxn+1<xn{\displaystyle {\begin{array}{rcl}x_{n}<{\sqrt {S}}&\implies &x_{n+1}>{\sqrt {S}}\\x_{n}={\sqrt {S}}&\implies &x_{n+1}=x_{n}\\x_{n}>{\sqrt {S}}&\implies &x_{n+1}<x_{n}\end{array}}}

In plain words:Once the iteration produces a value greater thanS{\displaystyle {\sqrt {S}}} (which happens immediately ifx0>S{\displaystyle x_{0}>{\sqrt {S}}}, or after one step ifx0<S{\displaystyle x_{0}<{\sqrt {S}}}), every following estimate remains aboveS{\displaystyle {\sqrt {S}}} but gets smaller each time — so the sequence “slides down” towardS{\displaystyle {\sqrt {S}}} and converges.

In line 41 of the program,guess is set to a valueS{\displaystyle \geq {\sqrt {S}}}. Then in line 46 of the code,δn=xnxn+1{\displaystyle \delta _{n}=x_{n}-x_{n+1}} cannot be negative.

Justification of the stopping criterion
[edit]

By using the difference between successive estimates,

δn=xnxn+1{\displaystyle \delta _{n}=x_{n}-x_{n+1}},

as the stopping criterion, the method ensures that the sequence of approximationsxn{\displaystyle x_{n}} is converging toward the true valueS{\displaystyle {\sqrt {S}}}. When the successive differencesδn{\displaystyle \delta _{n}} become sufficiently small, the given goal is reached. The key insight is that theabsolute error

εn=xnS{\displaystyle \varepsilon _{n}=x_{n}-{\sqrt {S}}},

is directly related to the size of the successive improvementδn{\displaystyle \delta _{n}}. Specifically, for iterative methods that converge linearly or quadratically, there exists a constantC<1{\displaystyle C<1} such that

εn+1=Cδn{\displaystyle \varepsilon _{n+1}=C\cdot \delta _{n}}.

This relationship implies that asδn{\displaystyle \delta _{n}} decreases, the absolute errorεn{\displaystyle \varepsilon _{n}} also becomes smaller. Therefore, stopping the iteration whenδn{\displaystyle \delta _{n}} falls below a given threshold ensures that the actual error is within at most that threshold.


Convergence

[edit]
Semilog graphs comparing the speed of convergence of Heron's method to find the square root of100 for different initial guesses. Negative guesses converge to the negative root, positive guesses to the positive root. Note that values closer to the root converge faster, and all approximations are overestimates. Inthe SVG file, hover over a graph to display its points.

Suppose that x0>0  and  S>0 .{\displaystyle \ x_{0}>0~~{\mathsf {and}}~~S>0~.} Then for any natural number n:xn>0 .{\displaystyle \ n:x_{n}>0~.} Let therelative error in xn {\displaystyle \ x_{n}\ } be defined by εn= xn  S  1>1 {\displaystyle \ \varepsilon _{n}={\frac {~x_{n}\ }{\ {\sqrt {S~}}\ }}-1>-1\ }and thus xn=S (1+εn) .{\displaystyle \ x_{n}={\sqrt {S~}}\cdot \left(1+\varepsilon _{n}\right)~.}

Then it can be shown that εn+1=εn22(1+εn)0 .{\displaystyle \ \varepsilon _{n+1}={\frac {\varepsilon _{n}^{2}}{2(1+\varepsilon _{n})}}\geq 0~.}

And thus that εn+2min{  εn+12 2, εn+1 2 } {\displaystyle \ \varepsilon _{n+2}\leq \min \left\{\ {\frac {\ \varepsilon _{n+1}^{2}\ }{2}},{\frac {\ \varepsilon _{n+1}\ }{2}}\ \right\}\ }and consequently that convergence is assured, andquadratic.

Worst case for convergence

[edit]

If using the rough estimate above with the Babylonian method, then the least accurate cases in ascending order are as follows:S= 1 ;x0= 2 ;x1= 1.250 ;ε1= 0.250 .S= 10 ;x0= 2 ;x1= 3.500 ;ε1< 0.107 .S= 10 ;x0= 6 ;x1= 3.833 ;ε1< 0.213 .S= 100 ;x0= 6 ;x1= 11.333 ;ε1< 0.134 .{\displaystyle {\begin{aligned}S&=\ 1\ ;&x_{0}&=\ 2\ ;&x_{1}&=\ 1.250\ ;&\varepsilon _{1}&=\ 0.250~.\\S&=\ 10\ ;&x_{0}&=\ 2\ ;&x_{1}&=\ 3.500\ ;&\varepsilon _{1}&<\ 0.107~.\\S&=\ 10\ ;&x_{0}&=\ 6\ ;&x_{1}&=\ 3.833\ ;&\varepsilon _{1}&<\ 0.213~.\\S&=\ 100\ ;&x_{0}&=\ 6\ ;&x_{1}&=\ 11.333\ ;&\varepsilon _{1}&<\ 0.134~.\end{aligned}}}

Thus in any case,ε122.ε2<25<101 .ε3<211<103 .ε4<223<106 .ε5<247<1014 .ε6<295<1028 .ε7<2191<1057 .ε8<2383<10115 .{\displaystyle {\begin{aligned}\varepsilon _{1}&\leq 2^{-2}.\\\varepsilon _{2}&<2^{-5}<10^{-1}~.\\\varepsilon _{3}&<2^{-11}<10^{-3}~.\\\varepsilon _{4}&<2^{-23}<10^{-6}~.\\\varepsilon _{5}&<2^{-47}<10^{-14}~.\\\varepsilon _{6}&<2^{-95}<10^{-28}~.\\\varepsilon _{7}&<2^{-191}<10^{-57}~.\\\varepsilon _{8}&<2^{-383}<10^{-115}~.\end{aligned}}}

Rounding errors will slow the convergence. It is recommended to keep at least one extra digit beyond the desired accuracy of the xn {\displaystyle \ x_{n}\ } being calculated, to avoid significant round-off error.

Halley's method

[edit]

When in the program above the estimates — the highlighted lines 41 and 43 —

guess=(guess+s/guess)/2# ...next_guess=(guess+s/guess)/2

are replaced by

guess*=(guess*guess+3*s)/(3*guess*guess+s)# ...next_guess=guess*(guess*guess+3*s)/(3*guess*guess+s)

the functionsqrt_Heron is transformed into an implementation ofHalley's method, where

xn+1=xnxn2+3S3xn2+S{\displaystyle x_{n+1}=x_{n}\cdot {\frac {x_{n}^{2}+3S}{3x_{n}^{2}+S}}}

As in Halley's method the estimates do not always move in one direction, line 46 will become

ifabs(guess-next_guess)<Decimal(f"1e-{precision}"):

Halley's method converges faster —therate of convergence to the root is cubic, which is better thanquadratic— iteration for iteration, but involves five multiplications per iteration (counting division as three multiplications). The five example computations are completed in 4, respectively 4, 14, 15, and 19 iteration steps. By contrast Heron's method only needs one division i.e. three multiplications, so Heron's method is slightly better in the long run.

Bakhshali method

[edit]

This method for finding an approximation to a square root was described in an AncientIndian manuscript, called theBakhshali manuscript. It is algebraically equivalent to two iterations of Heron's method and thus quartically convergent, meaning that the number of correct digits of the approximation roughly quadruples with each iteration.[8] The original presentation, using modern notation, is as follows: To calculateS{\displaystyle {\sqrt {S}}}, letx02{\displaystyle x_{0}^{2}} be the initial approximation toS{\displaystyle S}. Then, successively iterate as:an=Sxn22xn,xn+1=xn+an,xn+2=xn+1an22xn+1.{\displaystyle {\begin{aligned}a_{n}&={\frac {S-x_{n}^{2}}{2x_{n}}},\\x_{n+1}&=x_{n}+a_{n},\\x_{n+2}&=x_{n+1}-{\frac {a_{n}^{2}}{2x_{n+1}}}.\end{aligned}}}

The valuesxn+1{\displaystyle x_{n+1}} andxn+2{\displaystyle x_{n+2}} are exactly the same as those computed by Heron's method. To see this, the second Heron's method step would computexn+2=xn+12+S2xn+1=xn+1+Sxn+122xn+1{\displaystyle x_{n+2}={\frac {x_{n+1}^{2}+S}{2x_{n+1}}}=x_{n+1}+{\frac {S-x_{n+1}^{2}}{2x_{n+1}}}}and we can use the definitions ofxn+1{\displaystyle x_{n+1}} andan{\displaystyle a_{n}} to rearrange the numerator into:Sxn+12=S(xn+an)2=Sxn22xnanan2=Sxn2(Sxn2)an2=an2.{\displaystyle {\begin{aligned}S-x_{n+1}^{2}&=S-(x_{n}+a_{n})^{2}\\&=S-x_{n}^{2}-2x_{n}a_{n}-a_{n}^{2}\\&=S-x_{n}^{2}-(S-x_{n}^{2})-a_{n}^{2}\\&=-a_{n}^{2}.\end{aligned}}}

This can be used to construct a rational approximation to the square root by beginning with an integer. Ifx0=N{\displaystyle x_{0}=N} is an integer chosen soN2{\displaystyle N^{2}} is close toS{\displaystyle S}, andd=SN2{\displaystyle d=S-N^{2}} is the difference whose absolute value is minimized, then the first iteration can be written as:SN+d2Nd28N3+4Nd=8N4+8N2d+d28N3+4Nd=N4+6N2S+S24N3+4NS=N2(N2+6S)+S24N(N2+S).{\displaystyle {\sqrt {S}}\approx N+{\frac {d}{2N}}-{\frac {d^{2}}{8N^{3}+4Nd}}={\frac {8N^{4}+8N^{2}d+d^{2}}{8N^{3}+4Nd}}={\frac {N^{4}+6N^{2}S+S^{2}}{4N^{3}+4NS}}={\frac {N^{2}(N^{2}+6S)+S^{2}}{4N(N^{2}+S)}}.}

The Bakhshali method can be generalized to the computation of an arbitrary root, including fractional roots.[9]

One might think the second half of the Bakhshali method could be used as a simpler form of Heron's iteration and used repeatedly, e.g.an+1=an22xn+1,xn+2=xn+1+an+1,an+2=an+122xn+2,xn+3=xn+2+an+2, etc.{\displaystyle {\begin{aligned}a_{n+1}&={\frac {-a_{n}^{2}}{2x_{n+1}}},&x_{n+2}&=x_{n+1}+a_{n+1},\\a_{n+2}&={\frac {-a_{n+1}^{2}}{2x_{n+2}}},&x_{n+3}&=x_{n+2}+a_{n+2},{\text{ etc.}}\end{aligned}}}however, this isnumerically unstable. Without any reference to the original input valueS{\displaystyle S}, the accuracy is limited by that of the original computation ofan{\displaystyle a_{n}}, and that rapidly becomes inadequate.

Example

[edit]

Using the same exampleS=125348{\displaystyle S=125348} as in theHeron's method example, the first iteration givesx0=600a0=12534860022×600=195.5433200x1=600+(200)=400x2=400(200)22×400=350{\displaystyle {\begin{alignedat}{3}x_{0}&=600\\[1ex]a_{0}&={\frac {125348-600^{2}}{2\times 600}}&&=-195.5433\approx -200\\[1ex]x_{1}&=600+(-200)&&={\phantom {-}}400\\[1ex]x_{2}&=400-{\frac {(-200)^{2}}{2\times 400}}&&={\phantom {-}}350\end{alignedat}}}

Likewise the second iteration givesa2=12534835022×350=004.06857x3=350+4.06857=354.06857x4=354.068574.0685722×354.06857=354.045194{\displaystyle {\begin{alignedat}{3}a_{2}&={\frac {125348-350^{2}}{2\times 350}}&&={\phantom {00}}4.06857\\[1ex]x_{3}&=350+4.06857&&=354.06857\\[1ex]x_{4}&=354.06857-{\frac {4.06857^{2}}{2\times 354.06857}}&&=354.045194\end{alignedat}}}Unlike in Heron's method,x3{\displaystyle x_{3}} must be computed to 8 digits because the formula forx4{\displaystyle x_{4}} does not correct any error inx3{\displaystyle x_{3}}.

Digit-by-digit calculation

[edit]

This is a method to find each digit of the square root in a sequence. The technique comes from the work ofFrançois Viète, published c. 1600.[10] This method is based on thebinomial theorem and is essentially an inverse algorithm solving(x+y)2=x2+2xy+y2{\displaystyle (x+y)^{2}=x^{2}+2xy+y^{2}}. It is slower than the Babylonian method, but it has several advantages:

  • It can be easier for manual calculations.
  • Every digit of the root found is known to be correct, i.e., it does not have to be changed later.
  • If the square root has an expansion that terminates, the algorithm terminates after the last digit is found. Thus, it can be used to check whether a given integer is asquare number.
  • The algorithm works for anybase, and naturally, the way it proceeds depends on the base chosen.

Disadvantages are:

  • It becomes unmanageable for higher roots.
  • It does not tolerate inaccurate guesses or sub-calculations; such errors lead to every following digit of the result being wrong, unlike withNewton's method, which self-corrects any approximation errors.
  • While digit-by-digit calculation is efficient enough on paper, it is much too expensive for software implementations. Each iteration involves larger numbers, requiring more memory, but only advances the answer by one correct digit. Thus algorithm takes more time for each additional digit.

Napier's bones include an aid for the execution of this algorithm. The shiftingnth root algorithm is a generalization of this method.

Basic principle

[edit]

First, consider the case of finding the square root of a numberS, that is the square of a base-10 two-digit numberXY, whereX is the tens digit andY is the units digit. Specifically:S=(10X+Y)2=100X2+20XY+Y2.{\displaystyle S=\left(10X+Y\right)^{2}=100X^{2}+20XY+Y^{2}.}S will consist of 3 or 4 decimal digits.

Now to start the digit-by-digit algorithm, we split the digits ofS in two groups of two digits, starting from the right. This means that the first group will be of 1 or 2 digits. Then we determine the value ofX as the largest digit such thatX2 is less than or equal to the first group.We then compute the difference between the first group andX2 and start the second iteration by concatenating the second group to it. This is equivalent to subtracting100X2{\displaystyle 100X^{2}} fromS, and we're left withS=20XY+Y2{\displaystyle S'=20XY+Y^{2}}.We divideS' by 10, then divide it by2X and keep the integer part to try and guessY.We concatenate2X with the tentativeY and multiply it byY. If our guess is correct, this is equivalent to computing:(10(2X)+Y)Y=20XY+Y2=S,{\displaystyle (10(2X)+Y)Y=20XY+Y^{2}=S',} and so the remainder, that is the difference betweenS' and the result, is zero; if the result is higher thanS', we lower our guess by 1 and try again until the remainder is 0.Since this is a simple case where the answer is a perfect square rootXY, the algorithm stops here.

The same idea can be extended to any arbitrary square root computation next. Suppose we are able to find the square root ofS by expressing it as a sum ofn positive numbers such thatS=(a1+a2+a3++an)2.{\displaystyle S=\left(a_{1}+a_{2}+a_{3}+\dots +a_{n}\right)^{2}.}

By repeatedly applying the basic identity(x+y)2=x2+2xy+y2,{\displaystyle (x+y)^{2}=x^{2}+2xy+y^{2},} the right-hand-side term can be expanded as(a1+a2+a3++an)2=a12+2a1a2+a22+2(a1+a2)a3+a32++an12+2(i=1n1ai)an+an2=a12+[2a1+a2]a2+[2(a1+a2)+a3]a3++[2(i=1n1ai)+an]an.{\displaystyle {\begin{aligned}&(a_{1}+a_{2}+a_{3}+\dotsb +a_{n})^{2}\\=&\,a_{1}^{2}+2a_{1}a_{2}+a_{2}^{2}+2(a_{1}+a_{2})a_{3}+a_{3}^{2}+\dots +a_{n-1}^{2}+2\left(\sum _{i=1}^{n-1}a_{i}\right)a_{n}+a_{n}^{2}\\=&\,a_{1}^{2}+[2a_{1}+a_{2}]a_{2}+[2(a_{1}+a_{2})+a_{3}]a_{3}+\dots +\left[2\left(\sum _{i=1}^{n-1}a_{i}\right)+a_{n}\right]a_{n}.\end{aligned}}}

This expression allows us to find the square root by sequentially guessing the values ofai{\displaystyle a_{i}}s. Suppose that the numbersa1,,am1{\displaystyle a_{1},\ldots ,a_{m-1}} have already been guessed, then them-th term of the right-hand-side of the above summation is given byYm=[2Pm1+am]am,{\displaystyle Y_{m}=\left[2P_{m-1}+a_{m}\right]a_{m},} wherePm1=i=1m1ai{\textstyle P_{m-1}=\sum _{i=1}^{m-1}a_{i}} is the approximate square root found so far. Now each new guessam{\displaystyle a_{m}} should satisfy the recursionXm=Xm1Ym,{\displaystyle X_{m}=X_{m-1}-Y_{m},} whereXm{\displaystyle X_{m}} is the sum of all the terms afterYm{\displaystyle Y_{m}}, i.e. the remainder, such thatXm0{\displaystyle X_{m}\geq 0} for all1mn,{\displaystyle 1\leq m\leq n,} with initializationX0=S.{\displaystyle X_{0}=S.} WhenXn=0,{\displaystyle X_{n}=0,} the exact square root has been found; if not, then the sum of theai{\displaystyle a_{i}}s gives a suitable approximation of the square root, withXn{\displaystyle X_{n}} being the approximation error.

For example, in the decimal number system we haveS=(a110n1+a210n2++an110+an)2,{\displaystyle S=\left(a_{1}\cdot 10^{n-1}+a_{2}\cdot 10^{n-2}+\cdots +a_{n-1}\cdot 10+a_{n}\right)^{2},} where10ni{\displaystyle 10^{n-i}} are place holders and the coefficientsai{0,1,2,,9}{\displaystyle a_{i}\in \{0,1,2,\ldots ,9\}}. At any m-th stage of the square root calculation, the approximate root found so far,Pm1{\displaystyle P_{m-1}} and the summation termYm{\displaystyle Y_{m}} are given byPm1=i=1m1ai10ni=10nm+1i=1m1ai10mi1,{\displaystyle P_{m-1}=\sum _{i=1}^{m-1}a_{i}\cdot 10^{n-i}=10^{n-m+1}\sum _{i=1}^{m-1}a_{i}\cdot 10^{m-i-1},}Ym=[2Pm1+am10nm]am10nm=[20i=1m1ai10mi1+am]am102(nm).{\displaystyle Y_{m}=\left[2P_{m-1}+a_{m}\cdot 10^{n-m}\right]a_{m}\cdot 10^{n-m}=\left[20\sum _{i=1}^{m-1}a_{i}\cdot 10^{m-i-1}+a_{m}\right]a_{m}\cdot 10^{2(n-m)}.}

Here since the place value ofYm{\displaystyle Y_{m}} is an even power of 10, we only need to work with the pair of most significant digits of the remainderXm1{\displaystyle X_{m-1}}, whose first term isYm{\displaystyle Y_{m}}, at any m-th stage. The section below codifies this procedure.

It is obvious that a similar method can be used to compute the square root in number systems other than the decimal number system. For instance, finding the digit-by-digit square root in the binary number system is quite efficient since the value ofai{\displaystyle a_{i}} is searched from a smaller set of binary digits {0,1}. This makes the computation faster since at each stage the value ofYm{\displaystyle Y_{m}} is eitherYm=0{\displaystyle Y_{m}=0} foram=0{\displaystyle a_{m}=0} orYm=2Pm1+1{\displaystyle Y_{m}=2P_{m-1}+1} foram=1{\displaystyle a_{m}=1}. The fact that we have only two possible options foram{\displaystyle a_{m}} also makes the process of deciding the value ofam{\displaystyle a_{m}} atm-th stage of calculation easier. This is because we only need to check ifYmXm1{\displaystyle Y_{m}\leq X_{m-1}} foram=1.{\displaystyle a_{m}=1.} If this condition is satisfied, then we takeam=1{\displaystyle a_{m}=1}; if not thenam=0.{\displaystyle a_{m}=0.} Also, the fact that multiplication by 2 is done by left bit-shifts helps in the computation.

Decimal (base 10)

[edit]

Write the original number in decimal form. The numbers are written similar to thelong division algorithm, and, as in long division, the root will be written on the line above. Now separate the digits into pairs, starting from the decimal point and going both left and right. The decimal point of the root will be above the decimal point of the square. One digit of the root will appear above each pair of digits of the square.

Beginning with the left-most pair of digits, do the following procedure for each pair:

  1. Starting on the left, bring down the most significant (leftmost) pair of digits not yet used (if all the digits have been used, write "00") and write them to the right of the remainder from the previous step (on the first step, there will be no remainder). In other words, multiply the remainder by 100 and add the two digits. This will be thecurrent valuec.
  2. Findp,y andx, as follows:
    • Letp be thepart of the root found so far, ignoring any decimal point. (For the first step,p = 0.)
    • Determine the greatest digitx such thatx(20p+x)c{\displaystyle x(20p+x)\leq c}. We will use a new variabley =x(20p +x).
      • Note: 20p +x is simply twicep, with the digitx appended to the right.
      • Note:x can be found by guessing whatc/(20·p) is and doing a trial calculation ofy, then adjustingx upward or downward as necessary.
    • Place the digitx{\displaystyle x} as the next digit of the root, i.e., above the two digits of the square you just brought down. Thus the nextp will be the oldp times 10 plusx.
  3. Subtracty fromc to form a new remainder.
  4. If the remainder is zero and there are no more digits to bring down, then the algorithm has terminated. Otherwise go back to step 1 for another iteration.

Examples

[edit]

Find the square root of 152.2756.

  1  2. 3  4       /     \/  01 52.27 56         01                   1*1 <= 1 < 2*2                 x=1 01                    y = x*x = 1*1 = 1         00 52                22*2 <= 52 < 23*3              x=2 00 44                 y = (20+x)*x = 22*2 = 44            08 27             243*3 <= 827 < 244*4           x=3 07 29              y = (240+x)*x = 243*3 = 729               98 56          2464*4 <= 9856 < 2465*5        x=4 98 56           y = (2460+x)*x = 2464*4 = 9856               00 00Algorithm terminates: Answer=12.34

Binary numeral system (base 2)

[edit]

This section uses the formalism fromthe digit-by-digit calculation section above, with the slight variation that we letN2=(an++a0)2{\displaystyle N^{2}=(a_{n}+\dotsb +a_{0})^{2}}, with eacham=2m{\displaystyle a_{m}=2^{m}} oram=0{\displaystyle a_{m}=0}.
We iterate all2m{\displaystyle 2^{m}}, from2n{\displaystyle 2^{n}} down to20{\displaystyle 2^{0}}, and build up an approximate solutionPm=an+an1++am{\displaystyle P_{m}=a_{n}+a_{n-1}+\ldots +a_{m}}, the sum of allai{\displaystyle a_{i}} for which we have determined the value.
To determine ifam{\displaystyle a_{m}} equals2m{\displaystyle 2^{m}} or0{\displaystyle 0}, we letPm=Pm+1+2m{\displaystyle P_{m}=P_{m+1}+2^{m}}. IfPm2N2{\displaystyle P_{m}^{2}\leq N^{2}} (i.e. the square of our approximate solution including2m{\displaystyle 2^{m}} does not exceed the target square) thenam=2m{\displaystyle a_{m}=2^{m}}, otherwiseam=0{\displaystyle a_{m}=0} andPm=Pm+1{\displaystyle P_{m}=P_{m+1}}.
To avoid squaringPm{\displaystyle P_{m}} in each step, we store the differenceXm=N2Pm2{\displaystyle X_{m}=N^{2}-P_{m}^{2}} and incrementally update it by settingXm=Xm+1Ym{\displaystyle X_{m}=X_{m+1}-Y_{m}} withYm=Pm2Pm+12=2Pm+1am+am2{\displaystyle Y_{m}=P_{m}^{2}-P_{m+1}^{2}=2P_{m+1}a_{m}+a_{m}^{2}}.
Initially, we setan=Pn=2n{\displaystyle a_{n}=P_{n}=2^{n}} for the largestn{\displaystyle n} with(2n)2=4nN2{\displaystyle (2^{n})^{2}=4^{n}\leq N^{2}}.

As an extra optimization, we storePm+12m+1{\displaystyle P_{m+1}2^{m+1}} and(2m)2{\displaystyle (2^{m})^{2}}, the two terms ofYm{\displaystyle Y_{m}} in case thatam{\displaystyle a_{m}} is nonzero, in separate variablescm{\displaystyle c_{m}},dm{\displaystyle d_{m}}:cm=Pm+12m+1{\displaystyle c_{m}=P_{m+1}2^{m+1}}dm=(2m)2{\displaystyle d_{m}=(2^{m})^{2}}Ym={cm+dmif am=2m0if am=0{\displaystyle Y_{m}={\begin{cases}c_{m}+d_{m}&{\text{if }}a_{m}=2^{m}\\0&{\text{if }}a_{m}=0\end{cases}}}

cm{\displaystyle c_{m}} anddm{\displaystyle d_{m}} can be efficiently updated in each step:cm1=Pm2m=(Pm+1+am)2m=Pm+12m+am2m={cm/2+dmif am=2mcm/2if am=0{\displaystyle c_{m-1}=P_{m}2^{m}=(P_{m+1}+a_{m})2^{m}=P_{m+1}2^{m}+a_{m}2^{m}={\begin{cases}c_{m}/2+d_{m}&{\text{if }}a_{m}=2^{m}\\c_{m}/2&{\text{if }}a_{m}=0\end{cases}}}dm1=dm4{\displaystyle d_{m-1}={\frac {d_{m}}{4}}}

Note that:c1=P020=P0=N,{\displaystyle c_{-1}=P_{0}2^{0}=P_{0}=N,} which is the final result returned in the function below.

Implementation

[edit]

ThePython program computesisqrt(n)=n.{\displaystyle \operatorname {isqrt} (n)=\lfloor {\sqrt {n}}\rfloor .}. The algorithm is adigit-by-digit (bit-by-bit) method forinteger square roots.[11]

defisqrt(x:int)->int:assertx>=0,"sqrt input should be non-negative"op:int=x# X_(n+1)res:int=0# c_n# d_n which starts at the highest power of four <= none:int=1whileone<=op:one<<=2# Now 'one' is the largest power of four <= xone>>=2# for dₙ … d₀whileone!=0:ifop>=res+one:# if X_(m+1) ≥ Y_m then a_m = 2^mop-=res+one# X_m = X_(m+1) - Y_mres+=2*one# c_m = c_m + 2*d_mres//=2# c_(m-1) = c_m / 2one//=4# d_(m-1) = d_m / 4# c_(-1)returnres

Faster algorithms, in binary and decimal or any other base, can be realized by using lookup tables—in effect tradingmore storage space for reduced run time.[12]

Exponential identity

[edit]
icon
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(May 2020) (Learn how and when to remove this message)

Pocket calculators typically implement good routines to compute theexponential function and thenatural logarithm, and then compute the square root ofS using the identity found using the properties of logarithms (lnxn=nlnx{\displaystyle \ln x^{n}=n\ln x}) and exponentials(elnx=x{\displaystyle e^{\ln x}=x}):S=e12lnS.{\displaystyle {\sqrt {S}}=e^{{\frac {1}{2}}\ln S}.}The denominator in the fraction corresponds to thenth root. In the case above the denominator is 2, hence the equation specifies that the square root is to be found. The same identity is used when computing square roots withlogarithm tables orslide rules.

A two-variable iterative method

[edit]

This method is applicable for finding the square root of0<S<3{\displaystyle 0<S<3\,\!} and converges best forS1{\displaystyle S\approx 1}.This, however, is no real limitation for a computer-based calculation, as in base 2 floating-point and fixed-point representations, it is trivial to multiplyS{\displaystyle S\,\!} by an integer power of 4, and thereforeS{\displaystyle {\sqrt {S}}} by the corresponding power of 2, by changing the exponent or by shifting, respectively. Therefore,S{\displaystyle S\,\!} can be moved to the range12S<2{\textstyle {\tfrac {1}{2}}\leq S<2}. Moreover, the following method does not employ general divisions, but only additions, subtractions, multiplications, and divisions by powers of two, which are again trivial to implement. A disadvantage of the method is that numerical errors accumulate, in contrast to single variable iterative methods such as the Babylonian one.

The initialization step of this method isa0=Sc0=S1{\displaystyle {\begin{aligned}a_{0}&=S\\c_{0}&=S-1\end{aligned}}}while the iterative steps readan+1=anancn/2cn+1=cn2(cn3)/4{\displaystyle {\begin{aligned}a_{n+1}&=a_{n}-a_{n}c_{n}/2\\c_{n+1}&=c_{n}^{2}(c_{n}-3)/4\end{aligned}}}Then,anS{\displaystyle a_{n}\to {\sqrt {S}}} (whilecn0{\displaystyle c_{n}\to 0}).

The convergence ofcn{\displaystyle c_{n}\,\!}, and therefore also ofan{\displaystyle a_{n}\,\!}, is quadratic.

The proof of the method is rather easy. First, rewrite the iterative definition ofcn{\displaystyle c_{n}} as1+cn+1=(1+cn)(112cn)2.{\displaystyle 1+c_{n+1}=(1+c_{n})(1-{\tfrac {1}{2}}c_{n})^{2}.}Then it is straightforward to prove by induction thatS(1+cn)=an2{\displaystyle S(1+c_{n})=a_{n}^{2}}and therefore the convergence ofan{\displaystyle a_{n}\,\!} to the desired resultS{\displaystyle {\sqrt {S}}} is ensured by the convergence ofcn{\displaystyle c_{n}\,\!} to 0, which in turn follows from1<c0<2{\displaystyle -1<c_{0}<2\,\!}.

This method was developed around 1950 byM. V. Wilkes,D. J. Wheeler andS. Gill[13] for use onEDSAC, one of the first electronic computers.[14] The method was later generalized, allowing the computation of non-square roots.[15]

Iterative methods for reciprocal square roots

[edit]

The following are iterative methods for finding the reciprocal square root ofS which is1/S{\displaystyle 1/{\sqrt {S}}}. Once it has been found, findS{\displaystyle {\sqrt {S}}} by simple multiplication:S=S(1/S){\displaystyle {\sqrt {S}}=S\cdot (1/{\sqrt {S}})}. These iterations involve only multiplication, and not division. They are therefore faster than theBabylonian method. However, they are not stable. If the initial value is not close to the reciprocal square root, the iterations will diverge away from it rather than converge to it. It can therefore be advantageous to perform an iteration of the Babylonian method on a rough estimate before starting to apply these methods.

Goldschmidt's algorithm

[edit]

Goldschmidt's algorithm is an extension ofGoldschmidt division, named after Robert Elliot Goldschmidt,[16][17] which can be used to calculate square roots. Some computers use Goldschmidt's algorithm to simultaneously calculateS{\displaystyle {\sqrt {S}}} and1/S{\displaystyle 1/{\sqrt {S}}}.Goldschmidt's algorithm findsS{\displaystyle {\sqrt {S}}} faster than Newton-Raphson iteration on a computer with afused multiply–add instruction and either a pipelined floating-point unit or two independent floating-point units.[18]

The first way of writing Goldschmidt's algorithm begins

b0=S{\displaystyle b_{0}=S}
Y01/S{\displaystyle Y_{0}\approx 1/{\sqrt {S}}} (typically using a table lookup)
y0=Y0{\displaystyle y_{0}=Y_{0}}
x0=Sy0{\displaystyle x_{0}=Sy_{0}}

and iteratesbn+1=bnYn2Yn+1=12(3bn+1)xn+1=xnYn+1yn+1=ynYn+1{\displaystyle {\begin{aligned}b_{n+1}&=b_{n}Y_{n}^{2}\\Y_{n+1}&={\tfrac {1}{2}}(3-b_{n+1})\\x_{n+1}&=x_{n}Y_{n+1}\\y_{n+1}&=y_{n}Y_{n+1}\end{aligned}}}untilbi{\displaystyle b_{i}} is sufficiently close to 1, or a fixed number of iterations. The iterations converge tolimnxn=S,{\displaystyle \lim _{n\to \infty }x_{n}={\sqrt {S}},} andlimnyn=1/S.{\displaystyle \lim _{n\to \infty }y_{n}=1/{\sqrt {S}}.}Note that it is possible to omit eitherxn{\displaystyle x_{n}} andyn{\displaystyle y_{n}} from the computation, and if both are desired thenxn=Syn{\displaystyle x_{n}=Sy_{n}} may be used at the end rather than computing it through in each iteration.

A second form, usingfused multiply-add operations, begins

y01/S{\displaystyle y_{0}\approx 1/{\sqrt {S}}} (typically using a table lookup)
x0=Sy0{\displaystyle x_{0}=Sy_{0}}
h0=12y0{\displaystyle h_{0}={\tfrac {1}{2}}y_{0}}

and iteratesrn=0.5xnhnxn+1=xn+xnrnhn+1=hn+hnrn{\displaystyle {\begin{aligned}r_{n}&=0.5-x_{n}h_{n}\\x_{n+1}&=x_{n}+x_{n}r_{n}\\h_{n+1}&=h_{n}+h_{n}r_{n}\end{aligned}}}untilri{\displaystyle r_{i}} is sufficiently close to 0, or a fixed number of iterations. This converges tolimnxn=S,{\displaystyle \lim _{n\to \infty }x_{n}={\sqrt {S}},} andlimn2hn=1/S.{\displaystyle \lim _{n\to \infty }2h_{n}=1/{\sqrt {S}}.}

Taylor series

[edit]

IfN is an approximation toS{\displaystyle {\sqrt {S}}}, a better approximation can be found by using theTaylor series of thesquare root function:N2+d=Nn=0(1)n(2n)!(12n)n!24ndnN2n=N(1+d2N2d28N4+d316N65d4128N8+){\displaystyle {\sqrt {N^{2}+d}}=N\sum _{n=0}^{\infty }{\frac {(-1)^{n}(2n)!}{(1-2n)n!^{2}4^{n}}}{\frac {d^{n}}{N^{2n}}}=N\left(1+{\frac {d}{2N^{2}}}-{\frac {d^{2}}{8N^{4}}}+{\frac {d^{3}}{16N^{6}}}-{\frac {5d^{4}}{128N^{8}}}+\cdots \right)}

As an iterative method, theorder of convergence is equal to the number of terms used. With two terms, it is identical to theBabylonian method. With three terms, each iteration takes almost as many operations as theBakhshali approximation, but converges more slowly.[citation needed] Therefore, this is not a particularly efficient way of calculation. To maximize the rate of convergence, chooseN so that|d|N2{\displaystyle {\frac {|d|}{N^{2}}}\,} is as small as possible.

Continued fraction expansion

[edit]
See also:Solving quadratic equations with continued fractions

Thecontinued fraction representation of a real number can be used instead of its decimal or binary expansion and this representation has the property that the square root of any rational number (which is not already a perfect square) has a periodic, repeating expansion, similar to how rational numbers have repeating expansions in the decimal notation system.

Quadratic irrationals (numbers of the forma+bc{\displaystyle {\frac {a+{\sqrt {b}}}{c}}}, wherea,b andc are integers), and in particular, square roots of integers, haveperiodic continued fractions. Sometimes what is desired is finding not the numerical value of a square root, but rather itscontinued fraction expansion, and hence its rational approximation. LetS be the positive number for which we are required to find the square root. Then assuminga to be a number that serves as an initial guess andr to be the remainder term, we can writeS=a2+r.{\displaystyle S=a^{2}+r.} Since we haveSa2=(S+a)(Sa)=r{\displaystyle S-a^{2}=({\sqrt {S}}+a)({\sqrt {S}}-a)=r}, we can express the square root ofS asS=a+ra+S.{\displaystyle {\sqrt {S}}=a+{\frac {r}{a+{\sqrt {S}}}}.}

By applying this expression forS{\displaystyle {\sqrt {S}}} to the denominator term of the fraction, we have:S=a+ra+(a+ra+S)=a+r2a+ra+S.{\displaystyle {\sqrt {S}}=a+{\frac {r}{a+(a+{\frac {r}{a+{\sqrt {S}}}})}}=a+{\frac {r}{2a+{\frac {r}{a+{\sqrt {S}}}}}}.}

Compact notationThe numerator/denominator expansion for continued fractions (above) is cumbersome to write as well as to embed in text formatting systems. So mathematicians have devised several alternative notations(see:Generalized continued fraction § Notation) such as:S=a+r2a+r2a+r2a+{\displaystyle {\sqrt {S}}=a+{\frac {r}{2a+}}\,{\frac {r}{2a+}}\,{\frac {r}{2a+}}\cdots }

Whenr=1{\displaystyle r=1} throughout, an even more compact notation is:[Note 7][a;2a,2a,2a,]{\displaystyle [a;2a,2a,2a,\cdots ]}For repeating continued fractions (which all square roots of non-perfect squares do), the repetend is represented only once, with an overline to signify a non-terminating repetition of the overlined part:[Note 8][a;2a¯]{\displaystyle [a;{\overline {2a}}]}

For2, the value ofa = 1, so its representation is:[1;2¯]{\displaystyle [1;{\overline {2}}]}

Proceeding this way, we get ageneralized continued fraction for the square root asS=a+r2a+r2a+r2a+{\displaystyle {\sqrt {S}}=a+{\cfrac {r}{2a+{\cfrac {r}{2a+{\cfrac {r}{2a+\ddots }}}}}}}

The first step to evaluating such a fraction[19] to obtain a root is to do numerical substitutions for the root of the number desired, and number of denominators selected. For example, in canonical form,r = 1 and for2,a = 1, so the numerical continued fraction for 3 denominators is:21+12+12+12{\displaystyle {\sqrt {2}}\approx 1+{\cfrac {1}{2+{\cfrac {1}{2+{\cfrac {1}{2}}}}}}}

Step 2 is to reduce the continued fraction from the bottom up, one denominator at a time, to yield a rational fraction whose numerator and denominator are integers. The reduction proceeds thus (taking the first three denominators):1+12+12+12=1+12+152=1+12+25=1+1125=1+512=1712{\displaystyle {\begin{aligned}1+{\cfrac {1}{2+{\cfrac {1}{2+{\cfrac {1}{2}}}}}}&=1+{\cfrac {1}{2+{\cfrac {1}{\frac {5}{2}}}}}\\&=1+{\cfrac {1}{2+{\cfrac {2}{5}}}}=1+{\cfrac {1}{\frac {12}{5}}}\\&=1+{\cfrac {5}{12}}={\frac {17}{12}}\end{aligned}}}

Finally (step 3), divide the numerator by the denominator of the rational fraction to obtain the approximate value of the root:17÷12=1.42{\displaystyle 17\div 12=1.42} rounded to three digits of precision.

The actual value of2 is 1.41 to three significant digits. The relative error is 0.17%, so the rational fraction is good to almost three digits of precision. Taking more denominators gives successively better approximations: four denominators yields the fraction4129=1.4137{\displaystyle {\frac {41}{29}}=1.4137}, good to almost 4 digits of precision, etc.

The following are examples of square roots, their simple continued fractions, and their first terms — calledconvergents — up to and including denominator 99:

S~decimalcontinued fractionconvergents
21.41421[1;2¯]{\displaystyle [1;{\overline {2}}]}32,75,1712,4129,9970{\displaystyle {\frac {3}{2}},{\frac {7}{5}},{\frac {17}{12}},{\frac {41}{29}},{\frac {99}{70}}}
31.73205[1;1,2¯]{\displaystyle [1;{\overline {1,2}}]}21,53,74,1911,2615,7141,9756{\displaystyle {\frac {2}{1}},{\frac {5}{3}},{\frac {7}{4}},{\frac {19}{11}},{\frac {26}{15}},{\frac {71}{41}},{\frac {97}{56}}}
52.23607[2;4¯]{\displaystyle [2;{\overline {4}}]}94,3817,16172{\displaystyle {\frac {9}{4}},{\frac {38}{17}},{\frac {161}{72}}}
62.44949[2;2,4¯]{\displaystyle [2;{\overline {2,4}}]}52,229,4920,21889{\displaystyle {\frac {5}{2}},{\frac {22}{9}},{\frac {49}{20}},{\frac {218}{89}}}
103.16228[3;6¯]{\displaystyle [3;{\overline {6}}]}196,11737{\displaystyle {\frac {19}{6}},{\frac {117}{37}}}
π{\displaystyle {\sqrt {\pi }}}1.77245[1;1,3,2,1,1,6...]{\displaystyle [1;1,3,2,1,1,6...]}21,74,169,2313,3922{\displaystyle {\frac {2}{1}},{\frac {7}{4}},{\frac {16}{9}},{\frac {23}{13}},{\frac {39}{22}}}
e{\displaystyle {\sqrt {e}}}1.64872[1;1,1,1,5,1,1...]{\displaystyle [1;1,1,1,5,1,1...]}21,32,53,2817,3320,6137{\displaystyle {\frac {2}{1}},{\frac {3}{2}},{\frac {5}{3}},{\frac {28}{17}},{\frac {33}{20}},{\frac {61}{37}}}
ϕ{\displaystyle {\sqrt {\phi }}}1.27202[1;3,1,2,11,3,7...]{\displaystyle [1;3,1,2,11,3,7...]}43,54,1411{\displaystyle {\frac {4}{3}},{\frac {5}{4}},{\frac {14}{11}}}

In general, the larger the denominator of a rational fraction, the better the approximation. It can also be shown that truncating a continued fraction yields a rational fraction that is the best approximation to the root of any fraction with denominator less than or equal to the denominator of that fraction — e.g., no fraction with a denominator less than or equal to 70 is as good an approximation to2 as 99/70.

Approximations that depend on the floating point representation

[edit]
See also:binary logarithm

A number is represented in afloating point format asm×bp{\displaystyle m\times b^{p}} which is also calledscientific notation. Its square root ism×bp/2{\displaystyle {\sqrt {m}}\times b^{p/2}} and similar formulae would apply for cube roots and logarithms. On the face of it, this is no improvement in simplicity, but suppose that only an approximation is required: then justbp/2{\displaystyle b^{p/2}} is good to an order of magnitude. Next, recognise that some powers,p, will be odd, thus for 3141.59 = 3.14159×103 rather than deal with fractional powers of the base, multiply the mantissa by the base and subtract one from the power to make it even. The adjusted representation will become the equivalent of 31.4159×102 so that the square root will be31.4159×101.

If the integer part of the adjusted mantissa is taken, there can only be the values 1 to 99, and that could be used as an index into a table of 99 pre-computed square roots to complete the estimate. A computer using base sixteen would require a larger table, but one using base two would require only three entries: the possible bits of the integer part of the adjusted mantissa are 01 (the power being even so there was no shift, remembering that anormalised floating point number always has a non-zero high-order digit) or if the power was odd, 10 or 11, these being the firsttwo bits of the original mantissa. Thus, 6.25 = 110.01 in binary, normalised to 1.1001 × 22 an even power so the paired bits of the mantissa are 01, while .625 = 0.101 in binary normalises to 1.01 × 2−1 an odd power so the adjustment is to 10.1 × 2−2 and the paired bits are 10. Notice that the low order bit of the power is echoed in the high order bit of the pairwise mantissa. An even power has its low-order bit zero and the adjusted mantissa will start with 0, whereas for an odd power that bit is one and the adjusted mantissa will start with 1. Thus, when the power is halved, it is as if its low order bit is shifted out to become the first bit of the pairwise mantissa.

A table with only three entries could be enlarged by incorporating additional bits of the mantissa. However, with computers, rather than calculate an interpolation into a table, it is often better to find some simpler calculation giving equivalent results. Everything now depends on the exact details of the format of the representation, plus what operations are available to access and manipulate the parts of the number. For example,Fortran offers anEXPONENT(x) function to obtain the power. Effort expended in devising a good initial approximation is to be recouped by thereby avoiding the additional iterations of the refinement process that would have been needed for a poor approximation. Since these are few (one iteration requires a divide, an add, and a halving) the constraint is severe.

Many computers follow theIEEE (or sufficiently similar) representation, and a very rapid approximation to the square root can be obtained for starting Newton's method. The technique that follows is based on the fact that the floating point format (in base two) approximates the base-2 logarithm. That islog2(m×2p)=p+log2(m){\displaystyle \log _{2}(m\times 2^{p})=p+\log _{2}(m)}

So for a 32-bit single precision floating point number in IEEE format (where notably, the power has abias of 127 added for the represented form) you can get the approximate logarithm by interpreting its binary representation as a 32-bit integer, scaling it by223{\displaystyle 2^{-23}}, and removing a bias of 127, i.e.xint223127log2(x).{\displaystyle x_{\text{int}}\cdot 2^{-23}-127\approx \log _{2}(x).}

For example, 1.0 is represented by ahexadecimal number0x3F800000, which would represent1065353216=127×223{\displaystyle 1065353216=127\times 2^{23}} if taken as an integer. Using the formula above you get1065353216×223127=0{\displaystyle 1065353216\times 2^{-23}-127=0}, as expected fromlog2(1.0){\displaystyle \log _{2}(1.0)}. In a similar fashion you get 0.5 from 1.5 (0x3FC00000).

To get the square root, divide the logarithm by 2 and convert the value back. The following program demonstrates the idea. The exponent's lowest bit is intentionally allowed to propagate into the mantissa. One way to justify the steps in this program is to assumeb is the exponent bias andn is the number of explicitly stored bits in the mantissa and then show that((12(xint/2nb))+b)2n=12(xint2n)+(12(b+1))2n.{\displaystyle \left(\left({\tfrac {1}{2}}\left(x_{\text{int}}/2^{n}-b\right)\right)+b\right)\cdot 2^{n}={\tfrac {1}{2}}\left(x_{\text{int}}-2^{n}\right)+\left({\tfrac {1}{2}}\left(b+1\right)\right)\cdot 2^{n}.}

/* Assumes that float is in the IEEE 754 single precision floating point format */#include<stdint.h>unionFloatUInt{floatf;uint32_ti;}floatsqrtApprox(floatz){unionFloatUIntval={z};// Convert type, preserving bit pattern/* * To justify the following code, prove that * * ((((val.i / 2^m) - b) / 2) + b) * 2^m = ((val.i - 2^m) / 2) + ((b + 1) / 2) * 2^m) * * where * * b = exponent bias * m = number of mantissa bits */val.i-=1<<23;// Subtract 2^m.val.i>>=1;// Divide by 2.val.i+=1<<29;// Add ((b + 1) / 2) * 2^m.// Interpret again as floatreturnval.f;}

The three mathematical operations forming the core of the above function can be expressed in a single line. An additional adjustment can be added to reduce the maximum relative error. So, the three operations, not including the cast, can be rewritten as

val.i=(1<<29)+(val.i>>1)-(1<<22)+a;

wherea is a bias for adjusting the approximation errors. For example, witha = 0 the results are accurate for even powers of 2 (e.g. 1.0), but for other numbers the results will be slightly too big (e.g. 1.5 for 2.0 instead of 1.414... with 6% error). Witha = −0x4B0D2, the maximum relative error is minimized to ±3.5%.

If the approximation is to be used for an initial guess forNewton's method to the equation(1/x2)S=0{\displaystyle (1/x^{2})-S=0}, then the reciprocal form shown in the following section is preferred.

Reciprocal of the square root

[edit]
Main article:Fast inverse square root

A variant of the above routine is included below, which can be used to compute thereciprocal of the square root, i.e.,x1/2{\displaystyle x^{-1/2}} instead, was written by Greg Walsh. The integer-shift approximation produced a relative error of less than 4%, and the error dropped further to 0.15% with one iteration ofNewton's method on the following line.[20] In computer graphics it is a very efficient way to normalize a vector.

unionFloatInt{floatx;inti;};floatinv_sqrt(floatx){floatxhalf=0.5f*x;unionFloatIntu;u.x=x;u.i=0x5f375a86-(u.i>>1);// The next line can be repeated any number of times to increase accuracyu.x=u.x*(1.5f-xhalf*u.x*u.x);returnu.x;}

Some VLSI hardware implements inverse square root using a second degree polynomial estimation followed by aGoldschmidt iteration.[21]

Negative or complex square

[edit]

IfS < 0, then its principal square root isS=|S|i.{\displaystyle {\sqrt {S}}={\sqrt {\vert S\vert }}\,\,i\,.}

IfS = a+bi wherea andb are real andb ≠ 0, then its principal square root isS=|S|+a2+sgn(b)|S|a2i.{\displaystyle {\sqrt {S}}={\sqrt {\frac {\vert S\vert +a}{2}}}\,+\,\operatorname {sgn}(b){\sqrt {\frac {\vert S\vert -a}{2}}}\,\,i\,.}

This can be verified by squaring the root.[22][23]Here|S|=a2+b2{\displaystyle \vert S\vert ={\sqrt {a^{2}+b^{2}}}}

is themodulus ofS. The principal square root of acomplex number is defined to be the root with the non-negative real part.

See also

[edit]

Notes

[edit]
  1. ^The factors two and six are used because they approximate thegeometric means of the lowest and highest possible values with the given number of digits:110=1041.78{\displaystyle {\sqrt {{\sqrt {1}}\cdot {\sqrt {10}}}}={\sqrt[{4}]{10}}\approx 1.78\,} and10100=100045.62{\displaystyle {\sqrt {{\sqrt {10}}\cdot {\sqrt {100}}}}={\sqrt[{4}]{1000}}\approx 5.62\,}.
  2. ^The unrounded estimate has maximum absolute error of 2.65 at 100 and maximum relative error of 26.5% at y=1, 10 and 100
  3. ^If the number is exactly half way between two squares, like 30.5, guess the higher number which is 6 in this case
  4. ^This is incidentally the equation of the tangent line toy =x2 aty = 1.
  5. ^The default precision can range from 1 todecimal.MAX_PREC
  6. ^Seeabove.
  7. ^see:Continued fraction#Notations
  8. ^see:Periodic continued fraction

References

[edit]
  1. ^Jackson 2011.
  2. ^Fowler & Robson 1998.
  3. ^abHeath 1921.
  4. ^Baumann 2024.
  5. ^Johnson 2015.
  6. ^Nemiroff & Bonnell 1994.
  7. ^Nemiroff & Bonnell 1994a.
  8. ^Bailey & Borwein 2012.
  9. ^Simply Curious 2018.
  10. ^Herrero Piñeyro, P. J.; Linero Bas, A.; Massa Esteve, M. R.; Mellado Romero, A. (2023)."A problem on the approximation of n-roots based on the Viète's work"(PDF).MATerials MATemàtics.5:1–27. Retrieved15 November 2025. See p. 8.
  11. ^Guy 1985.
  12. ^Steinarson, Corbit & Hendry 2003.
  13. ^Wilkes, Wheeler & Gill 1951, pp. 146.
  14. ^Campbell-Kelly 2009.
  15. ^Gower 1958.
  16. ^Goldschmidt, Robert E. (1964).Applications of Division by Convergence(PDF) (Thesis). M.Sc. dissertation. M.I.T.OCLC 34136725.Archived(PDF) from the original on 10 December 2015. Retrieved15 September 2015.
  17. ^"Authors".IBM Journal of Research and Development.11:125–127. 1967.doi:10.1147/rd.111.0125. Archived fromthe original on 18 July 2018.
  18. ^Markstein 2004.
  19. ^Sardina 2007, p. 10, 2.3j.
  20. ^Lomont 2003.
  21. ^Piñeiro & Díaz Bruguera 2002.
  22. ^Abramowitz & Stegun 1970, p. 17, Section 3.7.26.
  23. ^Cooke 2008, p. 59.

Bibliography

[edit]

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Square_root_algorithms&oldid=1337416590"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp