Skip to content

Instantly share code, notes, and snippets.

@ryanknutson
Created April 5, 2018 13:42
Show Gist options
  • Save ryanknutson/1ae5409c4137ca2ba15b84d9e1258da6 to your computer and use it in GitHub Desktop.
Save ryanknutson/1ae5409c4137ca2ba15b84d9e1258da6 to your computer and use it in GitHub Desktop.
JavaScript to compute pi
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