Article 5FAH8 Calculating π with factorials

Calculating π with factorials

by
John
from John D. Cook on (#5FAH8)

In honor of Pi Day, I present the following equation for calculating using factorials.

stirling_pi3.svg

It's not a very useful formula for , but an amusing one. It takes a practical formula for approximating factorials, Stirling's formula, and turns it around to make an impractical formula for approximating .

It does converge to albeit slowly. If you stick in n = 100, for example, you get 3.1468....

stirling_pi_plot3.png

You can find a more practical formula for , based on work of Ramanujan, that was used for setting several records for computing decimals of here.

You could code up the formula above in basic Python, but it will overflow quickly. Better to compute its logarithm, then take the exponential.

 from scipy.special import gammaln from numpy import log, exp def stirling_pi(n): return exp(2*(gammaln(n+1) + n - n*log(n)) - log(2) - log(n))
The post Calculating with factorials first appeared on John D. Cook.DZVpB_mtpYU
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments