#!/usr/bin/env python3 """congruence-law.py -- why most of the large zero-repeat gap values CANNOT repeat a neighbour: the forced-zero class of the twin-admissible tile T_x, from first principles. Object (route 82 / return #1296). T_x = {r : r and r+2 both coprime to x#}; the word is the increasing list of T_x residues inside one period x#; the gaps are g_i = r_{i+1} - r_i with the cyclic wrap r_0 + x# - r_last. For a gap value g, h_g counts its occurrences, R_g = #{i : g_i = g_{i+1} = g} counts adjacent EQUAL pairs, and E_g = h_g(h_g-1)/(D-1) is its exact exchangeable expectation (the null of route 82: a uniform permutation of the gap multiset). R/E_rep = sum_g R_g / sum_g E_g is the fold-free repeat ratio of return #1296. LEMMA (forced zero). Two adjacent gaps both equal to g mean three consecutive residues in arithmetic progression with common difference g. A triple that is admissible in the tile is admissible mod every prime p <= x, i.e. for each p some r mod p has r, r+g, r+2g all outside the forbidden classes {0, p-2} (r shares a factor with p, or r+2 does). So if NO r mod p has that property, R_g = 0 is FORCED, in every period, for every tile built from that prime set. The test below is exhaustive over r mod p -- a necessary condition, so a 'forced' verdict is a proof of R_g = 0, never a fit to data. Two independent checks are printed: part 1 the forced/unforced split of the measured diag spectra, with the counterexample and share-of-expectation accounting (t23/t29/t31.diag.json); part 2 a DEFINITIONAL control at T13: the residues and every R_g are rebuilt from the definition above with no sieve, no wheel and no diag path, and compared with t13.json. usage: python congruence-law.py [--work DIR] [--out FILE] """ import argparse import json import os TILES = (23, 29, 31) def primes_upto(x): ps = [] for n in range(2, x + 1): if all(n % d for d in range(2, int(n ** 0.5) + 1)): ps.append(n) return ps def ap_triple_mod(p, g): """Is there r mod p with r, r+g, r+2g all admissible mod p (not = 0, not = -2)?""" bad = {0, (p - 2) % p} for r in range(p): if r % p in bad: continue if (r + g) % p in bad or (r + 2 * g) % p in bad: continue return True return False def forcing_primes(primes, g): return [p for p in primes if not ap_triple_mod(p, g)] def definitional_word(x): """Rebuild the T_x word and its gap list from the definition, no sieve, no wheel.""" P = 1 for p in primes_upto(x): P *= p res = [r for r in range(P) if all(r % p and (r + 2) % p for p in primes_upto(x))] gaps = [res[i + 1] - res[i] for i in range(len(res) - 1)] + [res[0] + P - res[-1]] return P, res, gaps def repeat_stats(gaps): D = len(gaps) h, R = {}, {} for g in gaps: h[g] = h.get(g, 0) + 1 for i in range(D): if gaps[i] == gaps[(i + 1) % D]: R[gaps[i]] = R.get(gaps[i], 0) + 1 E = {g: n * (n - 1) / (D - 1) for g, n in h.items()} return D, h, R, E def main(): ap = argparse.ArgumentParser() ap.add_argument("--work", default=os.path.dirname(os.path.abspath(__file__))) ap.add_argument("--out", default=None) a = ap.parse_args() out = a.out or os.path.join(a.work, "congruence-law.txt") lines = [] def say(s=""): print(s) lines.append(s) say("== congruence-law.py -- forced zero-repeat gap values on the twin-admissible tile") say(" the test: does any r mod p put r, r+g, r+2g all in the admissible classes" " {1..p-1}\\{p-2}?") # ---- part 1: measured diag spectra ------------------------------------------------ w = [] for x in TILES: f = os.path.join(a.work, "t%d.diag.json" % x) if os.path.exists(f): w.append((x, json.load(open(f, encoding="utf-8")))) for x, d in w: P = d["P"] ps = primes_upto(x) vals = d["diag"]["values"] # gap values with h_g >= 2 D = d["D"] forced, unforced, counter = [], [], [] for v in vals: fp = forcing_primes(ps, v["g"]) (forced if fp else unforced).append((v, fp)) if fp and v["R_g"] != 0: counter.append((v["g"], v["R_g"], fp)) E_for = sum(v["E_g"] for v, _ in forced) E_un = sum(v["E_g"] for v, _ in unforced) R = d["diag"]["R_from_diag"] E = d["diag"]["E_from_diag"] zero_un = [(v["g"], v["h_g"], v["E_g"]) for v, _ in unforced if v["R_g"] == 0] say() say("T%-3d D=%d P=%d values(h_g>=2)=%d E_rep=%.1f R=%d R/E_rep=%.5f" % (x, D, P, len(vals), E, R, R / E)) say(" forced-zero values : %2d carrying E=%s = %.2f%% of E_rep " "measured R_g=0 in %d of %d" % (len(forced), fmt(E_for), 100 * E_for / E, len(forced) - len(counter), len(forced))) say(" COUNTEREXAMPLES (forced but R_g != 0): %d %s" % (len(counter), counter)) say(" zeros NOT forceable : %2d E=%s (%.4f%% of E_rep) %s" % (len(zero_un), fmt(E_un), 100 * sum(e for _, _, e in zero_un) / E, [(g, n) for g, n, _ in zero_un][:6])) say(" CORE with forced values removed: %.5f (uncorrected %.5f) -> the forced class " "accounts for %.1f%% of the suppression" % (R / (E - E_for), R / E, 100 * E_for / E)) mods = {} for v, fp in forced: mods.setdefault(tuple(fp), []).append(v["g"] % fp[0]) for fp, gs in mods.items(): say(" forcing prime(s) %s : %s (residues %s)" % (fp, sorted(gs), sorted(set(gs)))) un_mods = sorted({v["g"] % 5 for v, _ in unforced}) say(" unforced values mod 5 (control, must contain no forced residue): %s" % un_mods) # ---- part 2: definitional control at T13 ------------------------------------------ say() say("== definitional control at T13: residues and every R_g rebuilt from the definition") x = 13 P, res, gaps = definitional_word(x) D, h, R, E = repeat_stats(gaps) E_rep = sum(E.values()) R_tot = sum(R.values()) ps = primes_upto(x) t13 = json.load(open(os.path.join(a.work, "t13.json"), encoding="utf-8")) say(" P=%d D=%d (expected %d) max gap=%d (filed %d)" % (P, D, t13["D_expected"], max(gaps), t13["maxgap"])) say(" R=%d (filed %d) E_rep=%.4f (filed %.4f) R/E_rep=%.5f (filed %.5f)" % (R_tot, t13["repeat"]["R"], E_rep, t13["repeat"]["E_repeat"], R_tot / E_rep, t13["repeat"]["ratio"])) forced13 = [g for g in h if h[g] >= 2 and forcing_primes(ps, g)] bad = [g for g in forced13 if R.get(g, 0) != 0] say(" forced values with h_g>=2 at T13: %d measured nonzero R_g among them: %d %s" % (len(forced13), len(bad), bad)) say(" forced residues mod 5: %s" % sorted({g % 5 for g in forced13})) pos = [g for g in h if h[g] >= 2 and not forcing_primes(ps, g) and R.get(g, 0) > 0] say(" POSITIVE control (unforced, repeats present): %d values, e.g. %s" % (len(pos), sorted((g, R.get(g, 0), h[g]) for g in pos)[:4])) say(" every forced value is congruent to +-1 mod 5, and no forced value repeats: OK" if not bad and all(g % 5 in (1, 4) for g in forced13) else " MISMATCH") open(out, "w", encoding="utf-8").write("\n".join(lines) + "\n") print("\n[written] %s" % out) def fmt(v): return ("%d" % round(v)) if v >= 1000 else ("%.1f" % v) if __name__ == "__main__": main()