From 067e9bd7fc6f033f3b04c9a1ab1551190d814cba Mon Sep 17 00:00:00 2001 From: Georgios Chatzopoulos Date: Sun, 5 Jul 2026 06:52:11 +0300 Subject: [PATCH] Fixed the no output when given a pair that cannot be computed. Also added more debug information. --- README.md | 2 +- scripts/analyze_stergios.py | 119 ++++++++++++++++++++++++++++++++++++ src/solve.c | 29 ++++++--- src/stergios.c | 16 +++-- 4 files changed, 150 insertions(+), 16 deletions(-) create mode 100644 scripts/analyze_stergios.py diff --git a/README.md b/README.md index 047858a..489206d 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ make --debug linux/windows (Compile with Makefile debug information) ```sh make && ./bin/stergios (Normal version) make && echo -e "3\n./exponentials-values/exponential-3.txt\n\n2\n" | time ./bin/stergios (Run the version for the exponential being 3 and low info printing and the amount of time it takes to execute) -make && echo -e "2\n\n2\n" | time ./bin/stergios (Run the version for the expontial being 7 and low info printing and the amount of time it takes to execute) +make && echo -e "2\n\n2\n7\n" | time ./bin/stergios (Run the version for the expontial being 7 and low info printing and the amount of time it takes to execute) ``` ## Nix Flakes diff --git a/scripts/analyze_stergios.py b/scripts/analyze_stergios.py new file mode 100644 index 0000000..1b739d5 --- /dev/null +++ b/scripts/analyze_stergios.py @@ -0,0 +1,119 @@ +#!/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 ") + 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) \ No newline at end of file diff --git a/src/solve.c b/src/solve.c index 2674d7d..98c9917 100644 --- a/src/solve.c +++ b/src/solve.c @@ -81,7 +81,11 @@ static bool bfs(int128_t* sum, int128_t gcd, } if (!found) { - if (print_mode == 1) printf("No solution found.\n"); + fprintf(output, "No solution found for ("); + fprint_int128(output, a0); fprintf(output, ", "); + fprint_int128(output, b0); fprintf(output, ", "); + fprint_int128(output, c0); + fprintf(output, ".\n"); return false; } @@ -125,15 +129,20 @@ static bool bfs(int128_t* sum, int128_t gcd, } } else if (print_mode == 3) { if (output) { - fprint_int128(output, a0 * gcd); fprintf(output, " "); - fprint_int128(output, b0 * gcd); fprintf(output, " "); - fprint_int128(output, c0 * gcd); fprintf(output, " "); + int128_t a = a0 * gcd, b = b0 * gcd, c = c0 * gcd; + int128_t k = mysqrt_uint128(a * a + b * b + c * c - 2 *a * b - 2 * b * c - 2 * c * a); + fprint_int128(output, a); fprintf(output, " "); + fprint_int128(output, b); fprintf(output, " "); + fprint_int128(output, c); fprintf(output, " "); fprint_int128(output, max_moves); fprintf(output, " "); - fprint_int128(output, c0 * gcd); fprintf(output, " "); - fprint_int128(output, gcd2(a0 * gcd, c0 * gcd)); fprintf(output, " "); - fprint_int128(output, gcd2(b0 * gcd, c0 * gcd)); fprintf(output, " "); - fprint_int128(output, (c0 * gcd) % (b0 * gcd)); fprintf(output, " "); - fprint_int128(output, (c0 * gcd) % (a0 * gcd)); fprintf(output, " "); + fprint_int128(output, gcd2(a, c)); fprintf(output, " "); + fprint_int128(output, gcd2(b, c)); fprintf(output, " "); + fprint_int128(output, c % b); fprintf(output, " "); + fprint_int128(output, c % a); fprintf(output, " "); + fprint_int128(output, k); fprintf(output, " "); + fprint_int128(output, c - a - b - k); fprintf(output, " "); + fprint_int128(output, c - a - b + k); fprintf(output, " "); + fprintf(output, "%s", k < 2 * (int128_t)mysqrt_uint128(a * b) ? "True" : "False"); fprintf(output, "\n"); } } @@ -148,6 +157,6 @@ static bool bfs(int128_t* sum, int128_t gcd, bool solve(int128_t* sum, int128_t gcd, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) { - /* Fallback to BFS */ + /* Execute to BFS */ return bfs(sum, gcd, a0, b0, c0, print_mode, output); } \ No newline at end of file diff --git a/src/stergios.c b/src/stergios.c index 46e5946..bf3102c 100644 --- a/src/stergios.c +++ b/src/stergios.c @@ -138,10 +138,10 @@ int main() { } if (a0 == 0 || b0 == 0 || c0 == 0) { - printf("Already zero present: ("); - print_int128(a0); printf(", "); - print_int128(b0); printf(", "); - print_int128(c0); printf(")\n"); + fprintf(f_output, "Already zero present: ("); + fprint_int128(f_output, a0); fprintf(f_output, ", "); + fprint_int128(f_output, b0); fprintf(f_output, ", "); + fprint_int128(f_output, c0); fprintf(f_output, ")\n"); return 0; } int128_t gcd = gcd3(a0, b0, c0); @@ -153,7 +153,13 @@ int main() { solve(&sum, gcd, a, b, c, print_mode, f_output); } else if (mode == 2) { clear_global_table(); /* Reset unique root tracker */ - int32_t p = 7; + char* line = prompt_line("Choose what exponential you want to use: "); + if (!line) { + fprintf(stderr, "Input error.\n"); + return 1; + } + int32_t p = atoi(line); + free(line); int32_t exponents[3] = {2*p + 2, p, p}; int128_t values[3] = {4 * power(a0, p) * power(b0, p), power(a0, p), power(b0, p)}; generate_factor_pairs(&sum, 0, exponents, values, 1, print_mode, f_output);