"""Question: do gap6 excursion permutations preserve unrestricted triple sums? Job705, preregistered127 controls plus an exact extremal witness on T13/T17. This extends my pending308 control to a different observable, not a new route. Two large folds are formula-derived; only the tiny gates enumerate their periods. """ from collections import Counter import json import math import pathlib import sys def primes_to(n): out = [] for p in range(2, n+1): if all(p % d for d in out if d*d <= p): out.append(p) return out PRIMES = primes_to(1000) def gaps(slots, period): return [slots[(i+1) % len(slots)] + (period if i+1 == len(slots) else 0)-n for i, n in enumerate(slots)] def tile(x): ps = [p for p in PRIMES if p <= x] W = math.prod(ps) slots = [n for n in range(W) if all(n % p and (n+2) % p for p in ps)] return W, slots, gaps(slots, W) def hist(word, length): return Counter(sum(word[(i+j) % len(word)] for j in range(length)) for i in range(len(word))) def ordered_pairs(word): return Counter(zip(word, word[1:]+word[:1])) def hjson(h): return sorted([d, n] for d, n in h.items()) def blend(h, a, j, b): result = Counter({d: a*n for d, n in h.items()}) result.update({d: b*n for d, n in j.items()}) return result def two_folds(word, W, q, r): H, J, K = (hist(word, k) for k in (1, 2, 3)) assert q > 2*max(word)+2 and r > q H1, J1 = blend(H, q-4, J, 2), blend(J, q-6, K, 4) H2 = blend(H1, r-4, J1, 2) assert all(n > 0 for n in H2.values()) assert sum(H2.values()) == len(word)*(q-2)*(r-2) assert sum(d*n for d, n in H2.items()) == W*q*r assert max(H2) == max(K) return H1, H2 def blocks(word): first = word.index(6) rotated = word[first:]+word[:first] starts = [i for i, g in enumerate(rotated) if g == 6]+[len(word)] return [rotated[a:b] for a, b in zip(starts, starts[1:])] def flatten(bs, order): return [g for i in order for g in bs[i]] def attainable_max(word, bs): # Only triples centered at a6 boundary change. All other triples are # determined by individual block interiors and their fixed adjacent6s. fixed = max((word[(i-1) % len(word)]+g+word[(i+1) % len(word)] for i, g in enumerate(word) if g != 6), default=0) right = sorted([(b[1] if len(b)>1 else 6, i) for i, b in enumerate(bs)], reverse=True) assert len(bs) >= 2 best = (-1, -1, -1) for a, b in enumerate(bs): value, other = right[0] if right[0][1] != a else right[1] best = max(best, (b[-1]+6+value, a, other)) junction, a, b = best if junction > fixed: order = [a, b]+[i for i in range(len(bs)) if i not in (a, b)] else: order = list(range(len(bs))) witness = flatten(bs, order) maximum = max(fixed, junction) assert max(hist(witness, 3)) == maximum return maximum, witness, {"left_block": a, "right_block": b, "junction_bound": junction, "fixed_bound": fixed} class Random32: def __init__(self, seed): self.state = seed def next(self): x = self.state x ^= (x << 13) & 0xffffffff x ^= x >> 17 x ^= (x << 5) & 0xffffffff self.state = x & 0xffffffff return self.state def permutation(self, n): order = list(range(n)) for i in range(n-1, 0, -1): # Reject excess range instead of introducing modulo bias. This # remains a deterministic algorithmic control, not a p-value. limit = ((2**32-1)//(i+1))*(i+1) v = self.next()-1 while v >= limit: v = self.next()-1 j = v % (i+1) order[i], order[j] = order[j], order[i] return order def l1(a, b): return sum(abs(a[d]-b[d]) for d in a.keys() | b.keys()) gates = [] for x in (5, 7): W, slots, word = tile(x) q = next(p for p in PRIMES if p > 2*max(word)+2) r = next(p for p in PRIMES if p > q) allowed = set(slots) fresh = [n for n in range(W*q*r) if n % W in allowed and n % q not in (0, q-2) and n % r not in (0, r-2)] _, expected = two_folds(word, W, q, r) actual = hist(gaps(fresh, W*q*r), 1) assert actual == expected gates.append({"x": x, "q": q, "r": r, "W_new": W*q*r, "D_new": len(fresh), "G_new": max(actual), "H_new": hjson(actual), "method": "independent_complete_integer_period"}) results = [] for x, expected_D, expected_G in [(13, 1485, 66), (17, 22275, 108)]: W, _, word = tile(x) assert len(word) == expected_D and max(word) == expected_G bs = blocks(word) H, J, K = (hist(word, k) for k in (1, 2, 3)) P = ordered_pairs(word) q = next(p for p in PRIMES if p > 2*max(word)+2) r = next(p for p in PRIMES if p > q) H1, H2 = two_folds(word, W, q, r) Mstar, witness, witness_boundary = attainable_max(word, bs) random = Random32(705000+x) controls = [] for label, altered in [("extremal", witness)]+[(i, flatten(bs, random.permutation(len(bs)))) for i in range(127)]: assert hist(altered, 1) == H assert ordered_pairs(altered) == P assert hist(altered, 2) == J Kc = hist(altered, 3) H1c, H2c = two_folds(altered, W, q, r) assert H1c == H1 assert l1(H2c, H2) == 8*l1(Kc, K) assert max(Kc) <= Mstar controls.append({"label": label, "M3": max(Kc), "K_l1": l1(Kc, K), "K_l1_per_D": l1(Kc, K)/len(word), "derived_twofold_H_l1": l1(H2c, H2)}) assert controls[0]["M3"] == Mstar results.append({"x": x, "W": W, "D": len(word), "G": max(word), "blocks": len(bs), "H": hjson(H), "J": hjson(J), "K": hjson(K), "M3": max(K), "M3_attainable": Mstar, "suppression_gap": Mstar-max(K), "q": q, "r": r, "folds_method": "formula_derived_not_large_period_enumeration", "random_changed_M3": sum(c["M3"] != max(K) for c in controls[1:]), "random_changed_K": sum(c["K_l1"] != 0 for c in controls[1:]), "extremal_boundary": witness_boundary, "controls": controls}) pathlib.Path(sys.argv[1] if len(sys.argv)>1 else "block-triples-705.json").write_text( json.dumps({"schema": "sah-block-triples-v1", "preregistered_controls": 127, "scope": "finite observable sensitivity; controls are not primorial tiles", "gates": gates, "results": results}, indent=2)+"\n")