Created
July 20, 2021 01:07
-
-
Save dno89/abbc34ca31ff34c47007e8a405035693 to your computer and use it in GitHub Desktop.
Compute PI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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