#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI 3.14159265

int main() {
    double a, b, s, x1, x2, y1, y2;
    double r, q1, q2, shinchi;
    int count, m, n;

    // 理論的な重複領域の面積
    shinchi = M_PI / 2.0 - 1.0;

    // 円の中心と半径の設定
    x1 = 1.0; y1 = 0.0;
    x2 = 0.0; y2 = 1.0;
    r = 1.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;
            b = (double)rand() / RAND_MAX;
            q1 = (a - x1) * (a - x1) + (b - y1) * (b - y1);
            q2 = (a - x2) * (a - x2) + (b - y2) * (b - y2);
            if (q1 < r * r && q2 < r * r) {
                s += 1.0;
            }
            count++;
        }
        s = s / count;
        s = s * 4;  // 単位正方形内の割合から全体の面積に換算
        printf("乱数の個数: %d, 誤差: %f\n", n, fabs(s - shinchi));
    }

    return 0;
}
