#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define TRUE_AREA 0.5398 // 理論的な重複領域の面積(事前に計算された値)
#define MAX_POINTS 100000 // 最大乱数の個数
// 点が円の中にあるかどうかを判定する関数
int is_in_overlap(double x, double y) {
double dist1 = (x - 0.5) * (x - 0.5) + y * y;
double dist2 = (x - 0.5) * (x - 0.5) + (y - 1) * (y - 1);
return (dist1 <= 0.25) && (dist2 <= 0.25);
}
int main() {
int i, count;
double x, y;
double estimated_area, error;
int num_points[] = {1000, 5000, 10000, 50000, 100000}; // 乱数の個数
// 各乱数の個数に対して推定面積と誤差を計算
for (int j = 0; j < sizeof(num_points) / sizeof(num_points[0]); j++) {
count = 0;
for (i = 0; i < num_points[j]; i++) {
x
= (double)rand() / RAND_MAX
; y
= (double)rand() / RAND_MAX
;
if (is_in_overlap(x, y)) {
count++;
}
}
// 重複領域の面積の推定値
estimated_area = 4.0 * (double)count / num_points[j];
// 真値との誤差
error
= fabs(estimated_area
- TRUE_AREA
);
// 結果を表示
printf("乱数の個数: %d, 推定面積: %f, 誤差: %f\n", num_points
[j
], estimated_area
, error
); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgVFJVRV9BUkVBIDAuNTM5OCAgLy8g55CG6KuW55qE44Gq6YeN6KSH6aCY5Z+f44Gu6Z2i56mN77yI5LqL5YmN44Gr6KiI566X44GV44KM44Gf5YCk77yJCiNkZWZpbmUgTUFYX1BPSU5UUyAxMDAwMDAgLy8g5pyA5aSn5Lmx5pWw44Gu5YCL5pWwCgovLyDngrnjgYzlhobjga7kuK3jgavjgYLjgovjgYvjganjgYbjgYvjgpLliKTlrprjgZnjgovplqLmlbAKaW50IGlzX2luX292ZXJsYXAoZG91YmxlIHgsIGRvdWJsZSB5KSB7CiAgICBkb3VibGUgZGlzdDEgPSAoeCAtIDAuNSkgKiAoeCAtIDAuNSkgKyB5ICogeTsKICAgIGRvdWJsZSBkaXN0MiA9ICh4IC0gMC41KSAqICh4IC0gMC41KSArICh5IC0gMSkgKiAoeSAtIDEpOwogICAgcmV0dXJuIChkaXN0MSA8PSAwLjI1KSAmJiAoZGlzdDIgPD0gMC4yNSk7Cn0KCmludCBtYWluKCkgewogICAgaW50IGksIGNvdW50OwogICAgZG91YmxlIHgsIHk7CiAgICBkb3VibGUgZXN0aW1hdGVkX2FyZWEsIGVycm9yOwogICAgaW50IG51bV9wb2ludHNbXSA9IHsxMDAwLCA1MDAwLCAxMDAwMCwgNTAwMDAsIDEwMDAwMH07IC8vIOS5seaVsOOBruWAi+aVsAoKICAgIHNyYW5kKHRpbWUoTlVMTCkpOyAvLyDkubHmlbDjga7nqK7jgpLoqK3lrpoKCiAgICAvLyDlkITkubHmlbDjga7lgIvmlbDjgavlr77jgZfjgabmjqjlrprpnaLnqY3jgajoqqTlt67jgpLoqIjnrpcKICAgIGZvciAoaW50IGogPSAwOyBqIDwgc2l6ZW9mKG51bV9wb2ludHMpIC8gc2l6ZW9mKG51bV9wb2ludHNbMF0pOyBqKyspIHsKICAgICAgICBjb3VudCA9IDA7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IG51bV9wb2ludHNbal07IGkrKykgewogICAgICAgICAgICB4ID0gKGRvdWJsZSlyYW5kKCkgLyBSQU5EX01BWDsKICAgICAgICAgICAgeSA9IChkb3VibGUpcmFuZCgpIC8gUkFORF9NQVg7CgogICAgICAgICAgICBpZiAoaXNfaW5fb3ZlcmxhcCh4LCB5KSkgewogICAgICAgICAgICAgICAgY291bnQrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgLy8g6YeN6KSH6aCY5Z+f44Gu6Z2i56mN44Gu5o6o5a6a5YCkCiAgICAgICAgZXN0aW1hdGVkX2FyZWEgPSA0LjAgKiAoZG91YmxlKWNvdW50IC8gbnVtX3BvaW50c1tqXTsKCiAgICAgICAgLy8g55yf5YCk44Go44Gu6Kqk5beuCiAgICAgICAgZXJyb3IgPSBmYWJzKGVzdGltYXRlZF9hcmVhIC0gVFJVRV9BUkVBKTsKCiAgICAgICAgLy8g57WQ5p6c44KS6KGo56S6CiAgICAgICAgcHJpbnRmKCLkubHmlbDjga7lgIvmlbA6ICVkLCDmjqjlrprpnaLnqY06ICVmLCDoqqTlt646ICVmXG4iLCBudW1fcG9pbnRzW2pdLCBlc3RpbWF0ZWRfYXJlYSwgZXJyb3IpOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==
乱数の個数: 1000, 推定面積: 0.000000, 誤差: 0.539800
乱数の個数: 5000, 推定面積: 0.000000, 誤差: 0.539800
乱数の個数: 10000, 推定面積: 0.000000, 誤差: 0.539800
乱数の個数: 50000, 推定面積: 0.000000, 誤差: 0.539800
乱数の個数: 100000, 推定面積: 0.000000, 誤差: 0.539800