#include "utils.h" #include "maths.h" #include "hash.h" #include "node.h" #include "queue.h" #include "bfs.h" #include "io.h" #include #include #include #include int128_t LIMIT = 0; typedef struct GlobalEntry { int128_t a, b, c; struct GlobalEntry* next; } GlobalEntry; static GlobalEntry* global_root_table[HASH_SIZE] = {0}; bool is_new_root(int128_t a, int128_t b, int128_t c) { sort_triplet(&a, &b, &c); uint32_t h = hash_three128((uint128_t)a, (uint128_t)b, (uint128_t)c); GlobalEntry* cur = global_root_table[h]; while (cur) { if (cur->a == a && cur->b == b && cur->c == c) return false; cur = cur->next; } GlobalEntry* e = malloc(sizeof(GlobalEntry)); if (!e) return true; /* Fallback to running BFS if malloc fails */ e->a = a; e->b = b; e->c = c; e->next = global_root_table[h]; global_root_table[h] = e; return true; } void clear_global_table() { for (int i = 0; i < HASH_SIZE; i++) { GlobalEntry* cur = global_root_table[i]; while (cur) { GlobalEntry* next = cur->next; free(cur); cur = next; } global_root_table[i] = NULL; } } int main() { /* Make default setup information */ int32_t max_nodes = 200000000; node_pool_init(max_nodes); queue_init(max_nodes); int mode = 0, print_mode = 0; int128_t a0 = 10, b0 = 6, c0 = 0, sum = 0; /* File Specific Settings */ FILE* file = NULL; char* input = NULL; size_t size = 0; while (mode == 0) { if (printf("1: manual\n") < 0) return 1; if (printf("2: auto\n") < 0) return 1; if (printf("3: Read file\n") < 0) return 1; if (printf("Choose what mode you want to enter: ") < 0) return 1; if (scanf(" %i", &mode) != 1) { fprintf(stderr, "Input error.\n"); return 1; } if (mode != 1 && mode != 2 && mode != 3) mode = 0; } if (mode == 3) { char* file_path = NULL; while (!file_path || *file_path == '\0') { file_path = readline("Enter file path: "); } int i = 0; while (file_path[i] != '\0') i++; if (file_path[i-1] == ' ') file_path[i-1] = '\0'; if (!(file = fopen(file_path, "r"))) { fprintf(stderr, "File not found.\n"); free(file_path); return 1; } free(file_path); } while (print_mode == 0) { if (printf("1: Print full?\n") < 0) return 1; if (printf("2: Print minimal?\n") < 0) return 1; if (printf("Choose what mode you want to enter: ") < 0) return 1; if (scanf(" %i", &print_mode) != 1) { fprintf(stderr, "Input error.\n"); return 1; } if (print_mode != 1 && print_mode != 2) print_mode = 0; } if (mode == 1) { if (read_int128_triplet("Enter initial a b c:", &a0, &b0, &c0) != 0) { fprintf(stderr, "Input error.\n"); return 1; } if (a0 == 0 || b0 == 0 || c0 == 0) { printf("Already zero present: ("); print_int128(a0); printf(", "); print_int128(b0); printf(", "); print_int128(c0); printf(")\n"); return 0; } 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; LIMIT = a >= greater ? a * greater : b * c; run_bfs(&sum, a, b, c, print_mode); } else if (mode == 2) { clear_global_table(); /* Reset unique root tracker */ int32_t p = 7; 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); printf("Total sum is: "); print_int128(sum); printf("\n"); } else if (mode == 3) { clear_global_table(); /* Reset unique root tracker */ if (read_line(file, &input, &size)) return 1; int p = atoi(input); a0 = power(a0, p); b0 = power(b0, p); while (!read_line(file, &input, &size)) { c0 = str_to_int128(input); int128_t gcd = gcd3(a0, b0, c0); 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; LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2; run_bfs(&sum, ra, rb, rc, print_mode); } } printf("Total sum is: "); print_int128(sum); printf("\n"); } queue_destroy(); node_pool_destroy(); return 0; }