"""Riemann zero re-verification, v3.

Two independent implementations, not one:
1. LOCATION (PARI/GP 2.19.1, C): lfunzeros on the Riemann zeta L-function
   finds every zero with imaginary part in [0, 1420] by the argument
   principle plus Z-function sign changes. Output: data/riemann_t.txt,
   one t value per line after a count line.
2. REAL-PART CHECK (mpmath, Python, dps 30): for each PARI t value, one
   Newton step from s0 = 0.5 + i t using zeta and zeta prime. The step is
   free to move off the line; the reported real part is what it gives.
   Also records |zeta(0.5 + i t)| per zero.
3. COUNT CHECK: number of zeros found vs the Riemann-von Mangoldt count
   N(T) at T = t_1000. A missed off-line zero would show as a mismatch.

Run: ./pari/bin/gp -q < compute/riemann_scan.gp > data/riemann_t.txt
     then python3 compute/riemann_verify.py
"""
import hashlib, json, time
from pathlib import Path
from mpmath import mp, mpf, mpc, zeta, pi, log

mp.dps = 30
DATA = Path(__file__).resolve().parent.parent / "data"


def main():
    started = time.time()
    lines = (DATA / "riemann_t.txt").read_text().split()
    count = int(lines[0])
    t_values = [mpf(x) for x in lines[1:]]
    assert len(t_values) == count, f"count line {count} != values {len(t_values)}"
    assert count == 1000, f"expected 1000 zeros, PARI found {count} in [0, 1420]"

    results = []
    max_re_dev = mpf('0')
    max_abs_zeta = mpf('0')
    worst_i = -1
    for i, tn in enumerate(t_values):
        s0 = mpc(mpf('0.5'), tn)
        s1 = s0 - zeta(s0) / zeta(s0, derivative=1)
        re_dev = abs(s1.real - mpf('0.5'))
        az = abs(zeta(s0))
        if re_dev > max_re_dev:
            max_re_dev, worst_i = re_dev, i
        if az > max_abs_zeta:
            max_abs_zeta = az
        results.append({
            "n": i + 1,
            "t_bisect": mp.nstr(tn, 25),
            "re": mp.nstr(s1.real, 25),
            "im": mp.nstr(s1.imag, 25),
            "re_deviation": mp.nstr(re_dev, 5),
            "abs_zeta_at_line": mp.nstr(az, 5),
        })
    total_seconds = time.time() - started

    scan_file = DATA / "riemann_scan_seconds.txt"
    scan_seconds = float(scan_file.read_text().strip()) if scan_file.exists() else None

    T = t_values[-1]
    expected = (T / (2 * pi)) * log(T / (2 * pi)) - T / (2 * pi) + mpf('0.875')
    s_of_T = 1000 - float(expected)

    out = {
        "run": "riemann-zero-verification",
        "date": "2026-11-20",
        "method": "Location: PARI/GP 2.19.1 lfunzeros on the Riemann zeta L-function, interval [0, 1420], realprecision 30. Real-part check: one mpmath Newton step per zero from 0.5 + i t using zeta and zeta prime, dps 30, free to move off the line.",
        "zeros_checked": 1000,
        "t_first": mp.nstr(t_values[0], 25),
        "t_last": mp.nstr(t_values[-1], 25),
        "max_re_deviation": mp.nstr(max_re_dev, 5),
        "worst_zero_n": worst_i + 1,
        "max_abs_zeta_at_line": mp.nstr(max_abs_zeta, 5),
        "von_mangoldt_expected_at_T": float(expected),
        "S_of_T": s_of_T,
        "scan_seconds": scan_seconds,
        "total_seconds": round(total_seconds, 2),
        "zeros": results,
    }
    p = DATA / "riemann_zeros.json"
    p.write_text(json.dumps(out, indent=2))
    print(json.dumps({k: v for k, v in out.items() if k != "zeros"}, indent=2))
    print("results_sha256:", hashlib.sha256(p.read_bytes()).hexdigest())


if __name__ == "__main__":
    main()
