#!/usr/bin/env python3 """Positive control for job 4037: reproduce the published Omega table from the definition. Published (`attack-0830-rec-cheapest.md` sec.4.4, "Exact, both s (S3)"): s = 3.0 Omega = 1, 2, 3, 3, 3, 9, 21, 36, 63, 100 at z = 13..47 s = 2.698721 Omega = 1, 2, 3, 6, 10, 18, 22, 30, 45, 63, 86, 111, 134, 168, 205, 251 at z = 13..73 Nothing below counts unless this reproduces. Usage: python control.py [zmax] [s] """ import sys import time from math import log sys.path.insert(0, __file__.rsplit("/", 1)[0].rsplit("\\", 1)[0]) import floor as F # noqa: E402 def floor_fast(fp, fm, ps_asc, zmax=None): """Exact max over admissible splits, by DFS over primes >= 3 (the prime 2 is free).""" k = len(ps_asc) best = [-1 << 60] arg = [None] def dfs(i, m1, m2): if i == k: for both in (0, 1): M1, M2 = m1 | both, m2 | both A1, B1 = fp[M1], -fm[M1] A2, B2 = fp[M2], -fm[M2] v = A1 * A2 + A1 * B2 + B1 * A2 if v > best[0]: best[0] = v arg[0] = (M1, M2, A1, B1, A2, B2) return b = 1 << i dfs(i + 1, m1, m2) dfs(i + 1, m1 | b, m2) dfs(i + 1, m1, m2 | b) if k == 0: dfs(0, 0, 0) else: dfs(1, 0, 0) # index 0 = the prime 2, handled as both/neither return best[0], arg[0] PUB_S3 = [1, 2, 3, 3, 3, 9, 21, 36, 63, 100] PUB_CHEAP = [1, 2, 3, 6, 10, 18, 22, 30, 45, 63, 86, 111, 134, 168, 205, 251] ZS3 = [13, 17, 19, 23, 29, 31, 37, 41, 43, 47] ZCHEAP = [13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73] def run(s, zs, pub, label): print("control: %s s = %.6f" % (label, s)) print(" z D nsup nsm Omega pub match lam+(P) A1 B1 A2 B2 time") allok = True for z, want in zip(zs, pub): t0 = time.time() ps_asc = F.primes_below(z) ps_desc = ps_asc[::-1] D = z ** s sp = F.rosser_support(ps_desc, D, True) sm = F.rosser_support(ps_desc, D, False) fp = F.lam_table(ps_asc, sp) fm = F.lam_table(ps_asc, sm) try: om, arg = floor_fast(fp, fm, ps_asc) except RecursionError: om, arg = None, None lamP = fp[(1 << len(ps_asc)) - 1] got = om ok = (got == want) allok = allok and ok print("%4d %-8.3g %5d %5d %8s %4d %-5s %9d %s %.1fs" % (z, D, len(sp), len(sm), got, want, "ok" if ok else "MISMATCH", lamP, ("" if arg is None else "%3d %4d %3d %4d" % (arg[2], arg[3], arg[4], arg[5])), time.time() - t0)) print("all matched: %s" % allok) return allok if __name__ == "__main__": top = int(sys.argv[1]) if len(sys.argv) > 1 else 47 which = sys.argv[2] if len(sys.argv) > 2 else "both" if which in ("both", "s3"): zs = [z for z in ZS3 if z <= top] run(3.0, zs, PUB_S3[:len(zs)], "s = 3.0 (the record's fixed s)") if which in ("both", "cheap"): zs = [z for z in ZCHEAP if z <= top] run(2.698721, zs, PUB_CHEAP[:len(zs)], "cheapest legal point")