Skip to content

Instantly share code, notes, and snippets.

@swuecho
Created August 31, 2012 01:02
Show Gist options
  • Select an option

  • Save swuecho/3547039 to your computer and use it in GitHub Desktop.

Select an option

Save swuecho/3547039 to your computer and use it in GitHub Desktop.
import scala.math._
/* cacluate pi
Pi = 4*(1 - 1/3 + 1/5 -1/7+....)
write a function that calculates pi to an accuracy of 5 decimal places
*/
// 1/(2*x+1)*4<1e-6
//4*1e6<2*x+1
// x>(4*1e6-1)/2
// x>2*1e6
def npi(n: Int) = pow(-1,n)/(2*n+1)
val N: Int = 2e6.toInt ;
val pi = (0 to N).map(npi).sum*4
println(pi)
// calculate the area of a circle
def circleArea(r:Double) = pi*r*r
val a = 3
println(circleArea(3))
// vim: set ts=4 sw=4 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment