"""Riemann zero re-verification, round 5 (4,000 zeros).

Same pipeline as compute/riemann_verify_r4.py:
1. LOCATION (PARI/GP 2.19.1, C): lfunzeros on the Riemann zeta
   L-function. The first 3,000 zeros are reused from round 4
   (data/riemann_zeros_r4.json t values); zeros 3,001-3,016 are the
   16 leftover values of the round 4 upper scan file
   (data/riemann_t_r4_upper.txt, which held 974 zeros of which the
   first 958 were used in round 4); zeros 3,017-4,000 come from a
   new scan on [3545, 4650] (data/riemann_t_r5_upper.txt,
   compute/riemann_scan_r5_upper.gp). The scan was run TWICE and the
   two output files required to be byte-identical (sha256) before
   this script ran. The full [0, 4650] scan was not attempted: the
   full [0, 2560] scan in round 2 overflowed the default 8 MB PARI
   stack, so the interval was split instead. The first 3,000 t values
   of the combined list are checked for an exact match against the
   round 4 file.
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_4000. A missed off-line zero would show as a mismatch.

Run: ./pari/bin/gp -q < compute/riemann_scan_r5_upper.gp > data/riemann_t_r5_upper.txt
     (twice; require identical sha256)
     (first 3,016 t values already in data/riemann_zeros_r4.json and
      data/riemann_t_r4_upper.txt)
     then python3 compute/riemann_verify_r5.py
   The Newton loop checkpoints every 100 zeros to
   data/riemann_verify_r5_progress.json and resumes from it, so a
   killed run restarts where it left off instead of from zero.
   The checkpoint is deterministic per zero, so a resumed run
   produces the same results as an uninterrupted one.
"""
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"
PROGRESS = DATA / "riemann_verify_r5_progress.json"
N_TARGET = 4000
N_PREVIOUS = 3000
SCAN_R1_SECONDS = 44.46
SCAN_R2_SECONDS = 907.3
SCAN_R4_SECONDS = 1699.8


def main():
    started = time.time()
    r4 = json.loads((DATA / "riemann_zeros_r4.json").read_text())
    assert r4["zeros_checked"] == N_PREVIOUS, "round-4 file must hold 3,000 zeros"
    lower = [mpf(z["t_bisect"]) for z in r4["zeros"]]
    assert len(lower) == N_PREVIOUS
    mid_lines = (DATA / "riemann_t_r4_upper.txt").read_text().split()
    mid_count = int(mid_lines[0])
    mid_all = [mpf(x) for x in mid_lines[1:]]
    assert len(mid_all) == mid_count == 974, "round-4 upper file must hold 974 zeros"
    mid = mid_all[N_PREVIOUS - 2042:]
    assert len(mid) == 16, "round-4 upper file must hold 16 zeros past the first 3,000"
    assert mid[0] > lower[-1], "middle zeros must continue above the round-4 last zero"
    upper_lines = (DATA / "riemann_t_r5_upper.txt").read_text().split()
    upper_count = int(upper_lines[0])
    upper_all = [mpf(x) for x in upper_lines[1:]]
    assert len(upper_all) == upper_count, f"count line {upper_count} != values {len(upper_all)}"
    upper = [x for x in upper_all if x > mid[-1]]
    assert len(upper) >= N_TARGET - N_PREVIOUS - len(mid), (
        f"upper scan holds only {len(upper)} new zeros, need {N_TARGET - N_PREVIOUS - len(mid)}")
    assert upper[0] > mid[-1], "upper scan must continue above the middle zeros"
    assert len(lower) + len(mid) + len(upper) >= N_TARGET, (
        f"combined {len(lower) + len(mid) + len(upper)} zeros, need {N_TARGET}")
    t_values = (lower + mid + upper)[:N_TARGET]

    results = []
    max_re_dev = mpf('0')
    max_abs_zeta = mpf('0')
    worst_i = -1
    elapsed_prior = 0.0
    if PROGRESS.exists():
        prog = json.loads(PROGRESS.read_text())
        assert len(prog["results"]) < N_TARGET, "progress file already complete"
        assert all(prog["results"][i]["t_bisect"] == mp.nstr(t_values[i], 25)
                   for i in range(len(prog["results"]))), "progress t values mismatch"
        results = prog["results"]
        max_re_dev = mpf(prog["max_re_deviation"])
        max_abs_zeta = mpf(prog["max_abs_zeta_at_line"])
        worst_i = prog["worst_zero_n"] - 1
        elapsed_prior = prog["elapsed_seconds"]
        print(f"resuming from checkpoint: {len(results)} zeros done", flush=True)
    for i in range(len(results), N_TARGET):
        tn = t_values[i]
        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),
        })
        if (i + 1) % 100 == 0:
            PROGRESS.write_text(json.dumps({
                "results": results,
                "max_re_deviation": mp.nstr(max_re_dev, 5),
                "max_abs_zeta_at_line": mp.nstr(max_abs_zeta, 5),
                "worst_zero_n": worst_i + 1,
                "elapsed_seconds": round(elapsed_prior + time.time() - started, 2),
            }))
            print(f"checkpoint: {i + 1} zeros done", flush=True)
    total_seconds = elapsed_prior + time.time() - started

    # Exact-match check of the first 3,000 t values against round 4.
    first3000_match = all(
        results[i]["t_bisect"] == r4["zeros"][i]["t_bisect"]
        for i in range(N_PREVIOUS))

    scan_file = DATA / "riemann_scan_r5_upper_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 = N_TARGET - float(expected)

    out = {
        "run": "riemann-zero-verification-r5",
        "date": "2026-11-20",
        "method": "Location: PARI/GP 2.19.1 lfunzeros on the Riemann zeta L-function, realprecision 30. First 3,000 zeros reused from round 4 (intervals [0, 1420], [1420, 2560], [2560, 3550]); zeros 3,001-3,016 are the 16 leftover values of the round 4 upper scan; zeros 3,017-4,000 from a new scan on [3545, 4650], run twice with identical sha256. 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": N_TARGET,
        "first_3000_match_round4": first3000_match,
        "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,
        "scan_seconds_round1_reused": SCAN_R1_SECONDS,
        "scan_seconds_round2_reused": SCAN_R2_SECONDS,
        "scan_seconds_round4_reused": SCAN_R4_SECONDS,
        "total_seconds": round(total_seconds, 2),
        "zeros": results,
    }
    p = DATA / "riemann_zeros_r5.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()
