"""2kwas independent check of iis:2611.00268 (counterexamples to Mandal-Mohan Conjecture 1.28).

Own method (fast doubling, not the paper's 2x2 matrix power):
  F(2k) = F(k) (2F(k+1) - F(k)),  F(2k+1) = F(k)^2 + F(k+1)^2,  all mod m.
1. For each claimed t: t odd, t >= 5, and F_t = -3 (mod 4t).
2. The proof's lemma on small cases, by brute force with exact integers: for every odd t with 5 <= t <= 400,
   Q^(4t) = I (mod F_t), i.e. F_(4t) = 0 and F_(4t+1) = 1 (mod F_t).
3. The implication itself on small cases: for every n with 2 <= n <= 20000, n | (F_1 + ... + F_n) iff
   F_(n+2) = 1 (mod n)  (sum identity), and the paper's criterion "4t | F_t + 3  =>  F_t in A" on every odd
   t <= 400 (where it never triggers, and the conjecture holds there, as expected from the 10^10 search).
"""
def fib_pair(n, m):
    """(F(n) mod m, F(n+1) mod m)."""
    if n == 0:
        return (0, 1 % m)
    a, b = fib_pair(n >> 1, m)
    c = (a * ((2 * b - a) % m)) % m
    d = (a * a + b * b) % m
    return (d, (c + d) % m) if n & 1 else (c, d)

CLAIMED = [11_722_668_338_515_531, 15_655_599_837_762_881, 43_751_231_619_735_900_774_341]

if __name__ == "__main__":
    for t in CLAIMED:
        r = fib_pair(t, 4 * t)[0]
        print(f"t={t}  odd={t % 2 == 1}  t mod 3={t % 3}  F_t mod 4t = 4t-{4 * t - r}  ok={r == 4 * t - 3}")
    print("38713*82613*3665399 == t1:", 38713 * 82613 * 3665399 == CLAIMED[0])

    # 2. lemma Q^(4t) = I mod F_t for odd t
    F = [0, 1]
    while len(F) < 1700:
        F.append(F[-1] + F[-2])
    bad = [t for t in range(5, 401, 2) if not (F[4 * t] % F[t] == 0 and F[4 * t + 1] % F[t] == 1)]
    print("lemma Q^(4t)=I mod F_t, odd 5<=t<=400, failures:", bad)

    # 3a. sum identity and membership test
    S, bad = 0, []
    G = [0, 1]
    for n in range(1, 20003):
        G.append(G[-1] + G[-2])
    for n in range(1, 20001):
        S += G[n]
        if n >= 2 and ((S % n == 0) != (G[n + 2] % n == 1 % n)):
            bad.append(n)
    print("n | sum F_1..F_n  <=>  F_(n+2) = 1 mod n, 2<=n<=20000, failures:", bad[:5])
    # 3b. conjecture on small odd t (direct: F_t | F_(F_t+2) - 1)
    hits = [t for t in range(5, 401, 2) if fib_pair(F[t] + 2, F[t])[0] == 1 % F[t]]
    crit = [t for t in range(5, 401, 2) if (F[t] + 3) % (4 * t) == 0]
    print("odd t<=400 with F_t in A (direct):", hits, " criterion 4t | F_t+3 holds for:", crit)
