#!/usr/bin/env python3 """Job #1910 verification package. emit -- recompute everything from scratch and write census_results_1910.json check -- recompute everything from scratch and compare against the published census_results_1910.json, then run a negative control (a corrupted copy of the target must be detected). Exit 0 iff both hold. The claim checked: on the exact primorial tile T_23 (P = 23# = 223 092 870, D = 7 952 175) and on the ladder tile T_29 (P_29 = 6 469 693 230, D = 214 708 725), the fusion index census over one full period of the folded word is exactly predicted by the Criterion identities a* = 2*N0 + N+- (adjacent-kill pairs) #j>=2 cells = a* - t, j_max >= 3 <=> t > 0 with N0 = #{old gaps g = 0 mod p}, N+- = #{g = +-2 mod p}, and t = #{cyclic i : (g_i,g_{i+1}) in {(0,0),(0,+-2),(+-2,0),(2,-2),(-2,2)} mod p}. """ import sys, os, json, copy import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) from fusion_census import build_tile, census_from_res, criterion # noqa: E402 import reading_b as rb # noqa: E402 from witness_and_controls import sieve_window, shift_union_window # noqa: E402 TARGET = os.path.join(HERE, 'census_results_1910.json') FOLD_PRIMES = [p for p in range(29, 200) if all(p % q for q in range(2, int(p ** .5) + 1))] def compute(): slots, Pm, g = build_tile() D = int(slots.size) out = {} out['tile_T23'] = { 'P': int(Pm), 'D': D, 'min_gap': int(g.min()), 'max_gap': int(g.max()), 'wrap_gap': int(g[-1]), 'sum_gaps': int(g.sum()), 'all_slots_odd': bool(np.all(slots % 2 == 1)), 'all_gaps_mult6': bool(np.all(g % 6 == 0)), } # full fold-prime sweep on T_23 (one full period p*P_23 each) rows = {} total_positions = 0 for p in FOLD_PRIMES: 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) j3p = sum(v for j, v in hist.items() if j >= 3) adj = sum((j - 1) * v for j, v in hist.items()) rows[str(p)] = { 'period_positions': p * int(Pm), 'kills': kills, 'gaps_j_ge_1': int(sum(hist.values())), 'j1': j1, 'j2': j2, 'j_ge_3': j3p, 'max_j': jmax, 'N0': N0, 'Npm': Npm, 'a_star': pred_adj, 't': t, 'measured_adj': adj, 'pred_j_ge_2': pred_adj - t, 'measured_j_ge_2': j2 + j3p, } total_positions += p * int(Pm) out['fold_sweep_T23'] = {'primes': FOLD_PRIMES, 'rows': rows, 'total_period_positions': total_positions} # window controls (folded word vs independent sieve) ctrl = [] for lo, hi in [(0, 1 << 24), (5 * Pm - 4096, 5 * Pm + 4096), (30 * Pm - 5000, 30 * Pm)]: A = sieve_window(max(lo, 0), hi) B = shift_union_window(slots, Pm, 31, max(lo, 0), hi) ctrl.append({'window': [max(lo, 0), hi], 'sieve_count': int(A.size), 'identical': bool(A.size == B.size and np.array_equal(A, B))}) for lo, hi in [(0, 1 << 24)]: A = sieve_window(lo, hi, primes=(3, 5, 7, 11, 13, 17, 19, 23, 29)) B = shift_union_window(slots, Pm, 29, lo, hi) ctrl.append({'window': [lo, hi], 'fold_p': 29, 'sieve_count': int(A.size), 'identical': bool(A.size == B.size and np.array_equal(A, B))}) out['fold_window_controls'] = ctrl # j = 3 witness at T_23 -> p = 31 p = 31 r = g % p ok = (r == 0) | (r == 2) | (r == p - 2) both = ok[:-1] & ok[1:] bad = ((r[:-1] == 2) & (r[1:] == 2)) | ((r[:-1] == p - 2) & (r[1:] == p - 2)) i = int(np.flatnonzero(both & ~bad)[0]) start = i alive = ~((slots % p == 29) | (slots % p == 27)) while start - 1 >= 0 and not alive[start - 1]: start -= 1 end = i + 2 while end + 1 < D and not alive[end + 1]: end += 1 k = 21 off = k * int(Pm) out['witness_j3_T23_p31'] = { 'x': 23, 'p': 31, 'strip_k': k, 'Y': int(slots[start - 1]) + off, 'fused_gap': [int(slots[start - 1]) + off, int(slots[end + 1]) + off], 'fused_gap_length': int(slots[end + 1] - slots[start - 1]), 'j': end - start + 1, 'killed_slots': [int(x) + off for x in slots[start:end + 1]], 'killed_absolute_residues_mod_p': [int((x + off) % p) for x in slots[start:end + 1]], 'old_gaps_merged': [int(x) for x in g[start - 1:end + 1]], } # ladder reading T_29 -> p = 31, one full period P_31 gaps29, r0, D29 = rb.build_t29_gaps(slots, Pm) res29 = rb.res_mod(gaps29, r0, p) hist, kills, jmax = census_from_res(res29, D29, rb.P29, p) N0, Npm, pred_adj, tri, okc, t = criterion(gaps29, p) j3 = sum(v for j, v in hist.items() if j >= 3) j2 = hist.get(2, 0) W = 1 << 24 cs = np.cumsum(gaps29[:1_200_000].astype(np.int64)) rec = r0 + np.concatenate(([0], cs[:int(np.searchsorted(cs, W))])) rec = rec[rec < W] direct29 = sieve_window(0, W, primes=(3, 5, 7, 11, 13, 17, 19, 23, 29)) fold31 = np.array([x for x in direct29.tolist() if x % 31 not in (0, 29)], dtype=np.int64) direct31 = sieve_window(0, W, primes=(3, 5, 7, 11, 13, 17, 19, 23, 29, 31)) out['ladder_T29_p31'] = { 'D29': int(D29), 'P29': rb.P29, 'P31': rb.P31, 'max_gap': int(gaps29.max()), 'sum_gaps': int(gaps29.sum()), 'period_positions': rb.P31, 'kills': kills, 'kills_expected': 2 * int(D29), 'gaps_j_ge_1': int(sum(hist.values())), 'j2': j2, 'j_ge_3': j3, 'max_j': jmax, 'N0': N0, 'Npm': Npm, 'a_star': pred_adj, 't': t, 'measured_adj': sum((j - 1) * v for j, v in hist.items()), 'pred_j_ge_2': pred_adj - t, 'measured_j_ge_2': j2 + j3, 't29_reconstruction_vs_sieve_identical': bool(rec.size == direct29.size and np.array_equal(rec, direct29)), 't31_window_vs_sieve_identical': bool(fold31.size == direct31.size and np.array_equal(fold31, direct31)), 't31_window_count': int(fold31.size), } return out def main(): mode = sys.argv[1] if len(sys.argv) > 1 else 'check' got = compute() if mode == 'emit': with open(TARGET, 'w') as f: json.dump(got, f, indent=1, sort_keys=True) print('wrote', TARGET) return 0 exp = json.load(open(TARGET)) def walk(a, b, diffs, path=''): if isinstance(a, dict) and isinstance(b, dict): for k in sorted(set(a) | set(b)): if k not in a: diffs.append(f'{path}/{k}: missing in recomputation') elif k not in b: diffs.append(f'{path}/{k}: missing in target') else: walk(a[k], b[k], diffs, f'{path}/{k}') elif a != b: diffs.append(f'{path}: target={b!r} recomputed={a!r}') diffs = [] walk(got, exp, diffs) bad = copy.deepcopy(exp) bad['fold_sweep_T23']['rows']['31']['max_j'] = 2 nd = [] walk(got, bad, nd, 'NEGCTL') neg_ok = bool(nd) print('recomputed == target :', not diffs) for d in diffs[:10]: print(' DIFF', d) print('negative control detected:', neg_ok) if diffs: print('CHECK FAIL') return 1 if not neg_ok: print('CHECK FAIL (negative control not detected)') return 1 print('CHECK PASS') return 0 if __name__ == '__main__': sys.exit(main())