inst/examples/pi.R

library(foreach)

w <- getDoParWorkers()
n <- 10000000
h <- 1 / n

pi <- foreach(i=1:w, .combine='+') %dopar% {
  x <- h * (seq(i, n, by=w) - 0.5)
  h * sum(4 / (1 + x * x))
}

cat(sprintf('pi = %f\n', pi))

Try the foreach package in your browser

Any scripts or data that you put into this service are public.

foreach documentation built on May 2, 2019, 6:09 p.m.