fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. double monte_carlo_simulacion(int iteraciones) {
  6. clock_t start_time = clock();
  7. int dentro_circulo = 0;
  8. for (int i = 0; i < iteraciones; i++) {
  9. double x = (double)rand() / RAND_MAX;
  10. double y = (double)rand() / RAND_MAX;
  11. if (x*x + y*y <= 1) {
  12. dentro_circulo++;
  13. }
  14. }
  15. double pi = (double)dentro_circulo / iteraciones * 4;
  16. clock_t end_time = clock();
  17. double time_spent = (double)(end_time - start_time) / CLOCKS_PER_SEC;
  18. printf("Tiempo de ejecuciĆ³n: %f segundos\n", time_spent);
  19. return pi;
  20. }
  21.  
  22. int main() {
  23. srand(time(0));
  24. printf("%f\n", monte_carlo_simulacion(1000000));
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0.03s 5272KB
stdin
Standard input is empty
stdout
Tiempo de ejecución: 0.023851 segundos
3.142748