Files
stergios/scripts/analyze_stergios.py
T
metallaxis 067e9bd7fc
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 4s
Fixed the no output when given a pair that cannot be computed. Also added more debug information.
2026-07-05 06:52:11 +03:00

119 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""
Analyze stergios(a,b,c) data to test candidate closed-form formulas.
Expected input format (one row per line, from your solver's dump):
a b c moves gcd(a,c) gcd(b,c) (c%b) (c%a) k d1 d2
Usage:
python3 analyze_stergios.py your_data_file.txt
"""
import sys
import math
def euclid_sum(x, y):
"""Sum of quotients in the Euclidean algorithm on |x|, |y|."""
x, y = abs(x), abs(y)
if x == 0 or y == 0:
return None
total = 0
while y != 0:
q = x // y
total += q
x, y = y, x % y
return total
def load_rows(path):
rows = []
with open(path) as f:
for line in f:
parts = line.split()
if len(parts) < 11:
continue
try:
a, b, c, moves, gac, gbc, cmb, cma, k, d1, d2 = map(int, parts[:11])
except ValueError:
continue # skip header / "Total sum is:" lines
rows.append(dict(a=a, b=b, c=c, moves=moves, k=k, d1=d1, d2=d2))
return rows
def check_reflection_identity(rows):
"""Verify: stergios(c) == stergios(2(a+b)-c) - 1 for c < a+b."""
lookup = {r['c']: r['moves'] for r in rows}
ok, bad = 0, []
for r in rows:
if r['d1'] < 0 and r['c'] != 0:
c2 = 2 * (r['a'] + r['b']) - r['c']
if c2 in lookup:
pred = lookup[c2] - 1
if pred == r['moves']:
ok += 1
else:
bad.append((r['c'], r['moves'], pred))
print(f"[Reflection identity] {ok}/{ok+len(bad)} match")
for b in bad:
print(" MISMATCH c=%d moves=%d pred=%d" % b)
return ok, bad
def check_euclid_formula(rows):
"""Verify: stergios(c) == euclid_sum(2b, d1) for c > a+b."""
ok, bad = 0, []
for r in rows:
if r['d1'] > 0:
pred = euclid_sum(2 * r['b'], r['d1'])
if pred == r['moves']:
ok += 1
else:
bad.append((r['c'], r['moves'], pred, r['d1']))
print(f"[Euclid(2b,d1) formula] {ok}/{ok+len(bad)} match")
for b in bad:
print(" MISMATCH c=%d moves=%d pred=%d d1=%d" % b)
return ok, bad
def full_prediction(a, b, c):
"""Best-known closed form. Returns None if it can't currently predict
this row (i.e. c falls in the known-unresolved band)."""
Q = (c - a - b) ** 2 - 4 * a * b
if Q < 0:
return None # not solvable (or needs separate check)
k = math.isqrt(Q)
if k * k != Q:
return None # not a perfect square -> not solvable
if c == a + b:
return None # degenerate, not covered
if c < a + b:
c2 = 2 * (a + b) - c
inner = full_prediction(a, b, c2)
return None if inner is None else inner - 1
d1 = (c - a - b) - k
return euclid_sum(2 * b, d1)
def check_full_formula(rows):
ok, bad, unresolved = 0, [], []
for r in rows:
if r['c'] == 0:
continue
pred = full_prediction(r['a'], r['b'], r['c'])
if pred is None:
unresolved.append(r['c'])
elif pred == r['moves']:
ok += 1
else:
bad.append((r['c'], r['moves'], pred))
print(f"[Full formula] {ok} matched, {len(bad)} WRONG, {len(unresolved)} unresolved (formula returned None)")
for b in bad:
print(" WRONG c=%d moves=%d pred=%d" % b)
if unresolved:
print(" Unresolved c values:", unresolved)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python3 analyze_stergios.py <data_file>")
sys.exit(1)
rows = load_rows(sys.argv[1])
print(f"Loaded {len(rows)} rows\n")
check_reflection_identity(rows)
print()
check_euclid_formula(rows)
print()
check_full_formula(rows)