Created
April 5, 2018 13:42
-
-
Save ryanknutson/1ae5409c4137ca2ba15b84d9e1258da6 to your computer and use it in GitHub Desktop.
JavaScript to compute pi
This file contains 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
var z = 1; | |
var sum = 0; | |
var neg = false; | |
// uses the Taylor series for arctan(1) | |
while (z < 100000000 ) { | |
if (neg == true) { | |
sum = sum - (Math.pow(1, z) / z); | |
z = z + 2; | |
neg = false; | |
} else { | |
sum = sum + (Math.pow(1, z) / z); | |
z = z + 2; | |
neg = true; | |
} | |
} | |
// multiply by 4 because arctan(1) = pi/4 | |
console.log(sum * 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment