# tct2458.py -- the tail-count transport inequality (return #159 / attack-foldL-03-transport.js) versus the exact # survival count of return #1072: for the fold T_x -> T_x join q (q prime, q not dividing x#), with old cyclic gaps g_i, # N(th) = #{i : g_i >= th}, N_new(th) = #{new gaps >= th}, # Q_L(th) = #{i : g_i + ... + g_{i+L} >= th and g_{i+1..i+L-1} all qualify}, qualify: g = 0, +-2 (mod q) # RHS_old(th) = (q-2) N(th) + 2 sum_{L>=1} Q_L(th) [#159's loose form] # RHS_sharp(th) = sum_{g_i >= th} (q-4+c(g_i)) + 2 sum_L Q_L(th), c(g)=2 if g=0 (mod q), 1 if g=+-2, else 0 # (#1072 items 3-4: each old slot is deleted in exactly two copies, so old gap i survives in exactly q-4+c(g_i) copies.) # N_new is computed directly by streaming the q copies. Prints max_th N_new/RHS under both forms and asserts the sharp form. import sys, math, time, numpy as np def primes_upto(n): s=bytearray([1])*(n+1); s[0]=s[1]=0 for i in range(2,int(n**0.5)+1): if s[i]: s[i*i::i]=bytearray(len(s[i*i::i])) return [i for i in range(n+1) if s[i]] def tile(x): W=math.prod(primes_upto(x)); ok=np.ones(W,dtype=bool) for p in primes_upto(x): ok[0::p]=False; ok[(p-2)%p::p]=False return W, np.flatnonzero(ok).astype(np.int64) def fold_stats(x,q,Lmax=40): t0=time.time(); W,T=tile(x); D=len(T) g=np.diff(np.append(T,T[0]+W)).astype(np.int64) # cyclic gap word, length D G2=int(g.max()); TH=int(G2*(Lmax+1)) # ---- new gaps by streaming copies j=0..q-1 (positions jW+t), removing t with jW+t = 0 or -2 mod q hist_new=np.zeros(TH+1,dtype=np.int64); carry=None; first=None for j in range(q): r=(j*(W%q)+T%q)%q; keep=(r!=0)&(r!=q-2); P=T[keep]+j*W if len(P)==0: continue if first is None: first=P[0] if carry is not None: d=int(P[0]-carry); hist_new[d]+=1 d=np.diff(P); hist_new+=np.bincount(d,minlength=TH+1)[:TH+1] carry=P[-1] d=int(first+q*W-carry); hist_new[d]+=1 Nnew=np.cumsum(hist_new[::-1])[::-1] # N_new(th) for th=0..TH # ---- old tail counts, by residue class hist_old=np.bincount(g,minlength=TH+1)[:TH+1]; N=np.cumsum(hist_old[::-1])[::-1] c=np.where(g%q==0,2,np.where((g%q==2)|(g%q==q-2),1,0)) hist_w=np.bincount(g,weights=(q-4+c).astype(np.float64),minlength=TH+1)[:TH+1]; S=np.cumsum(hist_w[::-1])[::-1] # ---- Q_L loose: windows of L+1 gaps with the L-1 interior gaps qualifying qual=((g%q==0)|(g%q==2)|(g%q==q-2)).astype(np.int64) gg=np.concatenate([g,g[:Lmax+2]]); qq=np.concatenate([qual,qual[:Lmax+2]]) ps=np.concatenate([[0],np.cumsum(gg)]); pq=np.concatenate([[0],np.cumsum(qq)]) idx=np.arange(D); Qsum=np.zeros(TH+1,dtype=np.int64); Lused=0 for L in range(1,Lmax+1): inner=pq[idx+L]-pq[idx+1] # qualifying count of g_{i+1..i+L-1} ok=inner==(L-1) if not ok.any(): break Lused=L; wsum=(ps[idx+L+1]-ps[idx])[ok] h=np.bincount(wsum,minlength=TH+1)[:TH+1]; Qsum+=np.cumsum(h[::-1])[::-1] RHS_old=(q-2)*N+2*Qsum; RHS_sharp=S+2*Qsum ths=np.arange(TH+1); m=(Nnew>0)|(RHS_old>0); m[0]=False ratio_old=np.where(RHS_old>0,Nnew/np.maximum(RHS_old,1),np.inf); ratio_sh=np.where(RHS_sharp>0,Nnew/np.maximum(RHS_sharp,1),np.inf) viol_old=int(((Nnew>RHS_old)&m).sum()); viol_sh=int(((Nnew>RHS_sharp)&m).sum()) G2new=int(np.flatnonzero(hist_new).max()); cert=int(np.flatnonzero(RHS_old>0).max()) io_=np.argmax(np.where(m,ratio_old,-1)); is_=np.argmax(np.where(m,ratio_sh,-1)) # in the isolated regime (2q-2 > G2) the sharp RHS minus N_new must vanish for th > G2 iso = 2*q-2>G2 slack_above = int((RHS_sharp[G2+1:]-Nnew[G2+1:]).max()) if iso else None ident = int(np.abs(RHS_sharp[1:G2new+1]-Nnew[1:G2new+1]).max()) # sharp form vs N_new over 1..G2(new); 0 = identity base=(q-4)/(q-2); sharp_at_old=float(ratio_sh[io_]); merge_share=float(2*Qsum[io_]/RHS_old[io_]) if RHS_old[io_]>0 else 0.0 print(f"fold {x}->{q}: D={D} G2(old)={G2} G2(new)={G2new} cert(loose)={cert} Lmax_used={Lused} thetas={int(m.sum())} | " f"old form: max ratio {ratio_old[io_]:.4f} at th={io_} viol={viol_old} | sharp form: max ratio {ratio_sh[is_]:.4f} at th={is_} viol={viol_sh} | " f"isolated={iso} slack_above_G2={slack_above} max|RHS_sharp-N_new|={ident} | baseline (q-4)/(q-2)={base:.4f} sharp ratio at loose argmax={sharp_at_old:.4f} merge share of RHS there={merge_share:.4f} | ratio at G2(new): old {ratio_old[G2new]:.4f} sharp {ratio_sh[G2new]:.4f}", flush=True) # The wall time goes to stderr (2026-09-25, finding #555(e)): stdout is the artifact and must be # byte-stable across runs, so the timing field is not part of it. print(f"fold {x}->{q}: {time.time()-t0:.1f}s", file=sys.stderr, flush=True) return dict(x=x,q=q,D=D,G2=G2,G2new=G2new,cert=cert,max_old=float(ratio_old[io_]),th_old=int(io_),max_sharp=float(ratio_sh[is_]),th_sharp=int(is_),viol_old=viol_old,viol_sharp=viol_sh,isolated=bool(iso),slack_above_G2=slack_above,max_abs_sharp_minus_new=ident,baseline=base,sharp_at_loose_argmax=sharp_at_old,merge_share_at_loose_argmax=merge_share) if __name__=='__main__': import json pairs=[tuple(map(int,a.split(','))) for a in sys.argv[1:]] or [(13,17),(17,19),(19,23)] out=[fold_stats(x,q) for x,q in pairs] json.dump(out,open('tct2458-'+'-'.join(f'{x}x{q}' for x,q in pairs)+'.json','w'),indent=1)