fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define M_PI 3.14159265
  6.  
  7. int main() {
  8. double a, b, s, x[4], y[4], r, true_value;
  9. int count, m, n;
  10.  
  11. x[0] = 1.0; y[0] = 0.0;
  12. x[1] = 0.0; y[1] = 1.0;
  13. x[2] = -1.0; y[2] = 0.0;
  14. x[3] = 0.0; y[3] = -1.0;
  15.  
  16. r = 1.0;
  17. true_value = (M_PI - pow(3, 1.5) + 3) / 3.0;
  18.  
  19. for (n = 10; n < 100000; n *= 5) {
  20. count = 0;
  21. s = 0.0;
  22.  
  23. for (m = 0; m < n; m++) {
  24. a = (double)rand() / RAND_MAX * 2.0 - 1.0;
  25. b = (double)rand() / RAND_MAX * 2.0 - 1.0;
  26.  
  27. int in_all_circles = 1;
  28. for (int i = 0; i < 4; i++) {
  29. double dist = (a - x[i]) * (a - x[i]) + (b - y[i]) * (b - y[i]);
  30. if (dist >= r) {
  31. in_all_circles = 0;
  32. break;
  33. }
  34. }
  35.  
  36. if (in_all_circles) {
  37. s += 1.0;
  38. }
  39. count++;
  40. }
  41.  
  42. s /= count;
  43. s *= 4.0;
  44.  
  45. printf("%d\t%f\n", n, fabs(s - true_value));
  46. }
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
10	0.315147
50	0.315147
250	0.315147
1250	0.315147
6250	0.315147
31250	0.315147