# ladder2483.py -- QUESTION (pre-registered in channel before any run, job #2483): does the exact freshness ladder # S_K = sum_q cap_K(q) of the staircase note deviate from the Mertens-factor model M_K (route 93) by more than an # independent-thinning null of the same admissible cofactors would? For each scour prime q, the admissible cofactors m # (P^-(m) >= q, side conditions on v = q m) are enumerated exactly (as in cap2vec2468.py); the depth of each cofactor is the # index of the first pool prime q' < q (pool = first K_max scour primes, ascending) at which the freshness condition fails # (A side: v = -2 mod q'; B side: v = 2 mod q'; v = 0 mod q' is impossible), so cap_K(q) = s(q) + #{depth >= K}. # Model: M_K = sum_q cap2(q) * prod_{q' in first K, q' < q} (1 - 1/(q'-1)). Null: S_K^null = sum_q [s(q) + Binomial(n_q, P_{q,K})] # with n_q = cap2(q) - s(q) and P_{q,K} the same product; sd computed analytically and by 1000 replicates. # Output: per K: S_K, M_K, R_K = (S_K - M_K)/M_K, sd_K (null), z_K = R_K/sd_K, and the control values (S_0, K*). import sys, math, json, 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 spf_np(n): spf=np.zeros(n+1,dtype=np.int32) for i in range(2,int(n**0.5)+1): if spf[i]==0: sl=spf[i*i::i]; m=(sl==0); sl[m]=i idx=np.flatnonzero(spf==0); spf[idx]=idx return spf x=int(sys.argv[1]); KMAX=int(sys.argv[2]); seed=int(sys.argv[3]) if len(sys.argv)>3 else 2483 t0=time.time(); wheel=primes_upto(x); W=math.prod(wheel); small=[p for p in wheel if p>=7]; N=2*math.prod(p-2 for p in small) scour=[q for q in primes_upto(math.isqrt(W)) if q>x]; pool=scour[:KMAX]; tmax=(W+1)//scour[0] spf=spf_np(tmax); m_all=np.arange(tmax+1,dtype=np.int64) print(f"x={x} W={W} N={N} scour={len(scour)} pool={pool} ({time.time()-t0:.0f}s)", file=sys.stderr, flush=True) S=np.zeros(KMAX+1,dtype=np.int64); M=np.zeros(KMAX+1); V=np.zeros(KMAX+1) # exact ladder, model, null variance nq_list=[]; P_list=[] for q in scour: tA=(W-1)//q; tB=(W+1)//q; s_q=1 if q%30 in (11,13,17,19) else 0 ok=spf[2:tB+1]>=q; mm=m_all[2:tB+1][ok]; v=q*mm; r30=v%30 A=(r30==11)|(r30==17); B=(r30==13)|(r30==19) for p in small: vp=v%p; A&=vp!=p-2; B&=vp!=2 A&=mm<=tA vA=v[A]; vB=v[B] depthA=np.full(len(vA),KMAX,dtype=np.int64); depthB=np.full(len(vB),KMAX,dtype=np.int64) pf=[qq for qq in pool if qq= K} for K in range(KMAX+1): S[K]+=s_q+tail[K] P=1.0 for qq in pf[:K]: P*=1-1/(qq-1) M[K]+=s_q+n_q*P; V[K]+=n_q*P*(1-P) nq_list.append(n_q); P_list.append([math.prod(1-1/(qq-1) for qq in pf[:K]) for K in range(KMAX+1)]) Kstar=next((K for K in range(KMAX+1) if S[K]