#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

int is_inside(double x, double y) {
    double r = 1.0;
    if ((x - 0) * (x - 0) + (y - 1) * (y - 1) <= r * r &&  
        (x - 1) * (x - 1) + (y - 0) * (y - 0) <= r * r &&  
        (x - 0) * (x - 0) + (y - 0) * (y - 0) <= r * r &&  
        (x - 1) * (x - 1) + (y - 1) * (y - 1) <= r * r) {  
        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;

    srand(time(NULL));

    printf("乱数の個数, 誤差\n");

    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);

            printf("%d,%f\n", i, error);
        }
    }

    printf("計算が完了しました。\n");

    return 0;
}
