#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM 10000 // 一回の試行で行うランダムな点の数
#define Ref 10000 // 試行回数
int main() {
double x, k = 0.0;
int j;
// モンテカルロ法によるeの近似
for (j = 0; j < Ref; j++) {
double sum = 0.0;
int count = 0;
// ランダムな数を生成して、1に達するまで足し算
while (sum <= 1.0) {
x
= rand() / (double)RAND_MAX
; // 0から1のランダムな数を生成 sum += x;
count++;
}
k += count; // 1に達するまでの回数を加算
}
// 結果を平均してeの近似値を出力
k /= Ref;
printf("自然対数の底eの近似値は: %lf\n", k
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgTlVNIDEwMDAwICAgICAvLyDkuIDlm57jga7oqabooYzjgafooYzjgYbjg6njg7Pjg4Djg6Djgarngrnjga7mlbAKI2RlZmluZSBSZWYgMTAwMDAgICAgLy8g6Kmm6KGM5Zue5pWwCgppbnQgbWFpbigpIHsKICAgIHNyYW5kKCh1bnNpZ25lZCl0aW1lKE5VTEwpKTsgIC8vIOS5seaVsOOBrueoruOCkuWIneacn+WMlgoKICAgIGRvdWJsZSB4LCBrID0gMC4wOwogICAgaW50IGo7CgogICAgLy8g44Oi44Oz44OG44Kr44Or44Ot5rOV44Gr44KI44KLZeOBrui/keS8vAogICAgZm9yIChqID0gMDsgaiA8IFJlZjsgaisrKSB7CiAgICAgICAgZG91YmxlIHN1bSA9IDAuMDsKICAgICAgICBpbnQgY291bnQgPSAwOwoKICAgICAgICAvLyDjg6njg7Pjg4Djg6DjgarmlbDjgpLnlJ/miJDjgZfjgabjgIEx44Gr6YGU44GZ44KL44G+44Gn6Laz44GX566XCiAgICAgICAgd2hpbGUgKHN1bSA8PSAxLjApIHsKICAgICAgICAgICAgeCA9IHJhbmQoKSAvIChkb3VibGUpUkFORF9NQVg7ICAvLyAw44GL44KJMeOBruODqeODs+ODgOODoOOBquaVsOOCkueUn+aIkAogICAgICAgICAgICBzdW0gKz0geDsKICAgICAgICAgICAgY291bnQrKzsKICAgICAgICB9CgogICAgICAgIGsgKz0gY291bnQ7ICAvLyAx44Gr6YGU44GZ44KL44G+44Gn44Gu5Zue5pWw44KS5Yqg566XCiAgICB9CgogICAgLy8g57WQ5p6c44KS5bmz5Z2H44GX44GmZeOBrui/keS8vOWApOOCkuWHuuWKmwogICAgayAvPSBSZWY7CiAgICBwcmludGYoIuiHqueEtuWvvuaVsOOBruW6lWXjga7ov5HkvLzlgKTjga86ICVsZlxuIiwgayk7CgogICAgcmV0dXJuIDA7Cn0K