"""Job #754 pilot: the maximal k-gap window profile A_k(x) of T_x against a permutation null. A_k(x) = max over cyclic i of (g_i + g_{i+1} + ... + g_{i+k-1}) (the repo's maxsum_k) A_1 = the maximal gap = G2(x#) exactly, and it is a function of the gap MULTISET alone, so uniform permutation of the tile's gap word cannot move it: the permutation null is a valid control only from k = 2 upward. That asymmetry is the point of the statistic: it separates a "value" anomaly (the multiset carries an unusually large gap) from an "arrangement" anomaly (large gaps sit adjacent). Null = 100 uniform permutations of the same gap multiset (seeded), same cyclic window construction. """ import numpy as np LMAX = 8 NPERM = 100 SEED = 20260914 def primes_upto(n): s = bytearray([1]) * (n + 1) s[0:2] = b"\x00\x00" for i in range(2, int(n ** 0.5) + 1): if s[i]: s[i * i::i] = b"\x00" * (((n - i * i) // i) + 1) return [i for i in range(n + 1) if s[i]] def build(x): ps = primes_upto(x) W = 1 for p in ps: W *= p mask = np.zeros(W + 1, dtype=bool) for p in ps: for r in (0, p - 2): first = p if r == 0 else r if first <= W: mask[first::p] = True slots = np.flatnonzero(~mask[1:]).astype(np.int64) + 1 gaps = np.empty(slots.size, dtype=np.int64) gaps[:-1] = np.diff(slots) gaps[-1] = slots[0] + W - slots[-1] return W, gaps def profile(gaps, LMAX=LMAX): """Cyclic maximal k-gap window sums, k = 1..LMAX.""" n = gaps.size dbl = np.concatenate([gaps, gaps]) out = [] acc = np.zeros(n, dtype=np.int64) for k in range(1, LMAX + 1): acc = acc + dbl[k - 1:k - 1 + n] out.append(int(acc.max())) return out rng = np.random.default_rng(SEED) D_OF = {11: 135, 13: 1485, 17: 22275, 19: 378675, 23: 7952175, 29: None} for x in [13, 17, 19, 23]: W, gaps = build(x) obs = profile(gaps) n = gaps.size null = np.empty((NPERM, LMAX), dtype=np.int64) work = gaps.copy() for j in range(NPERM): rng.shuffle(work) null[j] = profile(work, LMAX) print("=" * 104) print("T%-3d W=%-14d D=%-9d (multiset check D = %s)" % (x, W, n, D_OF.get(x))) print(" k A_k(x) null mean null p95 null max z=(obs-mean)/sd permutation-invariant?") for k in range(LMAX): col = null[:, k].astype(float) sd = col.std(ddof=1) z = (obs[k] - col.mean()) / sd if sd > 0 else float("nan") print(" %-3d %12d %13.1f %13.0f %13.0f %16.2f %s" % (k + 1, obs[k], col.mean(), np.percentile(col, 95), col.max(), z, "YES (multiset only)" if sd == 0 else "no")) print(" A_k/A_1: %s" % " ".join("%.3f" % (obs[k] / obs[0]) for k in range(LMAX)))