# Run with sage --nodotsage A014777.spyx. Not A014777.spyx.txt

from mpmath import mp

def pi():
    return "3."+getpi()

# From https://en.wikipedia.org/wiki/Chudnovsky_algorithm
# Adapted

def getpi(prec1):
    disp=prec1
    prec1 += 1
    prec_ratio=14.18
    mp.dps = prec1
    if disp==0:
        pi_str=''
    elif disp==1:
        pi_str='1'
    else:
        maxK = int(mp.ceil(prec1/prec_ratio)) + 1
        k1, m, l, x, s = 6, mp.mpf(1), mp.mpf(13591409), mp.mpf(1), mp.mpf(13591409)
        for j in range(1, maxK + 1):
            m = ((k1 ** 3) - (k1<<4)) * (m / (j ** 3))
            l += mp.mpf(545140134)
            x *= mp.mpf(-262537412640768000)
            s += m*l/ x
            k1 += 12
        pi2 = (mp.mpf(426880) * (mp.sqrt(mp.mpf(10005))))/s       
        pi_str=str(pi2)
        pi_str = pi_str.replace('3.', '')
        pi_str = pi_str[:disp] 
    return pi_str


def firstA014777(wanted_values,lst=[]):
    finished = False
    counter = len(lst)
    precition=10*wanted_values
    while not finished:
        pi1 = getpi(precition)
        for i in range(counter, wanted_values+counter):
            p = pi1.find(str(i))
            if p != -1:
                lst.append(p+1)
            else:
                counter = i
                finished = False
                precition *= 10
                break
            finished = True
    return lst

def A014777(n):
    return firstA014777(n,[])[-1]

def writetofile(l):
    ln=(len(l))
    f1=open("b014777.txt","w")
    f2=open("a014777.txt","w")
    for i in range(ln):
        line1=str(i)+" "+str(l[i])+"\n"
        line2=str(l[i])+", "
        f1.write(line1)
        f2.write(line2)
    f1.close()
    f2.close()

print("===Start===")
writetofile(firstA014777(1000))
print("===End===")