Skip to content

Instantly share code, notes, and snippets.

@U1F30C
Created February 6, 2024 03:05
Show Gist options
  • Select an option

  • Save U1F30C/2ffef9ea18d361a0ac76dd734e951d09 to your computer and use it in GitHub Desktop.

Select an option

Save U1F30C/2ffef9ea18d361a0ac76dd734e951d09 to your computer and use it in GitHub Desktop.
PI calculation
function piStep(n) {
return (
(
Math.pow(-1, n)
/ (2 * n + 1)
)
* (
4 / Math.pow(5, 2 * n + 1)
- 1 / Math.pow(239, 2 * n + 1)
)
);
}
function calcPi(n) {
var sum = 0;
for (var i = 0; i <= n; i++) {
sum += piStep(i);
}
return sum * 4;
}
console.log(calcPi(100000000));
// compare to built in pi
console.log(Math.PI);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment