Fixed a major issue in the stergios.c where LIMIT was int32_t instead of int128_t and also implented a version where a user can run the stergios step counter from a file (example format being in the example file).

This commit is contained in:
metallaxis committed 2026-06-28 00:03:49 +03:00
1 parent 9d7b7016d2
commit 7decb09e2f
31 files changed
+65368 -15

No files matched your search

+43 -2
View File
@@ -1,7 +1,48 @@
#include "io.h"
#include <stdio.h>
#include <stdlib.h>
bool read_line(FILE* stream, char** msgptr, size_t* n) {
if (msgptr == NULL || n == NULL) return 1;
if (*msgptr == NULL) {
*n = 128;
*msgptr = malloc(*n);
if (*msgptr == NULL) return 1;
}
while (1) {
size_t pos = 0;
int32_t c;
while ((c = fgetc(stream)) != EOF) {
/* If the string dosent fit, double the size */
if (pos + 1 >= *n) {
size_t new_size = *n * 2;
char* new_ptr = realloc(*msgptr, new_size);
if (new_ptr == NULL) return 1;
*msgptr = new_ptr;
*n = new_size;
}
if (c == '\n' || c == '#') {
/* Skip the info after comments */
if (c == '#') {
/* Read and discard the rest of the line until a newline or EOF */
while ((c = fgetc(stream)) != EOF && c != '\n');
}
break; /* Stop reading for this line. */
}
(*msgptr)[pos++] = (char)c; /* Add the corresponding characters */
}
if (pos == 0) {
if (c == EOF) return 1;
else continue;
}
(*msgptr)[pos] = '\0'; /* Add null byte */
return 0;
}
}
int read_int128_triplet(const char* prompt, int128_t* a, int128_t* b, int128_t* c) {
char sa[256], sb[256], sc[256];
+4 -4
View File
@@ -71,13 +71,13 @@ 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 = values[2] >= val1 ? values[2] : val1;
LIMIT = values[1] >= greater ? values[1] * greater / (2 * gcd1 * gcd1) : values[2] * val1 / (2 * gcd1 * gcd1);
int128_t greater = r1b >= r1c ? r1b : r1c;
LIMIT = r1a >= greater ? r1a * greater / 2 : r1b * r1c / 2;
run_bfs(sum, r1a, r1b, r1c, print_mode);
}
if (val2 >= 0 && is_new_root(r2a, r2b, r2c)) {
int128_t greater = values[2] >= val2 ? values[2] : val2;
LIMIT = values[1] >= greater ? values[1] * greater / (2 * gcd2 * gcd2) : values[2] * val2 / (2 * gcd2 * gcd2);
int128_t greater = r2b >= r2c ? r2b : r2c;
LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2;
run_bfs(sum, r2a, r2b, r2c, print_mode);
}
}
+50 -6
View File
@@ -8,8 +8,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
int32_t LIMIT = 0;
int128_t LIMIT = 0;
typedef struct GlobalEntry {
int128_t a, b, c;
@@ -50,22 +52,45 @@ void clear_global_table() {
int main() {
/* Make default setup information */
int32_t max_nodes = 160000000;
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 = 0;
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) {
@@ -99,8 +124,7 @@ int main() {
LIMIT = a >= greater ? a * greater : b * c;
run_bfs(&sum, a, b, c, print_mode);
}
else if (mode == 2) {
} else if (mode == 2) {
clear_global_table(); /* Reset unique root tracker */
int32_t p = 7;
int32_t exponents[3] = {2*p + 2, p, p};
@@ -109,7 +133,27 @@ int main() {
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();