#!/usr/bin/env python3 """Job #1035 (route 3 pursuit) -- the matched permutation null of the interior share. Question the route asks: is the realized interior share of a certified record span typical or extreme under the fixed-multiset permutation null, at the two levels whose realized share range is positive? Construction. A certified span at level x is the maximal gap [p, p+G] of T_x (G = A_1(x)); its gaps are the T_{x-1} gaps inside it, i.e. the trajectory of one k-gap window. With multiset M = (m_1..m_L), sum M = G, the *matched null* is the uniform distribution over the L! linear arrangements of M; an arrangement is read as (end, ..., end) and its statistic is the interior share share = (G - m_end1 - m_end2) / G . Exact, no sampling: the support is the multiset of shares over the L! permutations, so p = count/L!. The class law of return #398 is imposed as a second, constrained null: the interior gaps must satisfy g = 0, +-c(x) mod 6x with c(x) = 6*(2*6^-1 mod x) mod 6x. Under that constraint the arrangement space can collapse to a single point, which decides whether the statistic carries arrangement information at all. Data: level 19# is ENUMERATED here from the definition (19# = 9,699,690 integers), so all 20 of its recorded spans and their multisets are exact. Level 43# cannot be enumerated here; its least witness is taken from LADDER (research/exact-g2-ladder.js / phase1-T2b-exact-ladder.md) and its span gaps are recomputed by trial division, and the remaining spans use the anatomy published in return #417 (four depth-2 spans, interior 0; four depth-3 spans, interior 84, end sum 534). Usage: python null_share.py Output: JSON on stdout (the artifact); progress on stderr. """ import json import sys from math import gcd from itertools import permutations PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] # LADDER: level -> (A_1(x), certified least position) [return #424 / splits.py LADDER] LADDER = {19: (150, 659), 43: (618, 830330079152051)} G2_19 = 150 def primorial(x): r = 1 for p in PRIMES: if p <= x: r *= p return r def class_c(x): mod = 6 * x return (6 * (2 * pow(6, -1, x) % x)) % mod def slots_between(p, lo, hi, P): return [n for n in range(lo, hi + 1) if gcd(n * (n + 2), P) == 1] def span_gaps(x, p, g): """The T_{x-1} gaps inside the span [p, p+g]; endpoints must be slots.""" P = primorial(x - 1) s = slots_between(x - 1, p, p + g, P) assert s[0] == p and s[-1] == p + g, "endpoints are not T_{x-1} slots" return [s[i + 1] - s[i] for i in range(len(s) - 1)] def null(M, G, x): """Exact matched null over the L! arrangements of the multiset M, plus the class-constrained subset.""" L = len(M) c = class_c(x) mod = 6 * x counts, cons_counts, cons_shares = {}, 0, set() total = 0 obs_share = None for perm in permutations(range(L)): total += 1 ends = (perm[0], perm[-1]) interior = [M[i] for i in perm[1:-1]] share = (G - M[ends[0]] - M[ends[1]]) / G counts[share] = counts.get(share, 0) + 1 if all((v % mod) in (0, c, mod - c) for v in interior): cons_counts += 1 cons_shares.add(share) return dict(L=L, multiset=sorted(M), c=c, mod=mod, n_arrangements=total, support=sorted(counts), p_ge_observed=None, _counts=counts, class_consistent_arrangements=cons_counts, class_consistent_shares=sorted(cons_shares), class_support_size=len(cons_shares)) def main(): out = {} # ---- 19#: full enumeration of the level, all recorded spans exact ---------------- P19 = primorial(19) sys.stderr.write("19# = %d, sieving ...\n" % P19) alive = bytearray(b"\x01") * P19 for p in (q for q in PRIMES if q <= 19): alive[0::p] = b"\x00" * len(alive[0::p]) alive[(p - 2) % p::p] = b"\x00" * len(alive[(p - 2) % p::p]) idx = [r for r in range(P19) if alive[r]] gaps = [idx[i + 1] - idx[i] for i in range(len(idx) - 1)] seam = idx[0] + P19 - idx[-1] G = max(max(gaps), seam) starts = [idx[i] for i in range(len(idx) - 1) if gaps[i] == G] + ([idx[-1]] if seam == G else []) sys.stderr.write("19#: K slots=%d A_1=%d (gate %d) spans=%d\n" % (len(idx), G, G2_19, len(starts))) assert G == G2_19, "enumeration disagrees with the ladder value" spans19 = [span_gaps(19, p, G) for p in starts] byL = {} for s in sorted(spans19, key=len): byL.setdefault(len(s), []).append(s) sys.stderr.write("19#: L distribution %s\n" % {k: len(v) for k, v in sorted(byL.items())}) res19 = [] for L, spans in sorted(byL.items()): uniq = sorted({tuple(sorted(s)) for s in spans}) for M in uniq: mine = [s for s in spans if tuple(sorted(s)) == M] n = null(list(M), G, 19) shares = sorted({(G - s[0] - s[-1]) / G for s in mine}) obs = shares[-1] n["observed_shares"] = shares n["observed_share"] = obs n["n_spans_with_this_multiset"] = len(mine) n["p_ge_observed"] = sum(v for sh, v in n.pop("_counts").items() if sh >= obs) / n["n_arrangements"] n["is_max_of_support"] = obs == max(n["support"]) n["observed_end_splits"] = sorted({(s[0], s[-1]) for s in mine}) res19.append(n) out["19#"] = dict(G=G, n_spans=len(starts), L_distribution={k: len(v) for k, v in sorted(byL.items())}, rows=res19) # ---- 43#: the least witness exactly; the other three depth-3 spans from #417 ---- G43, p43 = LADDER[43] M43 = span_gaps(43, p43, G43) sys.stderr.write("43#: least witness %d, span gaps %s (sum %d, G=%d)\n" % (p43, M43, sum(M43), G43)) assert sum(M43) == G43 r43 = null(list(M43), G43, 43) obs = (G43 - M43[0] - M43[-1]) / G43 r43["observed_share"] = obs r43["n_spans_with_this_multiset"] = 1 r43["p_ge_observed"] = sum(v for sh, v in r43.pop("_counts").items() if sh >= obs) / r43["n_arrangements"] r43["is_max_of_support"] = obs == max(r43["support"]) r43["note"] = ("the other three depth-3 43# spans share the anatomy (interior 84, end sum 534, " "return #417) but their individual end split is not in custody; the null depends on it") out["43#"] = dict(G=G43, least_position=p43, n_spans=8, L_distribution={"2": 4, "3": 4}, rows=[r43]) print(json.dumps(out, indent=1)) if __name__ == "__main__": main()