Pi

Pi can be estimated by summing elements in an infinite series derived from the following equation:

Pi series

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:

CPU load
object Main extends App {

  import org.apache.spark.sql.Dataset
  import Spark.sparkSession.implicits._

  val ks: Dataset[Int] =
    Spark.sparkSession.sparkContext
      .parallelize(0 to 100000000)
      .toDS()

  val pi: Double =
    ks
      .map(k => 4 * math.pow(-1, k) / (2 * k + 1))
      .reduce(_ + _)
   
  println(s"Pi: ${pi}")

  Spark.sparkSession.close()
}