Created
February 6, 2024 03:05
-
-
Save U1F30C/2ffef9ea18d361a0ac76dd734e951d09 to your computer and use it in GitHub Desktop.
PI calculation
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
| 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