From 8fe6a6aa17ff5e4db4860a3bf27ed747a3533220 Mon Sep 17 00:00:00 2001 From: Georgios Chatzopoulos Date: Thu, 9 Jul 2026 02:49:29 +0300 Subject: [PATCH] Minor Changes. Plus found a solution for this mathematically online. --- .gitea/workflows/publish.yaml | 23 ++++- README.md | 6 +- Things to consider doing.md | 6 -- exponentials-values/exponential-1.txt | 11 +++ exponentials-values/exponential-2.txt | 33 +++++++ include/solve.h | 3 +- scripts/analyze_stergios.py | 119 -------------------------- scripts/generate-all-exponentials.sh | 2 +- src/solve.c | 14 ++- 9 files changed, 77 insertions(+), 140 deletions(-) delete mode 100644 Things to consider doing.md create mode 100644 exponentials-values/exponential-1.txt create mode 100644 exponentials-values/exponential-2.txt delete mode 100644 scripts/analyze_stergios.py diff --git a/.gitea/workflows/publish.yaml b/.gitea/workflows/publish.yaml index d44d430..2e336f0 100644 --- a/.gitea/workflows/publish.yaml +++ b/.gitea/workflows/publish.yaml @@ -10,4 +10,25 @@ jobs: uses: actions/checkout@v4 - name: build - run: make \ No newline at end of file + run: make + + - name: Run for p = 1 + run: echo -e "3\n./exponentials-values/exponential-1.txt\n\n3\n" | time ./bin/stergios + + - name: Run for p = 2 + run: echo -e "3\n./exponentials-values/exponential-2.txt\n\n3\n" | time ./bin/stergios + + - name: Run for p = 3 + run: echo -e "3\n./exponentials-values/exponential-3.txt\n\n3\n" | time ./bin/stergios + + - name: Run for p = 4 + run: echo -e "3\n./exponentials-values/exponential-4.txt\n\n3\n" | time ./bin/stergios + + - name: Run for p = 5 + run: echo -e "3\n./exponentials-values/exponential-5.txt\n\n3\n" | time ./bin/stergios + + - name: Run for p = 10 + run: echo Not completed yet #echo -e "3\n./exponentials-values/exponential-10.txt\n\n3\n" | time ./bin/stergios + + - name: Run for p = 20 + run: echo Not completed yet #echo -e "3\n./exponentials-values/exponential-20.txt\n\n3\n" | time ./bin/stergios \ No newline at end of file diff --git a/README.md b/README.md index 489206d..45956a2 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ For anyone using nix flakes to enter the developemnt environment with everything nix develop . ``` -## Contributing - ## Sources -[Stergios Information](https://progintro.github.io/assets/pdf/stergios.pdf) \ No newline at end of file +[Stergios Information](https://progintro.github.io/assets/pdf/stergios.pdf)
+[Similar Question](https://projecteuler.net/problem=876)
+[The Solver Using Mathematics](https://github.com/cirosantilli/project-euler-solutions/blob/master/solvers/876.py) \ No newline at end of file diff --git a/Things to consider doing.md b/Things to consider doing.md deleted file mode 100644 index 1760ea2..0000000 --- a/Things to consider doing.md +++ /dev/null @@ -1,6 +0,0 @@ -# Things to try to implement to make the code go faster - -Replace the chained hash table with open addressing. (Probably 2× speedup.)
-~~Replace the linked-list queue with a circular array. (10–30% speedup.)~~
-Implement bidirectional BFS. (Potentially 10×–100× speedup for deep searches.)
-Then start looking for mathematical pruning. \ No newline at end of file diff --git a/exponentials-values/exponential-1.txt b/exponentials-values/exponential-1.txt new file mode 100644 index 0000000..326d201 --- /dev/null +++ b/exponentials-values/exponential-1.txt @@ -0,0 +1,11 @@ +# Exponential +1 + +# Values +0 +32 +33 +35 +39 +48 +77 diff --git a/exponentials-values/exponential-2.txt b/exponentials-values/exponential-2.txt new file mode 100644 index 0000000..4331989 --- /dev/null +++ b/exponentials-values/exponential-2.txt @@ -0,0 +1,33 @@ +# Exponential +2 + +# Values +0 +6 +11 +13 +14 +16 +256 +258 +259 +261 +266 +272 +286 +305 +310 +336 +354 +377 +391 +448 +506 +545 +594 +742 +861 +1040 +1339 +1938 +3737 diff --git a/include/solve.h b/include/solve.h index 88e34c0..3048831 100644 --- a/include/solve.h +++ b/include/solve.h @@ -21,7 +21,6 @@ void try_push_children(Node* curr); * @param output The file pointer for output (can be NULL) * @return 1 if a solution is found, 0 otherwise */ -bool solve(int128_t* sum, int128_t gcd, - int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output); +bool solve(int128_t* sum, int128_t gcd, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output); #endif /* SOLVE_H */ \ No newline at end of file diff --git a/scripts/analyze_stergios.py b/scripts/analyze_stergios.py deleted file mode 100644 index 1b739d5..0000000 --- a/scripts/analyze_stergios.py +++ /dev/null @@ -1,119 +0,0 @@ -#!/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/scripts/generate-all-exponentials.sh b/scripts/generate-all-exponentials.sh index 540e232..c0c153a 100755 --- a/scripts/generate-all-exponentials.sh +++ b/scripts/generate-all-exponentials.sh @@ -1,7 +1,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" gcc $SCRIPT_DIR/calc_i.c -o $SCRIPT_DIR/calc_i -for i in $(seq 3 20); do +for i in $(seq 1 20); do echo -e "# Exponential\n$i\n\n# Values" > $SCRIPT_DIR/../exponentials-values/exponential-$i.txt $SCRIPT_DIR/calc_i $i 0 | sort -n >> $SCRIPT_DIR/../exponentials-values/exponential-$i.txt done diff --git a/src/solve.c b/src/solve.c index 1ec1f4f..93bd3f0 100644 --- a/src/solve.c +++ b/src/solve.c @@ -52,8 +52,7 @@ void try_push_children(Node* curr) { } } -static bool bfs(int128_t* sum, int128_t gcd, - int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) { +static bool bfs(int128_t* sum, int128_t gcd, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) { node_pool_reset(); queue_clear(); clear_hash_table(); @@ -82,10 +81,10 @@ static bool bfs(int128_t* sum, int128_t gcd, if (!found) { 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"); + fprint_int128(output, a0 * gcd); fprintf(output, ", "); + fprint_int128(output, b0 * gcd); fprintf(output, ", "); + fprint_int128(output, c0 * gcd); + fprintf(output, ").\n"); return false; } @@ -157,8 +156,7 @@ static bool bfs(int128_t* sum, int128_t gcd, return true; } -bool solve(int128_t* sum, int128_t gcd, - int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) { +bool solve(int128_t* sum, int128_t gcd, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) { /* Execute to BFS */ return bfs(sum, gcd, a0, b0, c0, print_mode, output); } \ No newline at end of file