"""Where the head's "R + 1/2" comes from, and which tie rule it is. Claim under test (job 4486, question Q-head-residual). Let T be one period W of the twin tile: the twin slots t_0 = 0 < t_1 < ... < t_{m-1} (coprime to y#, and n + 2 coprime to y#), and the cyclic gaps g_i = t_{i+1} - t_i (mod W, the wrapped gap taken as W - t_{m-1} + t_0), so sum g_i = W. With R = sum g_i^2 / (2 W) ( == E[g^2] / (2 E[g]), the note's continuum functional ), (A) STRICT tie rule (next twin slot strictly above h): mean_h fwd(h) = R + 1/2 (B) NON-STRICT tie rule (a slot at h itself counts at distance 0): mean_h fwd(h) = R - 1/2 Both are exact over the finite population {0, ..., W-1}, and A is what the record calls "the integer-origin null sees R + 1/2 exactly". Proof is the interval summation sum_h fwd = sum_i g_i (g_i + 1)/2 = (sum g_i^2 + W)/2 (strict) sum_h fwd = sum_i g_i (g_i - 1)/2 = (sum g_i^2 - W)/2 (non-strict) so the half is the boundary convention and nothing else. This script CHECKS A and B exactly (Fractions, no floating point, no tolerance) by an independent exhaustive pass, and additionally reports what other origin populations read (odd, even, coprime to 30, holes), because the record's row says the head's forward convention also has "R + 1" exact. Every twin slot is odd (2 | y#, so t coprime to y# implies t odd), so on the 2-adic split both rules agree on the even origins, and mean(all) = ( mean(odd) + mean(even) ) / 2 is exact by the two equal populations. The script asserts the chain that follows: all = R + 1/2, odd = R + 1, even = R (strict), and all = R - 1/2, odd = R - 1, even = R (non-strict); the odd-origin pair differs by exactly 2, the summed gap after the m slots divided by the W/2 odd origins. Python 3, standard library only. No shared code with the note's producers. """ import sys from fractions import Fraction from math import gcd def primes_upto(n): s = bytearray([1]) * (n + 1) s[0] = s[1] = 0 for i in range(2, int(n ** 0.5) + 1): if s[i]: s[i * i::i] = bytearray(len(s[i * i::i])) return [i for i in range(n + 1) if s[i]] def tile(y): """One exact period of the twin tile T_y: (W, twins, gaps, R).""" ps = primes_upto(y) W = 1 for p in ps: W *= p cop = bytearray([1]) * W for p in ps: cop[0::p] = bytearray(len(cop[0::p])) twins = [n for n in range(W) if cop[n] and cop[(n + 2) % W]] m = len(twins) gaps = [(twins[(i + 1) % m] - twins[i]) % W or W for i in range(m)] assert sum(gaps) == W, "gaps must tile the period" R = Fraction(sum(g * g for g in gaps), 2 * W) return W, twins, gaps, R, cop def means_by_population(y): """Exact mean forward distance per origin population, both tie rules.""" W, twins, gaps, R, cop = tile(y) m = len(twins) pops = ("all", "odd", "even", "cop30", "holes") tot = {p: {0: 0, 1: 0} for p in pops} # 0 = non-strict (>=), 1 = strict (>) cnt = {p: 0 for p in pops} idx = 0 # number of twins <= h; twins[idx] is the first twin strictly above h for h in range(W): while idx < m and twins[idx] <= h: idx += 1 up = twins[idx] if idx < m else twins[0] + W fwd_strict = up - h on_slot = idx > 0 and twins[idx - 1] == h fwd_ge = 0 if on_slot else fwd_strict for p in pops: if p == "all": ok = True elif p == "odd": ok = h % 2 == 1 elif p == "even": ok = h % 2 == 0 elif p == "cop30": ok = h > 0 and gcd(h, 30) == 1 else: ok = h > 0 and cop[h] == 1 if ok: tot[p][0] += fwd_ge tot[p][1] += fwd_strict cnt[p] += 1 out = {} for p in pops: out[p] = (Fraction(tot[p][0], cnt[p]) - R, Fraction(tot[p][1], cnt[p]) - R, cnt[p]) return y, W, len(twins), R, out def fmt(fr): return f"{str(fr):>7} ({float(fr):+.6f})" def main(argv): levels = [int(a) for a in argv] or [11, 13, 17, 19] print("Exact check: mean forward distance from a uniform integer origin, minus R.") print("tie rule: '>=' a slot at the origin counts at distance 0; '>' skips to the next slot.") print() print("y | W = y# | twins | R | population | origins | mean(>=) - R | mean(>) - R") bad = [] for y in levels: y, W, m, R, out = means_by_population(y) for p in ("all", "odd", "even", "cop30", "holes"): ge, gt, n = out[p] print(f"{y:>2} | {W:>8} | {m:>6} | {R} | {p:>6} | {n:>8} | {fmt(ge)} | {fmt(gt)}") if p == "all": want_ge, want_gt = Fraction(-1, 2), Fraction(1, 2) elif p == "odd": want_ge, want_gt = Fraction(-1), Fraction(1) elif p == "even": want_ge, want_gt = Fraction(0), Fraction(0) else: continue if ge != want_ge: bad.append((y, p, ">=", str(ge), str(want_ge))) if gt != want_gt: bad.append((y, p, ">", str(gt), str(want_gt))) # consistency of the 2-adic chain: all = (odd + even)/2 exactly if (out["odd"][1] + out["even"][1]) / 2 != out["all"][1]: bad.append((y, "chain", ">", "(odd+even)/2 != all", "")) print() if bad: print("FAIL exact identity:", bad) return 1 print("PASS: at every level, exactly (no tolerance, exact rationals):") print(" strict (>): all = R + 1/2, odd = R + 1, even = R") print(" non-strict (>=): all = R - 1/2, odd = R - 1, even = R") print(" and (mean(odd) + mean(even)) / 2 = mean(all) on both rules.") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))