Skip to content

Instantly share code, notes, and snippets.

@aussetg
Created February 3, 2013 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aussetg/4701723 to your computer and use it in GitHub Desktop.
Save aussetg/4701723 to your computer and use it in GitHub Desktop.
library(compiler)
enableJIT(3)
pimc = function(n) {
v1 <- runif(n)
v2 <- runif(n)
vc <- v1^2 + v2^2
vc[vc<=1] <- 1
vc[vc>1] <- 0
vt <- v1 + v2
vt[vt<=1] <- 1
vt[vt>1] <- 0
nc <- sum(vc)
nt <- sum(vt)
return( (4*nc/n + 2*nc/nt)/2 )
}
system.time(pimc(10^7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment