#!/usr/bin/env python3 """Job #1910 / route 76: fusion-index census on the exact tile T_x at fold p. Frozen conventions (served producer docs/research/localized-03-merge-lemma.js; return #1012; AGENTS.md): T_x = { r in [0, P_x) : gcd(r,P_x) = gcd(r+2,P_x) = 1 }, P_x = prod_{q<=x} q D(T_x) = #T_x = prod_{3<=q<=x}(q-2) (q = 2 skipped: every slot is odd) fold p = a prime with p not dividing P_x; a KILL is a T_x slot with r = 0 or r = -2 (mod p) fused word = T_x with the kills removed fusion index j of a fused (new) gap = number of kills strictly inside it = number of old gaps it merges, minus one = length of a maximal run of consecutive killed T_x slots. Instrument -- the SHIFT-UNION identity: p does not divide P_x, so the fused word is periodic mod p*P_x. In strip k of [0, p*P_x) the kills are the local slots of class {a_k, a_k-2} (mod p), a_k = (-k*P_x) mod p, and as k runs over 0..p-1 the a_k run over all of Z_p. Hence p censuses of the single in-memory tile word T_x ARE the census over the full period p*P_x. Run counting is exact and cyclic; blocks are the p strips. Criterion quantities (all decided from the old-gap multiset / gap sequence): a* = 2*N0 + N+- EXACT number of ADJACENT KILL PAIRS over the full period, where N0 = #{gaps g = 0 (mod p)} and N+- = #{gaps g = +-2 (mod p)}. Proof: two consecutive slots r, r+g are both killed in exactly 2 strips if g=0 (mod p), 1 strip if g=+-2 (mod p), 0 otherwise; the count is independent of the strip because every residue class is realised. t = #{cyclic i : (g_i, g_{i+1}) in {(0,0),(0,+-2),(+-2,0),(2,-2),(-2,2)} (mod p)} = #{i : both gaps in {0,+-2}, not both +2, not both -2} = sum over runs of length L>=3 of (L-2) [each such triple lies in a long run] EXACT predictions from the old-gap sequence alone: #j>=2 cells = a* - t j_max >= 3 <=> t > 0. (j_max>=3 iff a triple of consecutive slots has residues in a 2-element set {c,c-2}; the two classes killed in one strip.) """ import sys, json, time import numpy as np PRIMES23 = (2, 3, 5, 7, 11, 13, 17, 19, 23) def build_tile(xmax=23): """Exact T_x word over [0,P_x): slots, period, cyclic gap array.""" Pm, primes = 1, [] for q in PRIMES23: if q > xmax: break Pm *= q primes.append(q) ok = np.ones(Pm, dtype=bool) for q in primes: ok[0::q] = False # gcd(r,P)=1 ok[(q - 2) % q::q] = False # gcd(r+2,P)=1 slots = np.flatnonzero(ok).astype(np.int64) del ok g = np.empty(slots.size, dtype=np.int64) g[:-1] = slots[1:] - slots[:-1] g[-1] = Pm - slots[-1] + slots[0] return slots, Pm, g def census_from_res(res, D, Pm, p): """Exact fusion-index histogram over the FULL period p*Pm. res[i] = (position of the i-th slot) mod p, in cyclic word order. Returns (hist: dict j->count, kills, max_j). """ hist = np.zeros(1, dtype=np.int64) carry = 0 head_run = None total = 0 nblocks = p for k in range(nblocks): a = (-k * Pm) % p c2 = (a - 2) % p b = (res == a) | (res == c2) if not b.any(): if carry: _bump(hist, carry) carry = 0 continue assert not b.all(), 'unexpected all-killed strip' total += int(b.sum()) d = np.diff(np.concatenate(([False], b, [False])).view(np.int8)) st = np.flatnonzero(d == 1) en = np.flatnonzero(d == -1) lens = en - st kf = bool(b[0]) kl = bool(b[-1]) first_run = kf and st[0] == 0 last_run = kl and (st[-1] + lens[-1] == D) if not first_run and carry: _bump(hist, carry) carry = 0 merged = None if first_run: merged = carry + int(lens[0]) if k == 0: head_run = merged # defer: merges with the final carry keep = np.ones(lens.size, dtype=bool) if first_run: keep[0] = False if last_run: keep[-1] = False il = lens[keep] if il.size: bc = np.bincount(il) if bc.size > hist.size: hist = np.concatenate((hist, np.zeros(bc.size - hist.size, dtype=np.int64))) hist[:bc.size] += bc if last_run: if merged is not None and not (k == 0 and merged is head_run): hist = _bump(hist, merged) carry = int(lens[-1]) else: if merged is not None and not (k == 0 and merged is head_run): hist = _bump(hist, merged) carry = 0 if head_run is not None: hist = _bump(hist, head_run + carry) elif carry: hist = _bump(hist, carry) nz = np.flatnonzero(hist) return {int(j): int(hist[j]) for j in nz}, total, int(nz[-1]) def _bump(hist, j): if j >= hist.size: hist = np.concatenate((hist, np.zeros(j + 1 - hist.size, dtype=np.int64))) hist[j] += 1 return hist def criterion(g, p): """Criterion quantities decided from the old-gap sequence.""" r = g % p N0 = int(np.count_nonzero(r == 0)) Npm = int(np.count_nonzero((r == 2) | (r == p - 2))) pred_adj = 2 * N0 + Npm # local j>=3 test on consecutive gaps ok = (r == 0) | (r == 2) | (r == p - 2) both = ok[:-1] & ok[1:] # exclude (+2,+2) and (-2,-2) bad = ((r[:-1] == 2) & (r[1:] == 2)) | ((r[:-1] == p - 2) & (r[1:] == p - 2)) tri = both & ~bad # cyclic pair (last, first) okc = ((r[-1] == 0) | (r[-1] == 2) | (r[-1] == p - 2)) and \ ((r[0] == 0) | (r[0] == 2) | (r[0] == p - 2)) and \ not ((r[-1] == 2 and r[0] == 2) or (r[-1] == p - 2 and r[0] == p - 2)) tri = int(np.count_nonzero(tri)) okc = bool(okc) t = tri + (1 if okc else 0) return N0, Npm, pred_adj, tri, okc, t def run_fold(slots, Pm, g, p, t0): D = slots.size res = (slots % p).astype(np.int64) hist, kills, jmax = census_from_res(res, D, Pm, p) N0, Npm, pred_adj, tri, okc, t = criterion(g, p) j1 = hist.get(1, 0) j2 = hist.get(2, 0) j3 = sum(v for j, v in hist.items() if j >= 3) adj = sum((j - 1) * v for j, v in hist.items()) return { 'x': 23, 'p': p, 'period_positions': p * Pm, 'slots_in_period': int(p * D), 'kills': kills, 'gaps_j_ge_1': sum(hist.values()), 'j1': j1, 'j2': j2, 'j_ge_3': j3, 'max_j': jmax, 'hist': {str(k): v for k, v in sorted(hist.items())}, 'N0': N0, 'Npm': Npm, 'pred_adj': pred_adj, 'measured_adj': adj, 'three_run_pairs': tri, 'three_run_wrap': okc, 't': t, 'pred_j_ge_2': pred_adj - t, 'measured_j_ge_2': j2 + j3, 'pred_has_j_ge_3': bool(t > 0), 'secs': round(time.time() - t0, 1), } if __name__ == '__main__': cmd = sys.argv[1] if len(sys.argv) > 1 else 'word' t0 = time.time() slots, Pm, g = build_tile() D = slots.size if cmd == 'word': print(json.dumps({ 'P': Pm, 'D': int(D), 'min_gap': int(g.min()), 'max_gap': int(g.max()), 'sum_gaps': int(g.sum()), 'wrap_gap': int(g[-1]), 'first_slot': int(slots[0]), 'last_slot': int(slots[-1]), 'all_odd': bool(np.all(slots % 2 == 1)), 'all_gaps_mult6': bool(np.all(g % 6 == 0)), 'build_secs': round(time.time() - t0, 1), }, indent=1)) elif cmd == 'fold': for p in [int(v) for v in sys.argv[2:]]: print(json.dumps(run_fold(slots, Pm, g, p, t0)), flush=True) elif cmd == 'sweep': lo, hi = int(sys.argv[2]), int(sys.argv[3]) for p in range(lo, hi + 1): if p < 3 or any(p % q == 0 for q in range(2, int(p ** .5) + 1)): continue print(json.dumps(run_fold(slots, Pm, g, p, t0)), flush=True) print('SWEEP_DONE', file=sys.stderr) print(f'# {time.time()-t0:.1f}s', file=sys.stderr)