avatarSunny Labh

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4648

Abstract

to smaller <i>subseries</i> and multiplies their partial sums, avoiding the need to compute large factorials and powers.</li><li>Compute <b>π</b> using the <i>series</i> and the <i>functions</i> defined above, and print the result with the desired number of digits.</li></ul><h2 id="15eb">The Code</h2><div id="f0ba"><pre><span class="hljs-comment"># Import modules</span> <span class="hljs-keyword">import</span> decimal

<span class="hljs-comment"># Set the precision</span> decimal.getcontext().prec = <span class="hljs-number">100</span> <span class="hljs-comment"># adjust as needed</span>

<span class="hljs-comment"># Define constants</span> K = <span class="hljs-number">6</span> <span class="hljs-comment"># the constant term in the series</span> C = <span class="hljs-number">640320</span> <span class="hljs-comment"># the coefficient in the series</span> L = <span class="hljs-number">13591409</span> <span class="hljs-comment"># the linear term in the series</span> X = <span class="hljs-number">1</span> <span class="hljs-comment"># the exponential term in the series</span> M = <span class="hljs-number">1</span> <span class="hljs-comment"># the factorial term in the series</span> S = L <span class="hljs-comment"># the sum of the series</span> N = <span class="hljs-number">0</span> <span class="hljs-comment"># the index of the series</span>

<span class="hljs-comment"># Define functions</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">sqrt</span> (x): <span class="hljs-comment"># Square root function using Newton's method</span> <span class="hljs-keyword">return</span> x ** decimal.Decimal (<span class="hljs-string">'0.5'</span>)

<span class="hljs-keyword">def</span> <span class="hljs-title function_">arctan</span> (x): <span class="hljs-comment"># Arctangent function using Taylor series</span> x = decimal.Decimal (x) y = x <span class="hljs-comment"># the first term</span> z = x <span class="hljs-comment"># the sum of the series</span> n = <span class="hljs-number">1</span> <span class="hljs-comment"># the index of the series</span> sign = <span class="hljs-number">1</span> <span class="hljs-comment"># the sign of the terms</span> <span class="hljs-keyword">while</span> y != <span class="hljs-number">0</span>: y *= x * x <span class="hljs-comment"># the next power of x</span> n += <span class="hljs-number">2</span> <span class="hljs-comment"># the next odd number</span> sign *= -<span class="hljs-number">1</span> <span class="hljs-comment"># the next sign</span> z += sign * y / n <span class="hljs-comment"># the next term</span> <span class="hljs-keyword">return</span> z

<span class="hljs-keyword">def</span> <span class="hljs-title function_">binary_split</span>(<span class="hljs-params">a, b</span>): <span class="hljs-comment"># Binary splitting function for the series</span> <span class="hljs-keyword">if</span> b == a + <span class="hljs-number">1</span>: <span class="hljs-comment"># Base case</span> Pab = -(<span class="hljs-number">6</span>a - <span class="hljs-number">5</span>)(<span class="hljs-number">2</span>a - <span class="hljs-number">1</span>)(<span class="hljs-number">6</span>a - <span class="hljs-number">1</span>) Qab = C*<span class="hljs-number">3</span> * a**<span class="hljs-number">3</span> Rab = Pab * (L*a + K) <span class="hljs-keyword">else</span>: <span class="hljs-comment"># Recursive case</span> m = (a + b) // <span class="hljs-number">2</span> Pam, Qam, Ram = binary_split(a, m) Pmb, Qmb, Rmb = binary_split(m, b) Pab = Pam * Pmb Qab = Qam * Qmb Rab = Qmb * Ram + Pam * Rmb <span class="hljs-keyword">return</span> Pab, Qab, Rab

<span class="hljs-comment"># Calculate the digits of pi</span> P1n, Q1n, R1n = binary_split(<span class="hljs-number">1</span>, N) pi = decimal.Decimal(C) * sqrt(C) / <span class="hljs-number">12</span> * (Q1n * L + R1n) / P1n

<span class="hljs-comment"># Print pi</span> <span class="hljs-built_in">print</span>(pi)</pre></div><h2 id="6541">The Optimizations and Challenges</h2><p id="3d4d"><i>The Chudnovsky algorithm</i> is a very efficient and elegant method for computing <b>π</b>, but it also has some optimizations and challenges that need to be considered, such as:</p><p id="7357">The algorithm requires <i>high-precision arithmetic</i>, which means that the numbers and operations involved have more digits than the standard ones. This can be achieved by using special libraries or modules, such as the deci

Options

mal module in <b><i>Python</i></b>, that allow arbitrary-precision arithmetic. However, this also increases the <i>computational complexity</i> and the memory usage of the algorithm, and it may introduce some errors or limitations due to rounding or truncation.</p><p id="45e1"><b>It uses binary splitting</b>, <b><i>which is a technique that reduces the number of multiplications and divisions needed to compute the series</i></b>. Binary splitting works by dividing the <i>series</i> into smaller <i>subseries</i> and multiplying their partial sums, avoiding the need to compute large factorials and powers. However, binary splitting also requires recursion, which is a process that calls a function within itself until a base case is reached. <i>Recursion</i> can be very elegant and simple, but it can also cause some problems, such as stack overflow or infinite loops, if not implemented correctly or efficiently.</p><p id="fc1e">The algorithm is based on some of the <a href="https://www.jstor.org/stable/24988986">formulas of <b><i>Ramanujan</i></b></a>, which are very beautiful and <i>mysterious</i>, but also very difficult and obscure. Ramanujan was a self-taught mathematical genius who discovered many amazing results and identities, often without providing any proofs or explanations. His formulas for <b>π</b> are examples of <a href="https://www.jstor.org/stable/24988986"><b><i>Ramanujan-Sato series</i></b></a>, which are rapidly convergent series that involve complex numbers, modular forms, and elliptic functions. These concepts and connections are very deep and fascinating, but they are also very advanced and challenging, and they require a lot of background knowledge and understanding. <i>Read more about the mathematical genius in the following stories,</i></p><div id="d861" class="link-block"> <a href="https://www.cantorsparadise.com/ramanujans-magnificent-formula-for-pi-9801-1103-8-%CF%80-22fd7197d650"> <div> <div> <h2>Ramanujan’s Magnificent Formula for Pi: 9801/(1103√8)=π</h2> <div><h3>A brief history of nature’s most important mathematical constant</h3></div> <div><p>www.cantorsparadise.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*V01m3_IOfTfc7_u6uVcgtw.png)"></div> </div> </div> </a> </div><div id="deb2" class="link-block"> <a href="https://readmedium.com/ramanujans-lost-notebook-b9dd13720b3"> <div> <div> <h2>Ramanujan’s Lost Notebook</h2> <div><h3>Divine Inspiration or Pure Intuition?</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*XoqnigAObasUO_expiS1ig.png)"></div> </div> </div> </a> </div><h2 id="5942">Final thoughts,</h2><p id="1784">I <i>hope</i> that you have enjoyed this article and that you have learned something new and interesting about <b>π</b> and <b><i>the Chudnovsky algorithm</i></b>. If you want to try the algorithm <i>yourself</i>, you can copy and paste the code above and run it in your <i>Python interpreter.</i> You can also modify the code and experiment with different values and parameters. Have fun and happy <i>mathematical</i> coding!</p><figure id="fc6b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*sPo5AXVotz99AWW0KXkRmQ.png"><figcaption>The Chudnovsky brothers: <b>David Volfovich Chudnovsky</b> and <b>Gregory Volfovich Chudnovsky. </b>Photograph by Manuel Litran. <a href="https://www.newyorker.com/books/double-take/richard-prestons-the-mountains-of-pi">Source</a>.</figcaption></figure><p id="c5f6"><i>References:</i></p><p id="5b49">Borwein, Jonathan M., and Peter B. Borwein. <a href="https://www.jstor.org/stable/24988986">“Ramanujan and pi.”</a> <i>Scientific American</i> 258.2 (1988): 112–117.</p><p id="89d6">Borwein, Peter. “<a href="https://www.math.leidenuniv.nl/~desmit/pop/naw5-2000-01-3-254.pdf">The amazing number Pi</a>.” <i>Nieuw Archief Voor Wiskunde</i> 1 (2000): 254–258.</p><p id="df08">Thank you so much for reading. If you liked this story don’t forget <b><i>to press that clap icon as many times as you want</i></b><i>. </i>If you like my work and want to support me then you can<b> B<a href="https://www.buymeacoffee.com/thePiggsBoson">uy me a coffee</a></b> ☕️. Keep following for more such stories!</p></article></body>

The Algorithm They Used to Compute the Value of π up to a Trillion Decimal Places

The Chudnovsky algorithm and how it can be implemented in Python

One of the oldest and most challenging problems in mathematics is to compute the value of π with high accuracy and efficiency. Many methods have been devised over the centuries, ranging from simple geometric approximations, such as using polygons or circles, to sophisticated analytical formulas, such as using infinite series or integrals.

However, most of these methods are either slow, complex, or impractical, and they require a lot of computational resources and time to produce a large number of digits of π.

I’ve talked more about the computation in the following article,

In 1988, two brothers, David and Gregory Chudnovsky, published a remarkable algorithm that revolutionized the computation of π. The Chudnovsky algorithm is a fast and elegant method that can generate 14 or more digits of π for every summation step, based on some of the formulas of the Indian mathematical genius Srinivasa Ramanujan.

The Chudnovsky algorithm has been used to achieve numerous world record calculations of π, such as 2.7 trillion digits in 2009, 10 trillion digits in 2011, 22.4 trillion digits in 2016, 31.4 trillion digits in 2019, 50 trillion digits in 2020, 62.8 trillion digits in 2021, and 100 trillion digits in 2022.

In this story, I’ll try to explain the main idea and the steps of the Chudnovsky algorithm, and I will show how it can be implemented in Python code.

The algorithm is based on the following identity,

This identity is similar to some of the formulas that Ramanujan discovered for π, and it is an example of a Ramanujan-Sato series.

The main idea of this algorithm is to invert this identity and use it to compute π as follows:

The advantage of this method is that the series converges very fast, meaning that each term adds a lot of digits to the approximation of π. In fact, each term adds about 14.18 digits, which is the logarithm of the ratio of two consecutive terms. This means that, for example, to compute 100 digits of π, we only need to sum about 8 terms of the series.

The Steps

  • Define the constants and the variables that are used in the series, such as,
  • Define a function that computes the square root of a number using Newton’s method, which iteratively improves an initial guess until it reaches the desired accuracy.
  • Define a function that computes the arctangent of a number using Taylor series, which approximates the function by adding infinitely many terms of increasing powers and decreasing coefficients.
  • Then define a function that computes the terms of the series using binary splitting, which is a technique that divides the series into smaller subseries and multiplies their partial sums, avoiding the need to compute large factorials and powers.
  • Compute π using the series and the functions defined above, and print the result with the desired number of digits.

The Code

# Import modules
import decimal

# Set the precision
decimal.getcontext().prec = 100 # adjust as needed

# Define constants
K = 6 # the constant term in the series
C = 640320 # the coefficient in the series
L = 13591409 # the linear term in the series
X = 1 # the exponential term in the series
M = 1 # the factorial term in the series
S = L # the sum of the series
N = 0 # the index of the series

# Define functions
def sqrt (x):
    # Square root function using Newton's method
    return x ** decimal.Decimal ('0.5')

def arctan (x):
    # Arctangent function using Taylor series
    x = decimal.Decimal (x)
    y = x # the first term
    z = x # the sum of the series
    n = 1 # the index of the series
    sign = 1 # the sign of the terms
    while y != 0:
        y *= x * x # the next power of x
        n += 2 # the next odd number
        sign *= -1 # the next sign
        z += sign * y / n # the next term
    return z

def binary_split(a, b):
    # Binary splitting function for the series
    if b == a + 1:
        # Base case
        Pab = -(6*a - 5)*(2*a - 1)*(6*a - 1)
        Qab = C**3 * a**3
        Rab = Pab * (L*a + K)
    else:
        # Recursive case
        m = (a + b) // 2
        Pam, Qam, Ram = binary_split(a, m)
        Pmb, Qmb, Rmb = binary_split(m, b)
        Pab = Pam * Pmb
        Qab = Qam * Qmb
        Rab = Qmb * Ram + Pam * Rmb
    return Pab, Qab, Rab

# Calculate the digits of pi
P1n, Q1n, R1n = binary_split(1, N)
pi = decimal.Decimal(C) * sqrt(C) / 12 * (Q1n * L + R1n) / P1n

# Print pi
print(pi)

The Optimizations and Challenges

The Chudnovsky algorithm is a very efficient and elegant method for computing π, but it also has some optimizations and challenges that need to be considered, such as:

The algorithm requires high-precision arithmetic, which means that the numbers and operations involved have more digits than the standard ones. This can be achieved by using special libraries or modules, such as the decimal module in Python, that allow arbitrary-precision arithmetic. However, this also increases the computational complexity and the memory usage of the algorithm, and it may introduce some errors or limitations due to rounding or truncation.

It uses binary splitting, which is a technique that reduces the number of multiplications and divisions needed to compute the series. Binary splitting works by dividing the series into smaller subseries and multiplying their partial sums, avoiding the need to compute large factorials and powers. However, binary splitting also requires recursion, which is a process that calls a function within itself until a base case is reached. Recursion can be very elegant and simple, but it can also cause some problems, such as stack overflow or infinite loops, if not implemented correctly or efficiently.

The algorithm is based on some of the formulas of Ramanujan, which are very beautiful and mysterious, but also very difficult and obscure. Ramanujan was a self-taught mathematical genius who discovered many amazing results and identities, often without providing any proofs or explanations. His formulas for π are examples of Ramanujan-Sato series, which are rapidly convergent series that involve complex numbers, modular forms, and elliptic functions. These concepts and connections are very deep and fascinating, but they are also very advanced and challenging, and they require a lot of background knowledge and understanding. Read more about the mathematical genius in the following stories,

Final thoughts,

I hope that you have enjoyed this article and that you have learned something new and interesting about π and the Chudnovsky algorithm. If you want to try the algorithm yourself, you can copy and paste the code above and run it in your Python interpreter. You can also modify the code and experiment with different values and parameters. Have fun and happy mathematical coding!

The Chudnovsky brothers: David Volfovich Chudnovsky and Gregory Volfovich Chudnovsky. Photograph by Manuel Litran. Source.

References:

Borwein, Jonathan M., and Peter B. Borwein. “Ramanujan and pi.” Scientific American 258.2 (1988): 112–117.

Borwein, Peter. “The amazing number Pi.” Nieuw Archief Voor Wiskunde 1 (2000): 254–258.

Thank you so much for reading. If you liked this story don’t forget to press that clap icon as many times as you want. If you like my work and want to support me then you can Buy me a coffee ☕️. Keep following for more such stories!

Mathematics
Pi
History
Technology
Computer Science
Recommended from ReadMedium