#!/usr/bin/env python3
"""Thresholds below which the conditional 'if 11(e+pi)=A/D then eventually ...' statements of the
Left to Solve notes (#602628, #614544, #620308, #621339) are vacuous, given D > 10^LOGD.
D bound: e+pi = A/(11D), so the lowest-terms denominator q of e+pi divides 11D and D >= q/11;
the certified CF gives log10 q > 5,000,038.14 (data/cf_result_10000000.json), so log10 D > 5,000,037.1.
Pell bands: p ~ rho^(2n+1)/2, rho = 1+sqrt 2 (note #602626); sieve modulus K_n = floor(2 sqrt n)! (#608728).
All logs are computed with mpmath at 50 digits; every rounding below is in the safe direction (floor)."""
import mpmath as mp
mp.mp.dps = 50
LOGQ = mp.mpf("5000038.14")          # log10 of the proved lower bound 2Q_K+Q_{K-1} (rounded down)
LOGD = LOGQ - mp.log10(11)           # log10 D > this
rho = 1 + mp.sqrt(2)
def band_for_log10p(lp):             # smallest n with rho^(2n+1)/2 >= 10^lp
    return int(mp.ceil(((lp + mp.log10(2)) / mp.log10(rho) - 1) / 2))
rows = []
# (a) #614544: least residue of q mod M=(p^2-1)/2 equals D needs M > D  ->  p > sqrt(2D)
lp = (LOGD + mp.log10(2)) / 2; rows.append(("q = D mod (p^2-1)/2 meaningful (#614544)", lp))
# (b) #602628: m/(p-1) has reduced denominator dividing 11D, (p-1)/(11D) integral -> p > 11D
lp = LOGD + mp.log10(11); rows.append(("(p-1)/(11D) integral (#602628)", lp))
# (c) #620308: Y_p = L - c/p + O(p^-2) with c > 33 D^2 and Y_p > -3 -> p > 33D^2/7
lp = 2 * LOGD + mp.log10(mp.mpf(33) / 7); rows.append(("Y_p expansion / monotonicity (#620308)", lp))
# (d) #621339: forward digit 2(p-1)/(11D^2)-1 >= 1 -> p >= 11 D^2 + 1
lp = 2 * LOGD + mp.log10(11); rows.append(("digit 2(p-1)/(11D^2)-1 positive (#621339)", lp))
print(f"log10 D > {mp.nstr(LOGD, 12)}")
for name, lp in rows:
    print(f"{name:45s} log10 p > {mp.nstr(lp, 12):>18s}   Pell band n >= {band_for_log10p(lp):,}")
# (e) factorial sieve: 11D | K_n = k! needs k! >= 11D  ->  smallest k, then n >= k^2/4
target = LOGQ                       # log10(11D) >= log10 q
lo, hi = 1, 10**8
while lo < hi:
    mid = (lo + hi) // 2
    if mp.loggamma(mid + 1) / mp.log(10) >= target: hi = mid
    else: lo = mid + 1
k = lo; n = (k * k + 3) // 4
print(f"11D | floor(2 sqrt n)! needs k = floor(2 sqrt n) >= {k:,} (log10 k! = {mp.nstr(mp.loggamma(k+1)/mp.log(10), 10)}), so n >= {n:,},"
      f" p ~ rho^(2n+1)/2 with log10 p ~ {mp.nstr((2*n+1)*mp.log10(rho) - mp.log10(2), 8)}")
# (f) what the band 6-8 audits can reach: p <= 2e6
pmax = mp.mpf(2e6)
print("largest D addressable at p <= 2e6:  by (a) D < p^2/2:", mp.nstr(pmax**2 / 2, 6),
      "  by (b) D < p/11:", int(pmax / 11), "  by (c)/(d) D < sqrt(p/11):", int(mp.sqrt(pmax / 11)))
