#!/usr/bin/env python3
"""2kwas: a sharper modulus for Theorem 1 of iis:2611.00285 (a suggestion, not a correction).
Claim: v_p(G(a)) <= N_p * v_p(C), where N_p = floor(m / z(p)) + 1 bounds the number of window
indices divisible by the rank of apparition z(p). Reason: in the paper's lemma only the d_i with
p | d_i contribute to the p-part of P/D, and if one exponent exceeds v_p(C) it is fully kept in D.
So G(a) | M' = prod_p p^(N_p v_p(C)), and any period of the Fibonacci pair modulo M' is a period of G.
"""
from fractions import Fraction
from math import gcd, lcm, prod
F = [0, 1]
while len(F) < 4000:
    F.append(F[-1] + F[-2])
def factor_small(n):
    out, p = {}, 2
    while p * p <= n:
        while n % p == 0:
            out[p] = out.get(p, 0) + 1; n //= p
        p += 1
    if n > 1: out[n] = out.get(n, 0) + 1
    return out
def z(p):
    i = 1
    while F[i] % p: i += 1
    return i
def pisano_iter(n):
    if n == 1: return 1
    a, b, t = 0, 1, 0
    while True:
        a, b = b, (a + b) % n; t += 1
        if a == 0 and b == 1: return t
def pisano(fs):
    t = 1
    for p, e in fs.items():
        pp, pp2 = pisano_iter(p), pisano_iter(p * p)
        assert pp2 == p * pp
        t = lcm(t, p ** (e - 1) * pp)
    return t
def G(a, m, k):
    d = [F[a + j] ** k for j in range(m + 1)]
    P = prod(d); E = sum(P // x for x in d)
    return gcd(P, E)
def vp(n, p):
    v = 0
    while n % p == 0: n //= p; v += 1
    return v
bad = 0
print(" m k   M (paper)                M' (sharper)          pi(M')        t*  max v_p(G) vs bound")
for m in range(0, 11):
    for k in (1, 2, 3):
        Cf = {p: e * k for p, e in factor_small(prod([1]) if m == 0 else 1).items()}
        L = 1
        for i in range(1, m + 1): L = lcm(L, F[i])
        Cf = {p: e * k for p, e in factor_small(L).items()} if L > 1 else {}
        Mf = {p: c * (m + 1) for p, c in Cf.items()}
        Mpf = {p: c * (m // z(p) + 1) for p, c in Cf.items()}
        Mp = prod(p ** e for p, e in Mpf.items())
        amax = 2400 if m <= 8 else 1200
        g = [None] + [G(a, m, k) for a in range(1, amax + 1)]
        worst = {}
        for a in range(1, amax + 1):
            if Mp % g[a]:
                bad += 1; print("BOUND FAILS", m, k, a, g[a])
            for p in Cf:
                worst[p] = max(worst.get(p, 0), vp(g[a], p))
        piMp = pisano(Mpf) if Mpf else 1
        ok_direct = None
        if piMp + 100 <= amax:
            ok_direct = all(g[a + piMp] == g[a] for a in range(1, amax - piMp + 1))
            if not ok_direct: bad += 1
        t = None
        for tt in range(1, amax // 3 + 1):
            if all(g[a + tt] == g[a] for a in range(1, amax - tt + 1)):
                t = tt; break
        if t and piMp % t: bad += 1; print("t* does not divide pi(M')", m, k)
        fsM = "*".join(f"{p}^{e}" for p, e in sorted(Mf.items())) or "1"
        fsMp = "*".join(f"{p}^{e}" for p, e in sorted(Mpf.items())) or "1"
        wv = ", ".join(f"v{p}:{worst[p]}/{Mpf[p]}" for p in sorted(Cf))
        print(f" {m:2d} {k}   {fsM:24s} {fsMp:20s} {piMp:>10d} {str(t) if t else '>'+str(amax//3):>9s}  {wv}  {'direct period ok' if ok_direct else ('' if ok_direct is None else 'DIRECT FAIL')}")
print("violations:", bad)
