monte_carlo_simulation <- function(iterations) {
inside_circle <- 0
for (i in 1:iterations) {
x <- runif(1)
y <- runif(1)
if (x^2 + y^2 <= 1) {
inside_circle <- inside_circle + 1
}
}
return (inside_circle / iterations) * 4
}
iterations <- 1e6
start_time
<- proc.
time()pi_estimate <- monte_carlo_simulation(iterations)
cat("EstimaciĆ³n de Pi:", pi_estimate, "\n")
cat("Tiempo de ejecuciĆ³n:", (end_time - start_time)["elapsed"], "segundos\n")
bW9udGVfY2FybG9fc2ltdWxhdGlvbiA8LSBmdW5jdGlvbihpdGVyYXRpb25zKSB7CiAgaW5zaWRlX2NpcmNsZSA8LSAwCiAgZm9yIChpIGluIDE6aXRlcmF0aW9ucykgewogICAgeCA8LSBydW5pZigxKQogICAgeSA8LSBydW5pZigxKQogICAgaWYgKHheMiArIHleMiA8PSAxKSB7CiAgICAgIGluc2lkZV9jaXJjbGUgPC0gaW5zaWRlX2NpcmNsZSArIDEKICAgIH0KICB9CiAgcmV0dXJuIChpbnNpZGVfY2lyY2xlIC8gaXRlcmF0aW9ucykgKiA0Cn0KCml0ZXJhdGlvbnMgPC0gMWU2CnN0YXJ0X3RpbWUgPC0gcHJvYy50aW1lKCkKcGlfZXN0aW1hdGUgPC0gbW9udGVfY2FybG9fc2ltdWxhdGlvbihpdGVyYXRpb25zKQplbmRfdGltZSA8LSBwcm9jLnRpbWUoKQoKY2F0KCJFc3RpbWFjacOzbiBkZSBQaToiLCBwaV9lc3RpbWF0ZSwgIlxuIikKY2F0KCJUaWVtcG8gZGUgZWplY3VjacOzbjoiLCAoZW5kX3RpbWUgLSBzdGFydF90aW1lKVsiZWxhcHNlZCJdLCAic2VndW5kb3NcbiIpCg==