Computing sqrt(2) by hand

(This is Yan Sheng.)

Today's blog post will be a change of pace from the usual contest math content: one day I was wondering to myself how many digits of 2\sqrt2 I could calculate by hand (who hasn't?). Math is more fun when you try stuff out for yourself, so here's some warm-up:

Exercise: Come up with a method of calculating the decimal expansion of 2\sqrt2 by hand. Then spend 5 or 10 minutes (or until you get bored) actually executing it. Can you get the right answer to 6 digits? 8? 10? How long would it take (and how painful would it be) to get 20 correct digits using your method?

Note: throughout this post, when I refer to "decimal places" or "digits" I always mean after the decimal point, eg. π3.125\pi\approx3.125 is an approximation with 1 correct digit.

History of computation records

Here's a (probably incomplete) table of past records for number of digits of 2\sqrt2 calculated, partly to add some space to avoid spoilers for those who are trying the above exercise, and partly because it turned out to be surprisingly hard to compile such a list.

Source Year Digits
YBC 7289 1800-1600 BCE 5
Baudhayana 800-500 BCE 5
Zhu Zaiyu 1584 24
J. M. Boorman 1887 315 (486 claimed)
R. Coustal 1950 1,032
H. S. Uhler 1950 1,544
K. Takahashi, M. Sibuya 1966 14,000
M. Lal 1967 19,600
M. Lal 1968 39,000
M. Lal, W. F. Lunnon 1968 100,000
J. Dutka 1971 1,000,000
R. Nemiroff, J. Bonnell 1994 10,000,000
Y. Kanada, D. Takahashi 1997 137,438,953,444
S. Kondo 2010 1,000,000,000,000
A. Yee 2012 2,000,000,000,050
R. Watkins 2016 2,000,000,000,100
R. Watkins 2016 5,000,000,000,000
R. Watkins 2016 10,000,000,000,000
T. Hanselmann 2022 10,000,000,001,000
J. Ranous 2023 20,000,000,000,000 (unverified)

Some notes on the above table:

  • Someone on Hacker News claims that Riemann computed 38 digits of 2\sqrt2 by hand, but I couldn't find any other source which corroborates this. If anyone wants to visit the Göttingen State and University Library to scan the relevant pages of Riemann's notes, please let me know.
  • The introduction to the Uhler paper is pretty funny:

    The present investigation was begun with the single intention of testing a value for 2\sqrt2 as computed by J. Marcus Boorman about 65 years ago. This datum was chosen because the author had acquired the impression that Boorman seldom if ever produced a reliable result in his calculations and it seemed fair to check this particular approximation since it was claimed that he had verified many of his digits.

    Spoiler alert: not all of Boorman's digits were correct.

  • Breaking news: the latest record was just announced yesterday (Nov 17, 2023)!

Back to calculation by hand: here are some of the things I tried, see how you compare!

Digit-by-digit

Let's start with some obvious facts: we have12=1<2<4=22,1^2=1<2<4=2^2,so 1<2<21<\sqrt2<2, ie. the digit of 2\sqrt2 before the decimal point is 11.

How should we find the next digit? Write 2=1+c/10\sqrt2=1+c/10, so we want to solve the equation(1+c10)2=2,\left(1+\frac c{10}\right)^2=2,which after expanding and clearing denominators gives(20+c)c=200102=100.(20+c)c=200-10^2=100.Now we check that 24×4<100<25×524\times4<100<25\times5, so 4<c<54<c<5, and thus we have 2=1.4\sqrt2=1.4\ldots.

We can repeat this process for the next digit: writing c=4+c/10c=4+c'/10, we want to solve(24+c10)(4+c10)=100,\left(24+\frac{c'}{10}\right)\left(4+\frac{c'}{10}\right)=100,or(280+c)c=100(10024×4)=400.(280+c')c'=100(100-24\times4)=400.Check that 281×1<400<282×2281\times1<400<282\times2, so 1<c<21<c'<2, and we have 2=1.41\sqrt2=1.41\ldots.

One more time, just to be sure: writing c=1+c/10c'=1+c''/10, we want to solve(281+c10)(1+c10)=400,\left(281+\frac{c''}{10}\right)\left(1+\frac{c''}{10}\right)=400,or(2820+c)c=100(400281×1)=11900.(2820+c'')c''=100(400-281\times1)=11900.Check that 2824×4<11900<2825×52824\times4<11900<2825\times5, so 4<c<54<c''<5, and we have 2=1.414\sqrt2=1.414\ldots.

Okay, so we can continue this process and just calculate the digits one by one. We don't want to write down a quadratic equation for every digit, so conventionally the above procedure is written in a way similar to long division, as follows: 104010402012000000000010000000000100000000009600000000(24×4)400000000281000000(281×1)119000000112960000(2824×4)60400005656400(28282×2)383600282841(282841×1)100759\begin{align*} 1\,\phantom{0}4\,\phantom{0}1\,\phantom{0}4\,\phantom{0}2\,\phantom{0}1\\ \sqrt{2\,00\,00\,00\,00\,00}\\ \underline{{}-1}\phantom{\,00\,00\,00\,00\,00}\\ 1\,00\phantom{\,00\,00\,00\,00}\\ \underline{{}-96}\phantom{\,00\,00\,00\,00}&&&(24\times4)\\ 4\,00\phantom{\,00\,00\,00}\\ \underline{{}-2\,81}\phantom{\,00\,00\,00}&&&(281\times1)\\ 1\,19\,00\phantom{\,00\,00}\\ \underline{{}-1\,12\,96}\phantom{\,00\,00}&&&(2824\times4)\\ 6\,04\,00\phantom{\,00}\\ \underline{{}-5\,65\,64}\phantom{\,00}&&&(28282\times2)\\ 38\,36\,00\\ \underline{{}-28\,28\,41}&&&(282841\times1)\\ 10\,07\,59 \end{align*}

Exercise: Figure out how the procedure we were performing with quadratic equations is equivalent to this "long division" method.

Exercise: Use this method to compute the square root of your favourite number between 1 and 100 (which is not a square) to 4 decimal places.

Of course, in principle this method gives us as many digits as we want, but in practice I get bored by the time the calculation gets as far as shown above. This suggests a rating system:

Digits Until I Got Bored: 5

Time to move on and brainstorm some other methods.

Heron's method

Suppose we have some approximation x02x_0\approx\sqrt2. If we had a procedure that uses x0x_0 to somehow generate a better approximation x1x_1, then by repeating same procedure we could get an even better approximation x2x_2, then x3x_3 and so on, giving us a sequence of numbers that converges to 2\sqrt2. Now the problem of calculating 2\sqrt2 to a given precision reduces to calculating a good enough approximation xnx_n.

Okay, so how do we generate a better approximation xx' from a given approximation xx? Actually, how do we generate any other approximation from xx? We probably need to think about some properties that characterise 2\sqrt2.

For example, we might consider the equation2=22.\sqrt2=\frac2{\sqrt2}.This suggests that if xx is a good approximation to 2\sqrt2, then so should 2x\dfrac2x. In fact, if we write x=2+εx=\sqrt2+\varepsilon, then we can expand the fraction 2x\dfrac2x as2x=2ε+ε22+ε.\frac2x=\sqrt2-\varepsilon+\frac{\varepsilon^2}{\sqrt2+\varepsilon}.This says that 2x\dfrac2x is nearly as good of an approximation as xx, but in the opposite direction: the error changes sign. Hence the average 12(x+2x)\dfrac12\left(x+\dfrac2x\right) should be an even better approximation of 2\sqrt2, with error on the order of ε2\varepsilon^2 instead of ε\varepsilon.

Thus we expect that the sequence defined byxn+1=12(xn+2xn)x_{n+1}=\dfrac12\left(x_n+\dfrac2{x_n}\right)should converge to 2\sqrt2, provided that the initial approximation x0x_0 is good enough. The convergence turns out to be pretty fast, since the error is roughly squared with each iteration (this is known as quadratic convergence); equivalently, the number of correct digits doubles at every step!

Exercise: Show that in fact xn2x_n\to\sqrt2 as long as x0>0x_0>0. What happens if x0<0x_0<0?

Exercise: Show that we also get the above recurrence by applying Newton's method to the function f(x)=x22f(x)=x^2-2.

The above procedure is known as Heron's method (yes, same guy as Heron's formula), or the Babylonian method (even though we don't know if the Babylonians knew about it).

Let's see this in action. We'll start with x0=1x_0=1, and calculate x1=12(1+21)=32=1.5000000x2=12(32+43)=1712=1.4166666x2=12(1712+2417)=577408=1.4142156\begin{align*} x_1=\frac12\left(1+\frac21\right)=\frac32&=1.5000000\ldots\\ x_2=\frac12\left(\frac32+\frac43\right)=\frac{17}{12}&=1.4166666\ldots\\ x_2=\frac12\left(\frac{17}{12}+\frac{24}{17}\right)=\frac{577}{408}&=1.4142156\ldots\\ \end{align*} Here x2x_2 gives 5 correct digits of 2\sqrt2.

At this point I realise a key flaw of this method: I don't actually want to divide large integers by hand, and the next term in this sequence (x3=665857470832x_3=\dfrac{665857}{470832}) already looks pretty bad to me, even though it probably has at least 10 correct digits. So maybe this is the end of the road, and thus

Digits Until I Got Bored: 5…

…unless?

Let's give the method one more shot, choosing a different starting value x0=1.4=75x_0=1.4=\dfrac75. Thenx1=12(75+107)=9970=1.4142857,x_1=\frac12\left(\frac75+\frac{10}7\right)=\frac{99}{70}=1.4142857\ldots,which is alright since dividing by 7 isn't too bad. Now the miracle is that dividing by 99 is also not too bad, so the next iteration is actually really easy: x1=9970=1.41428571422x1=14099=1.4141414141x2=1.4142135642\begin{align*} x_1=\frac{99}{70}&=1.4142857142\ldots\\ \frac2{x_1}=\frac{140}{99}&=1.4141414141\ldots\\ \hline x_2&=1.4142135642\ldots \end{align*} This approximation has 8 correct digits. But now our luck runs out, because x2=1980113860x_2=\dfrac{19801}{13860}, and I'm not really up to dividing by 19801. Final verdict:

Digits Until I Got Bored: 8

Exercise: Could we have come up with a recurrence without using division by xnx_n? Consider the identity2=1223.\sqrt2=\dfrac12{\sqrt2^3}.Hence if x2x\approx\sqrt2, then 12x32\dfrac12x^3\approx\sqrt2 as well. How good is the latter approximation compared to the former? (Note that here the two errors have the same sign.) Hence show that the sequencexn+1=32xn14xn3x_{n+1}=\frac32x_n-\frac14x_n^3converges quadratically to 2\sqrt2, if x0x_0 is close enough to 2\sqrt2. Which function is this the Newton's method iteration for?

Exercise: We can simplify the above recurrence even further for hand calculations. Let bn=12xn2b_n=\dfrac12x_n^2; show thatbn+1=bn(3bn2)2.b_{n+1}=b_n\left(\frac{3-b_n}2\right)^2.Writing Yn=3bn2Y_n=\dfrac{3-b_n}2, we have bn=b0Y02Y12Yn12b_n=b_0Y_0^2Y_1^2\cdots Y_{n-1}^2, and bn1b_n\to1. Hence deduce the relation between the product Y0Y1Y_0Y_1\cdots and 2\sqrt2. If Yn=1+εnY_n=1+\varepsilon_n, express εn+1\varepsilon_{n+1} as a function of εn\varepsilon_n. Now actually try to approximate 2\sqrt2 by calculating a few steps of this recurrence. (This method is known as Goldschmidt's algorithm.)

Rational approximations

If we want a good rational approximation ab2\dfrac ab\approx\sqrt2, one possible requirement is for ab2\lvert a-b\sqrt2\rvert to be small. How do we generate numbers of the form ab2a-b\sqrt2 (with a,ba,b nonzero integers) which are close to 0? Here's an idea: since 12<1\lvert1-\sqrt2\rvert<1, we could consider its powers(12)n=anbn2,(1-\sqrt2)^n=a_n-b_n\sqrt2,which converge to 0 exponentially fast. Then anbn\dfrac{a_n}{b_n} is a sequence of rational approximations converging to 2\sqrt2. The terms ana_n and bnb_n can be computed with the recurrence an+1bn+12=(12)(anbn2)=(an+2bn)(an+bn)2,\begin{align*} a_{n+1}-b_{n+1}\sqrt2&=(1-\sqrt2)(a_n-b_n\sqrt2)\\ &=(a_n+2b_n)-(a_n+b_n)\sqrt2, \end{align*} ie. an+1=an+2bn,bn+1=an+bn.\begin{align*} a_{n+1}&=a_n+2b_n,\\ b_{n+1}&=a_n+b_n. \end{align*} The first few values are given as follows: n012345an11371741bn01251229\begin{matrix} n&0&1&2&3&4&5\\ \hline a_n&1&1&3&7&17&41\\ b_n&0&1&2&5&12&29 \end{matrix}

How good is this approximation, ie. what is the error like? Note that the above recurrence remains unchanged when we replace 2-\sqrt2 with 2\sqrt2, so that we also have(1+2)n=an+bn2.(1+\sqrt2)^n=a_n+b_n\sqrt2.But (12)(1+2)=1(1-\sqrt2)(1+\sqrt2)=-1, so anbn2=1bnanbn2=1bn(an+bn2),\begin{align*} \left\lvert\frac{a_n}{b_n}-\sqrt2\right\rvert&=\frac1{b_n}\lvert a_n-b_n\sqrt2\rvert\\ &=\frac1{b_n(a_n+b_n\sqrt2)}, \end{align*} which is close to 122bn2\dfrac1{2\sqrt2b_n^2} for large nn.

Hey, here's an idea: why calculate all the powers of 121-\sqrt2, when I could get to higher powers with less calculation by repeated squaring? Let's try it out: (12)2=322(12)4=(322)2=17122(12)8=(17122)2=5774082(12)16=(5774082)2=6658574708322\begin{align*} (1-\sqrt2)^2&=3-2\sqrt2\\ (1-\sqrt2)^4=(3-2\sqrt2)^2&=17-12\sqrt2\\ (1-\sqrt2)^8=(17-12\sqrt2)^2&=577-408\sqrt2\\ (1-\sqrt2)^{16}=(577-408\sqrt2)^2&=665857-470832\sqrt2 \end{align*} Hang on, don't these numbers look kinda familiar?

Exercise: Show that squaring ab2a-b\sqrt2 is equivalent to performing one iteration of Heron's method on ab\dfrac ab.

There are several other approaches to getting good rational approximations of 2\sqrt2, which I'll leave as exercises:

Exercise: Notice that21=12+1=12+(21).\sqrt2-1=\frac1{\sqrt2+1}=\frac1{2+(\sqrt2-1)}.Show that the sequence1,1+12,1+12+12,1+12+12+12,1,\quad1+\frac12,\quad1+\frac1{2+\dfrac12},\quad1+\frac1{2+\dfrac1{2+\dfrac12}},\quad\ldotsconverges to 2\sqrt2. Simplify each of these continued fractions; what do you see?

Exercise: Another possible indication that a rational approximation ab2\dfrac ab\approx\sqrt2 is good is when a22b2\lvert a^2-2b^2\rvert is small. Clearly a22b2a^2-2b^2 cannot be 0, unless a=b=0a=b=0. Find some small integer solutions to a22b2=±1a^2-2b^2=\pm1; what do you see? Guess what the complete set of solutions to a22b2=±1a^2-2b^2=\pm1 are, then prove your conjecture. (In general, the Pell's equation a2Nb2=1a^2-Nb^2=1 is closely related to the continued fraction expansion of N\sqrt N.)

Power series

Maybe we should generalise the problem, from computing 2\sqrt2 to eg. 1+x\sqrt{1+x}, where xx is small enough so that 1 is a good initial guess. What's a good first order correction to the initial guess? If 1+x1+a1x,\sqrt{1+x}\approx1+a_1x, then we want1+x(1+a1x)2=1+2a1x+a1x2,1+x\approx(1+a_1x)^2=1+2a_1x+a_1x^2,so when xx is small our best choice is a1=12a_1=\dfrac12.

What's the second order correction? If 1+x1+12x+a2x2\sqrt{1+x}\approx1+\dfrac12x+a_2x^2, then we want 1+x(1+12x+a2x2)2=1+x+(2a2+14)x2+O(x3),\begin{align*} 1+x&\approx\left(1+\frac12x+a_2x^2\right)^2\\ &=1+x+\left(2a_2+\frac14\right)x^2+O(x^3), \end{align*} so we should choose a2=18a_2=-\dfrac18.

Of course, we could continue this procedure for higher order corrections, and obtain a power series expansion1+x=1+a1x+a2x2+a3x3+,\sqrt{1+x}=1+a_1x+a_2x^2+a_3x^3+\cdots,by equating coefficients on both sides of1+x=(1+a1x+a2x2+a3x3+)2,1+x=(1+a_1x+a_2x^2+a_3x^3+\cdots)^2,and solving for the ana_n recursively.

Fortunately, we don't actually have to do that calculation ourselves, because Newton's binomial series does the hard work for us: for all x<1\lvert x\rvert<1 and real α\alpha, we have (1+x)α=1+αx+α(α1)2!x2+α(α1)(α2)3!x3+.(1+x)^\alpha=1+\alpha x+\frac{\alpha(\alpha-1)}{2!}x^2+\frac{\alpha(\alpha-1)(\alpha-2)}{3!}x^3+\cdots.

Exercise: Derive the above series by computing the nn-th derivative of (1+x)α(1+x)^\alpha at x=0x=0, and using Taylor's theorem.

Substituting α=12\alpha=\dfrac12, we get1+x=1+12x18x2+116x35128x4+7256x5.\sqrt{1+x}=1+\frac12x-\frac18x^2+\frac1{16}x^3-\frac5{128}x^4+\frac7{256}x^5-\cdots.It is tempting at this point to just substitute x=1x=1 to get 2\sqrt2, but that's a bad idea: the series has radius of convergence of 1, so even though the series does converge at x=1x=1 (because it's alternating), it does so very slowly:

Exercise: Show that the nn-th coefficient of the above power series, namelyan=12(12)(32)(2n32)n!,a_n=\frac{\frac12(-\frac12)(-\frac32)\cdots(-\frac{2n-3}2)}{n!},satisfies14n3/2an12n3/2.\dfrac1{4n^{3/2}}\leq\lvert a_n\rvert\leq\dfrac1{2n^{3/2}}.(Hence we need roughly 102k/310^{2k/3} terms to approximate 2\sqrt2 to kk decimal places by using the series with x=1x=1.)

A much better alternative is to substitute x=12x=-\dfrac12 to get112=12=122,\sqrt{1-\frac12}=\sqrt{\frac12}=\frac12\sqrt2,so that the series converges geometrically, although it still takes roughly 3k3k terms to get kk decimal places of 2\sqrt2, and I don't think I'd be too happy to compute the decimal expansion of terms like a3kx3ka_{3k}x^{3k}.

Can we do better? Instead of using the series to calculate 2\sqrt2 directly, we would really want to calculate the square root of a number closer to 1, so that the series converges faster. Here's where the rational approximations of 2\sqrt2 that we found earlier can help us: for instance, since 232\sqrt2\approx\dfrac32, we could instead calculate232=89=119\frac23\sqrt2=\sqrt{\frac89}=\sqrt{1-\frac19}by substituting x=19x=-\dfrac19 into the series, and then multiply the sum by 32\dfrac32. Or we could calculate342=322=98=1+18,\frac34\sqrt2=\frac3{2\sqrt2}=\sqrt{\frac98}=\sqrt{1+\frac18},by substituting x=18x=\dfrac18 into the series, and then multiply the sum by 43\dfrac43. In either case, we now only need roughly kk terms to get kk decimal places of 2\sqrt2, which is still unpleasant, but at least it's 3 times less unpleasant than before.

So now we go through each rational approximation of 2\sqrt2 from before to write down similar identities, and see if any catches our eye. Eventually we find a miracle:7102=752=4950=1150,\frac7{10}\sqrt2=\frac7{5\sqrt2}=\sqrt{\frac{49}{50}}=\sqrt{1-\frac1{50}},so we should substitute x=150x=-\dfrac1{50} into the series. This is very good for us, since it is very easy to divide by powers of 50.

Let's actually do this. We'll rewrite the series a little, substituting x2xx\to-2x to get12x=1x12!x2133!x31354!x4+.\sqrt{1-2x}=1-x-\frac1{2!}x^2-\frac{1\cdot3}{3!}x^3-\frac{1\cdot3\cdot5}{4!}x^4+\cdots.We now want to take x=0.01x=0.01. It's easy to get each term in the series from the previous term by multiplying by 2n3nx\dfrac{2n-3}nx, so we compute: 17102=0.01+0.00005+0.0000005+0.00000000625+0.0000000000875+0.0000000000013125+0.0000000000000206+0.000000000000000417102=0.01005050633883357102=0.98994949366116652=01.414213562373095\begin{align*} 1-\tfrac7{10}\sqrt2&=0.01\\ &+\phantom{0.00}005\\ &+\phantom{0.0000}005\\ &+\phantom{0.000000}00625\\ &+\phantom{0.00000000}00875\\ &+\phantom{0.0000000000}013125\\ &+\phantom{0.000000000000}0206\\ &+\phantom{0.00000000000000}04\\ \hline 1-\tfrac7{10}\sqrt2&=0.0100505063388335\\ \tfrac7{10}\sqrt2&=0.9899494936611665\\ \sqrt2&=\phantom01.414213562373095 \end{align*} That's 15 digits of 2\sqrt2 while barely breaking a sweat. Wow!

Digits Until I Got Bored: 15++

Exercise: If you're thinking to yourself, "hmm, I bet Euler could do something like that," you'd be correct! In fact, in his 1755 textbook on differential calculus, he did it even better, and avoids the division by 7 that we had to do at the end of our calculation. By using the binomial series with α=12\alpha=-\dfrac12 and the identity572=(1150)1/2,\frac57\sqrt2=\left(1-\frac1{50}\right)^{-1/2},compute 2\sqrt2 to 12 (or 15, or 20) decimal places.

Exercise: Instead of using the binomial series, we could also custom-design our own power series expansion. For example, using the approximation 29970\sqrt2\approx\dfrac{99}{70}, we can write991402=1+19800.\frac{99}{140}\sqrt2=\sqrt{1+\frac1{9800}}.Find the first few terms of the power series 1+12x2+1+\frac12x^2+\cdots whose square is1+x212x=1+x2+2x3+4x4+8x5+1+\frac{x^2}{1-2x}=1+x^2+2x^3+4x^4+8x^5+\cdotsBy substituting the appropriate value for xx, compute 2\sqrt2 to 12 (or 15, or 20) decimal places. (If you don't know the nice trick for dividing by 99, you'll figure it out by the end of this exercise. Hint: group the digits into pairs.)

More fun things to try

  • Explain the pattern in the decimal expansion of12156=0.0101020514\frac12-\frac15\sqrt6=0.0101020514\ldotsHence compute 6\sqrt6 to 12 (or 15, or 20) decimal places.
  • (MIT Integration Bee 2023 Finals) Evaluate10202x9x2048x10+575dx.\left\lfloor10^{20}\int_2^\infty\frac{x^9}{x^{20}-48x^{10}+575}\,dx\right\rfloor.(Time limit: 4 mins)
  • Generalise the methods here to computing arbitrary square roots N\sqrt N.
    • In particular, there doesn't seem to be a great power series for 3\sqrt3, in the same way that we had miracles for 2\sqrt2 and 6\sqrt6. This comes from the fact that the expressions 2m2102n\lvert2m^2-10^{2n}\rvert and 6m2102n\lvert6m^2-10^{2n}\rvert can take surprisingly small values for m,nm,n positive integers. What is the smallest possible value of 3m2102n\lvert3m^2-10^{2n}\rvert? The second smallest?
  • Of course, "Digits Until I Got Bored" isn't a very scientific metric. Come up with a more objective measurement for the efficiency the different methods we've seen (ideas: number of 1-digit operations required for kk decimal places, or maybe just multiplications).
    • (hard) Generalise to a different activity that you enjoy: come up with a metric that quantifies the amount of fun you'll have, and optimise for that metric. Was that a good idea?
  • Suppose you're a handheld calculator (ie. suppose you're a programmer at Hewlett-Packard in the 1970s). What method would you implement for the square root function? Compare your proposal with CORDIC, the actual algorithm used for the first handheld calculators; why did HP use CORDIC instead of your proposal?
  • Suppose you're feeling very inspired by this blog post, and you want to start a project of computing 2\sqrt2 by hand to 50 or 100 decimal places. Which method would you use? How would you check for mistakes in your intermediate calculations?

Comments

Popular posts from this blog

SMO Open 2024 ??% Speedrun

Musings about the IMO