Skip to content

Instantly share code, notes, and snippets.

@peter0749
Last active March 26, 2019 10:28
Show Gist options
  • Save peter0749/12ef74328cbefae3549c94c7028e6aff to your computer and use it in GitHub Desktop.
Save peter0749/12ef74328cbefae3549c94c7028e6aff to your computer and use it in GitHub Desktop.
Pi
val n_pt = 1000000
val rd_g = scala.util.Random
(for ( i <- 0 until n_pt ) yield (rd_g.nextFloat, rd_g.nextFloat)).map( x => (x._1-0.5,x._2-0.5) ).map( x => if (x._1*x._1+x._2*x._2<0.25) 1 else 0 ).reduce(_ + _).toFloat / n_pt.toFloat * 4.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment