Skip to content

Instantly share code, notes, and snippets.

@galleon
Last active August 29, 2015 14:25
Show Gist options
  • Save galleon/70fa4740ce8592ba8531 to your computer and use it in GitHub Desktop.
Save galleon/70fa4740ce8592ba8531 to your computer and use it in GitHub Desktop.
import org.apache.spark.api.java.JavaSparkContext
def ctx = new JavaSparkContext("local", "GroovySparkPi")
def slices = 1000
def n = 10000 * slices
def l = 0..n
def dataSet = ctx.parallelize(l, slices)
def count = dataSet.map({
def x = Math.random() * 2 - 1
def y = Math.random() * 2 - 1
return (x*x + y*y < 1) ? 1 : 0
}).reduce({a, b -> a + b})
println("Pi is roughly ${4.0 * count / n}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment