Skip to content

Instantly share code, notes, and snippets.

@dno89
Created July 20, 2021 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dno89/abbc34ca31ff34c47007e8a405035693 to your computer and use it in GitHub Desktop.
Save dno89/abbc34ca31ff34c47007e8a405035693 to your computer and use it in GitHub Desktop.
Compute PI
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int, char**) {
long long inside = 0;
for(long long ii = 0; ii < 1e9; ++ii) {
double x = rand()/double(RAND_MAX);
double y = rand()/double(RAND_MAX);
if(x*x+y*y <= 1.0) {
++inside;
}
if(ii % 1000000 == 0) {
double ratio = double(ii+1)/inside;
cout << "i: " << ii << "ratio: " << ratio;
cout << ", pi: " << 4.0/ratio << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment