Pi Day MMXVIII

Today it is the fourteenth of March 2018. Today’s date — when written in the M/D/Y format –, 3/14/18, looks close enough to Archimedes’ constant’s decimal representation for it to be the constant’s celebratory day.
As always on Pi Day, I have implemented an algorithm to generate \pi, albeit this year’s accuracy is not the greatest (Try it online).

                        typedef double d;typedef long l;l f(l n          
                   ){l f=1;while(n>1)f*=n--;return f;}d ne(d v,          
                 l p){d r=1;for(l k=0;k<p;k++)r*=v;return r;}d           
                ps(d(*c)(l),l i,d x){d s=0;for(l k=0;k<i;k++)s           
               +=c(k)*       ne(x,        k);return                      
              s;}           d exc         (     l                        
             n){            return       1./f (n)                        
                           ; } d         exp(d x                         
                          )   {         return                           
                         ps(exc        ,20,x);}                          
                        d G( d         x){return                         
                        exp(-x        *x);}d I                           
                       (d a,d         b,d g,d                            
                     (* f)(d         )){d cs=                            
                    0;for( d         x=a;x<=                             
                   b;x +=g)         cs+=f(x)                             
                 *g;return          cs ;  }          int                 
               main( ) { d          pi_root         =I(                  
              -2.5, 2.5 ,           1e-4,G);      d pi                   
             = pi_root *            pi_root+(0xf&0xf0                    
             ) ; printf(             "%c%c%c%c%c%f%c"                    
             ,'p','i',                ' ','=',' ',pi                     
               ,'\n'                     ) ; }                           

I use various methods of generating \pi throughout the Pi Days; this time I chose to use an improper integral paired with a power series. \pi is calculated using a famous identity involving infinite continuous sums, roots, e, statistics and — of course — \pi.

\int\limits_{-\infty}^\infty e^{-x^2}\mathrm{d}x = \sqrt{\pi}

Furthermore, to compute e, the following identity is used.

\exp{x} = \sum\limits_{n=0}^\infty\frac{x^n}{n!}

Both formulae are combined, the approximated value of \sqrt{\pi} is squared and \pi is printed to stdout.

You can download this program’s prettified (some call it obfuscated, see above) source code pi.c and also the (nearly, as #include is not missing so that the compiler does not need to guess my dependencies) equivalent code in a more traditional source layout tpi.c.

Happy Pi Day!

Advertisements

Pi Day MMXVII

Every year on March the 14th, for one day the world gets irrationally excited about the famous constant \pi. As is tradition, you try to calculate \pi in unusual ways, demonstrating the constant’s ubiquity as it crops up in the most unexpected circumstances.

                        lnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn       
                    nJ$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$w       
                 `v$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$        
                n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$z        
               u$$mnn:       Y$$$i        .@$$$$$,                    
              $$n           )$$$*         W$$$$$m                     
             -n[            $$$$.        ]$$$$$$                      
                           h$$$w         $$$$$$Y                      
                          [$$$$         X$$$$$$                       
                         "$$$$n        '$$$$$${                       
                        .$$$$$         8$$$$$$                        
                        *$$$$}        :$$$$$$+                        
                       #$$$$u         $$$$$$%                         
                     t$$$$$$         ;$$$$$$`                         
                    u$$$$$$!         $$$$$$W                          
                   Y$$$$$$M         .$$$$$$,                          
                 f$$$$$$$$.         Z$$$$$Z          nn               
               `w$$$$$$$$|          $$$$$$(         v$z               
              n$$$$$$$$$W           $$$$$$$1      'X$8                
             Y$$$$$$$$$$            *$$$$$$$8nnnn$$$p                 
             $$$$$$$$$@.             W$$$$$$$$$$$$$n                  
             _$$$$$$${                x$$$$$$$$$0>                    
                -n{.                     !|nt_.                       

A fairly well-known way to approximate pi is to randomly choose points in a square (often thought of as throwing darts at a square piece of cardboard), determine their distance to a circle’s center and do a division, as I did in my π Generator post.

However, \pi does not only appear in the formula for a circle’s area, A=\pi \cdot r^2, yet also in the formula for a sphere’s volume, V=\frac{4}{3} \cdot \pi \cdot r^3, and for all the infinite hyperspheres above dimension three (view this Wikipedia article for more about volumes of higher-dimensional spheres).

In particular, the formula for the hypervolume of a hypersphere in four dimensions is defined as being V=\frac{\pi^2}{2} \cdot r^4. Using this formula, my Python script randomly chooses four-dimensional points (each in the interval \left[0, 1\right)), calculates their distance to the point \left(0.5, 0.5, 0.5, 0.5\right) and determines if they are in the hypersphere around that point with radius 0.5.
By dividing the number of random points which lie in the hypersphere by the number of iterations used (10^6 in the example below), the script approximates the hypersphere’s hypervolume. By then rearranging the equation V=\frac{\pi^2}{2} \cdot r^4 with r=0.5 to \pi=\sqrt{V\cdot 32}, the desired constant can be approximated.

$ python pi.py
3.14196371717

# Python 2.7.7 Code
# Jonathan Frech, 13th of March 2017

Continue reading