import math


def pi(max_k):
    """ Approximate pi using the first max_k+1 terms of
    Ramanujans series. Works for max_k up to 1228.
    """
    ans = 0
    for k in range(max_k+1):
        ans += math.factorial(4*k)*(1103+26390*k)/(
        math.factorial(k)**4 * 396**(4*k))
    return 9801/(2*math.sqrt(2)*ans)