#!/usr/bin/env python3 """Sympy verification of the route 146 job #2953 arithmetic. Separate from verify_independent.py: here the claims are stated as exact integer/m modular relations and each is solved or refuted with sympy, so the tally is not checked by the same code that produced it. Run: python3 verify_sympy.py (exit 0 iff every claim verifies) """ import sys from sympy import Integer, Mod, Rational, isprime, symbols from sympy.ntheory.residue_ntheory import n_order checks = [] def check(name, ok, detail=""): checks.append((name, bool(ok), detail)) print(f"[{'PASS' if ok else 'FAIL'}] {name} {detail}") PLIST = [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83] PSKIP = [2, 5, 4, 9, 6, 13, 8, 10, 21, 25, 14, 29, 16, 18, 20, 41, 45, 24, 49, 53, 28] def solve_c_p(p): """Solve 6*c = 2 (mod p) for c in Z/pZ with sympy, without reusing pow(6,-1,p).""" c = symbols("c", integer=True) # brute the small modulus with sympy Integer arithmetic for cand in range(p): if Mod(Integer(6) * cand - Integer(2), Integer(p)) == 0: return cand return None def test_c_p_sympy(): bad = [] for p, want in zip(PLIST, PSKIP): got = solve_c_p(p) if got != want: bad.append((p, got, want)) check("sympy solves 6*c = 2 (mod p) to the engine's pskip for every prime", not bad, f"bad={bad}") check("all p in the covered range are prime", all(isprime(p) for p in PLIST)) check("6 is invertible mod every p (gcd = 1)", all(Integer(6).gcd(Integer(p)) == 1 for p in PLIST)) def test_node_tally_sympy(): levels = { 16: (446919, 447305), 17: (1789137, 1789523), 18: (6538348, 6538734), 19: (8603850, 8604236), } diffs = {n: Integer(b) - Integer(a) for n, (a, b) in levels.items()} check("every seeded N=4 - N=1 difference is exactly 386", all(d == 386 for d in diffs.values()), f"{diffs}") prod = Integer(1) for a, _ in levels.values(): prod *= a check("the four N=1 totals are pairwise distinct (four genuinely different levels)", len({a for a, _ in levels.values()}) == 4) check("the largest N=1 total exceeds 15x the smallest", Rational(max(a for a, _ in levels.values()), min(a for a, _ in levels.values())) > 15, f"ratio={Rational(max(a for a,_ in levels.values()), min(a for a,_ in levels.values()))}") def test_worker_identity_sympy(): w = [430582, 438056, 448414, 472471] check("n=17 N=4 worker counts sum to 1789523", Integer(sum(w)) == 1789523, f"sum={sum(w)}") check("worker counts are positive and pairwise distinct (four live workers)", len(set(w)) == 4 and all(x > 0 for x in w)) def test_replicate_band_sympy(): got = Integer(26568575) for label, ref in (("#1542", Integer(26423767)), ("#1532", Integer(26537468))): rel = abs(got - ref) / ref check(f"n=19 replicate within 1% of {label}", rel < Rational(1, 100), f"delta={float(rel):.5%}") factor = Rational(got, 7754620) check("18->19 factor in [3.42, 3.43)", Rational(342, 100) <= factor < Rational(343, 100), f"factor={float(factor):.4f}") def test_value_relations_sympy(): # the engine prints value = 6*best+5; check the two completed value arms for best, value in ((160, 965), (213, 1283)): check(f"value = 6*{best}+5 = {value}", 6 * best + 5 == value) check("the n=17 seed is exactly (a(17)-5)/6 for the published a(17)=965", Rational(965 - 5, 6) == 160) check("213 = (a(19)-5)/6 for the published a(19)=1283", Rational(1283 - 5, 6) == 213) if __name__ == "__main__": test_c_p_sympy() test_node_tally_sympy() test_worker_identity_sympy() test_replicate_band_sympy() test_value_relations_sympy() bad = [c for c in checks if not c[1]] print(f"\n{len(checks)-len(bad)}/{len(checks)} SYMPY CHECKS PASSED") if bad: print("FAILED:", [c[0] for c in bad]) sys.exit(1) print("ALL SYMPY CHECKS PASS")