the chudnovsky algorithm is a fast method for calculating the digits of pi based on ramanujans pi formulae https en wikipedia orgwikichudnovskyalgorithm pi constantterm multinomialterm linearterm exponentialterm where constantterm 426880 sqrt10005 the linearterm and the exponentialterm can be defined iteratively as follows lk1 lk 545140134 where l0 13591409 xk1 xk 262537412640768000 where x0 1 the multinomialterm is defined as follows 6k 3k k 3 where k is the kth iteration this algorithm correctly calculates around 14 digits of pi per iteration pi10 3 14159265 pi100 3 14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706 pi hello traceback most recent call last typeerror undefined for nonintegers pi1 traceback most recent call last valueerror undefined for nonnatural numbers 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
from decimal import Decimal, getcontext
from math import ceil, factorial


def pi(precision: int) -> str:
    

    if not isinstance(precision, int):
        raise TypeError("Undefined for non-integers")
    elif precision < 1:
        raise ValueError("Undefined for non-natural numbers")

    getcontext().prec = precision
    num_iterations = ceil(precision / 14)
    constant_term = 426880 * Decimal(10005).sqrt()
    exponential_term = 1
    linear_term = 13591409
    partial_sum = Decimal(linear_term)
    for k in range(1, num_iterations):
        multinomial_term = factorial(6 * k) // (factorial(3 * k) * factorial(k) ** 3)
        linear_term += 545140134
        exponential_term *= -262537412640768000
        partial_sum += Decimal(multinomial_term * linear_term) / exponential_term
    return str(constant_term / partial_sum)[:-1]


if __name__ == "__main__":
    n = 50
    print(f"The first {n} digits of pi is: {pi(n)}")