#!/usr/bin/env python3 """ Job #1328 / route 25 triage: maxsum_m on the twin-prime residue tile T_31. T_x = { n in [0, x#) : n and n+2 both coprime to x# } (the twin admissible tile) |T_x| = prod_{3<=p<=x} (p-2), gbar(x) = x#/|T_x| m*(T_x) = max{ m : maxsum_m(T_x) < 4*Ghat(x) }, maxsum_m = max span of m consecutive gaps. maxsum_m is strictly increasing in m (gaps are positive), hence m*(T_31) <= 23 <=> maxsum_24(T_31) >= 4*Ghat(31) = 1392, an EXISTENCE statement: one window of 24 consecutive tile elements spanning >= 1392 certifies it. Its negation (m* >= 24) is universal and needs the whole tile. Scans [lo, hi) of the tile directly by sieving residues {0, -2} mod p for odd p <= x (and the odd residue for p = 2). No Copying-Theorem lift, no per-copy census. stdout is the artifact; progress and timing go to stderr. """ import sys, time, argparse import numpy as np ALLP = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] # published ladder (redteam-0830-doubling.js OUTPUT, #584/#587) LEVEL = {29: dict(census=214708725, ghat=258), 31: dict(census=6226553025, ghat=348)} PRIMES, PRIMORIAL, CENSUS, GHAT, BUDGET = [], 1, 0, 0, 0 def set_level(x): global PRIMES, PRIMORIAL, CENSUS, GHAT, BUDGET PRIMES = [p for p in ALLP if p <= x] PRIMORIAL = 1 for p in PRIMES: PRIMORIAL *= p CENSUS, GHAT = LEVEL[x]["census"], LEVEL[x]["ghat"] BUDGET = 4 * GHAT # the census must be the product form, or the tile definition is wrong prod = 1 for p in PRIMES: if p > 2: prod *= (p - 2) assert prod == CENSUS, (prod, CENSUS) def tile_positions(lo, hi): """Absolute n in [lo, hi) with n and n+2 both coprime to x#.""" keep = np.ones(hi - lo, dtype=bool) for p in PRIMES: keep[(-lo) % p::p] = False # n == 0 mod p if p > 2: keep[(-2 - lo) % p::p] = False # n == -2 mod p return lo + np.flatnonzero(keep) def scan(lo, hi, mmin, mmax, block): """max span of m consecutive tile elements, windows starting in [lo, hi).""" best = {m: (-1, None) for m in range(mmin, mmax + 1)} maxgap = (-1, None) carry = np.empty(0, dtype=np.int64) count = 0 t0 = time.time() pos = lo tail = mmax * 4096 # room to close windows crossing hi while pos < hi: end = min(pos + block, hi + tail) cur = tile_positions(pos, end) if end <= hi: count += int(cur.size) else: count += int((cur < hi).sum()) arr = np.concatenate((carry, cur)) if carry.size else cur if arr.size > mmax: starts_all = arr[:-mmax] valid = starts_all < hi g = arr[1:] - arr[:-1] gi = int(np.argmax(g)) if int(g[gi]) > maxgap[0] and arr[gi] < hi: maxgap = (int(g[gi]), int(arr[gi])) for m in range(mmin, mmax + 1): spans = (arr[m:] - arr[:-m])[:starts_all.size][valid] if spans.size: i = int(np.argmax(spans)) if int(spans[i]) > best[m][0]: best[m] = (int(spans[i]), int(starts_all[valid][i])) carry = arr[-mmax:] else: carry = arr if end > hi: break pos = end el = time.time() - t0 print(f" .. {pos-lo:,}/{hi-lo:,} best_{mmax}={best[mmax][0]} " f"{(pos-lo)/el/1e6:.1f} M/s", file=sys.stderr) return best, maxgap, count, time.time() - t0 def main(): ap = argparse.ArgumentParser() ap.add_argument("--x", type=int, default=31, choices=[29, 31]) ap.add_argument("--lo", type=int, default=0) ap.add_argument("--hi", type=int, default=-1, help="-1 = whole tile") ap.add_argument("--mmin", type=int, default=18) ap.add_argument("--mmax", type=int, default=26) ap.add_argument("--block", type=int, default=2 * 10**8) a = ap.parse_args() set_level(a.x) hi = PRIMORIAL if a.hi < 0 else a.hi full = (a.lo == 0 and hi == PRIMORIAL) print(f"# x = {a.x} x# = {PRIMORIAL} census = {CENSUS} " f"gbar = {PRIMORIAL/CENSUS:.6f}") print(f"# Ghat({a.x}) = {GHAT} 4*Ghat = {BUDGET}") print(f"# range [{a.lo}, {hi}) = {(hi-a.lo)/PRIMORIAL*100:.4f}% of the tile") best, maxgap, count, secs = scan(a.lo, hi, a.mmin, a.mmax, a.block) print(f"# tile elements in range: {count}") if full: print(f"# ASSERT census: {count} == {CENSUS} -> {count == CENSUS}") assert count == CENSUS, (count, CENSUS) print(f"# ASSERT max gap: {maxgap[0]} == {GHAT} -> {maxgap[0] == GHAT}") print(f"# max gap seen = {maxgap[0]} at n = {maxgap[1]}") # timing is not part of the artifact: stdout must reproduce byte for byte print(f"elapsed {secs:.1f}s ({(hi-a.lo)/secs/1e6:.1f} M ints/s)", file=sys.stderr) print("#") print("m\tmaxsum_m\tat_n\t\tvs_4Ghat") mstar = None for m in range(a.mmin, a.mmax + 1): s, at = best[m] under = s < BUDGET if under: mstar = m print(f"{m}\t{s}\t\t{at}\t{'<' if under else '>='} {BUDGET}") if full: print(f"#\nm_star(T_{a.x}) = {mstar}") print(f"lambda({a.x}) = {mstar*(PRIMORIAL/CENSUS)/GHAT:.4f}") w = best[24][0] if 24 in best else None if w is not None: print(f"#\nWITNESS for m*(T_{a.x}) <= 23 (maxsum_24 >= {BUDGET}): " f"{'YES' if w >= BUDGET else 'NO'} (best 24-span = {w})") if __name__ == "__main__": main()