# finding Pi with C++

## Recommended Posts

I am working on trying to generate Pi with C++. How it should work is it gets the ratio of area in a circle to the area outside a circle within a square where the diameter of the circle is the same as the width of the square (it seems to be about 3.66:1).

I want to know if there is formula to find Pi using this.

```// I will foo your bar

#include &lt;iostream&gt;
using namespace std;
#include &lt;cmath&gt;
#include &lt;time.h&gt;

int main ()
{
cout &lt;&lt; "Setting variablesn";
int range = 32768,pointamount = 259499, z = 0;
cout &lt;&lt; "Done setting variables nSetting up float variablesn";
float distance, x[pointamount], y[pointamount], pie, incircle = 0, outcircle = 0;
cout &lt;&lt; "Done setting float variables n";

srand ( time(NULL) );
cout &lt;&lt; "Starting. n";
while(z &lt;= pointamount)
{
x[z] = rand() % range;
y[z] = rand() % range;
cout &lt;&lt; z &lt;&lt; "n";
z++;
}
cout &lt;&lt; "Done with generating x[z] and y[z]n";
z = 0;

while(z &lt;= pointamount)
{
//find distance
distance = sqrt( pow(x[z], 2) + pow(y[z], 2) );
if( distance &lt;= range)
{
incircle++;
}
else
{
outcircle++;
}
z++;
}

cout &lt;&lt; "Amount inside circle: " &lt;&lt; incircle &lt;&lt;"n";
cout &lt;&lt; "Amount outside circle: " &lt;&lt; outcircle &lt;&lt;"n";
cout &lt;&lt; "incircle / outcircle = " &lt;&lt; incircle / outcircle &lt;&lt;"n";
cout &lt;&lt; "inside + circle = " &lt;&lt; incircle + outcircle &lt;&lt;"n";
system("PAUSE");
return 0; //guess what
}```

##### Share on other sites
I am working on trying to generate Pi with C++. How it should work is it gets the ratio of area in a circle to the area outside a circle within a square where the diameter of the circle is the same as the width of the square (it seems to be about 3.66:1).

I want to know if there is formula to find Pi using this.

```// I will foo your bar

#include &lt;iostream&gt;
using namespace std;
#include &lt;cmath&gt;
#include &lt;time.h&gt;

int main ()
{
cout &lt;&lt; "Setting variablesn";
int range = 32768,pointamount = 259499, z = 0;
cout &lt;&lt; "Done setting variables nSetting up float variablesn";
float distance, x[pointamount], y[pointamount], pie, incircle = 0, outcircle = 0;
cout &lt;&lt; "Done setting float variables n";

srand ( time(NULL) );
cout &lt;&lt; "Starting. n";
while(z &lt;= pointamount)
{
x[z] = rand() % range;
y[z] = rand() % range;
cout &lt;&lt; z &lt;&lt; "n";
z++;
}
cout &lt;&lt; "Done with generating x[z] and y[z]n";
z = 0;

while(z &lt;= pointamount)
{
//find distance
distance = sqrt( pow(x[z], 2) + pow(y[z], 2) );
if( distance &lt;= range)
{
incircle++;
}
else
{
outcircle++;
}
z++;
}

cout &lt;&lt; "Amount inside circle: " &lt;&lt; incircle &lt;&lt;"n";
cout &lt;&lt; "Amount outside circle: " &lt;&lt; outcircle &lt;&lt;"n";
cout &lt;&lt; "incircle / outcircle = " &lt;&lt; incircle / outcircle &lt;&lt;"n";
cout &lt;&lt; "inside + circle = " &lt;&lt; incircle + outcircle &lt;&lt;"n";
system("PAUSE");
return 0; //guess what
}```

Trying to decipher this. Do you mean you wish to know the forumla for Pi?  ... C/D      Circumference over diameter. You could just find a predefined circle circumference and diameter but hey :D

##### Share on other sites
Trying to decipher this. Do you mean you wish to know the forumla for Pi?  ... C/D      Circumference over diameter. You could just find a predefined circle circumference and diameter but hey :D

Yes but to do that I would need 2 constants pulled out of my ass. What I am trying to trying to figure out how to figure out what Pi is.

Over the weekend my dad helped me do this (and while doing it decided to rewrite all my code). It now gives out the numbers every 100000 points. It now evens out to about  3.141522, I think this is due to the to the fact that rand() is not that random or some how the numbers are wrong.

```// I will foo your bar

#include &lt;iostream&gt;
using namespace std;
#include &lt;cmath&gt;
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;

#define range   32768

int main ()
{
double count = 0;
double inside = 0;
int q=0;

srand ( time(NULL) );
cout &lt;&lt; "Starting. n";
while(1)
{
int x = rand() % range;
int y = rand() % range;
inside += (x*x + y*y &lt; (range-1)*(range-1));
count++;
q++;
if(q&gt;=100000)
{
printf("inside=%.0f count=%.0f ratio=%18.16fn",inside, count,inside*4/count);
q=0;
}
}

return 0;
}```

##### Share on other sites

Trying to decipher this. Do you mean you wish to know the forumla for Pi?  ... C/D       Circumference over diameter. You could just find a predefined circle circumference and diameter but hey :D

Yes but to do that I would need 2 constants pulled out of my ass. What I am trying to trying to figure out how to figure out what Pi is.

Over the weekend my dad helped me do this (and while doing it decided to rewrite all my code). It now gives out the numbers every 100000 points. It now evens out to about  3.141522, I think this is due to the to the fact that rand() is not that random or some how the numbers are wrong.

```// I will foo your bar

#include &lt;iostream&gt;
using namespace std;
#include &lt;cmath&gt;
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;

#define range   32768

int main ()
{
double count = 0;
double inside = 0;
int q=0;

srand ( time(NULL) );
cout &lt;&lt; "Starting. n";
while(1)
{
int x = rand() % range;
int y = rand() % range;
inside += (x*x + y*y &lt; (range-1)*(range-1));
count++;
q++;
if(q&gt;=100000)
{
printf("inside=%.0f count=%.0f ratio=%18.16fn",inside, count,inside*4/count);
q=0;
}
}

return 0;
}```

There seems to be a problem, because the first few numbers of pi are:

3.141592654

##### Share on other sites
• 3 weeks later...

Just a couple things to caveat:

1) Is there a reason why you are trying to compute Pi? Why not use M_Pi as defined in cmath?

2) Depending on your printing routine, cout only displays for the first six digits beyond the decimal. You may want to look in to setting the precision.

~hexlax

##### Share on other sites
• 2 weeks later...

Also, using pseudo random numbers (your computer most probably can't produce real randomness) to calculate Pi is not advisable. If you want to pretend that your method is sort of scientific, you should find a way to produce two numbers a and b and show that a < Pi < b so that you get an idea how large your variance is.

edit:

Out of boredom i've written a small Perl program. It should be easy enough to understand, even if you don't know Perl. It runs at descent speed up to an argument of 1000000.

```#!/usr/bin/perl

use strict;
use warnings;

my \$r  = \$ARGV;
my \$kt = 0;
my \$qt = \$r ** 2;

for my \$y ( 0 .. \$r ) {
\$kt += sqrt( \$r**2 - \$y**2 );
}

print 4 * ( \$kt - sqrt( \$r ** 2 ) ) / \$qt;
print " &lt; Pi &lt; ";
print 4 * \$kt / \$qt;
print "\n";```

If there is someone here with a mathematical degree, i'd like to know if my method is mathematically sound. If anyone is interested in the method but can't understand how the code works, i'll try to illustrate it with some pictures, just ask.

##### Share on other sites
If there is someone here with a mathematical degree, i'd like to know if my method is mathematically sound. If anyone is interested in the method but can't understand how the code works, i'll try to illustrate it with some pictures, just ask.

Don't have a math degree, but i've taught my self a large amount. But to awnser, yes, your method is mathematically sound, but very ineficent from a performance perspective (but for understanding pi, its a nice starter )

ohh hi, stumbled onto this site, it looks nice.

##### Share on other sites

I'm sure there are far more efficient methods. This program was mainly born out of curiosity. I wanted to see if i could come up with a method to calculate pi on my own.

I'll try and learn some more advanced mathematics this summer, mainly because i want to get a deeper understanding on cryptography and stenography. But maybe I'll revisit this problem in the future.

Btw. if you know where to find more efficient methods which can be fully understood with high school level math skills, please point me to them.

##### Share on other sites
• 2 weeks later...

what point of highshool level math algebera 2 & gemoetry, or entering pre calc? and also, do you want to be able to udnerstand why they work or to be able to make / code one and understand what it is doing? these are two separate things

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×