#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Job #1414: the seam convention decides the fold-11 disagreement. A tile T_x is the admissible residue set mod M = x#. After slot res[i] the next period's slot is res[i] + M, so its residue mod p is (res[i] + M) mod p, NOT res[i] mod p. Doubling the residue array ("payload * 2") is therefore only correct when p | M -- and the capacity question is asked for p > x, where p never divides M. #627 documented this exact defect: "The first revision closed the cyclic seam with the unshifted head -- right only if p | P, never for p > level. At T_23, p = 173 that invented a compatible seam pair (true residue difference 30, not 2)." This script recomputes L(T_{p-}, p) both ways on the SAME corpus tile, so the difference is isolated: naive : residues doubled unshifted (the defect; #642's `payload = ... * 2`) corrected: second copy shifted by M mod p (the true cyclic continuation) Both use the definitional object: L = max over translate a of the longest cyclic run of consecutive slots with r mod p in {a, a+2}. Tile builder is the CRT one that reproduces |T_19| / |T_23|. """ import json, sys, time from pathlib import Path import numpy as np DIAGONAL = {7: 2, 11: 1, 13: 2, 17: 2, 19: 2, 23: 3, 29: 2, 31: 4} GATES_622 = {(5, 7): 2, (7, 11): 1, (11, 13): 2, (13, 17): 2, (17, 19): 2, (19, 23): 3, (23, 29): 2} def primes_upto(n): out, c = [], 2 while c <= n: if all(c % q for q in out if q * q <= c): out.append(c) c += 1 return out def prev_prime(n): ps = primes_upto(n - 1) return ps[-1] if ps else None def tile_crt(level): res, M = [1], 2 for q in [x for x in primes_upto(level) if x >= 3]: inv = pow(M % q, -1, q) classes = [c for c in range(q) if c % q != 0 and (c + 2) % q != 0] res = [r + M * (((c - r) * inv) % q) for r in res for c in classes] M *= q res.sort() return res, M def L_of(res, M, p, corrected): """Longest cyclic run of consecutive slots killed by some pair {a, a+2} mod p.""" n = len(res) R = np.asarray(res, dtype=np.int64) % p second = (R + (M % p)) % p if corrected else R # <-- the one difference dbl = np.concatenate([R, second]) best, best_a = 0, None for a in range(p): killed = (dbl == a) | (dbl == (a + 2) % p) if killed.all(): m = n else: miss = np.flatnonzero(~killed) d = np.diff(np.concatenate([miss, [miss[0] + 2 * n]])) - 1 m = int(min(d.max(), n)) if m > best: best, best_a = m, a return best, best_a def main(): out = Path(__file__).parent rec = {"tool": "diagonal-check2.py", "diagonal_brief": {str(k): v for k, v in DIAGONAL.items()}, "rows": [], "note": "naive = unshifted doubled residues (#642's convention)"} ok_naive = ok_corr = 0 for p in (7, 11, 13, 17, 19, 23, 29): x = prev_prime(p) res, M = tile_crt(x) t = time.time() Ln, an = L_of(res, M, p, corrected=False) Lc, ac = L_of(res, M, p, corrected=True) exp = DIAGONAL[p] row = {"fold_p": p, "tile_x": x, "tile_size": len(res), "period_M": M, "M_mod_p": M % p, "L_naive": Ln, "translate_naive": an, "L_corrected": Lc, "translate_corrected": ac, "diagonal_expected": exp, "gate_622": GATES_622.get((x, p)), "naive_matches": Ln == exp, "corrected_matches": Lc == exp, "seconds": round(time.time() - t, 2)} ok_naive += Ln == exp ok_corr += Lc == exp rec["rows"].append(row) # stdout is the artifact and must reproduce byte for byte: emit only the deterministic fields # (rate/timing goes to stderr, per the project's files guidance). print(json.dumps({k: v for k, v in row.items() if k != "seconds"})) print(f"fold p={p} took {row['seconds']}s", file=sys.stderr) rec["totals"] = {"folds": 7, "naive_matches": ok_naive, "corrected_matches": ok_corr} rec["observed_utc"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) (out / "diagonal-check2.json").write_text(json.dumps(rec, indent=1), encoding="utf-8") print(json.dumps(rec["totals"])) if __name__ == "__main__": main()