fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. srand(time(0));
  9. int N = 100000, M = 0;
  10.  
  11. for (int i = 1; i <= N; i++) {
  12.  
  13. double x = (double) rand() / RAND_MAX;
  14. double y = (double) rand() / RAND_MAX;
  15.  
  16. if (x * x + y * y < 1) M++;
  17. }
  18.  
  19. cout << "pi ~ " << 4.0 * M / N << endl;
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
pi ~ 3.1466