Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://en.wikipedia.org/wiki/Monte_Carlo_method" rel="noreferrer">Monte Carlo method</a>, as mentioned, applies some great concepts but it is, clearly, not the fastest, not by a long shot, not by any reasonable measure. Also, it all depends on what kind of accuracy you are looking for. The fastest π I know of is the one with the digits hard coded. Looking at <a href="http://functions.wolfram.com/Constants/Pi/" rel="noreferrer" title="pi">Pi</a> and <a href="http://functions.wolfram.com/PDF/Pi.pdf" rel="noreferrer" title="pi formulas">Pi[PDF]</a>, there are a lot of formulae.</p> <p>Here is a method that converges quickly — about 14 digits per iteration. <a href="http://numbers.computation.free.fr/Constants/PiProgram/pifast.html" rel="noreferrer" title="PiFast">PiFast</a>, the current fastest application, uses this formula with the <a href="http://www.ele.uri.edu/~hansenj/projects/ele436/fft.pdf" rel="noreferrer">FFT</a>. I'll just write the formula, since the code is straightforward. This formula was almost found by <a href="http://numbers.computation.free.fr/Constants/Pi/piramanujan.html" rel="noreferrer">Ramanujan and discovered by Chudnovsky</a>. It is actually how he calculated several billion digits of the number — so it isn't a method to disregard. The formula will overflow quickly and, since we are dividing factorials, it would be advantageous then to delay such calculations to remove terms.</p> <p><img src="https://i.stack.imgur.com/aQMkk.gif" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/2y2l9.gif" alt="enter image description here"></p> <p>where,</p> <p><img src="https://i.stack.imgur.com/QqVnB.gif" alt="enter image description here"></p> <p>Below is the <a href="http://mathworld.wolfram.com/Brent-SalaminFormula.html" rel="noreferrer" title="Brent Salamin Formula">Brent–Salamin algorithm</a>. Wikipedia mentions that when <strong>a</strong> and <strong>b</strong> are "close enough" then <strong>(a + b)² / 4t</strong> will be an approximation of π. I'm not sure what "close enough" means, but from my tests, one iteration got 2 digits, two got 7, and three had 15, of course this is with doubles, so it might have an error based on its representation and the <em>true</em> calculation could be more accurate.</p> <pre><code>let pi_2 iters = let rec loop_ a b t p i = if i = 0 then a,b,t,p else let a_n = (a +. b) /. 2.0 and b_n = sqrt (a*.b) and p_n = 2.0 *. p in let t_n = t -. (p *. (a -. a_n) *. (a -. a_n)) in loop_ a_n b_n t_n p_n (i - 1) in let a,b,t,p = loop_ (1.0) (1.0 /. (sqrt 2.0)) (1.0/.4.0) (1.0) iters in (a +. b) *. (a +. b) /. (4.0 *. t) </code></pre> <p>Lastly, how about some pi golf (800 digits)? 160 characters!</p> <pre><code>int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload