Skip to content

Instantly share code, notes, and snippets.

@kos59125
Last active August 29, 2015 14:12
Show Gist options
  • Save kos59125/ac50afe8fcc79aee385a to your computer and use it in GitHub Desktop.
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
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