#include <stdio.h>
#include <stdlib.h>
#include <math.h> // M_PI を利用
#define NUM_RANDOM 12 // 各試行で使用する乱数のペア数
int main() {
double a, b, s, shinchi;
int count, n, i, trial, inside_count;
// 真の値(4つの単位円で囲まれた領域の理論値)
shinchi = M_PI - 2.0;
// 試行ごとに乱数の個数を12に固定し、n回繰り返す
for (n = 10; n < 100000; n = n * 5) {
count = 0;
s = 0.0;
for (trial = 0; trial < n; trial++) {
inside_count = 0;
// 12個の乱数ペア (a, b) を生成して判定
for (i = 0; i < NUM_RANDOM; i++) {
a
= (double)rand() / RAND_MAX
; // 乱数aを生成 b
= (double)rand() / RAND_MAX
; // 乱数bを生成
// 4つの円に含まれるか判定
if ((a * a + b * b <= 1.0) && // (0, 0) を中心とする円
((a - 1) * (a - 1) + b * b <= 1.0) && // (1, 0) を中心とする円
(a * a + (b - 1) * (b - 1) <= 1.0) && // (0, 1) を中心とする円
((a - 1) * (a - 1) + (b - 1) * (b - 1) <= 1.0)) { // (1, 1) を中心とする円
inside_count++; // 円の中に入っている点をカウント
}
}
// 試行ごとの成功割合を計算
s += (double)inside_count / NUM_RANDOM;
count++;
}
// 推定された面積の平均
s /= count;
// 理論値との誤差を表示
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4gIC8vIE1fUEkg44KS5Yip55SoCgojZGVmaW5lIE5VTV9SQU5ET00gMTIgIC8vIOWQhOippuihjOOBp+S9v+eUqOOBmeOCi+S5seaVsOOBruODmuOCouaVsAoKaW50IG1haW4oKSB7CiAgICBkb3VibGUgYSwgYiwgcywgc2hpbmNoaTsKICAgIGludCBjb3VudCwgbiwgaSwgdHJpYWwsIGluc2lkZV9jb3VudDsKCiAgICAvLyDnnJ/jga7lgKTvvIg044Gk44Gu5Y2Y5L2N5YaG44Gn5Zuy44G+44KM44Gf6aCY5Z+f44Gu55CG6KuW5YCk77yJCiAgICBzaGluY2hpID0gTV9QSSAtIDIuMDsKCiAgICAvLyDoqabooYzjgZTjgajjgavkubHmlbDjga7lgIvmlbDjgpIxMuOBq+WbuuWumuOBl+OAgW7lm57nubDjgorov5TjgZkKICAgIGZvciAobiA9IDEwOyBuIDwgMTAwMDAwOyBuID0gbiAqIDUpIHsKICAgICAgICBjb3VudCA9IDA7CiAgICAgICAgcyA9IDAuMDsKCiAgICAgICAgZm9yICh0cmlhbCA9IDA7IHRyaWFsIDwgbjsgdHJpYWwrKykgewogICAgICAgICAgICBpbnNpZGVfY291bnQgPSAwOwoKICAgICAgICAgICAgLy8gMTLlgIvjga7kubHmlbDjg5rjgqIgKGEsIGIpIOOCkueUn+aIkOOBl+OBpuWIpOWumgogICAgICAgICAgICBmb3IgKGkgPSAwOyBpIDwgTlVNX1JBTkRPTTsgaSsrKSB7CiAgICAgICAgICAgICAgICBhID0gKGRvdWJsZSlyYW5kKCkgLyBSQU5EX01BWDsgIC8vIOS5seaVsGHjgpLnlJ/miJAKICAgICAgICAgICAgICAgIGIgPSAoZG91YmxlKXJhbmQoKSAvIFJBTkRfTUFYOyAgLy8g5Lmx5pWwYuOCkueUn+aIkAoKICAgICAgICAgICAgICAgIC8vIDTjgaTjga7lhobjgavlkKvjgb7jgozjgovjgYvliKTlrpoKICAgICAgICAgICAgICAgIGlmICgoYSAqIGEgKyBiICogYiA8PSAxLjApICYmICAgICAgICAgICAgLy8gKDAsIDApIOOCkuS4reW/g+OBqOOBmeOCi+WGhgogICAgICAgICAgICAgICAgICAgICgoYSAtIDEpICogKGEgLSAxKSArIGIgKiBiIDw9IDEuMCkgJiYgLy8gKDEsIDApIOOCkuS4reW/g+OBqOOBmeOCi+WGhgogICAgICAgICAgICAgICAgICAgIChhICogYSArIChiIC0gMSkgKiAoYiAtIDEpIDw9IDEuMCkgJiYgLy8gKDAsIDEpIOOCkuS4reW/g+OBqOOBmeOCi+WGhgogICAgICAgICAgICAgICAgICAgICgoYSAtIDEpICogKGEgLSAxKSArIChiIC0gMSkgKiAoYiAtIDEpIDw9IDEuMCkpIHsgLy8gKDEsIDEpIOOCkuS4reW/g+OBqOOBmeOCi+WGhgogICAgICAgICAgICAgICAgICAgIGluc2lkZV9jb3VudCsrOyAgLy8g5YaG44Gu5Lit44Gr5YWl44Gj44Gm44GE44KL54K544KS44Kr44Km44Oz44OICiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIC8vIOippuihjOOBlOOBqOOBruaIkOWKn+WJsuWQiOOCkuioiOeulwogICAgICAgICAgICBzICs9IChkb3VibGUpaW5zaWRlX2NvdW50IC8gTlVNX1JBTkRPTTsKICAgICAgICAgICAgY291bnQrKzsKICAgICAgICB9CgogICAgICAgIC8vIOaOqOWumuOBleOCjOOBn+mdouepjeOBruW5s+WdhwogICAgICAgIHMgLz0gY291bnQ7CgogICAgICAgIC8vIOeQhuirluWApOOBqOOBruiqpOW3ruOCkuihqOekugogICAgICAgIHByaW50ZigiJWRcdCVmXG4iLCBuLCBmYWJzKHMgLSBzaGluY2hpKSk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K