Pi can be estimated by summing elements in an infinite series derived from the following equation:
The evaluation of each element in the series can be carried out independently, then combined with one another at the end. This busies the cores:
object Main extends App {
import org.apache.spark.sql.Dataset
import Spark.sparkSession.implicits._
val ks: Dataset[Int] =
.sparkSession.sparkContext
Spark.parallelize(0 to 100000000)
.toDS()
val pi: Double =
ks.map(k => 4 * math.pow(-1, k) / (2 * k + 1))
.reduce(_ + _)
println(s"Pi: ${pi}")
.sparkSession.close()
Spark}