#!/usr/bin/env python3 """Reproduce the finite T37 census. Default includes full census; --quick only checks structural claims and independent seam/witness sieve windows. Run from the manifest directory. C compiler and Python stdlib suffice. """ import argparse,collections,json,math,os,pathlib,resource,subprocess,sys,tempfile P=pathlib.Path(__file__).resolve().parent PR=[2,3,5,7,11,13,17,19,23,29,31,37] def validate(d): assert d['complete'] and d['D']==217929355875 assert d['period']==d['gapsum']==7420738134810 assert d['min_gap']==6 and d['max_gap']==528 assert d['maxsum'][:4]==[528,540,582,630] # Two consecutive zero residues must both be246 (their sum<=540). # The six twin offsets 0,2,246,248,492,494 cover every class mod5. assert d['zerozero_pairs']==0 assert len(d['maxsum'])==24 assert d['N0']==61460 and d['Npm']==1688653320 assert d['a_star']==d['measured_adj']==1688776240 h={int(j):v for j,v in d['hist'].items()} assert all(00 for j,v in h.items()) assert max(h)==d['j_max'] assert sum(j*v for j,v in h.items())==d['kills_from_hist']==2*d['D'] assert sum((j-1)*v for j,v in h.items())==d['a_star'] assert sum(max(j-2,0)*v for j,v in h.items())==d['t']==d['t_from_hist'] assert sum(v for j,v in h.items() if j>=2)==d['j_ge_2']==d['a_star']-d['t'] mp=min(g for g in range(6,247,6) if g%41 in (0,2,39)) first_fail=next(m for m,v in enumerate(d['maxsum'],1) if v<246*(m//2)+mp*(m%2)) assert d['j_max']<=first_fail return first_fail def direct(L,R,primes): a=bytearray(b'\1')*(R-L) for p in primes: for r in {0,(p-2)%p}: s=(r-L)%p;a[s::p]=b'\0'*len(a[s::p]) return [L+i for i,b in enumerate(a) if b] def windows(exe,d): evidence=[] for center in [6469693230,200560490130,7420738134810,2*7420738134810,41*7420738134810]: L,R=center-4096,center+4096 got=json.loads(subprocess.check_output([str(exe),'29','31','37','41','1','0','window',str(L),str(R)],timeout=60))['slots'] expected=direct(L,R,PR);assert got==expected assert [n for n in got if n%41 not in(0,39)]==direct(L,R,PR+[41]) evidence.append({'center':center,'old_slots':len(got),'new_slots':len(direct(L,R,PR+[41])),'pass':True}) w=d['witness'];old=direct(w['left'],w['right']+1,PR) assert old[0]==w['left'] and old[-1]==w['right'] assert len(old)-2==w['j']==d['j_max'] assert all(n%41 in(0,39) for n in old[1:-1]) assert direct(w['left'],w['right']+1,PR+[41])==[w['left'],w['right']] return {'windows':evidence,'witness':dict(w,kills=old[1:-1]),'pass':True} def main(): ap=argparse.ArgumentParser();ap.add_argument('--quick',action='store_true');a=ap.parse_args() d=json.loads((P/'full4539.json').read_text());J=validate(d) certificate=json.loads(subprocess.check_output([sys.executable,str(P/'pattern4539.py')])) assert certificate==json.loads((P/'pattern4539.json').read_text()) assert certificate['all_excluded'] and certificate['candidates']==6 assert d['j_max']<=4 with tempfile.TemporaryDirectory(prefix='fusion4539-') as td: exe=pathlib.Path(td)/'fusion4539' subprocess.run([os.environ.get('CC','cc'),'-O3','-pthread',str(P/'fusion4539.c'),'-o',str(exe)],check=True) evidence=windows(exe,d) if not a.quick: def limit():resource.setrlimit(resource.RLIMIT_CPU,(7200,7200)) got=json.loads(subprocess.check_output([str(exe),'29','31','37','41','4','24'],timeout=3700,preexec_fn=limit)) assert got==d,'full output mismatch' print('PASS: T37 census identities, J bound, five sieve windows and maximum-run witness'+(' (quick; full census not rerun)' if a.quick else '; full census reproduced')) print(json.dumps({'J':J,'conditional_upper_bound':4,'independent_windows':evidence},sort_keys=True)) if __name__=='__main__':main()