diff --git a/.gitea/workflows/publish.yaml b/.gitea/workflows/publish.yaml index c0d07ad..d44d430 100644 --- a/.gitea/workflows/publish.yaml +++ b/.gitea/workflows/publish.yaml @@ -1,5 +1,5 @@ name: Stergios Upload Compile Checker -run-name: ${{ gitea.actor }} has pushed to ${{ gitea.repository.name }} at ${{ gitea.ref }} +run-name: ${{ gitea.actor }} has pushed an update to Stergios Problem Solver on: [push] # This triggers the build every time you push code jobs: diff --git a/Makefile b/Makefile index 1b57936..50e21d7 100644 --- a/Makefile +++ b/Makefile @@ -143,5 +143,5 @@ windows: # Debug flag .PHONY: debug -debug: CFLAGS += DEBUG +debug: CFLAGS += $(DEBUG) debug: $(OUT) \ No newline at end of file diff --git a/Things to consider doing.md b/Things to consider doing.md new file mode 100644 index 0000000..1760ea2 --- /dev/null +++ b/Things to consider doing.md @@ -0,0 +1,6 @@ +# 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/flake.nix b/flake.nix index 1f4b4f3..9eabb7d 100644 --- a/flake.nix +++ b/flake.nix @@ -16,6 +16,7 @@ gnumake readline gdb + valgrind ]; shellHook = '' diff --git a/include/maths.h b/include/maths.h index 8107c3c..1542d4b 100755 --- a/include/maths.h +++ b/include/maths.h @@ -34,6 +34,23 @@ int128_t gcd3(int128_t a, int128_t b, int128_t c); */ int128_t gcd2(int128_t a, int128_t b); +/** + * @brief Returns the maximum of 3 128 bit integers + * @param a The first integer + * @param b The second integer + * @param c The third integer + * @return The maximum of the three integers + */ +int128_t max3(int128_t a, int128_t b, int128_t c); + +/** + * @brief Returns the maximum of 2 128 bit integers + * @param a The first integer + * @param b The second integer + * @return The maximum of the two integers + */ +int128_t max2(int128_t a, int128_t b); + /** * @brief Returns the minimum of 3 128 bit integers * @param a The first integer diff --git a/src/maths.c b/src/maths.c index a31f49c..b5dd23a 100755 --- a/src/maths.c +++ b/src/maths.c @@ -40,6 +40,14 @@ int128_t gcd2(int128_t a, int128_t b) { return a; } +int128_t max3(int128_t a, int128_t b, int128_t c) { + return max2(max2(a, b), c); +} + +int128_t max2(int128_t a, int128_t b) { + return a > b ? a : b; +} + int128_t min3(int128_t a, int128_t b, int128_t c) { return min2(min2(a, b), c); } @@ -79,12 +87,12 @@ void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponent int128_t r2a = values[1] / gcd2, r2b = values[2] / gcd2, r2c = val2 / gcd2; if (is_new_root(r1a, r1b, r1c)) { - int128_t greater = r1b >= r1c ? r1b : r1c; + int128_t greater = max2(r1b, r1c); LIMIT = r1a >= greater ? r1a * greater / 2 : r1b * r1c / 2; solve(sum, r1a, r1b, r1c, print_mode); } if (val2 >= 0 && is_new_root(r2a, r2b, r2c)) { - int128_t greater = r2b >= r2c ? r2b : r2c; + int128_t greater = max2(r2b, r2c); LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2; solve(sum, r2a, r2b, r2c, print_mode); } diff --git a/src/queue.c b/src/queue.c index 5d96793..3d44c85 100644 --- a/src/queue.c +++ b/src/queue.c @@ -1,63 +1,66 @@ #include "queue.h" + #include #include -static Node* queue_front = NULL; -static Node* queue_back = NULL; -static int32_t current_size = 0; -static int32_t capacity = 0; +static Node** queue; +static int32_t head; +static int32_t tail; +static int32_t size; +static int32_t capacity; void queue_init(int32_t max_elements) { capacity = max_elements; - queue_front = NULL; - queue_back = NULL; - current_size = 0; + queue = malloc(capacity * sizeof(*queue)); + + if (!queue) { + fprintf(stderr,"Queue allocation failed\n"); + exit(1); + } + + head = 0; + tail = 0; + size = 0; } void queue_push(Node* n) { - if (current_size >= capacity) return; /* Pool safety */ + if (size == capacity) return; - /* Exact logic from your 'enqueue' function */ - n->next = NULL; - if (!queue_back) { - queue_front = queue_back = n; - } else { - queue_back->next = n; - queue_back = n; - } - current_size++; + queue[tail] = n; + tail++; + + if (tail == capacity) tail = 0; + + size++; } Node* queue_pop() { - /* Exact logic from your 'dequeue' function */ - if (!queue_front) return NULL; - - Node* r = queue_front; - queue_front = queue_front->next; - - if (!queue_front) { - queue_back = NULL; - } - - r->next = NULL; /* Clean up the link */ - current_size--; - + if (size == 0) return NULL; + + Node* r = queue[head]; + head++; + + if (head == capacity) head = 0; + + size--; return r; } int32_t queue_size() { - return current_size; + return size; } void queue_clear() { - queue_front = NULL; - queue_back = NULL; - current_size = 0; + head = 0; + tail = 0; + size = 0; } void queue_destroy() { - queue_front = NULL; - queue_back = NULL; - current_size = 0; - capacity = 0; + if (queue) { + free(queue); + queue = NULL; + } + + head = tail = size = capacity = 0; } \ No newline at end of file diff --git a/src/solve.c b/src/solve.c index 1e895d0..d1f4d15 100644 --- a/src/solve.c +++ b/src/solve.c @@ -126,24 +126,6 @@ static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_ } bool solve(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) { - /* Try greedy first */ - int32_t g = -1;//greedy(a0, b0, c0); - if (g >= 0) { - *sum += g; - if (print_mode == 1) { - printf("\nSolution found!\n"); - printf("Initial: ("); - print_int128(a0); printf(", "); - print_int128(b0); printf(", "); - print_int128(c0); printf(")\n"); - printf("Moves (%i): [greedy]\n", g); - } else if (print_mode == 2) { - print_int128(c0); - printf(" the required amount of moves is: %i\n", g); - } - return true; - } - /* Fallback to BFS */ return bfs(sum, a0, b0, c0, print_mode); } \ No newline at end of file diff --git a/src/stergios.c b/src/stergios.c index b702b32..9610c62 100644 --- a/src/stergios.c +++ b/src/stergios.c @@ -120,7 +120,7 @@ int main() { int128_t gcd = gcd3(a0, b0, c0); int128_t a = a0 / gcd, b = b0 / gcd, c = c0 / gcd; - int128_t greater = b >= c ? b : c; + int128_t greater = max2(b, c); LIMIT = a >= greater ? a * greater : b * c; solve(&sum, a, b, c, print_mode); @@ -145,7 +145,7 @@ int main() { int128_t ra = a0 / gcd, rb = b0 / gcd, rc = c0 / gcd; if (is_new_root(ra, rb, rc)) { - int128_t greater = rb >= rc ? rb : rc; + int128_t greater = max2(rb, rc); LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2; solve(&sum, ra, rb, rc, print_mode); }