"""Machine checks behind the proof that g(a,b) >= 4 f(a,b) for all coprime a,b >= 2
(Jaffe, arXiv 2610.17267, Conjecture 7.2).  f(a,b) = ab - a - b.

Lemma A.  Let M be squarefree, m an integer, y0 >= 4a-4 with y0 = -a*m (mod M), and suppose
every t in 0..floor(y0/a) has gcd(m+t, M) > 1 or a*t = y0.  Then for every b >= M with
gcd(a,b) = 1, N = a*x0 + b*y0 with x0 = b*m mod M is not expressible, and N >= b*y0 >= 4f(a,b).
(Solutions are (x0+bt, y0-at); x_t = b(m+t), y_t = -a(m+t) mod M, so a prime of gcd(m+t, M)
divides both; if a*t = y0 then y_t = 0 and x_t >= b > 1.)
"""
from math import gcd
from jaffe_g import g_val
from jaffe_exact import check_inexpressible

def lemmaA_ok(a, M, m, y0):
    if y0 < 4*a - 4 or (y0 + a*m) % M: return False
    return all(gcd(m+t, M) > 1 or a*t == y0 for t in range(y0//a + 1))

# certificates for 3 <= a <= 24 (found by search; checked here)
CERT = {3:(10,4,8), 4:(42,6,18), 5:(30,2,20), 6:(110,32,28), 7:(42,32,28), 8:(42,6,36),
        9:(30,2,42), 10:(30,2,40), 11:(66,8,44), 12:(42,6,54), 13:(30,2,64), 14:(30,2,62),
        15:(30,2,60), 16:(30,24,66), 17:(30,24,72), 18:(30,2,84), 19:(30,2,82), 20:(30,2,80),
        21:(30,24,96), 22:(30,2,106), 23:(30,2,104), 24:(30,2,102)}
for a, (M, m, y0) in CERT.items():
    assert lemmaA_ok(a, M, m, y0), a
print("Lemma A certificates for a = 3..24: OK; largest M =", max(c[0] for c in CERT.values()))

# a >= 25: M = 30, m = 2 (run 2,3,4,5,6), y0 in [4a-4, 5a] with y0 = -2a mod 30 (window a+5 >= 30).
def y0_25(a):
    return next(y for y in range(4*a-4, 5*a+1) if (y + 2*a) % 30 == 0)
for a in range(25, 5000):
    assert lemmaA_ok(a, 30, 2, y0_25(a)), a
print("Lemma A with (M, m) = (30, 2) for 25 <= a < 5000: OK (general a: window argument)")

# spot checks of the construction itself, by brute-force enumeration of all solutions
cnt = 0
for a in list(range(3, 40)) + [97, 250, 1001]:
    M, m, y0 = CERT[a] if a in CERT else (30, 2, y0_25(a))
    for b in list(range(M, M + 60)) + [M + 997, 3 * M + 1]:
        if gcd(a, b) != 1: continue
        N = a * ((b*m) % M) + b * y0
        ok, R = check_inexpressible(N, a, b)
        assert ok and N >= 4*(a*b - a - b), (a, b, N)
        cnt += 1
print("brute-force spot checks of the construction:", cnt, "pairs OK")

# a = 2: N = 4b-2 (Jaffe Prop. 5.2) -- checked directly for odd b < 2000
for b in range(3, 2000, 2):
    ok, _ = check_inexpressible(4*b - 2, 2, b); assert ok
print("a = 2: 4b-2 inexpressible for odd 3 <= b < 2000: OK")

# finite part: every coprime pair 3 <= a < b <= 109 (covers b < M_a for a <= 24,
# and 25 <= a < b <= 29).  Find an inexpressible N >= 4f by scanning down from 60ab.
worst = None; n = 0
for b in range(4, 110):
    for a in range(3, b):
        if gcd(a, b) != 1: continue
        N = g_val(a, b, pow(a, -1, b), 60*a*b)       # largest inexpressible N <= 60ab
        f = a*b - a - b
        ok, _ = check_inexpressible(N, a, b)
        assert ok and N >= 4*f, (a, b, N)
        n += 1
        if worst is None or N / f < worst[0]: worst = (N / f, a, b, N)
print("finite part: %d pairs, each has an inexpressible N >= 4f; smallest N/f = %.4f at (a,b)=(%d,%d), N=%d" % ((n,) + worst))
print("ALL CHECKS PASSED")
