From: Daniel Moore Date: 2010-04-23T06:17:28+09:00 Subject: [QUIZ][SUMMARY] Digits of e (#226) --001e680f0ac826af8e0484d9d6f7 Content-Type: multipart/alternative; boundary=001e680f0ac826af7b0484d9d6f5 --001e680f0ac826af7b0484d9d6f5 Content-Type: text/plain; charset=ISO-8859-7 Content-Transfer-Encoding: quoted-printable This quiz harkens back to another mathematically themed computation competition Digits of Pi (#202)[1]. Many of the same techniques that can be applied to the computation of pi can be applied to the computation of e, an= d there are some surprising results. Jack Rouse used the fast converging continued fraction series from Wikipedia[2] to create a short program that quickly calculates e to 100_000 digits. Jack's solution is quite quick, yielding the output within several seconds. Jean-Julien Fleck started with a benchmark of the the standard library call and estimated that it would take 10**5 seconds to compute the first 100_000 digits. Jean-Julien provides a handy one liner that can be used in irb: fact =3D 1 ; (1..34_000).inject(1) {|sum,i| fact *=3D i ; sum + Rational(1,fact)} This computation makes use of the Taylor series definition. The results use Rational, so can be quite slow, it is advised to start with a smaller numbe= r of iterations, possibly 500 or so. Thorsten Hater created a program based on the spigot algorithm from Rabonit= z and Wagon[3]. From the paper: > This algorithm is a "spigot" algorithm: it pumps out digits one > at a time and does not use the digits after they are computed [...]; > the entire algorithm uses only ordinary integer arithmetic on > relatively small integers. The paper is actually about computing the digits of pi with the same method= , but in doing so covers the case of e, which is simpler. An interesting approach, and worth looking at if you are into mathematics. Benoit Daloze explored eight different methods of computing e with a focus on breadth rather than depth. See them all in the attached solutions supplement. Benoit also came across this interesting bit of information: lim(n->inf) ((2n+1)/(2n-1))**n =3D> e lim(n->0) ((2n+1)/(2n-1))**n =3D> =F0 Jay Anderson and and David Springer used the Taylor series definition of e, which is quite fast at converging and delivering 100_000 digits. These solutions are much faster that other Taylor series implementations because they use only integer arithmatic and a very simple loop. Here is Jay Anderson's solution: digits =3D 100000 fudge =3D 10 unity =3D 10**(digits + fudge) e =3D unity n =3D unity i =3D 0 while (n>0) i +=3D 1 n /=3D i e +=3D n end e /=3D 10**fudge p e Setting unity to be 10^digits shifts everything into the integers so that floating point math won't be required. The extra fudge factor ensures that there aren't rounding errors near the end. Each step of the loop requires only an increment, an integer division, and an addition. Notice how the division in the loop accumulates the factorial, because the result of 1/2 i= s stored when it is divided by 3 it is set equal to 1/3!. The loop terminates when the series term becomes 0, i.e. too small to add anything that will matter within the chosen number of digits. Thanks everyone for your solutions to the quiz! [Digits of e (#226) - Solutions][4] P.S. Brian Candler linked to an xkcd comic involving e and =F0[5], not all contributions need to be code. [1] http://rubyquiz.strd6.com/quizzes/202-digits-of-pi [2] http://en.wikipedia.org/wiki/E_(mathematical_constant) [3] http://www.mathpropress.com/stan/bibliography/spigot.pdf [4] http://rubyquiz.strd6.com/quizzes/226.tar.gz [5] http://xkcd.com/217/ --001e680f0ac826af7b0484d9d6f5 Content-Type: text/html; charset=ISO-8859-7 Content-Transfer-Encoding: quoted-printable
This quiz harkens back to another mathematically themed= computation competition Digits of Pi (#202)[1]. Many of the same technique= s that can be applied to the computation of pi can be applied to the comput= ation of e, and there are some surprising results.

Jack Rouse used the fast converging continued fraction = series from Wikipedia[2] to create a short program that quickly calculates = e to 100_000 digits. Jack's solution is quite quick, yielding the outpu= t within several seconds.

Jean-Julien Fleck started with a benchmark of the the s= tandard library call and estimated that it would take 10**5 seconds to comp= ute the first 100_000 digits. Jean-Julien provides a handy one liner that c= an be used in irb:

    fact =3D 1 ; (1..34_000).inject(1) {= |sum,i| fact *=3D i ; sum + Rational(1,fact)}

This= computation makes use of the Taylor series definition. The results use Rat= ional, so can be quite slow, it is advised to start with a smaller number o= f iterations, possibly 500 or so.

Thorsten Hater created a program based on the spigot al= gorithm from Rabonitz and Wagon[3]. From the paper:

    > This algorithm is a “spigot” algorith= m: it pumps out digits one
    > at a time and does not use the digits after th= ey are computed [...];
    > the entire algorit= hm uses only ordinary integer arithmetic on 
   &n= bsp;> relatively small integers.

The paper is actually about computing the digits of pi with = the same method, but in doing so covers the case of e, which is simpler. An= interesting approach, and worth looking at if you are into mathematics.

Benoit Daloze explored eight different methods of compu= ting e with a focus on breadth rather than depth. See them all in the attac= hed solutions supplement. Benoit also came across this interesting bit of i= nformation:

    lim(n->inf) ((2n+1)/(2n-1))**n &n= bsp;  =3D> e
    lim(n->0)   ((2n+= 1)/(2n-1))**n    =3D> =F0

Jay Anderso= n and and David Springer used the Taylor series definition of e, which is q= uite fast at converging and delivering 100_000 digits. These solutions are = much faster that other Taylor series implementations because they use only = integer arithmatic and a very simple loop.

Here is Jay Anderson's solution:

    digits =3D 100000
    fu= dge =3D 10
    unity =3D 10**(digits + fudge)

    e =3D unity
   =  n =3D unity
    i =3D 0

    = ;while (n>0)
      i +=3D 1
&nbs= p;     n /=3D i
      e +=3D n=
    end

   &nb= sp;e /=3D 10**fudge
    p e

Setting unity to be 10^digits shifts everything into the integer= s so that floating point math won't be required. The extra fudge factor= ensures that there aren't rounding errors near the end. Each step of t= he loop requires only an increment, an integer division, and an addition. N= otice how the division in the loop accumulates the factorial, because the r= esult of 1/2 is stored when it is divided by 3 it is set equal to 1/3!. The= loop terminates when the series term becomes 0, i.e. too small to add anyt= hing that will matter within the chosen number of digits.

Thanks everyone for your solutions to the quiz!

[Digits of e (#226) - Solutions][4]

<= div>P.S. Brian Candler linked to an xkcd comic involving e and =F0[5], not = all contributions need to be code.

[1] http://rubyquiz.strd6.com/quizzes/202-digits-of-pi
[2] ht= tp://en.wikipedia.org/wiki/E_(mathematical_constant)
[3] http://www.mathpropress.com/stan/bibliography/spigot.pdf
[= 4] http://rubyquiz= .strd6.com/quizzes/226.tar.gz
[5] http://xkcd.com/217/
--001e680f0ac826af7b0484d9d6f5-- --001e680f0ac826af8e0484d9d6f7 Content-Type: application/x-gzip; name="226.tar.gz" Content-Disposition: attachment; filename="226.tar.gz" Content-Transfer-Encoding: base64 X-Attachment-Id: f_g8c2jrwj0 H4sIAMO70EsAA+1c63LbSHb2X+EpWtZuCeAFJMCLHNvylnfGs5fanZoau7I/WIwKJJokLBCAcbEk W0rNz32GVJI/eZ/sO8yT5DunGyBp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