#!/usr/bin/env python3 """Route 56 engine test: independent references vs the fast exact engines. Two references, both written from the walk definition and sharing no code with the fast engines: ref_kstar : scan every admissible integer in [0, P*prod(Q)) in increasing order (natural order) and take the longest run of killed slots. ref_maxsum : build the tile T_s (admissible residues mod P) by an independent sieve, then evaluate the producer's own span formula (line 176 of attack-0829n-doubling-bridge.js) directly over all |A|*prod(Q) admissible integers of the walk. Cross-checks the two fast engines: longest_run_columns (2-D DP) and walk_gap_maxsum (closed form). Run: python3 route56_walk_test.py """ import sys, os sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from math import gcd FAIL = [] def tile_sieve(P): """Admissible residues r in [0,P) with gcd(r,P)=gcd(r+2,P)=1 (own sieve).""" fac = [] n, d = P, 2 while d * d <= n: if n % d == 0: fac.append(d) while n % d == 0: n //= d d += 1 if n > 1: fac.append(n) return [r for r in range(P) if all(r % p and (r + 2) % p for p in fac)] def ref_kstar(P, Q): R = 1 for q in Q: R *= q M = P * R best = run = 0 for v in range(M): if gcd(v, P) == 1 and gcd(v + 2, P) == 1: if any(v % q == 0 or v % q == (q - 2) % q for q in Q): run += 1 best = max(best, run) else: run = 0 return best def ref_maxsum(P, Q, m): """Producer span formula over the walk, evaluated copy by copy (no shortcuts). Window of m consecutive gaps starting at walk position u = c*nA + i: end position u+m; value of the endpoint = A[(i+m) % nA] + P*floor((i+m)/nA) when the endpoint stays inside the walk list, and the span is endpoint-start. """ A = tile_sieve(P) nA = len(A) R = 1 for q in Q: R *= q N = nA * R best = 0 for c in range(R): for i in range(nA): if c * nA + i + m > N - 1: continue U = c * nA + i + m end = A[U % nA] + P * (U // nA) start = A[i] + c * P span = end - start if span > best: best = span return best def main(): import route56_walk as W cases = [(3, [5]), (5, [7]), (7, [11]), (7, [11, 13]), (11, [13]), (13, [17]), (13, [17, 19]), (13, [17, 19, 23])] for s, Q in cases: P = 1 for p in W.primes_upto(s): P *= p nA = len(tile_sieve(P)) Kref = ref_kstar(P, Q) Kc = W.longest_run_columns(P, Q)[0] Ke = W.exact_walk(P, Q, chunk_copies=512)["K"] Kr = W.run_walk(P, Q, chunk_copies=512)["K"] bad = [] if not (Kref == Kc == Ke == Kr): bad.append("K* ref=%d cols=%d exact=%d run=%d" % (Kref, Kc, Ke, Kr)) R = 1 for q in Q: R *= q if nA * R <= 300000: for m in (1, 2, 3, 5, 7): if m >= nA: continue r = ref_maxsum(P, Q, m) g = W.walk_gap_maxsum(P, Q, m) if r != g: bad.append("maxsum_%d ref=%d engine=%d" % (m, r, g)) print(("OK " if not bad else "FAIL") + " s=%2d Q=%-16s nA=%-8d R=%-8d K*=%d" % (s, str(Q), nA, R, Kref)) for b in bad: print(" ", b) if bad: FAIL.append((s, Q, bad)) # served producer ladder rows (K* values quoted from # research/history/staging/attack-0829n-doubling-bridge.js OUTPUT block) for s, Q, Kwant in [(13, [17, 19, 23], 8), (15, [17, 19, 23, 29], 10), (16, [17, 19, 23, 29, 31], 17), (17, [19, 23, 29, 31], 13)]: P = 1 for p in W.primes_upto(s): P *= p K = W.run_walk(P, Q, chunk_copies=1024)["K"] ok = K == Kwant print(("OK " if ok else "FAIL") + " ladder s=%2d K*=%d (served %d)" % (s, K, Kwant)) if not ok: FAIL.append((s, Q, "ladder K* %d != %d" % (K, Kwant))) print("\nindependent cross-check:", "PASS" if not FAIL else "FAIL") return 1 if FAIL else 0 if __name__ == "__main__": sys.exit(main())