"""Riemann zero re-verification: first 1,000 non-trivial zeros.

Method (re-verification of known ground, not a new record):
1. Scan the Riemann-Siegel Z function Z(t) on the critical line in steps
   of 0.05, from t=0.1 upward. Every sign change brackets a zero on the
   line. Bisect each bracket to 30 digits.
2. For each bracketed t_n, run a 2D Newton search (mpmath findroot on the
   complex zeta function) starting at 0.5 + i*t_n. The solver is free to
   move off the line; the reported real part is what it converges to.
3. Count check: compare the number of zeros found against the
   Riemann-von Mangoldt count N(T) = (T/2pi) ln(T/2pi) - T/2pi + 7/8 + S(T).
   A missed off-line zero would show up as a count mismatch.
"""
import hashlib, json, time
from pathlib import Path
from mpmath import mp, mpf, mpc, siegelz, zeta, findroot, pi, log

mp.dps = 30
TARGET = 1000
STEP = mpf('0.05')

def main():
    started = time.time()
    zeros = []          # refined t values from Z(t) bisection
    t = mpf('0.1')
    prev_z = siegelz(t)
    prev_t = t
    while len(zeros) < TARGET:
        t = prev_t + STEP
        z = siegelz(t)
        if prev_z == 0:
            zeros.append(prev_t)
        elif (prev_z < 0 < z) or (z < 0 < prev_z):
            a, b = prev_t, t
            za = prev_z
            for _ in range(80):
                m = (a + b) / 2
                zm = siegelz(m)
                if za * zm <= 0:
                    b = m
                else:
                    a, za = m, zm
            zeros.append((a + b) / 2)
        prev_t, prev_z = t, z
    scan_seconds = time.time() - started

    # Complex Newton refinement: free to leave the line.
    results = []
    max_re_dev = mpf('0')
    max_abs_zeta = mpf('0')
    worst_i = -1
    for i, tn in enumerate(zeros):
        s = findroot(zeta, mpc(mpf('0.5'), tn))
        re_dev = abs(s.real - mpf('0.5'))
        az = abs(zeta(mpc(mpf('0.5'), tn)))
        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(s.real, 25),
            "im": mp.nstr(s.imag, 25),
            "re_deviation": mp.nstr(re_dev, 5),
            "abs_zeta_at_line": mp.nstr(az, 5),
        })
    total_seconds = time.time() - started

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

    out = {
        "run": "riemann-zero-verification",
        "date": "2026-11-20",
        "method": "Riemann-Siegel Z scan step 0.05 plus complex Newton (mpmath findroot) per zero, dps 30",
        "zeros_checked": TARGET,
        "t_first": mp.nstr(zeros[0], 25),
        "t_last": mp.nstr(zeros[-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": round(scan_seconds, 2),
        "total_seconds": round(total_seconds, 2),
        "zeros": results,
    }
    p = Path(__file__).resolve().parent.parent / "data" / "riemann_zeros.json"
    p.write_text(json.dumps(out, indent=2))
    sha = hashlib.sha256(p.read_bytes()).hexdigest()
    print(json.dumps({k: v for k, v in out.items() if k != "zeros"}, indent=2))
    print("results_sha256:", sha)
    print("results_file:", str(p))

if __name__ == "__main__":
    main()
