Page 1 of 1

Pi (not the raspberry, but our irrational friend)

Posted: Wed Sep 12, 2012 4:51 am
by CommRLock78
This is particularly off topic, but anyway, I'm taking a computer science course as a co-requisite for the ordinary differential equations class that I'm also taking, and we had to write a couple of programs for MATLAB that approximated our irrational friend, pi (also known as the ratio Circumference/Diameter :mrgreen: ).

I thought this one we did was particularly cute, since it involves using random numbers (which is why I thought I'd share it ;) ).

(Incidentally, one does not need MATLAB to run this program, GNU Octave is an open source matrix lab for Linux, BSD, and Cygwin).

So the idea here is if you have a unit square centered at the origin, with a circle of r=1/2 inscribed within. If you were to take N number of darts and throw them at the circle/square, as N increases, the ratio #hits in circle/#hits in the square is approximately equal to the ratio Area of Circle/Area of Square. With an area of the circle pi/4 units^2 and the area of the square 1 unit^2, the ratio Area of Circle/Area of Square = pi/4. Solving for pi, we get pi=4*Area of Circle/Area of Square=4*#hits in circle/#hits in the square

Code: Select all

% Defining how many hits within the unit square total
N=1000;

% Defining x and y coordinates of the hits within the unit square
xr=rand(1,N)-0.5;
yr=rand(1,N)-0.5;

% Distance of each hit from the origin
dfo=((xr).^2+(yr).^2).^0.5;

% Determining the hit totals
hin=sum(dfo<0.5);    % hits within the circle
hout=sum(dfo>0.5);   % hits outside the circle - for fun

% Approximating Pi
pi_apprx=4*hin/N;

% Percent Error calculation
Err=100.*(abs(pi-pi_apprx)./pi);

A=[xr;yr;dfo]
pi_apprx
Err
Edit: I forgot to mention, if you get a chance to run the program, try messing around with N - it can go up to at least N=100*10^6 (which even on my i5 quad core takes about 3 seconds for MATLAB to compute, and then that wasn't even printing out matrix A, which I just added for fun to pair up the coordinates with their distance from the origin).

Re: Pi (not the raspberry, but our irrational friend)

Posted: Wed Sep 12, 2012 7:02 am
by JensAyton
A blog I follow used to use calculating π from quasi-random numbers as a sort of CAPTCHA, although that was based on prime number factorization.

And, of course, there’s always Buffon's needle.

Re: Pi (not the raspberry, but our irrational friend)

Posted: Wed Sep 12, 2012 7:28 pm
by CommRLock78
Ahruman wrote:
A blog I follow used to use calculating π from quasi-random numbers as a sort of CAPTCHA, although that was based on prime number factorization.
I almost like this even better than what I did in my assignment - and talk about nailing two birds with one stone :lol: . It really is amazing how often the number shows up, like the permeability of free space (which, as readers may know, is 4*pi*10^-7 J/(A^2*m)).
Ahruman wrote:
And, of course, there’s always Buffon's needle.
I not heard of this before, but after reading the article, it seems like a beautiful approach. A couple years ago I was playing around with trying to approximate pi using a rational number, but fortunately I don't recall where I scribbled it down (I do know that it had many more 3 digits in the numerator and denominator - not nearly as nice as what Lazzarini came up with 355/113, which is very nice indeed).

Re: Pi (not the raspberry, but our irrational friend)

Posted: Thu Sep 13, 2012 9:22 pm
by Gimbal Locke
If you want to calculate the 6000th digit of pi in decimal, you have to calculate all 5999 digits before it.

However, if you use binay, octal, hexadecimal or any other base which is a power of 2, the digits of pi can be calculated independently from the preceding digits.

From this I conclude that Randomus Factoria has 16 fingers.

Re: Pi (not the raspberry, but our irrational friend)

Posted: Sat Sep 15, 2012 7:00 pm
by CommRLock78
Gimbal Locke wrote:
If you want to calculate the 6000th digit of pi in decimal, you have to calculate all 5999 digits before it.

However, if you use binay, octal, hexadecimal or any other base which is a power of 2, the digits of pi can be calculated independently from the preceding digits.
I like the infinite series representations there. For fun, I used series expansions of sine and cosine to prove euler's formula last fall - quite an beautiful way to prove it, really.
Gimbal Locke wrote:
From this I conclude that Randomus Factoria has 16 fingers.
LOL, 16 fingers would make hexadecimal the more logical base system. (Base 10 really is silly when you consider 10 has only four factors, unlike the Sexagesimal system the Babylonians used with 12 factors).

Re: Pi (not the raspberry, but our irrational friend)

Posted: Thu Sep 20, 2012 4:44 am
by CommRLock78
Image

Some of you may have seen this, but it seems appropriate :mrgreen: .