Pi Day – approximation in R

To celebrate pi day with my school I created a function to compute an approximation for pi.  It seems R has a maximum digit preference of 16 digits by default.  Here is my code:

piestimate<-function(r,s){

inscribed.p<-s*sin(2*pi/s)/sin((pi-(2*pi)/s)/2)/2
circumscribed.p<-s*sin(pi/s)/sin(pi/2-(pi/s))
(inscribed.p+circumscribed.p)/2->estimate
return(format(estimate,digits=20))}

I used the format command to control how many digits were produced by the approximation, but the maximum output seems to be 16.  I used the following command to run the program and find the first 16 correct digits of pi.
piestimate(1,99999999)

This computation took miliseconds to compute in R.  It took mathematicians without computer capabilities years to compute this approximation accurately.  I wonder if there is some way to make R compute this more precisely?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s