#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define NUM_POINTS 1000000
#define TRUE_AREA (M_PI - 3 * sqrt(3) + 3) / 3
#define NUM_STEPS 12
// 4つの単位円の重複領域にあるかを判定する関数
int is_inside(double x, double y) {
double r = 1.0;
if ((x - 0) * (x - 0) + (y - 1) * (y - 1) <= r * r && // 中心 (0, 1) の円
(x - 1) * (x - 1) + (y - 0) * (y - 0) <= r * r && // 中心 (1, 0) の円
(x - 0) * (x - 0) + (y - 0) * (y - 0) <= r * r && // 中心 (0, 0) の円
(x - 1) * (x - 1) + (y - 1) * (y - 1) <= r * r) { // 中心 (1, 1) の円
return 1;
}
return 0;
}
int main() {
int i, inside_count = 0;
double x, y;
double estimated_area;
double error;
int step_size = NUM_POINTS / NUM_STEPS; // 乱数の個数を12ステップに分割
for (i = 1; i <= NUM_POINTS; i++) {
x
= (double)rand() / RAND_MAX
; y
= (double)rand() / RAND_MAX
;
if (is_inside(x, y)) {
inside_count++;
}
if (i % step_size == 0) {
estimated_area = 4.0 * (double)inside_count / i;
error
= fabs(estimated_area
- TRUE_AREA
);
}
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgTlVNX1BPSU5UUyAxMDAwMDAwCiNkZWZpbmUgVFJVRV9BUkVBIChNX1BJIC0gMyAqIHNxcnQoMykgKyAzKSAvIDMKI2RlZmluZSBOVU1fU1RFUFMgMTIKCi8vIDTjgaTjga7ljZjkvY3lhobjga7ph43opIfpoJjln5/jgavjgYLjgovjgYvjgpLliKTlrprjgZnjgovplqLmlbAKaW50IGlzX2luc2lkZShkb3VibGUgeCwgZG91YmxlIHkpIHsKICAgIGRvdWJsZSByID0gMS4wOwogICAgaWYgKCh4IC0gMCkgKiAoeCAtIDApICsgKHkgLSAxKSAqICh5IC0gMSkgPD0gciAqIHIgJiYgIC8vIOS4reW/gyAoMCwgMSkg44Gu5YaGCiAgICAgICAgKHggLSAxKSAqICh4IC0gMSkgKyAoeSAtIDApICogKHkgLSAwKSA8PSByICogciAmJiAgLy8g5Lit5b+DICgxLCAwKSDjga7lhoYKICAgICAgICAoeCAtIDApICogKHggLSAwKSArICh5IC0gMCkgKiAoeSAtIDApIDw9IHIgKiByICYmICAvLyDkuK3lv4MgKDAsIDApIOOBruWGhgogICAgICAgICh4IC0gMSkgKiAoeCAtIDEpICsgKHkgLSAxKSAqICh5IC0gMSkgPD0gciAqIHIpIHsgIC8vIOS4reW/gyAoMSwgMSkg44Gu5YaGCiAgICAgICAgcmV0dXJuIDE7CiAgICB9CiAgICByZXR1cm4gMDsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgaSwgaW5zaWRlX2NvdW50ID0gMDsKICAgIGRvdWJsZSB4LCB5OwogICAgZG91YmxlIGVzdGltYXRlZF9hcmVhOwogICAgZG91YmxlIGVycm9yOwogICAgaW50IHN0ZXBfc2l6ZSA9IE5VTV9QT0lOVFMgLyBOVU1fU1RFUFM7IC8vIOS5seaVsOOBruWAi+aVsOOCkjEy44K544OG44OD44OX44Gr5YiG5YmyCgogICAgc3JhbmQodGltZShOVUxMKSk7CgogICAgcHJpbnRmKCLkubHmlbDjga7lgIvmlbAsIOiqpOW3rlxuIik7CgogICAgZm9yIChpID0gMTsgaSA8PSBOVU1fUE9JTlRTOyBpKyspIHsKICAgICAgICB4ID0gKGRvdWJsZSlyYW5kKCkgLyBSQU5EX01BWDsKICAgICAgICB5ID0gKGRvdWJsZSlyYW5kKCkgLyBSQU5EX01BWDsKCiAgICAgICAgaWYgKGlzX2luc2lkZSh4LCB5KSkgewogICAgICAgICAgICBpbnNpZGVfY291bnQrKzsKICAgICAgICB9CgogICAgICAgIGlmIChpICUgc3RlcF9zaXplID09IDApIHsKICAgICAgICAgICAgZXN0aW1hdGVkX2FyZWEgPSA0LjAgKiAoZG91YmxlKWluc2lkZV9jb3VudCAvIGk7CiAgICAgICAgICAgIGVycm9yID0gZmFicyhlc3RpbWF0ZWRfYXJlYSAtIFRSVUVfQVJFQSk7CgogICAgICAgICAgICBwcmludGYoIiVkLCVmXG4iLCBpLCBlcnJvcik7CiAgICAgICAgfQogICAgfQoKICAgIHByaW50Zigi6KiI566X44GM5a6M5LqG44GX44G+44GX44Gf44CCXG4iKTsKCiAgICByZXR1cm4gMDsKfQo=
乱数の個数, 誤差
83333,0.941258
166666,0.938234
249999,0.939754
333332,0.941450
416665,0.943255
499998,0.942354
583331,0.943967
666664,0.943658
749997,0.944149
833330,0.944863
916663,0.945007
999996,0.945866
計算が完了しました。