def A(n,stack=1):##I ran this program with pypy3 which ran much faster than python3 and it took over a day to compute n=4, so n=5 would take months #stack=0 produces stepping stones sequence (A337663), stack=1 Stacking stones sequence (A340000) ##ENTER YOUR FILE NAMES FIRST ON LINES 90+91!!!## gridsize=26 ##gridsize to test with steppingdist=5 ##distance between 2 different 1's which can't be connected by adding numbers in the cells between them. I think 5 is safe. if n<2:n=2 oo,nx,kk,mm=2,(2**n)*8+2,2,2 ##oo=counter for the 3 configurations of starting 1's, nx=(over) estimate of a(n) to initialise lists, kk=numerate maximum solutions, mm=maximum a(n) so far while n<=255: g=[[0 for xp in range(gridsize)] for yp in range(gridsize)] dim=[[len(g)//2-2,len(g)//2,len(g)//2-2,len(g)//2-2+oo] for xp in range(nx+n-4)] p=[[0,0] for yp in range(nx+n-4)] c=[[-1,-1] for xp in range(nx+n-4)] r=[0 for xp in range(nx+n-4)] g[len(g)//2-2][len(g)//2-2],g[len(g)//2][len(g)//2-2+oo],i,xx,yy=1,1,4-n,1,1 while i>3-n: ii,ip,io=i+n-4,i+n-5,i+n-3 if i<2: xx=len(g) if c[ii][0]>-1: xx=c[ii][0] while xx>0: x=(len(g)+(len(g)//2-xx))%len(g) if abs(x-(len(g)//2-1))>steppingdist+1: iz=ii-1 while iz>=0: if abs(x-p[iz][0])<=steppingdist:break iz-=1 if iz>-1: xx-=1 continue if (ii!=0 or x>=len(g)//2-1) and x<=len(g)-(5+ii*2) and x>=(5+ii*2): yy=len(g) if c[ii][1]>-1 and xx==c[ii][0]: yy=c[ii][1] if yy<1: xx-=1 continue while yy>0: y=(len(g)+(len(g)//2-yy))%len(g) if abs(y-(len(g)//2-1))>steppingdist+1: iz=ii-1 while iz>=0: if abs(y-p[iz][1])<=steppingdist:break iz-=1 if iz>-1: yy-=1 continue if y<=len(g)-(5+ii*2) and y>=(5+ii*2): if g[x][y]+g[x-1][y-1]+g[x-1][y]+g[x-1][y+1]+g[x][y-1]+g[x][y+1]+g[x+1][y-1]+g[x+1][y]+g[x+1][y+1]==0: g[x][y],dim[io][0],dim[io][1],dim[io][2],dim[io][3]=1,min(dim[ii][0],x),max(dim[ii][1],x),min(dim[ii][2],y),max(dim[ii][3],y) p[ii],c[ii]=[x,y],[xx,yy-1] xx=-1 break yy-=1 xx-=1 else: xx=dim[ii][1]+1 if c[ii][0]>-1: xx=c[ii][0] while xx>=dim[ii][0]-1: y=dim[ii][3]+1 if c[ii][1]>-1 and xx==c[ii][0]: y=c[ii][1] if y=dim[ii][2]-1: if ((xx==len(g)//2-1 and y>=len(g)//2-3 and y<=len(g)//2-1) or i>2) and (stack==1 or g[xx][y]==0): if g[xx-1][y-1]+g[xx-1][y]+g[xx-1][y+1]+g[xx][y-1]+g[xx][y+1]+g[xx+1][y-1]+g[xx+1][y]+g[xx+1][y+1]==i: r[ii]=g[xx][y] g[xx][y]=i dim[io][0],dim[io][1],dim[io][2],dim[io][3]=min(dim[ii][0],xx),max(dim[ii][1],xx),min(dim[ii][2],y),max(dim[ii][3],y) p[ii],c[ii]=[xx,y],[xx,y-1] xx=-1 break y-=1 xx-=1 if xx>-1: i-=1 if i>(n-1)*(10+stack)+4: ##to print only solutions close to maximum (16,27,38,49...) if i==mm: kk=kk+1 elif i>mm: kk=1 mm=i print(str(n)+"__"+str(i)+"/"+str(kk)) f=0 ##!!! THIS PROGRAM SAVES TO FILES !!! ##!!! CREATE 2 EMPTY FILES FIRST AND ENTER THEIR PATHS HERE !!! if i==mm: f = open('/path/file.txt', 'a') ##file for solutions that are new records or equal to it else: f = open('/path/file2.txt', 'a') ##file for all other big solutions f.write(str(n)+"__"+str(i)+"/"+str(kk)+"\n") y=dim[ii][2] while y<=dim[ii][3]: x=dim[ii][0] while x<=dim[ii][1]: if g[x][y]==0: print(" ",end='') f.write(" ") else: if g[x][y]<10: print(" ",end='') f.write(" ") print(" "+str(g[x][y]),end='') f.write(" "+str(g[x][y])) x+=1 print("") f.write("\n") y+=1 if stack==1: jj,st,rr=ip,"",r[:] while jj>n-2: if rr[jj]>0: if st=="":st=str(jj-n+4)+" -> "+str(rr[jj]) else: st+=", "+str(jj-n+4)+" -> "+str(rr[jj]) jjj=rr[jj]+n-4 while rr[jjj]>0: rj,st=rr[jjj],st+" -> "+str(rr[jjj]) rr[jjj]=0 jjj=rj+n-4 jj-=1 if st!="": print("") f.write("\n") print(st) f.write(st+"\n") print("") f.write("\n") f.close() g[p[ip][0]][p[ip][1]],c[ii]=r[ip],[-1,-1] else: i+=1 oo-=1 if oo<0: oo,n=2,n+1 nx,kk,mm=(2**n)*8+2,2,2