3
$\begingroup$

I'm interested in generating digits of $\pi$ (I'm programming it in Python) and from my research it seems Chudnovsky algorithm is the fastest. Unfortunately for me, the Wikipedia page only really gives a massive equation with lots of symbols.

Edit: What would it look like as an infinite series?

I'm in year 11 so simpler explanations would be appreciated.

$\endgroup$
  • 4
    $\begingroup$ Do you know what a geometric series is? The Chudnovsky algorithm doesn't give one, so it's not clear what you mean. $\endgroup$ – Gerry Myerson Aug 8 '12 at 13:08
  • $\begingroup$ It isn't a geometric series. What part of the Chudnovsky series do you have trouble understanding? $\endgroup$ – J. M. is a poor mathematician Aug 8 '12 at 13:09
  • $\begingroup$ It might be worthwhile to spend time trying to understand the massive equation rather than let it intimidate you! You will also probably be able to find less cryptic descriptions of the algorithm outside of WP. Good luck! $\endgroup$ – rschwieb Aug 8 '12 at 13:13
  • $\begingroup$ Perhaps that the 'mathematical constants' link will help you (they have excellent stuff concerning high precision computation of constants and even a software pifast that you may try). $\endgroup$ – Raymond Manzoni Aug 8 '12 at 13:26
  • 2
    $\begingroup$ By combining Machin's Formula ($\frac{\pi}{4} = 4 \arctan\left(\frac{1}{5}\right) + \arctan\left(\frac{1}{239}\right)$) and the series expansion of $\arctan$ ($\arctan(x) = \sum_{n = 0}^{\infty} \frac{(-1)^n x^{2n+1}}{2n+1}$), you get a way to compute $\pi$ that is reasonably fast and simple to implement (see en.wikipedia.org/wiki/Machin-like_formula for more info). $\endgroup$ – Joel Cohen Aug 8 '12 at 19:28
3
$\begingroup$

The Chudnovsky series is based on a hypergeometric series, which may be why you think it is expressible as a simple geometric series. However, in general hypergeometric series are not expressible as geometric series.

However, using the expression you've linked to at wikipedia, you can write a trivial implementation in Python:

$$\frac{1}{\pi}=12\sum_{k=0}^{\infty}{\frac{(-1)^{k}(6k)!(13591409+545140134k)}{(3k)!(k!)^{3}640320^{3k+3/2}}}$$

So we have:

$$\pi\approx1/\left(12\sum_{k=0}^{x}{\frac{(-1)^{k}(6k)!(13591409+545140134k)}{(3k)!(k!)^{3}640320^{3k+3/2}}}\right)$$

For some large value of $x$. So we can write the following:

# x is the limit of the summation, increase the value of x
# for a more accurate approximation.
def Chudnovsky(x):
    sum = 0
    while x >= 0:
        sum += (((-1)**x) * factorial(6*x) * (13591409 + 545140134*k))/(factorial(3*k)*(factorial(k)**3) * (640320**(3*x + (3/2))))
        x -= 1
    sum *= 12
    return (1/sum)

However, bear in mind it's been a while since I've done Python scripting, this script was made just based on documentation I could find on the Python site, so I'm unsure if the syntax/semantics are correct, but the concept is there.

Hope this helps.

$\endgroup$
  • $\begingroup$ For purposes of efficiency, one might consider generating the factorial portions of the series through multiplicative updates; e.g. for the $k!^3$ term, start with $1$ and multiply with $k^3$ at each iteration. $\endgroup$ – J. M. is a poor mathematician Aug 8 '12 at 16:14
  • $\begingroup$ @J.M. I completely disregarded efficiency in this example, because it seemed to me that the OP was having trouble understanding the series notation etc. So I focused on having simple to a simple-to-read script. $\endgroup$ – Thomas Russell Aug 8 '12 at 16:16
  • $\begingroup$ Also, conventionally, one starts from the $k=1$ term, and goes on summing up until the ratio of the $k$-th term to the corresponding partial sum is less than, say, machine epsilon. $\endgroup$ – J. M. is a poor mathematician Aug 8 '12 at 16:16
  • 1
    $\begingroup$ The sum needs to start at $k=0$, and the $k=0$ term already yields $\pi \approx 3.1415926535897345$, i.e., about $12$ significant figures. $\endgroup$ – mjqxxxx Aug 8 '12 at 18:14
  • $\begingroup$ @Shaktal I understand this isn't optimised but I must say, I can read your python much better than the actual equation. Thanks! $\endgroup$ – Jonathan Milford Aug 8 '12 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.