# -*- coding: utf-8 -*- """ collatz_verify_Nstar_range.py For n=1 to N, run Collatz trajectory until 1. Use U_step from collatz_k_core for accelerated trajectory on odds. Output CSV: n, steps, ok. Usage: python collatz_verify_Nstar_range.py --Nstar N --output CSV_PATH """ from __future__ import annotations import argparse import csv from pathlib import Path from collatz_k_core import U_step def collatz_steps_to_one(n: int) -> tuple[int, bool]: """ Run Collatz trajectory from n until 1. Returns (steps, ok) where ok=True if trajectory reached 1. Uses U_step for accelerated trajectory on odd numbers. """ steps = 0 while n != 1: if n <= 0: return steps, False if n % 2 == 1: n, a = U_step(n) steps += 1 + a # one 3n+1, then a divisions by 2 else: n = n // 2 steps += 1 return steps, True def main() -> None: parser = argparse.ArgumentParser( description="Verify Collatz conjecture for n=1..N, output CSV" ) parser.add_argument("--Nstar", type=int, required=True, help="Upper bound N (inclusive)") parser.add_argument("--output", required=True, help="Output CSV path") args = parser.parse_args() nstar = args.Nstar if nstar < 1: raise SystemExit("Nstar must be >= 1") out_path = Path(args.output) out_path.parent.mkdir(parents=True, exist_ok=True) with out_path.open("w", newline="", encoding="utf-8") as f: w = csv.writer(f) w.writerow(["n", "steps", "ok"]) for n in range(1, nstar + 1): steps, ok = collatz_steps_to_one(n) w.writerow([n, steps, ok]) if __name__ == "__main__": main()