Calculate Pi to a Huge Number of Digits
Calculate pi to digits. (Be careful going over 100,000 as it may take awhile.)
The algorithm is actually the Maclaurin series for the arcsin of pi/6:
(where |z| ≤ 1, 0!! = 1, (−1)!! = 1 )
Approximations of π: Arcsine and Inverse trigonometric functions: Infinite series at Wikipedia, and
Maclaurin expansion of arcsin x and Finding the power series of arcsin x at Mathematics Stack Exchange for details about the algorithm.
Even though the algorithm yields only about three digits per five iterations, the numbers are small and the operations are simple, so it runs fast. Of note, it converges much faster on pi/6 than on pi/2. Many more algorithms are available; the next-fastest seems to be Newton’s formula; Plouffe’s is also fast.
You can check the results at princeton.edu (pi-10million.txt, 10MB). The final 100 of various numbers of digits:
1,000: 5982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989 10,000: 2645600162374288021092764579310657922955249887275846101264836999892256959688159205600101655256375678 100,000: 8575016363411314627530499019135646823804329970695770150789337728658035712790913767420805655493624646 300,000: 0649079063277209044524503248575792807082252032623968335485158606931459783852836169456336186314956895 1,000,000: 0315614033321272849194418437150696552087542450598956787961303311646283996346460422090106105779458151
Not all browsers are created equal. Firefox (81.0) will not create a Big Integer with more than 315,633 digits; the script above pads the numbers with 10 extra digits to ensure accuracy, so the practial limit is 315,623. Chromium (86.0) will run more than 1,000,000 digits, but the speed drops off significantly. Some benchmarks, Intel® Core™ i5-4440 CPU @ 3.10GHz × 4:
digits Chromium Firefox (seconds) 100,000 35 52 300,000 371 498 1,000,000 4075 –
Here is a Bash script, bcpi, that uses bc to run the algorithm above. It is not as fast as a browser, 995 seconds for 100,000 digits. But it is much faster, and more accurate, than using the arctan function (bc -l <<< "scale=10000;4*a(1)", 9 seconds vs. 99 for 10,000 digits). It was written on Ubuntu MATE 20.04.
russell@focal-desktop:~$ bcpi Calculate pi to how many decimal places? 1000 .. 3.141592653589793238462643383279502884197169399375105820974944592307\ 81640628620899862803482534211706798214808651328230664709384460955058\ 22317253594081284811174502841027019385211055596446229489549303819644\ 28810975665933446128475648233786783165271201909145648566923460348610\ 45432664821339360726024914127372458700660631558817488152092096282925\ 40917153643678925903600113305305488204665213841469519415116094330572\ 70365759591953092186117381932611793105118548074462379962749567351885\ 75272489122793818301194912983367336244065664308602139494639522473719\ 07021798609437027705392171762931767523846748184676694051320005681271\ 45263560827785771342757789609173637178721468440901224953430146549585\ 37105079227968925892354201995611212902196086403441815981362977477130\ 99605187072113499999983729780499510597317328160963185950244594553469\ 08302642522308253344685035261931188171010003137838752886587533208381\ 42061717766914730359825349042875546873115956286388235378759375195778\ 18577805321712268066130019278766111959092164201989 .109 seconds russell@focal-desktop:~$
Please visit russellcottrell.com | blog