"""Locate the convention of the section-2a 'hole-origin head' (tile T_y). R = E[g^2]/(2E[g]) is the note's renewal functional (head-residual-null.md sec 1). The note says the uniform-integer origin reads R + 1/2 exactly, and the hole-origin head reads R + 3.2524, 3.4903, 3.6719, 3.8263, 3.9604 at y = 11, 13, 17, 19, 23. This script computes several plausible readings of the head, plus the uniform-integer control, so a mismatch can be attributed to the definition rather than to a defect. """ import sys import bisect 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]] EXPECT = {11: 3.2524, 13: 3.4903, 17: 3.6719, 19: 3.8263, 23: 3.9604} def measure(y): 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]] holes = [n for n in range(W) if cop[n]] twin_set = set(twins) m = len(twins) gaps = [(twins[(i + 1) % m] - twins[i]) % W or W for i in range(m)] Eg = sum(gaps) / m Eg2 = sum(g * g for g in gaps) / m R = Eg2 / (2 * Eg) # note's renewal functional def fwd(src, strict): tot = 0 for h in src: i = bisect.bisect_right(twins, h) if strict else bisect.bisect_left(twins, h) if i < m: tot += twins[i] - h else: tot += twins[0] + W - h return tot / len(src) uni = sum((lambda h: (lambda i: twins[i] - h if i < m else twins[0] + W - h)( bisect.bisect_left(twins, h)))(h) for h in range(W)) / W non_twin = [h for h in holes if h not in twin_set] return dict(y=y, W=W, holes=len(holes), twins=m, Eg=Eg, Eg2=Eg2, R=R, uni=uni, A_all_ge=fwd(holes, False), A_all_gt=fwd(holes, True), C_nt_gt=fwd(non_twin, True), C_nt_ge=fwd(non_twin, False), exp=EXPECT.get(y)) print("y | R | uniformInt | A(all,>=) | A(all,>) | C(nonTwin,>) | C(nonTwin,>=) | expect") for y in [int(a) for a in sys.argv[1:]] or [11, 13, 17, 19]: r = measure(y) print(f"{r['y']:>2} | R={r['R']:.4f} uni={r['uni']:.4f} (uni-R={r['uni']-r['R']:+.4f}) " f"| A>={r['A_all_ge']:.4f}({r['A_all_ge']-r['R']:+.4f}) " f"A>{r['A_all_gt']:.4f}({r['A_all_gt']-r['R']:+.4f}) " f"C>{r['C_nt_gt']:.4f}({r['C_nt_gt']-r['R']:+.4f}) " f"C>={r['C_nt_ge']:.4f}({r['C_nt_ge']-r['R']:+.4f}) exp={r['exp']}")