#!/usr/bin/env python3 """Independent checker for the job-763 tight-capacity non-coverability certificate. Part A validates the method where truth is computable: on small supports the coverability of every instance is decided by enumerating ALL phase vectors, and the counting rule and the tight-capacity rigidity rule are compared against it. Part B re-derives the frozen certificate from art/input.json with its own code paths (endpoint-division support rebuild, residue-histogram capacity, direct membership test, its own exhaustive disjoint-tiling search). stdout is the artifact; timing -> stderr. Exit 0 iff every check passes. """ import itertools import json import sys import time T0 = time.time() # Byte-stable artifact on any platform: force LF newlines on stdout. sys.stdout.reconfigure(newline='\n') def log(*a): print(*a, file=sys.stderr) def is_prime(n): return n >= 2 and all(n % d for d in range(2, n) if d * d <= n) def support(p, a, L): old = [n for n in range(2, p + 1) if is_prime(n)] qs = [n for n in range(p + 1, 2 * p + 1) if is_prime(n)] D = [s for s in range(a, a + L) if all(s % r and (s + 2) % r for r in old)] return old, qs, D def M_and_argmax(D, qs): """M[q] = max_b |K(q,b)| and all maximising b, via residue histograms.""" M, am = {}, {} for q in qs: hist = [0] * q for s in D: hist[(-s) % q] += 1 hist[(-s - 2) % q] += 1 top = max(hist) if hist else 0 M[q] = top am[q] = tuple(b for b in range(q) if hist[b] == top) return M, am def brute_coverable(D, qs): """Exhaustive: enumerate every phase vector; True iff some vector covers D.""" if not D: return True masks = {} for q in qs: masks[q] = [] for b in range(q): m = 0 for i, s in enumerate(D): if (s + b) % q == 0 or (s + b + 2) % q == 0: m |= 1 << i masks[q].append(m) full = (1 << len(D)) - 1 for combo in itertools.product(*[masks[q] for q in qs]): u = 0 for m in combo: u |= m if u == full: return True return False def method_verdict(D, qs): """The two rules under test. 'noncoverable' | 'coverable' | None (silent).""" M, am = M_and_argmax(D, qs) sM = sum(M.values()) if sM < len(D): return 'noncoverable', 'counting' # F1 > 0 if sM > len(D): return None, 'uninformative' # F1 < 0 # tight: coverable iff the argmax kill sets can tile D dead = [s for s in D if not any((s + b) % q == 0 or (s + b + 2) % q == 0 for q in qs for b in am[q])] if dead: return 'noncoverable', f'dead_slot={dead[0]}' tiles = {} for q in qs: tiles[q] = [] for b in am[q]: mask = 0 for i, s in enumerate(D): if (s + b) % q == 0 or (s + b + 2) % q == 0: mask |= 1 << i tiles[q].append((b, mask)) full = (1 << len(D)) - 1 order = sorted(qs, key=lambda q: -M[q]) def rec(i, used): if used == full: return True if i == len(order): return False for b, mask in tiles[order[i]]: if mask & used: continue if rec(i + 1, used | mask): return True return False return ('coverable', 'tiling') if rec(0, 0) else ('noncoverable', 'no_tiling') def dfs_coverable(D, qs, cap=3_000_000): """Independent oracle: complete search over phase assignments. Uses no capacity, no maxima and no argmax sets -- only the clause meaning of a cover -- so it is algorithmically independent of the rules under test. Returns True/False, or None if the node cap was hit. """ if not D: return True killphases = {s: {q: ((-s) % q, (-s - 2) % q) for q in qs} for s in D} nodes = [0] def rec(unc, used): nodes[0] += 1 if nodes[0] > cap: raise RuntimeError('node cap') if not unc: return True s = min(unc) for q in qs: if q in used: continue for b in killphases[s][q]: nxt = [t for t in unc if (t + b) % q and (t + b + 2) % q] used.add(q) if rec(nxt, used): used.discard(q) return True used.discard(q) return False try: return rec(list(D), set()) except RuntimeError: return None def dfs_coverable_bounded(D, qs, cap=2_000_000): """Complete phase-assignment search with an admissible residual bound. At each node the remaining primes can kill at most sum_U max_b |K(q,b) cap U| slots of the still-uncovered set U, so a deficit proves non-coverability of U and, by monotonicity, of D. The bound is exact per prime, hence sound. The search never uses per-prime maxima of D, argmax sets, or the rigidity lemma; it decides by phases alone. """ from collections import Counter nodes = [0] def best(U, q): h = Counter() for t in U: h[(-t) % q] += 1 h[(-t - 2) % q] += 1 return max(h.values()) if h else 0 def rec(U, unused): nodes[0] += 1 if nodes[0] > cap: raise RuntimeError('node cap') if not U: return True if sum(best(U, q) for q in unused) < len(U): return False s = U[0] for q in sorted(unused): for b in ((-s) % q, (-s - 2) % q): nxt = tuple(t for t in U if (t + b) % q and (t + b + 2) % q) if rec(nxt, unused - {q}): return True return False try: return rec(tuple(sorted(D)), frozenset(qs)), nodes[0] except RuntimeError: return None, nodes[0] def part_a1(): """Exhaustive-enumeration oracle on tiny supports (all phase vectors).""" trials = 0 tight_seen = [] for p in (13, 17, 19): for L in (12, 18, 24, 33): for a in range(3, 300, 7): old, qs, D = support(p, a, L) if not D or len(qs) < 2: continue prod = 1 for q in qs: prod *= q if prod > 3_000_000: continue truth = brute_coverable(D, qs) verdict, why = method_verdict(D, qs) trials += 1 M, am = M_and_argmax(D, qs) if sum(M.values()) == len(D): tight_seen.append((p, a, L, len(D), truth, verdict, why)) if verdict != ('coverable' if truth else 'noncoverable'): return False, f'FAIL tight {p} {a} {L} {why}', tight_seen, trials elif verdict is not None and truth: return False, f'FAIL counting {p} {a} {L} {why}', tight_seen, trials if not tight_seen: return False, 'FAIL no tight instance found (rigidity rule untested)', [], trials return True, (f'{trials} instances decided by full phase enumeration; ' f'{len(tight_seen)} tight, all agreeing'), tight_seen, trials def part_a2(): """Independent DFS oracle over a wider range; reports the non-silent tally.""" stat = {'instances': 0, 'agree': 0, 'disagree': 0, 'silent': 0, 'tight': 0, 'tight_dead': 0} bad = [] for p in (13, 17, 19, 23): for L in (12, 20, 30, 45, 60, 90, 140): for a in range(3, 400): old, qs, D = support(p, a, L) if not D: continue truth = dfs_coverable(D, qs) if truth is None: continue stat['instances'] += 1 M, am = M_and_argmax(D, qs) if sum(M.values()) == len(D): stat['tight'] += 1 if any(not any((s + b) % q == 0 or (s + b + 2) % q == 0 for q in qs for b in am[q]) for s in D): stat['tight_dead'] += 1 verdict, why = method_verdict(D, qs) if verdict is None: stat['silent'] += 1 elif (verdict == 'coverable') == truth: stat['agree'] += 1 else: stat['disagree'] += 1 bad.append((p, a, L, len(D), truth, verdict, why)) return stat['disagree'] == 0, stat, bad def part_b(path): meta = json.load(open(path)) p, a, L = 97, 9409, 4432 old, qs, D = support(p, a, 32768) D = [s for s in D if s < a + meta['L']] assert (p, a, meta['L']) == (97, 9409, 4432) assert qs == meta['Q'] and D == meta['slots'] and len(D) == meta['N'] == 68 M, am = M_and_argmax(D, qs) sM = sum(M.values()) checks = [] checks.append(('frozen D reproduced', D == meta['slots'])) checks.append(('sum_q M(q) = 68', sM == 68)) checks.append(('|D| = 68', len(D) == 68)) dead = [s for s in D if not any((s + b) % q == 0 or (s + b + 2) % q == 0 for q in qs for b in am[q])] checks.append(('exactly one slot in no argmax kill set', dead == [10037])) checks.append(('that slot is 10037', dead == [10037])) # direct: for every (q, argmax b) verify 10037 is not killed; and for every # non-argmax phase the count drops (so the rigidity lemma is not vacuous) witness_ok = all((10037 + b) % q != 0 and (10037 + b + 2) % q != 0 for q in qs for b in am[q]) checks.append(('10037 killed by no argmax phase', witness_ok)) drops = 0 for q in qs: hist = [0] * q for s in D: hist[(-s) % q] += 1 hist[(-s - 2) % q] += 1 for b in range(q): if b in am[q]: continue if hist[b] < M[q]: drops += 1 checks.append(('every non-argmax phase is strictly smaller', drops == sum(q - len(am[q]) for q in qs))) # the count of "only argmax phases could be used" instances verdict, why = method_verdict(D, qs) checks.append(('method says noncoverable', verdict == 'noncoverable')) # second decision on the frozen instance itself: a complete search over phase # assignments with an admissible residual bound, using no rigidity argument dfs, dfs_nodes = dfs_coverable_bounded(D, qs) checks.append(('independent phase-assignment search also finds no cover', dfs is False)) # N=67 tight prefix: independent exhaustive tiling search D67 = D[:67] M67, am67 = M_and_argmax(D67, qs) checks.append(('N=67 prefix is tight', sum(M67.values()) == 67)) v67, why67 = method_verdict(D67, qs) checks.append(('N=67 prefix noncoverable', v67 == 'noncoverable')) report = { 'witness_slot': dead, 'why': why, 'why_N67': why67, 'dfs_frozen': dfs, 'dfs_nodes': dfs_nodes, 'sum_M': sM, 'q': qs, 'M': [M[q] for q in qs], 'argmax_counts': [len(am[q]) for q in qs], 'checks': checks, } return all(c[1] for c in checks), report if __name__ == '__main__': ok_a1, msg_a1, tight, trials = part_a1() print('PART A1 method validation against full phase enumeration (tiny supports)') print(f' {msg_a1}') if tight: print(' tight instances (p, a, L, |D|, enumerated coverable, rule verdict, rule):') for r in tight[:40]: print(f' {r}') print(f' verdict: {"PASS" if ok_a1 else "FAIL"}') print() ok_a2, stat, bad = part_a2() print('PART A2 method validation against an independent phase-assignment search') print(f' instances = {stat["instances"]} (p in 13,17,19,23)') print(f' non-silent = {stat["agree"] + stat["disagree"]} ' f'(agree {stat["agree"]}, disagree {stat["disagree"]})') print(f' silent (F1 < 0, method gives no verdict) = {stat["silent"]}') print(f' tight instances = {stat["tight"]}; of those with a dead slot = {stat["tight_dead"]}') for b in bad[:5]: print(f' DISAGREEMENT {b}') print(f' verdict: {"PASS" if ok_a2 else "FAIL"}') print() ok_a = ok_a1 and ok_a2 ok_b, rep = part_b(sys.argv[1] if len(sys.argv) > 1 else 'art/input.json') print('PART B frozen-instance certificate') print(f' q = {rep["q"]}') print(f' M(q) = {rep["M"]} sum = {rep["sum_M"]}') print(f' #argmax = {rep["argmax_counts"]}') print(f' dead slots = {rep["witness_slot"]} rule = {rep["why"]}') print(f' frozen phase search: coverable = {rep["dfs_frozen"]} ' f'({rep["dfs_nodes"]} nodes, admissible-bound pruning)') print(f' N=67 prefix = {rep["why_N67"]}') for name, val in rep['checks']: print(f' [{"ok" if val else "FAIL"}] {name}') print(f' verdict: {"PASS" if ok_b else "FAIL"}') print() print(f'OVERALL {"PASS" if (ok_a and ok_b) else "FAIL"}') log(f'checked in {time.time() - T0:.3f}s') sys.exit(0 if (ok_a and ok_b) else 1)