#!/usr/bin/env python3 """Independent checker for job #1068 (route 13 rev5): rebuild everything from scratch. Nothing here trusts the producer. It re-derives, in its own plain-Python code: * the admissible slot list of each (p, a) window by its own coprimality test (no sieve, no imported toolkit), the first prefix with F1 > 0 and its predecessor, and both F1 values -- so "member is counting-silent" is re-established rather than read; * each member's slot-list sha256 and compares it to the artifact's recorded hash; * every k-fold union from the member slot lists, its sha256, its F1 under both the plain integer formula and the piecewise-linear max-sum expression, and the exact uniform-weight inequality sum_q max_b W(q,b) < sum_s w_s that is the whole content of a decisive certificate; * the per-k min/mean F1/k, the monotonicity and positivity verdicts, and the outcome string. Exit status is 0 only if every check passes and every counted quantity matches the artifact. """ import hashlib import itertools import json import sys from pathlib import Path ART = Path(sys.argv[1] if len(sys.argv) > 1 else 'kfold1068.json') SRC = Path(sys.argv[2] if len(sys.argv) > 2 else '../job1064/pairs1064.json') CAP = 20000 fails = [] nchk = 0 def check(ok, label): global nchk nchk += 1 if not ok: fails.append(label) print('FAIL', label) return ok def is_prime(n): if n < 2: return False d = 2 while d * d <= n: if n % d == 0: return False d += 1 return True def slots_of(p, a, cap): """Independent admissible-slot generator over the interval [a, a+cap): the doc convention -- s is admissible iff s and s+2 are both coprime to p#, i.e. to every prime r <= p. This is a separate plain-Python implementation, not the toolkit's.""" old = [r for r in range(2, p + 1) if is_prime(r)] return [s for s in range(a, a + cap) if all(s % r and (s + 2) % r for r in old)] def f1_plain(D, qs): """F1 straight from the definition: |D| - sum_q max_b |K_D(q,b)|, integers only.""" total = 0 for q in qs: best = 0 for b in range(q): c = 0 for s in D: if (s + b) % q == 0 or (s + b + 2) % q == 0: c += 1 if c > best: best = c total += best return len(D) - total def f1_slotmax(D, qs): """Same quantity through the per-prime argmax expression (m = max_b W) as a second path.""" Ds = set(D) total = 0 for q in qs: mx = 0 for b in range(q): c = sum(1 for s in Ds if (s + b) % q == 0 or (s + b + 2) % q == 0) mx = max(mx, c) total += mx return len(D) - total def weights_decisive(D, qs, w): """sum_q max_b W(q,b) < sum_s w_s with the given weights: the certificate inequality.""" lhs = 0 for q in qs: mx = 0 for b in range(q): mx = max(mx, sum(ws for s, ws in zip(D, w) if (s + b) % q == 0 or (s + b + 2) % q == 0)) lhs += mx return lhs, sum(w) art = json.loads(ART.read_text(encoding='utf-8')) src = json.loads(SRC.read_text(encoding='utf-8')) # ---------------------------------------------------------------- members, from scratch mems = {} for m in art['members']: p, a = m['p'], m['a'] qs = [n for n in range(p + 1, 2 * p + 1) if is_prime(n)] slots = slots_of(p, a, CAP) nf = None for N in range(1, len(slots) + 1): if f1_plain(slots[:N], qs) > 0: nf = N break D = slots[:nf - 1] sha = hashlib.sha256(','.join(map(str, D)).encode()).hexdigest() check(sha == m['slot_sha256'], 'member sha p=%d a=%d' % (p, a)) check(len(D) == m['N_member'], 'member N p=%d a=%d' % (p, a)) check(nf == m['N_F'], 'member N_F p=%d a=%d' % (p, a)) fm = f1_plain(D, qs) check(fm == m['F1_member'], 'member F1 p=%d a=%d (%d vs %d)' % (p, a, fm, m['F1_member'])) check(fm <= 0, 'member silent p=%d a=%d' % (p, a)) check(f1_plain(slots[:nf], qs) > 0, 'N_F is the FIRST positive prefix p=%d a=%d' % (p, a)) check(f1_slotmax(D, qs) == fm, 'member F1 two paths p=%d a=%d' % (p, a)) mems[(p, a)] = {'qs': qs, 'D': D} ref_pairs = {(r['p'], tuple(r['pair_starts'])): r['F1_uniform'] for r in src['pairs']} by_k = {} rows = [] for rec in art['unions']: p, combo = rec['p'], tuple(rec['members']) qs = mems[(p, combo[0])]['qs'] N = min(len(mems[(p, a)]['D']) for a in combo) parts = [mems[(p, a)]['D'][:N] for a in combo] for x, y in itertools.combinations(parts, 2): check(not (set(x) & set(y)), 'members disjoint p=%d %s' % (p, combo)) U = sorted(s for part in parts for s in part) sha = hashlib.sha256(','.join(map(str, U)).encode()).hexdigest() check(sha == rec['U_sha256'], 'union sha p=%d k=%d %s' % (p, rec['k'], combo)) check(len(U) == rec['union_size'] and N == rec['common_N'], 'union size p=%d %s' % (p, combo)) f1 = f1_plain(U, qs) check(f1 == rec['F1_union'], 'union F1 p=%d k=%d %s (%d vs %d)' % (p, rec['k'], combo, f1, rec['F1_union'])) check(f1_slotmax(U, qs) == f1, 'union F1 two paths p=%d %s' % (p, combo)) check(abs(f1 / rec['k'] - rec['F1_per_k']) < 1e-12, 'F1/k p=%d %s' % (p, combo)) check(rec['gain_over_members'] == f1 - rec['member_F1_sum'], 'gain p=%d %s' % (p, combo)) check(rec['member_F1_sum'] == sum(next(m['F1_member'] for m in art['members'] if m['p'] == p and m['a'] == a) for a in combo), 'member F1 sum p=%d %s' % (p, combo)) check(f1 >= rec['member_F1_sum'], 'subadditivity F1_union >= sum members p=%d %s' % (p, combo)) cert = rec.get('certificate') if rec['rule'] == 'uniform_weights': check(cert is not None and cert['slots'] == U, 'certificate slots p=%d %s' % (p, combo)) w = cert['weights'] if cert else [] check(w == [1] * len(U), 'certificate weights are all ones p=%d %s' % (p, combo)) check(cert['ratio'] == [len(U) - f1, len(U)], 'certificate ratio p=%d %s' % (p, combo)) lhs, rhs = weights_decisive(U, qs, w) check(lhs < rhs, 'STRICT inequality p=%d %s (%d < %d)' % (p, combo, lhs, rhs)) check(rhs - lhs == f1, 'slack equals F1 p=%d %s' % (p, combo)) check(f1 > 0, 'decided union has F1 > 0 p=%d %s' % (p, combo)) check(rec.get('independent_check') is True, 'producer independent_check p=%d %s' % (p, combo)) else: check(False, 'union not decided by uniform weights: rule=%s p=%d %s' % (rec['rule'], p, combo)) if rec['k'] == 2: ref = ref_pairs.get((p, tuple(sorted(combo)))) check(ref == f1, 'cross-check vs #439 p=%d %s (%s vs %s)' % (p, combo, ref, f1)) check(rec.get('F1_cross_check_439') == ref, 'recorded cross-check p=%d %s' % (p, combo)) by_k.setdefault((p, rec['k']), []).append(rec['F1_per_k']) rows.append(rec) check(len(rows) == 33, 'union count 18+12+3') check(len(ref_pairs) == len([r for r in art['unions'] if r['k'] == 2]), 'every pair in the artifact appears in #439') # ---------------------------------------------------------------- fit and verdict, recomputed ok_all = True for p in art['primes']: for k in sorted({r['k'] for r in rows}): vals = by_k.get((p, k), []) f = art['fit'][str(p)] if vals: check(abs(min(vals) - f['F1_per_k_min'][str(k)]) < 1e-12, 'fit min p=%d k=%d' % (p, k)) check(abs(sum(vals) / len(vals) - f['F1_per_k_mean'][str(k)]) < 1e-12, 'fit mean p=%d k=%d' % (p, k)) check(min(r['F1_union'] for r in rows if r['p'] == p and r['k'] == k) == f['F1_union_min_by_k'][str(k)], 'fit union min p=%d k=%d' % (p, k)) ks = sorted(f['F1_per_k_min']) mono = all(f['F1_per_k_min'][a] <= f['F1_per_k_min'][b] for a, b in zip(ks, ks[1:])) pos = all(f['F1_per_k_min'][k] > 0 for k in ks) check(mono == f['min_nondecreasing'], 'monotone flag p=%d' % p) check(pos == f['min_positive'], 'positive flag p=%d' % p) ok_all = ok_all and mono and pos check(ok_all == art['summary']['min_nondecreasing_at_every_prime'], 'summary monotone flag') check(ok_all is True, 'F1/k nondecreasing and positive at every prime') check(art['summary']['unions'] == 33 and art['summary']['by_rule'] == {'uniform_weights': 33}, 'summary counts') check(art['summary']['cross_check_vs_439_all_agree'] is True, 'summary cross-check flag') check(all(c['agree'] and c['this'] == c['return_439'] for c in art['cross_check_vs_439']), 'cross-check rows all agree') check(art['outcome'].startswith('success:'), 'outcome string') check('success' in art['outcome'] and art['summary']['all_certificates_pass'] is True, 'all certificates pass') print('checks: %d, failures: %d' % (nchk, len(fails))) for f in fails: print(' ', f) print('CHECKER', 'PASS' if not fails else 'FAIL') raise SystemExit(0 if not fails else 1)