# lambda1906.py -- route 35 rescue (job #1906): the per-admissible-slot twin occupancy rate lambda_hat = (twin pairs with # smaller member in [M, (k+1)M)) / (k * |T_x|), with EXACT exposure over k complete periods starting at the first full # period [M, 2M) (period zero excluded because small primes <= x are not admissible there), twins counted by their # smaller member n with n and n+2 prime. Review 131 refuted #657's lambda_hat (0.126 / 0.148 / 0.121 at x = 11/13/17) # for clipping the exposure; this recomputes it with exact exposure at x = 11, 13, 17 (the periods review 131 named: # 900, 66, 4) and supplies it for the first time at x = 19 (8 periods) and x = 23 (2 periods). # Segmented sieve of Eratosthenes with numpy; per-period twin counts reported so the spread is visible. import sys, math, time, json import numpy as np PR = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] def primorial(x): return math.prod(q for q in PR if q <= x) def slots(x): return math.prod(q - 2 for q in PR if 3 <= q <= x) def base_primes(n): s = np.ones(n + 1, dtype=bool); s[:2] = False for i in range(2, int(n ** 0.5) + 1): if s[i]: s[i * i::i] = False return np.flatnonzero(s) def twin_counts_by_period(x, k, seg=1 << 22): M = primorial(x); lo, hi = M, (k + 1) * M + 2 # need primality up to hi to test n+2 for n < (k+1)M bp = base_primes(int(hi ** 0.5) + 1) counts = np.zeros(k, dtype=np.int64); carry_prime = None; carry_n = None for s in range(lo, hi, seg): e = min(hi, s + seg); a = np.ones(e - s, dtype=bool) for p in bp: st = max(p * p, ((s + p - 1) // p) * p) if st < e: a[st - s::p] = False # twins: n and n+2 prime with n in [s, e); n+2 may fall in the next segment -> handle the two boundary n's via carry idx = np.flatnonzero(a) + s inseg = idx[(idx + 2 < e) & (idx + 2 >= s)] tw = inseg[a[inseg + 2 - s]] # boundary: n = e-2, e-1 checked when the next segment arrives if carry_n is not None: for n in carry_n: if n + 2 >= s and a[n + 2 - s] and n < (k + 1) * M: tw = np.append(tw, n) carry_n = [n for n in (e - 2, e - 1) if n >= s and a[n - s]] tw = tw[tw < (k + 1) * M] if len(tw): per = (tw - M) // M counts += np.bincount(per, minlength=k)[:k] return M, counts out = {} t0 = time.time() for x, k in ((11, 900), (13, 66), (17, 4), (19, 8), (23, 2)): M, counts = twin_counts_by_period(x, k) D = slots(x); lam = counts.sum() / (k * D) out[x] = dict(M=M, periods=k, exposure=[M, (k + 1) * M], slots_per_period=D, twins_per_period=counts.tolist(), twins_total=int(counts.sum()), lambda_hat=lam, lambda_min_period=float(counts.min() / D), lambda_max_period=float(counts.max() / D)) print(f"x={x}: M={M}, {k} complete periods [{M}, {(k+1)*M}), slots/period {D}, twins {int(counts.sum())} " f"(per period min {counts.min()} max {counts.max()}), lambda_hat = {lam:.6f} [per-period range {counts.min()/D:.6f}, {counts.max()/D:.6f}]", flush=True) print(f" x={x} done {time.time()-t0:.1f}s", file=sys.stderr) # control: pi_2(10^6) = 8169 (OEIS A001359), twins with smaller member < 10^6 M, c = twin_counts_by_period(2, 0) if False else (None, None) bp = base_primes(1001) a = np.ones(1000002 + 1, dtype=bool); a[:2] = False for p in bp: a[p * p::p] = False tw = int(np.count_nonzero(a[:1000000] & a[2:1000002])) print(f"control: twin pairs with smaller member < 10^6 = {tw} (OEIS A001359: 8169)") out['control_pi2_1e6'] = tw json.dump(out, open('lambda1906.json', 'w'), indent=0)