"""Exact enumeration of the tile T_y (attack-0830-head-remainder.md section 2a). Definitions used (from the note + its comparators): * y# = primorial of y; a HOLE is a position coprime to y#. * a TWIN SLOT is a position n with both n and n+2 coprime to y#. * the tile is cyclic with period y#. * twin-gaps g are the cyclic distances between consecutive twin slots. * "the continuum functional R of the tile's own gaps": for a stationary renewal process E[forward distance to the next event] = E[g^2]/(2 E[g]), so the uniform-integer origin sees R + 1/2 with R = E[g^2]/(2E[g]) - 1/2, which is exactly the note's stated "integer-origin null sees R + 1/2". * the HOLE-ORIGIN HEAD is the mean over all holes of the forward distance to the next twin slot; the note records it as R + 3.2524, 3.4903, 3.6719, 3.8263, 3.9604 at y = 11, 13, 17, 19, 23. Pure Python stdlib; independent of the note's JS producer. """ import sys 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 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])) # twin slots: n and n+2 both coprime 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]] if not twins: return None # cyclic twin gaps 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) - 0.5 # hole-origin head: mean forward distance hole -> next twin slot (cyclic) import bisect total = 0 for h in holes: i = bisect.bisect_left(twins, h) if i < m: d = twins[i] - h else: d = twins[0] + W - h total += d head = total / len(holes) return dict(y=y, W=W, holes=len(holes), twins=m, Eg=Eg, R=R, head=head, head_minus_R=head - R) EXPECT = {11: 3.2524, 13: 3.4903, 17: 3.6719, 19: 3.8263, 23: 3.9604} for y in [int(a) for a in sys.argv[1:]] or [11, 13, 17, 19]: r = run(y) if r is None: print(y, "no twins"); continue exp = EXPECT.get(y) d = (r["head_minus_R"] - exp) if exp else None print(f"y={r['y']:>2} W={r['W']:>9} holes={r['holes']:>8} twins={r['twins']:>8} " f"E[g]={r['Eg']:.4f} R={r['R']:.4f} head={r['head']:.4f} " f"head-R={r['head_minus_R']:.4f}" + (f" expect {exp} diff {d:+.4f}" if exp else ""))