Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Approximation of pi
Index -> Programming, C++ -> C++ Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
PaulButler




PostPosted: Tue Jul 10, 2007 11:07 am   Post subject: Approximation of pi

I was playing around in C++ and decided to write a program to approximate pi using a Riemann sum for the area of a semicircle (it's easier than it sounds; a simple explanation is below the code)

c++:

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

double pi_approx(int accuracy){
        double area = 0.;
        double h = (1. / accuracy);
       
        for(int x = 1; x <= accuracy; x++){
                double i = (h * x) - (h / 2.);
                double x = sqrt(1 - i * i);
                area += h * x;
        }
       
        return 4 * area;
}

int main () {
        cout << "Level of accuracy (any positive integer greater than 10): ";
        int i;
        cin >> i;
        assert(i >= 10);
        double pi = pi_approx(i);
        cout << setprecision(20);
        cout << pi;
        return 0;
}


Here is how it works:

We know the area of a circle is pi * r^2. The area of a unit circle is therefore pi. If we draw a square around the unit circle (-1, -1) to (1, 1), the square has an area of 4. The fraction of the area of the square that is covered by the circle is pi / 4.

Since the square and circle are both symmetrical both vertically and horizontally, we can simplify things by only taking the section of both that is in the first quadrant. The area of the semicircle in the first quadrant over the area of the square in the first quadrant is still pi / 4. Since the area of the square is 1 * 1 = 1, we can simplify this to the area of the semicircle = pi / 4. Therefore, pi = the area of the semicircle * 4.

Now all we need to do is find the area of the semicircle without using pi. I used a Riemann sum for this. I found a number of points (the number was determined by the accuracy required) along the y-axis and used the Pythagorean theorem to find the width of the bar at that point. The height of the bar is 1 over the number of points. For each point I multiplied the height by the width and added this to the total area of the semicircle. Then I just multiply the area by 4 to get the approximation of pi.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Tue Jul 10, 2007 11:08 pm   Post subject: RE:Approximation of pi

Good stuff. I wonder if this solution is more efficient than the solution where you take that square you drew and make it into a pentagon, then into a hexagon, etc. Anyone know off-hand?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: