#include <iostream>
#include <random>
#include <chrono>
double monte_carlo_simulation(int iterations) {
std::default_random_engine generator;
std::uniform_real_distribution<double> distribution(0.0, 1.0);
int inside_circle = 0;
for (int i = 0; i < iterations; ++i) {
double x = distribution(generator);
double y = distribution(generator);
if (x * x + y * y <= 1) {
++inside_circle;
}
}
return 4.0 * inside_circle / iterations;
}
int main() {
int iterations = 1000000;
auto start = std::chrono::high_resolution_clock::now();
double pi_estimate = monte_carlo_simulation(iterations);
auto end = std::chrono::high_resolution_clock::now();
std::cout << "EstimaciĆ³n de Pi: " << pi_estimate << "\n";
std::cout << "Tiempo de ejecuciĆ³n: "
<< std::chrono::duration<double>(end - start).count() << " segundos\n";
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cmFuZG9tPgojaW5jbHVkZSA8Y2hyb25vPgoKZG91YmxlIG1vbnRlX2NhcmxvX3NpbXVsYXRpb24oaW50IGl0ZXJhdGlvbnMpIHsKICAgIHN0ZDo6ZGVmYXVsdF9yYW5kb21fZW5naW5lIGdlbmVyYXRvcjsKICAgIHN0ZDo6dW5pZm9ybV9yZWFsX2Rpc3RyaWJ1dGlvbjxkb3VibGU+IGRpc3RyaWJ1dGlvbigwLjAsIDEuMCk7CgogICAgaW50IGluc2lkZV9jaXJjbGUgPSAwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBpdGVyYXRpb25zOyArK2kpIHsKICAgICAgICBkb3VibGUgeCA9IGRpc3RyaWJ1dGlvbihnZW5lcmF0b3IpOwogICAgICAgIGRvdWJsZSB5ID0gZGlzdHJpYnV0aW9uKGdlbmVyYXRvcik7CiAgICAgICAgaWYgKHggKiB4ICsgeSAqIHkgPD0gMSkgewogICAgICAgICAgICArK2luc2lkZV9jaXJjbGU7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDQuMCAqIGluc2lkZV9jaXJjbGUgLyBpdGVyYXRpb25zOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBpdGVyYXRpb25z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