#!/usr/bin/env python3 """Exact gap-word builder for the two-class primorial tile. T_x = { r in [0, P_x) : gcd(r, P_x) = gcd(r+2, P_x) = 1 }, P_x = prod_{q<=x} q, D(T_x) = prod_{3<=q<=x} (q-2). The cyclic gap word g has D entries, g[i] = S[i+1]-S[i] and g[D-1] = S[0]+P_x-S[D-1]; sum(g) = P_x. Two exact constructions, both integer: tile_residues(x) - recursive lift, the producer's buildTiles lift_gaps(S, W, p) - one lift of T_x by the next prime p (producing T_p), with no full-period residue list retained (used for T29). Usage: gen_tile.py selftest gen_tile.py make 23 outputs/1925/t23_gaps.bin gen_tile.py make 29 outputs/1925/t29_gaps.bin # lifts T23 by 29 """ import sys, json, hashlib import numpy as np PRIMES = [3, 5, 7, 11, 13, 17, 19, 23, 29] def tile_residues(x): """Explicit residue list S (sorted, in [0,W)) and W = prod_{q<=x} q.""" assert x >= 3 S = np.array([5], dtype=np.int64) # T3: r = 5 (mod 6) W = 6 for p in [q for q in PRIMES if 3 <= q <= x][1:]: newW = W * p keep = [] for k in range(p): r = S + k * W ok = (r % p != 0) & ((r + 2) % p != 0) keep.append(r[ok]) S = np.concatenate(keep) W = newW return S, W def gaps_from_residues(S, W): g = np.empty(S.size, dtype=np.uint16) g[:-1] = np.diff(S) g[-1] = S[0] + W - S[-1] return g def lift_gaps(S, W, p): """Gap word of T_{x*p} (one full period) from the gap word of T_x. Copy k holds S + k*W; it keeps the residues with r mod p not in {0,-2}. Consecutive survivors inside a copy give np.diff(kept); the seam from the last survivor of copy k to the first survivor of copy k+1 is (S[first_{k+1}] + (k+1)W) - (S[last_k] + kW) = S[first_{k+1}] - S[last_k] + W. """ Sm = S % p Wm = W % p idxs = [] for k in range(p): c = (k * Wm) % p r = (Sm + c) % p ok = (r != 0) & (((r + 2) % p) != 0) idxs.append(np.nonzero(ok)[0]) total = int(sum(len(ix) for ix in idxs)) out = np.empty(total, dtype=np.uint16) firsts = [int(ix[0]) for ix in idxs] pos = 0 for k in range(p): ix = idxs[k] keep = S[ix] within = np.diff(keep) out[pos:pos + within.size] = within pos += within.size jn = firsts[(k + 1) % p] out[pos] = (S[jn] + W) - S[ix[-1]] pos += 1 assert pos == total return out def stats(g, W, expected_D): h = {} for v in g.tolist(): h[v] = h.get(v, 0) + 1 return { "D": int(g.size), "D_expected": expected_D, "period": W, "sum_gaps": int(g.sum()), "min_gap": int(g.min()), "max_gap": int(g.max()), "ndistinct": len(h), "all_div6": bool(all(v % 6 == 0 for v in h)), "hist": {str(k): v for k, v in sorted(h.items())}, "sha256_u16le": hashlib.sha256(g.tobytes()).hexdigest(), } def expected_D(x): d = 1 for q in [q for q in PRIMES if 3 <= q <= x]: d *= (q - 2) return d def expected_P(x): p = 2 for q in [q for q in PRIMES if q <= x]: p *= q return p def build(x): if x <= 23: S, W = tile_residues(x) return gaps_from_residues(S, W), W assert x == 29 S, W = tile_residues(23) return lift_gaps(S, W, 29), W * 29 def cmd_selftest(): ok = True prev = None for x in [5, 7, 11, 13, 17, 19, 23]: S, W = tile_residues(x) g = gaps_from_residues(S, W) st = stats(g, W, expected_D(x)) good = (st["D"] == st["D_expected"] and st["sum_gaps"] == expected_P(x) and st["all_div6"] and st["min_gap"] == 6) print(f"x={x:3d} D={st['D']:>9d} sum={st['sum_gaps']:>10d} " f"min={st['min_gap']} max={st['max_gap']} ndist={st['ndistinct']} " f"{'ok' if good else 'FAIL'}") ok &= good if prev is not None: S0, W0, p = prev gl = lift_gaps(S0, W0, p) same = (gl.size == g.size and int((gl != g).sum()) == 0) print(f" lift control x={x}: gap words identical: {same}") ok &= same prev = (S, W, [q for q in PRIMES if q > x][0] if x < 23 else None) # the lift that builds T29, checked structurally S23, W23 = tile_residues(23) g29 = lift_gaps(S23, W23, 29) st = stats(g29, W23 * 29, expected_D(29)) good = (st["D"] == st["D_expected"] and st["sum_gaps"] == expected_P(29) and st["all_div6"] and st["min_gap"] == 6 and st["max_gap"] == 258) print(f"x= 29 D={st['D']} sum={st['sum_gaps']} min={st['min_gap']} " f"max={st['max_gap']} ndist={st['ndistinct']} {'ok' if good else 'FAIL'}") ok &= good print("SELFTEST", "PASS" if ok else "FAIL") return 0 if ok else 1 def cmd_make(x, path): g, W = build(x) st = stats(g, W, expected_D(x)) good = (st["D"] == st["D_expected"] and st["sum_gaps"] == expected_P(x) and st["all_div6"] and st["min_gap"] == 6) st["ok"] = bool(good) with open(path, "wb") as f: f.write(g.tobytes()) with open(path + ".json", "w") as f: json.dump(st, f, indent=2) print(json.dumps({k: st[k] for k in ("D", "sum_gaps", "min_gap", "max_gap", "ndistinct", "all_div6", "ok", "sha256_u16le")}, indent=2)) return 0 if good else 1 if __name__ == "__main__": if sys.argv[1] == "selftest": sys.exit(cmd_selftest()) elif sys.argv[1] == "make": sys.exit(cmd_make(int(sys.argv[2]), sys.argv[3])) else: print(__doc__) sys.exit(2)