Last active
August 29, 2015 14:12
-
-
Save kos59125/ac50afe8fcc79aee385a to your computer and use it in GitHub Desktop.
Monte Carlo pi in R when the number of iteration is too large
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
calc_pi <- function(n, k) { | |
stopifnot(n %% k == 0) | |
r <- n %/% k | |
s <- 0 | |
for (loop in seq_len(r)) { | |
x <- runif(k) | |
y <- runif(k) | |
s <- s + sum(x * x + y * y <= 1) | |
} | |
4 * s / n | |
} | |
system.time(calc_pi(100000000L, 10000L)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment