Pi (not the raspberry, but our irrational friend)

Off topic discussion zone.

Moderators: winston, another_commander, Cody

Post Reply
User avatar
CommRLock78
---- E L I T E ----
---- E L I T E ----
Posts: 1138
Joined: Sat Apr 07, 2012 7:35 pm
Location: US
Contact:

Pi (not the raspberry, but our irrational friend)

Post 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).
"I'll laser the mark all while munching a fistful of popcorn." - Markgräf von Ededleen, Marquess, Brutal Great One, Assassins' Guild Exterminator
---------------------------
At the helm of the Caduceus Omega, 'Murderous Morrígan'
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

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

Post 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.
User avatar
CommRLock78
---- E L I T E ----
---- E L I T E ----
Posts: 1138
Joined: Sat Apr 07, 2012 7:35 pm
Location: US
Contact:

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

Post 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).
"I'll laser the mark all while munching a fistful of popcorn." - Markgräf von Ededleen, Marquess, Brutal Great One, Assassins' Guild Exterminator
---------------------------
At the helm of the Caduceus Omega, 'Murderous Morrígan'
User avatar
Gimbal Locke
---- E L I T E ----
---- E L I T E ----
Posts: 293
Joined: Sun Jan 08, 2012 11:32 pm
Location: Brussels
Contact:

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

Post 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.
User avatar
CommRLock78
---- E L I T E ----
---- E L I T E ----
Posts: 1138
Joined: Sat Apr 07, 2012 7:35 pm
Location: US
Contact:

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

Post 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).
"I'll laser the mark all while munching a fistful of popcorn." - Markgräf von Ededleen, Marquess, Brutal Great One, Assassins' Guild Exterminator
---------------------------
At the helm of the Caduceus Omega, 'Murderous Morrígan'
User avatar
CommRLock78
---- E L I T E ----
---- E L I T E ----
Posts: 1138
Joined: Sat Apr 07, 2012 7:35 pm
Location: US
Contact:

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

Post by CommRLock78 »

Image

Some of you may have seen this, but it seems appropriate :mrgreen: .
"I'll laser the mark all while munching a fistful of popcorn." - Markgräf von Ededleen, Marquess, Brutal Great One, Assassins' Guild Exterminator
---------------------------
At the helm of the Caduceus Omega, 'Murderous Morrígan'
Post Reply