#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double monte_carlo_simulacion(int iteraciones) {
clock_t start_time
= clock(); int dentro_circulo = 0;
for (int i = 0; i < iteraciones; i++) {
double x
= (double)rand() / RAND_MAX
; double y
= (double)rand() / RAND_MAX
; if (x*x + y*y <= 1) {
dentro_circulo++;
}
}
double pi = (double)dentro_circulo / iteraciones * 4;
clock_t end_time
= clock(); double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("Tiempo de ejecuciĆ³n: %f segundos\n", time_spent
); return pi;
}
int main() {
printf("%f\n", monte_carlo_simulacion
(1000000)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCmRvdWJsZSBtb250ZV9jYXJsb19zaW11bGFjaW9uKGludCBpdGVyYWNpb25lcykgewogICAgY2xvY2tfdCBzdGFydF90aW1lID0gY2xvY2soKTsKICAgIGludCBkZW50cm9fY2lyY3VsbyA9IDA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IGl0ZXJhY2lvbmVzOyBpKyspIHsKICAgICAgICBkb3VibGUgeCA9IChkb3VibGUpcmFuZCgpIC8gUkFORF9NQVg7CiAgICAgICAgZG91YmxlIHkgPSAoZG91YmxlKXJhbmQoKSAvIFJBTkRfTUFYOwogICAgICAgIGlmICh4KnggKyB5KnkgPD0gMSkgewogICAgICAgICAgICBkZW50cm9fY2lyY3VsbysrOwogICAgICAgIH0KICAgIH0KICAgIGRvdWJsZSBwaSA9IChkb3VibGUpZGVudHJvX2NpcmN1bG8gLyBpdGVyYWNpb25lcyAqIDQ7CiAgICBjbG9ja190IGVuZF90aW1lID0gY2xvY2soKTsKICAgIGRvdWJsZSB0aW1lX3NwZW50ID0gKGRvdWJsZSkoZW5kX3RpbWUgLSBzdGFydF90aW1lKSAvIENMT0NLU19QRVJfU0VDOwogICAgcHJpbnRmKCJUaWVtcG8gZGUgZWplY3VjacOzbjogJWYgc2VndW5kb3NcbiIsIHRpbWVfc3BlbnQpOwogICAgcmV0dXJuIHBpOwp9CgppbnQgbWFpbigpIHsKICAgIHNyYW5kKHRpbWUoMCkpOwogICAgcHJpbnRmKCIlZlxuIiwgbW9udGVfY2FybG9fc2ltdWxhY2lvbigxMDAwMDAwKSk7CiAgICByZXR1cm4gMDsKfQo=