"""Own numerical checks for claims IIS-C-2222 (Erdős #15) and IIS-C-2216 (Erdős #726).

Both conjectures.io "disproofs" were verified against formal-conjectures statements
(catalog commit 379fc029) that were later corrected upstream.

#726, old statement: the remainder is the real-number remainder (n : ℝ) % (p : ℝ), which in a
field is n - p * (n / p) = 0, so the filtered sum is empty and the sum is 0 for every n.
The corrected statement takes n % p in ℕ.  Here we evaluate the corrected sum
    S(n) = sum over primes p <= n with p/2 < (n mod p) of 1/p
and compare it with (log log n)/2, only to show the corrected statement is not trivially
false (numerics prove nothing about the limit).

#15, old statement: Summable (over ℚ) of (-1)^(k+1) (k+1) / p_k.  Summability in Mathlib is
unconditional, which forces sum |a_k| = sum (k+1)/p_k < oo, and that sum diverges.  Erdős asked
about the ordinary convergence of the partial sums, the corrected statement.  Here we
print sum_{k<K} (k+1)/p_k (growing like log log or faster) and the alternating partial
sums, whose behaviour is the actual open question.
"""
import math

def primes_upto(N):
    s = bytearray([1]) * (N + 1); s[0] = s[1] = 0
    for i in range(2, int(N ** 0.5) + 1):
        if s[i]: s[i * i::i] = bytearray(len(s[i * i::i]))
    return [i for i in range(N + 1) if s[i]]

def check726(ns, P):
    print("#726 corrected sum S(n) vs (log log n)/2   [old real-remainder sum is 0 for all n]")
    for n in ns:
        S = sum(1.0 / p for p in P if p <= n and p / 2 < n % p)
        print(f"  n={n:>9}  S(n)={S:.4f}  loglog(n)/2={math.log(math.log(n)) / 2:.4f}")

def check15(P):
    print("#15: absolute sum sum_{k<K} (k+1)/p_k (diverges) and alternating partial sums A_K")
    absum = 0.0; alt = 0.0
    marks = {10 ** j for j in range(1, 7)}
    for k, p in enumerate(P):
        absum += (k + 1) / p
        alt += (-1) ** (k + 1) * (k + 1) / p
        if k + 1 in marks:
            print(f"  K={k + 1:>8}  abs-sum={absum:12.2f}  A_K={alt:+.6f}")

if __name__ == "__main__":
    P = primes_upto(16_000_000)
    check726([10 ** 3, 10 ** 4, 10 ** 5, 10 ** 6, 2 * 10 ** 6, 5 * 10 ** 6, 10 ** 7], P)
    check15(P)
    # self-checks: the real remainder of n by p in a field is n - p*(n/p) == 0 exactly (use fractions)
    from fractions import Fraction
    assert all(Fraction(n) - Fraction(p) * (Fraction(n) / Fraction(p)) == 0 for n in range(1, 200) for p in P[:20])
    print("self-check: field remainder n - p*(n/p) is 0 for all tested n, p")
