"""Resolve the 'integer-origin null sees R + 1/2' claim on the tile T_y. R = E[g^2]/(2E[g]) is the note's renewal functional (head-residual-null.md sec 1). The claim under test is exact arithmetic: the mean forward distance from a uniform integer to the next twin slot, against R. Two discrete tie conventions: * NON-STRICT (>=): a position that IS a twin slot has forward distance 0. * STRICT (>): a position that IS a twin slot skips to the NEXT twin slot. Both are exact finite counts over one period, so whichever matches R +/- 1/2 names the convention the record's "+1/2" uses. """ 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]] def run(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]] 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) def mean_fwd(strict): tot = 0 for h in range(W): i = bisect.bisect_right(twins, h) if strict else bisect.bisect_left(twins, h) tot += twins[i] - h if i < m else twins[0] + W - h return tot / W return y, W, m, Eg, Eg2, R, mean_fwd(False), mean_fwd(True) print("y | E[g] | R | uni(>=) | uni(>=)-R | uni(>) | uni(>)-R") for y in [int(a) for a in sys.argv[1:]] or [11, 13, 17, 19]: y, W, m, Eg, Eg2, R, mge, mgt = run(y) print(f"{y:>2} | {Eg:.4f} | R={R:.4f} | {mge:.4f} | {mge-R:+.4f} | {mgt:.4f} | {mgt-R:+.4f}")