Skip to content

Instantly share code, notes, and snippets.

@barisere
Created March 12, 2017 20:44
Show Gist options
  • Save barisere/d706c66e908ff114a323010cf1b234ea to your computer and use it in GitHub Desktop.
Save barisere/d706c66e908ff114a323010cf1b234ea to your computer and use it in GitHub Desktop.
The Pi Day Challenge for the Most Descriptive Code in C++
/*Pi Day Challenge: compute an estimate of pi by observing the
*probability
*that randomly generated points within a square fall inside of a circle
*inscribed in the square, given that the square's length is the
*diameter of the circle.
*
*Solution by Jonathan Barisere (jthankgod@ymail.com)
*/
#include <iostream>
#include <iomanip>
#include "pi_day.h++"
int main() {
srand(time(0)); // seed the rand function with a random value
std::pair<int, int> origin; // (x, y) origin in 2D cartesian plane
std::pair<float, float> midpoint; /* midpoint of the circle coincides
with that of the square */
std::cout << "Input x-origin: ";
std::cin >> origin.first;
std::cout << "Input y-origin: ";
std::cin >> origin.second;
for(int n = 0; n < 10; n++) {
int diameter;
float radius;
diameter = pow(10, n);
radius = diameter / 2;
int limit_x = diameter + origin.first; // max x-coordinate
int limit_y = diameter + origin.second; // max y-coordinate
float x_coord = static_cast<float>(origin.first + limit_x); //x1 + x2
float y_coord = static_cast<float>(origin.second + limit_y);//y1 + y2
midpoint = std::make_pair(x_coord / 2,
y_coord / 2);
int m_points = 8;
int num_points = 0, num_in_circle = 0;
for(int i = 0; i < m_points; ++i) {
std::pair<int, int> *point = coordinate(origin.first,
origin.second,
diameter);
if(point) {
if(withinCircle(point, &midpoint, radius)) {
++num_in_circle;
}
++num_points;
free(point);
}
std::cout << std::setw(16) << std::fixed << std::setprecision(9)
<< 3.14159265359 - estimatePi(num_points,
num_in_circle
);
}
std::cout << std::endl;
}
}
/*Pi Day Challenge: compute an estimate of pi by observing the
*probability
*that randomly generated points within a square fall inside of a circle
*inscribed in the square, given that the square's length is the
*diameter of the circle.
*
*Solution by Jonathan Barisere (jthankgod@ymail.com)
*/
#include "pi_day.h++"
// Allocate memory for a tuple containing a randomly generated
// coordinate, and return a pointer to its location
std::pair<int, int> *coordinate(int floor_x,
int floor_y,
int ceiling) {
int x_val = static_cast<int>(rand()) % ceiling + floor_x;
int y_val = static_cast<int>(rand()) % ceiling + floor_y;
std::pair<int, int> *pair_ptr =
(std::pair<int, int>*)malloc(sizeof(std::pair<int, int>));
if (pair_ptr) {
pair_ptr -> first = x_val;
pair_ptr -> second = y_val;
} else pair_ptr = 0;
return pair_ptr;
}
// determine whether the randomly generated point is within the circle
bool withinCircle(std::pair<int, int> *coordinate,
std::pair<float, float> *midpoint,
float radius) {
float x = static_cast<float>(coordinate -> first) - midpoint -> first;
float y = static_cast<float>(coordinate -> second)
- midpoint -> second;
x = pow(x, 2);
y = pow(y, 2);
if((x + y) < pow(radius, 2)) return true;
else return false;
}
// estimate the constant pi
float estimatePi(int num_points, int num_in_circle) {
float ratio = static_cast<float>(num_in_circle)
/ static_cast<float>(num_points);
return 4 * ratio;
}
#ifndef PI_DAY_H
#define PI_DAY_H
#include <cmath>
#include <utility>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
std::pair<int, int> *coordinate(int, int, int);
bool withinCircle(std::pair<int, int>*,
std::pair<float, float>*,
float);
float estimatePi(int, int);
#endif /* PI_DAY_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment