#!/usr/bin/env python3 # job66: finite-level check of the sieve prediction for L0/R0 (twin-primes record). # Own computation, no network. numpy sieve to W+2 at x=23. import numpy as np, math, time, sys t0 = time.time() levels = [11, 13, 17, 19, 23] L0_known = {11: 62, 13: 558, 17: 6813, 19: 98340, 23: 1784710} R0_known = {11: 64, 13: 547, 17: 6775, 19: 98245, 23: 1783974} gamma = 0.5772156649 egamma_half = math.exp(gamma) / 2 def primorial(x): return math.prod(p for p in range(2, x + 1) if all(p % q for q in range(2, int(p**0.5) + 1))) Wmax = primorial(23) LIMIT = Wmax + 2 # 223092872 # full sieve on [0, LIMIT] sieve = np.ones(LIMIT + 1, dtype=np.bool_) sieve[:2] = False sieve[4::2] = False for i in range(3, int(LIMIT**0.5) + 1, 2): if sieve[i]: sieve[i * i::2 * i] = False primes = np.flatnonzero(sieve).astype(np.int64) print(f"pi({LIMIT}) = {len(primes)}") print(f"sieve to {LIMIT} done in {time.time()-t0:.1f}s", file=sys.stderr) def pi_upto(n): # number of primes <= n return int(np.searchsorted(primes, n, side='right')) def pi_class_below(n, m, a): # primes r < n, r == a (mod m) k = int(np.searchsorted(primes, n, side='left')) return int(np.count_nonzero(primes[:k] % m == a)) def scour_prime(W): y = math.isqrt(W + 2) while not sieve[y]: y -= 1 return y print() print("level W y N_bar pi(W) phi(W)") rows = {} for x in levels: W = primorial(x) y = scour_prime(W) ps_le_x = [int(p) for p in primes[:pi_upto(x)]] P7 = [p for p in ps_le_x if p >= 7] Nbar = 2 * math.prod(p - 2 for p in P7) phiW = math.prod(p - 1 for p in ps_le_x) piW = pi_upto(W - 1) print(f"x={x:2d} W={W} y={y} N_bar={Nbar} pi(W)={piW} phi(W)={phiW}") rows[x] = (W, y, P7, Nbar, phiW, piW) print() print("=== Task 1: sieve prediction ===") for x in levels: W, y, P7, Nbar, phiW, piW = rows[x] sieve_factor = math.prod(1 - 1 / (p - 1) for p in P7) a11 = pi_class_below(W, 30, 11); a17 = pi_class_below(W, 30, 17) b13 = pi_class_below(W + 2, 30, 13); b19 = pi_class_below(W + 2, 30, 19) # corrections: primes r <= y in classes 11,17 (strict r>y); and ry -> subtract primes s<=y in classes 13,19; also s<=y+2 variant cR_le = pi_class_below(y + 1, 30, 13) + pi_class_below(y + 1, 30, 19) cR_le2 = pi_class_below(y + 3, 30, 13) + pi_class_below(y + 3, 30, 19) L0p = (a11 + a17 - cL_le) * sieve_factor R0p = (b13 + b19 - cR_le) * sieve_factor L0, R0 = L0_known[x], R0_known[x] print(f"x={x:2d}: pi(W;30,11)={a11} pi(W;30,17)={a17} pi(W+2;30,13)={b13} pi(W+2;30,19)={b19}") print(f" corrections: primes<=y in {{11,17}}: {cL_le} (primes y))) R0 = int(np.count_nonzero(sieve[comb + 2] & (comb + 2 > y))) print(f"x={x}: |N|={len(comb)} (N_bar={rows[x][3]}) L0={L0} (expected {L0_known[x]}) R0={R0} (expected {R0_known[x]})") print(f"\ntotal time {time.time()-t0:.1f}s", file=sys.stderr)