Minor Changes. Plus found a solution for this mathematically online.
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Failing after 19s
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Failing after 19s
This commit is contained in:
9 files changed
+76
-139
No files matched your search
@@ -11,3 +11,24 @@ jobs:
|
|||||||
|
|
||||||
- name: build
|
- name: build
|
||||||
run: make
|
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
|
||||||
@@ -28,7 +28,7 @@ For anyone using nix flakes to enter the developemnt environment with everything
|
|||||||
nix develop .
|
nix develop .
|
||||||
```
|
```
|
||||||
|
|
||||||
## Contributing
|
|
||||||
|
|
||||||
## Sources
|
## Sources
|
||||||
[Stergios Information](https://progintro.github.io/assets/pdf/stergios.pdf)
|
[Stergios Information](https://progintro.github.io/assets/pdf/stergios.pdf)<br>
|
||||||
|
[Similar Question](https://projecteuler.net/problem=876)<br>
|
||||||
|
[The Solver Using Mathematics](https://github.com/cirosantilli/project-euler-solutions/blob/master/solvers/876.py)
|
||||||
@@ -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.)<br>
|
|
||||||
~~Replace the linked-list queue with a circular array. (10–30% speedup.)~~<br>
|
|
||||||
Implement bidirectional BFS. (Potentially 10×–100× speedup for deep searches.)<br>
|
|
||||||
Then start looking for mathematical pruning.
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
# Exponential
|
||||||
|
1
|
||||||
|
|
||||||
|
# Values
|
||||||
|
0
|
||||||
|
32
|
||||||
|
33
|
||||||
|
35
|
||||||
|
39
|
||||||
|
48
|
||||||
|
77
|
||||||
@@ -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
|
||||||
+1
-2
@@ -21,7 +21,6 @@ void try_push_children(Node* curr);
|
|||||||
* @param output The file pointer for output (can be NULL)
|
* @param output The file pointer for output (can be NULL)
|
||||||
* @return 1 if a solution is found, 0 otherwise
|
* @return 1 if a solution is found, 0 otherwise
|
||||||
*/
|
*/
|
||||||
bool solve(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);
|
||||||
int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output);
|
|
||||||
|
|
||||||
#endif /* SOLVE_H */
|
#endif /* SOLVE_H */
|
||||||
@@ -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 <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)
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
gcc $SCRIPT_DIR/calc_i.c -o $SCRIPT_DIR/calc_i
|
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
|
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
|
$SCRIPT_DIR/calc_i $i 0 | sort -n >> $SCRIPT_DIR/../exponentials-values/exponential-$i.txt
|
||||||
done
|
done
|
||||||
|
|||||||
+6
-8
@@ -52,8 +52,7 @@ void try_push_children(Node* curr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool bfs(int128_t* sum, int128_t gcd,
|
static bool bfs(int128_t* sum, int128_t gcd, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) {
|
||||||
int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) {
|
|
||||||
node_pool_reset();
|
node_pool_reset();
|
||||||
queue_clear();
|
queue_clear();
|
||||||
clear_hash_table();
|
clear_hash_table();
|
||||||
@@ -82,10 +81,10 @@ static bool bfs(int128_t* sum, int128_t gcd,
|
|||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
fprintf(output, "No solution found for (");
|
fprintf(output, "No solution found for (");
|
||||||
fprint_int128(output, a0); fprintf(output, ", ");
|
fprint_int128(output, a0 * gcd); fprintf(output, ", ");
|
||||||
fprint_int128(output, b0); fprintf(output, ", ");
|
fprint_int128(output, b0 * gcd); fprintf(output, ", ");
|
||||||
fprint_int128(output, c0);
|
fprint_int128(output, c0 * gcd);
|
||||||
fprintf(output, ".\n");
|
fprintf(output, ").\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,8 +156,7 @@ static bool bfs(int128_t* sum, int128_t gcd,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool solve(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) {
|
||||||
int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) {
|
|
||||||
/* Execute to BFS */
|
/* Execute to BFS */
|
||||||
return bfs(sum, gcd, a0, b0, c0, print_mode, output);
|
return bfs(sum, gcd, a0, b0, c0, print_mode, output);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user