maths.chudnovsky_algorithm

Attributes

n

Functions

pi(→ str)

The Chudnovsky algorithm is a fast method for calculating the digits of PI,

Module Contents

maths.chudnovsky_algorithm.pi(precision: int) str

The Chudnovsky algorithm is a fast method for calculating the digits of PI, based on Ramanujan’s PI formulae.

https://en.wikipedia.org/wiki/Chudnovsky_algorithm

PI = constant_term / ((multinomial_term * linear_term) / exponential_term)

where constant_term = 426880 * sqrt(10005)

The linear_term and the exponential_term can be defined iteratively as follows:

L_k+1 = L_k + 545140134 where L_0 = 13591409 X_k+1 = X_k * -262537412640768000 where X_0 = 1

The multinomial_term is defined as follows:
6k! / ((3k)! * (k!) ^ 3)

where k is the k_th iteration.

This algorithm correctly calculates around 14 digits of PI per iteration

>>> pi(10)
'3.14159265'
>>> pi(100)
'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706'
>>> pi('hello')
Traceback (most recent call last):
    ...
TypeError: Undefined for non-integers
>>> pi(-1)
Traceback (most recent call last):
    ...
ValueError: Undefined for non-natural numbers
maths.chudnovsky_algorithm.n = 50