#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI 3.14159265
int main() {
double a, b, s, q1, q2, q3, q4, r, actual_area;
int count, m, n;
// Set the actual area (based on theoretical calculation for verification)
actual_area = M_PI - 2; // Exact area of the region (adjust this according to problem specifics)
r = 1.0; // Radius of the unit circle
count = 0;
for(n = 10; n < 100000; n = n * 5) {
count = 0;
s = 0.0;
for(m = 0; m < n; m++) {
a
= (double)rand() / RAND_MAX
; // Random x-coordinate b
= (double)rand() / RAND_MAX
; // Random y-coordinate
// Check if point (a,b) is within all four unit circles centered at (0,0), (0,1), (1,0), (1,1)
q1 = (a - 0.0) * (a - 0.0) + (b - 0.0) * (b - 0.0); // Distance from center (0,0)
q2 = (a - 1.0) * (a - 1.0) + (b - 0.0) * (b - 0.0); // Distance from center (1,0)
q3 = (a - 0.0) * (a - 0.0) + (b - 1.0) * (b - 1.0); // Distance from center (0,1)
q4 = (a - 1.0) * (a - 1.0) + (b - 1.0) * (b - 1.0); // Distance from center (1,1)
// Count if the point lies inside all four circles
if(q1 < r && q2 < r && q3 < r && q4 < r) {
s += 1.0;
}
count++;
}
s /= count; // Proportion of points within the common region
double estimated_area = s * 1.0; // Bounding box area is 1.0 (since we're in a 1x1 square)
// Print the number of random points and the error compared to the theoretical area
printf("Points: %d\tEstimated Area: %f\tError: %f\n", n
, estimated_area
, fabs(estimated_area
- actual_area
)); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2RlZmluZSBNX1BJIDMuMTQxNTkyNjUKCmludCBtYWluKCkgewogICAgZG91YmxlIGEsIGIsIHMsIHExLCBxMiwgcTMsIHE0LCByLCBhY3R1YWxfYXJlYTsKICAgIGludCBjb3VudCwgbSwgbjsKICAgIAogICAgLy8gU2V0IHRoZSBhY3R1YWwgYXJlYSAoYmFzZWQgb24gdGhlb3JldGljYWwgY2FsY3VsYXRpb24gZm9yIHZlcmlm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