#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}; // 乱数の個数

    srand(time(NULL)); // 乱数の種を設定

    // 各乱数の個数に対して推定面積と誤差を計算
    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;
}
