#!/usr/bin/env python3 # -*- coding: utf-8 -*- """exact_check.py -- route 27 pursue (#1376): is L(T_x,p) = K*({p})? Two objects, both computed EXACTLY, no search heuristic. K*({p}) (route 26's covering capacity, anchored rule). Level x, P = x#. Level-x slots are the r with gcd(r,P) = gcd(r+2,P) = 1; a slot is killed by p iff p | r or p | r+2. Since slots are periodic mod P and the kill is periodic mod p with p not dividing P, the joint pattern has exact period P*p, so K*({p}) = the longest cyclic run of consecutive slots in ONE PERIOD with the anchored rule. Enumerating that period is decisive by construction (no window bound). L(T_x,p) (#161's measured object, free translate). max over a in Z/p of the longest run of consecutive slots of the tile T_x whose residues mod p lie in the 2-set {a, a+2} (the wraparound difference p-2 is a-2-... i.e. the same class of pairs). Computed here BOTH ways: cyclic in the tile (the seam joined, which is what "cyclic gap word" means in #161) and linear (no wrap), so the linear/cyclic question -- the named alternative explanation -- is settled by the data rather than assumed. K*({p,q}) (the unpublished two-class capacity, arm B). Period P*p*q; killed iff p | r or r+2, or q | r or r+2. The phase pair (a_m, b_m) = (-m*P mod p, -m*P mod q) runs over ALL p*q pairs by CRT, so the period is the concatenation of the p*q per-pair tile masks, and the free-translate analogue is the max over pairs. Cost. Every value is one linear pass over the period; runs are detected with C-level bytes.find, so the whole table is seconds to a couple of minutes. """ import json import sys from math import gcd from pathlib import Path def primorial(x): P = 1 for q in range(2, x + 1): if all(q % d for d in range(2, int(q ** 0.5) + 1)): P *= q return P def is_prime(n): return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1)) def slots_of(P): """The level-x slots in one period [0,P): r with gcd(r,P)=gcd(r+2,P)=1.""" return [r for r in range(P) if gcd(r, P) == 1 and gcd(r + 2, P) == 1] def max_run_cyclic(seq): """Longest run of 1-bytes in the cyclic sequence seq (length M).""" M = len(seq) if M == 0: return 0 if b"\x00" not in seq: return M dbl = seq + seq # 2M so every wrapped window of length <= M appears contiguously L = 0 while L < M: L += 1 if dbl.find(b"\x01" * L) < 0: return L - 1 return M def max_run_linear(seq): M = len(seq) if M == 0: return 0 if b"\x00" not in seq: return M dbl = b"\x00" + seq + b"\x00" L = 0 while L < M: L += 1 if dbl.find(b"\x01" * L) < 0: return L - 1 return M def tile_masks(slots, Q): """bytes mask per phase tuple: 1 iff the slot is killed by some q in Q with that phase. Keys are tuples (a_q for q in Q) with the anchored meaning: residue mod q lies in {a_q, a_q+2}. """ res = {q: [r % q for r in slots] for q in Q} out = {} def rec(i, key): if i == len(Q): qs = list(Q) def killed(idx): for j, q in enumerate(qs): v = res[q][idx] + key[j] v %= q if v == 0 or v == q - 2: return 1 return 0 out[key] = bytes(killed(idx) for idx in range(len(slots))) return q = Q[i] for a in range(q): rec(i + 1, key + (a,)) rec(0, ()) return out def kstar(slots, P, Q): """K*(Q) exactly: cyclic max run over one period P*prod(Q) with the anchored rule.""" masks = tile_masks(slots, Q) prod = 1 for q in Q: prod *= q # Block m is the tile shifted by m*P, so the anchored kill r+m*P = 0 or -2 (mod q) is the mask whose # key is (m*P mod q). Using -m*P here would reverse the block order but NOT the order inside a block, # which is a different sequence and changes the run lengths (checked: it gave K*({23}) = 2 at level 5 # where the direct construction gives 1). seq = bytearray() for m in range(prod): k = tuple((m * P) % q for q in Q) seq += masks[k] return max_run_cyclic(bytes(seq)), len(seq) def max_translate_tile(slots, p, cyclic=True): """#161's object: max over a of the longest run in the tile word, residues in {a, a+2}.""" res = [r % p for r in slots] best = 0 for a in range(p): want = {a, (a + 2) % p} mask = bytes(1 if v in want else 0 for v in res) run = max_run_cyclic(mask) if cyclic else max_run_linear(mask) if run > best: best = run return best def main(): DIAG = {5: 7, 7: 11, 11: 13, 13: 17, 17: 19, 19: 23, 23: 29, 29: 31} PUBLISHED_DIAG = {7: 2, 11: 1, 13: 2, 17: 2, 19: 2, 23: 3, 29: 2, 31: 4} COL_READS_1_FROM = {5: 11, 7: 11, 11: 23, 13: 37} out = {"arm_a": [], "columns": [], "arm_b": [], "notes": []} levels = [int(v) for v in (sys.argv[1:] or ["5", "7", "11", "13", "17", "19"])] for x in levels: P = primorial(x) slots = slots_of(P) p = DIAG[x] ka, M = kstar(slots, P, [p]) cyc = max_translate_tile(slots, p, cyclic=True) lin = max_translate_tile(slots, p, cyclic=False) row = {"level": x, "P": P, "slots": len(slots), "p_diag": p, "period": M, "K_star_p": ka, "tile_cyclic": cyc, "tile_linear": lin, "published_L_diag": PUBLISHED_DIAG.get(p), "match": ka == PUBLISHED_DIAG.get(p)} out["arm_a"].append(row) print(f"level {x:>2} P={P:>12} slots={len(slots):>8} p={p:>3} period={M:>12} " f"K*({p})={ka} tile_cyc={cyc} tile_lin={lin} #161={PUBLISHED_DIAG.get(p)} " f"{'MATCH' if row['match'] else 'DIFF'}") # Column statements of #161: "column reads 1 from p = X" (small levels, cheap to sweep). for x, from_p in COL_READS_1_FROM.items(): P = primorial(x) slots = slots_of(P) vals = [] for p in range(DIAG[x], 220): if not is_prime(p) or p <= x: continue ka, _ = kstar(slots, P, [p]) vals.append((p, ka)) first_one = next((p for p, v in vals if v == 1), None) ok = first_one == from_p and all(v == 1 for p, v in vals if p >= from_p) out["columns"].append({"level": x, "published_reads_1_from": from_p, "observed_reads_1_from": first_one, "all_1_from_then": ok, "swept": [p for p, _ in vals], "values": [v for _, v in vals]}) print(f"column T{x}: reads 1 from p={first_one} (published {from_p}) all-1-after={ok}") # Arm B: the unpublished two-class capacity, small levels. ARM_B = {5: (7, 11), 7: (11, 13), 11: (13, 17), 13: (17, 19), 17: (19, 23)} for x, (p, q) in ARM_B.items(): if x not in levels: continue P = primorial(x) slots = slots_of(P) kp, _ = kstar(slots, P, [p]) kpq, M = kstar(slots, P, [p, q]) masks = tile_masks(slots, [p, q]) mob = max(max_run_cyclic(v) for v in masks.values()) row = {"level": x, "P": P, "slots": len(slots), "p": p, "q": q, "period": M, "K_star_p": kp, "K_star_pq": kpq, "tile_mobile_pq": mob, "delta_vs_K_p": kpq - kp, "delta_ge_1": kpq >= kp + 1} out["arm_b"].append(row) print(f"level {x:>2} K*({{{p}}})={kp} K*({{{p},{q}}})={kpq} tile_mobile={mob} " f"delta={kpq - kp} delta>=1: {row['delta_ge_1']}") out["notes"].append("level 23 (p=29) and level 29 (p=31) periods are 6.47e9 and 2.01e11; " "not enumerated here, so their published values stay externally reported") dest = Path(__file__).resolve().parent / "exact-check.json" dest.write_text(json.dumps(out, indent=1), encoding="utf-8") print(f"\nwrote {dest.name}") if __name__ == "__main__": main()