Jump to content

Recommended Posts

Posted

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 <iostream>
using namespace std;
#include <cmath>
#include <time.h>


int main ()
{
    cout << "Setting variablesn";
    int range = 32768,pointamount = 259499, z = 0;
    cout << "Done setting variables nSetting up float variablesn";
    float distance, x[pointamount], y[pointamount], pie, incircle = 0, outcircle = 0;
    cout << "Done setting float variables n";
    
    srand ( time(NULL) );
    cout << "Starting. n";
    while(z <= pointamount) 
    {
            x[z] = rand() % range;
            y[z] = rand() % range;
            cout << z << "n";
            z++;
    }
    cout << "Done with generating x[z] and y[z]n";
    z = 0;
    
    while(z <= pointamount)
    {
            //find distance
            distance = sqrt( pow(x[z], 2) + pow(y[z], 2) );
            if( distance <= range)
            {
                incircle++;
            } 
            else
            { 
                outcircle++;
            }
            z++;
    } 
    
    cout << "Amount inside circle: " << incircle <<"n";
    cout << "Amount outside circle: " << outcircle <<"n";
    cout << "incircle / outcircle = " << incircle / outcircle <<"n";
    cout << "inside + circle = " << incircle + outcircle <<"n";
    system("PAUSE");
    return 0; //guess what
}

Posted
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 <iostream>
using namespace std;
#include <cmath>
#include <time.h>


int main ()
{
    cout << "Setting variablesn";
    int range = 32768,pointamount = 259499, z = 0;
    cout << "Done setting variables nSetting up float variablesn";
    float distance, x[pointamount], y[pointamount], pie, incircle = 0, outcircle = 0;
    cout << "Done setting float variables n";
    
    srand ( time(NULL) );
    cout << "Starting. n";
    while(z <= pointamount) 
    {
            x[z] = rand() % range;
            y[z] = rand() % range;
            cout << z << "n";
            z++;
    }
    cout << "Done with generating x[z] and y[z]n";
    z = 0;
    
    while(z <= pointamount)
    {
            //find distance
            distance = sqrt( pow(x[z], 2) + pow(y[z], 2) );
            if( distance <= range)
            {
                incircle++;
            } 
            else
            { 
                outcircle++;
            }
            z++;
    } 
    
    cout << "Amount inside circle: " << incircle <<"n";
    cout << "Amount outside circle: " << outcircle <<"n";
    cout << "incircle / outcircle = " << incircle / outcircle <<"n";
    cout << "inside + circle = " << incircle + outcircle <<"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

Posted
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 <iostream>
using namespace std;
#include <cmath>
#include <time.h>
#include <stdio.h>

#define range   32768

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

    return 0;
}

Posted

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 <iostream>
using namespace std;
#include <cmath>
#include <time.h>
#include <stdio.h>

#define range   32768

int main ()
{
    double count = 0;
    double inside = 0;
    int q=0;
    
    srand ( time(NULL) );
    cout << "Starting. n";
    while(1) 
    {
        int x = rand() % range;
        int y = rand() % range;
        inside += (x*x + y*y < (range-1)*(range-1));
        count++;
        q++;
        if(q>=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

  • 3 weeks later...
Posted

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

  • 2 weeks later...
Posted

As it is often the case nowadays, Wikipedia holds the answer.

http://en.wikipedia.org/wiki/Computing_%CF%80

http://en.wikipedia.org/wiki/Leibniz_formula_for_pi

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[0];
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.

Posted
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 :lol: )

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

Posted

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.

  • 2 weeks later...
Posted

what point of highshool level math :blink: 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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...