#!/usr/bin/env python3 """Characterize #594's 'block+phase collapse' reduction for K*(s). #594 (route 23) claims, for base P = prod_{p<=s} p and entering primes Q=(s,2s]: K*(s) >= L IFF some CYCLIC window of L consecutive slots of the single block [0,P) is covered by ONE choice of a 2-set {a, a-2} per prime q in Q. We compute four quantities on small s and compare: TRUE — the exact multi-block walk with the per-block phase a_q(k)=(-kP) mod q (each block carries its OWN phase tuple), over the full period. SINGLE — longest run that lies entirely within ONE block (no boundary crossing), with that block's fixed phase. This is the largest run the single-block single-phase search can honestly certify as a TRUE run when non-wrapping. REDUCED — #594's single-block single-phase cyclic search (free phases, wrapping windows allowed). Wrapping windows are the source of overcount. CROSS — longest run that actually CROSSES a block boundary (needs per-block phases). If CROSS > SINGLE, the reduction under-counts. Pure Python; gcd + modular arithmetic only. """ import math, itertools def primes_upto(n): s = list(range(n+1)); s[0] = s[1] = 0 for p in range(2, int(n**0.5)+1): if s[p]: for m in range(p*p, n+1, p): s[m] = 0 return [i for i in range(2, n+1) if s[i]] def twin_admissible(P): return [r for r in range(P) if math.gcd(r, P) == 1 and math.gcd(r+2, P) == 1] def setup(s): pr = primes_upto(2*s) P = 1 for p in pr: if p <= s: P *= p Q = [p for p in pr if s < p <= 2*s] R = twin_admissible(P) return P, Q, R def killed(r, k, P, q): a = (-k * P) % q return (r % q) in (a % q, (a - 2) % q) def true_and_cross(s): """Return (true_Kstar, cross_Kstar, single_Kstar, example_cross_witness).""" P, Q, R = setup(s) D = len(R) period_blocks = 1 for q in Q: period_blocks *= q N = period_blocks * D # build kill flags for 2 periods (wrap-safe) seq = [] for t in range(2 * N): k = (t // D) % period_blocks i = t % D r = R[i] kk = any(killed(r, k, P, q) for q in Q) seq.append(kk) # longest run (true) true_best = 0; cur = 0 for x in seq: cur = cur + 1 if x else 0 true_best = max(true_best, cur) # cross-block vs single-block: for each run, record whether it crosses a boundary # a run of consecutive slots in seq; boundary between slot index j and j+1 when # (j % D) == D-1 (i.e. last residue of a block -> first residue of next block) cross_best = 0; single_best = 0 # find maximal runs via scanning positions (accounting wrap by scanning N..2N) runs = [] i = 0 while i < len(seq): if not seq[i]: i += 1; continue j = i while j < len(seq) and seq[j]: j += 1 runs.append((i, j)) i = j for (i, j) in runs: L = j - i crosses = any((pos % D) == D-1 for pos in range(i, j-1)) if crosses: cross_best = max(cross_best, L) else: single_best = max(single_best, L) return true_best, cross_best, single_best def reduced_Kstar(s): """#594 single-block single-phase cyclic search (free phases, wrapping).""" P, Q, R = setup(s) D = len(R) def window_coverable(idxs): n = len(idxs) for assign in itertools.product(range(len(Q)), repeat=n): ok = True for qi, q in enumerate(Q): req = None for slot_i, qi2 in enumerate(assign): if qi2 != qi: continue r = R[idxs[slot_i]] ks = {(r % q), ((r + 2) % q)} req = ks if req is None else (req & ks) if req is not None and not req: ok = False break if ok: return True return False best = 0 for L in range(1, D+1): found = False for start in range(D): idxs = [(start + j) % D for j in range(L)] if window_coverable(idxs): found = True break if found: best = L else: break return best if __name__ == "__main__": print(f"{'s':>3} {'P':>6} {'Q':<12} {'TRUE':>5} {'CROSS':>6} {'SINGLE':>6} {'REDUCED':>7}") for s in range(6, 11): P, Q, R = setup(s) if len(Q) > 5: continue t, c, sg = true_and_cross(s) r = reduced_Kstar(s) mark = [] if c > sg: mark.append("CROSS>SINGLE(undercount)") if r != t: mark.append(f"REDUCED!=TRUE({'over' if r>t else 'under'})") print(f"{s:>3} {P:>6} {str(Q):<12} {t:>5} {c:>6} {sg:>6} {r:>7} {'; '.join(mark)}")