Created
August 31, 2012 01:02
-
-
Save swuecho/3547039 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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