Added a way to print out instead of stdout but to a file (everything about the process but errors (those still go to stderr) and made it so now someone can actually use echo -e "" | ./bin/stergios (prior this was impossible to do due to readline quirks)
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 7s
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 7s
This commit is contained in:
10 files changed
+191
-63
No files matched your search
@@ -18,7 +18,8 @@ make --debug linux/windows (Compile with Makefile debug information)
|
|||||||
## Running
|
## Running
|
||||||
```sh
|
```sh
|
||||||
make && ./bin/stergios (Normal version)
|
make && ./bin/stergios (Normal version)
|
||||||
make && echo -e "2\n2\n" | time ./bin/stergios (Run the version for the expontial being 7 and low info printing and time the amount of time it takes to execute)
|
make && echo -e "3\n./exponentials-values/exponential-3.txt\n\n2\n" | time ./bin/stergios (Run the version for the exponential being 3 and low info printing and the amount of time it takes to execute)
|
||||||
|
make && echo -e "2\n\n2\n" | time ./bin/stergios (Run the version for the expontial being 7 and low info printing and the amount of time it takes to execute)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Nix Flakes
|
## Nix Flakes
|
||||||
|
|||||||
@@ -6,6 +6,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Prompts the user for a line of input, handling both interactive and non-interactive modes.
|
||||||
|
* @param prompt The message to display to the user before reading input
|
||||||
|
* @return A dynamically allocated string containing the user's input. The caller is responsible for freeing this.
|
||||||
|
*/
|
||||||
|
char* prompt_line(const char* prompt);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reads a line from the given stream, dynamically allocating memory for the line.
|
* @brief Reads a line from the given stream, dynamically allocating memory for the line.
|
||||||
* The line is terminated by a newline character or EOF. If a '#' character is encountered, the rest of the line is ignored (treated as a comment).
|
* The line is terminated by a newline character or EOF. If a '#' character is encountered, the rest of the line is ignored (treated as a comment).
|
||||||
@@ -33,6 +40,13 @@ int read_int128_triplet(const char* prompt, int128_t* a, int128_t* b, int128_t*
|
|||||||
*/
|
*/
|
||||||
int128_t str_to_int128(const char* s);
|
int128_t str_to_int128(const char* s);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Prints a 128 bit integer
|
||||||
|
* @param output The file pointer to print to (e.g., stdout or a file)
|
||||||
|
* @param n The integer to print
|
||||||
|
*/
|
||||||
|
void fprint_int128(FILE* output, int128_t n);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Prints a 128 bit integer
|
* @brief Prints a 128 bit integer
|
||||||
* @param n The integer to print
|
* @param n The integer to print
|
||||||
|
|||||||
+4
-1
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Checks if a 128 bit integer is a prime number
|
* @brief Checks if a 128 bit integer is a prime number
|
||||||
* @param n The integer to check
|
* @param n The integer to check
|
||||||
@@ -91,9 +93,10 @@ int128_t power(int128_t b, int128_t ex);
|
|||||||
* @param values Pointer to the array of values
|
* @param values Pointer to the array of values
|
||||||
* @param n1 The first integer
|
* @param n1 The first integer
|
||||||
* @param print_mode The mode for printing the results
|
* @param print_mode The mode for printing the results
|
||||||
|
* @param output The file pointer for output (can be NULL)
|
||||||
*/
|
*/
|
||||||
void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponents,
|
void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponents,
|
||||||
int128_t* values, int128_t n1, int print_mode);
|
int128_t* values, int128_t n1, int print_mode, FILE* output);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calculates the square root of an unsigned 128 bit integer
|
* @brief Calculates the square root of an unsigned 128 bit integer
|
||||||
|
|||||||
+2
-6
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
typedef struct Node {
|
typedef struct Node {
|
||||||
int128_t a, b, c;
|
int128_t a, b, c;
|
||||||
char move;
|
char move;
|
||||||
@@ -16,12 +18,6 @@ typedef struct Node {
|
|||||||
*/
|
*/
|
||||||
void node_pool_init(int32_t max_nodes);
|
void node_pool_init(int32_t max_nodes);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Allocates a node from the pool
|
|
||||||
* @return A pointer to the allocated node, or NULL if the pool is exhausted
|
|
||||||
*/
|
|
||||||
Node* node_pool_alloc();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Resets the node pool without freeing memory
|
* @brief Resets the node pool without freeing memory
|
||||||
*/
|
*/
|
||||||
|
|||||||
+2
-1
@@ -17,8 +17,9 @@ void try_push_children(Node* curr);
|
|||||||
* @param b0 The initial value of b
|
* @param b0 The initial value of b
|
||||||
* @param c0 The initial value of c
|
* @param c0 The initial value of c
|
||||||
* @param print_mode The mode for printing the results (1 for full, 2 for minimal)
|
* @param print_mode The mode for printing the results (1 for full, 2 for minimal)
|
||||||
|
* @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 a0, int128_t b0, int128_t c0, int print_mode);
|
bool solve(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output);
|
||||||
|
|
||||||
#endif /* SOLVE_H */
|
#endif /* SOLVE_H */
|
||||||
@@ -1,5 +1,67 @@
|
|||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
|
|
||||||
|
static bool read_line_raw(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 && c == EOF) return 1;
|
||||||
|
|
||||||
|
(*msgptr)[pos] = '\0'; /* Add null byte */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char* prompt_line(const char* prompt) {
|
||||||
|
if (isatty(fileno(stdin))) {
|
||||||
|
char* line = readline(prompt);
|
||||||
|
if (line && *line) add_history(line);
|
||||||
|
return line;
|
||||||
|
} else {
|
||||||
|
printf("%s", prompt);
|
||||||
|
fflush(stdout);
|
||||||
|
char* buf = NULL;
|
||||||
|
size_t size = 0;
|
||||||
|
if (read_line_raw(stdin, &buf, &size)) {
|
||||||
|
free(buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool read_line(FILE* stream, char** msgptr, size_t* n) {
|
bool read_line(FILE* stream, char** msgptr, size_t* n) {
|
||||||
if (msgptr == NULL || n == NULL) return 1;
|
if (msgptr == NULL || n == NULL) return 1;
|
||||||
|
|
||||||
@@ -64,6 +126,20 @@ int128_t str_to_int128(const char* s) {
|
|||||||
return sign * v;
|
return sign * v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void fprint_int128(FILE* output, int128_t n) {
|
||||||
|
if (n == 0) { fprintf(output, "0"); return; }
|
||||||
|
char buf[40];
|
||||||
|
int i = 0;
|
||||||
|
int neg = n < 0;
|
||||||
|
if (neg) n = -n;
|
||||||
|
while (n > 0) {
|
||||||
|
buf[i++] = '0' + n % 10;
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
if (neg) fputc('-', output);
|
||||||
|
while (i--) fputc(buf[i], output);
|
||||||
|
}
|
||||||
|
|
||||||
void print_int128(int128_t n) {
|
void print_int128(int128_t n) {
|
||||||
if (n == 0) { printf("0"); return; }
|
if (n == 0) { printf("0"); return; }
|
||||||
char buf[40];
|
char buf[40];
|
||||||
|
|||||||
+4
-4
@@ -72,7 +72,7 @@ int128_t power(int128_t b, int128_t ex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponents,
|
void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponents,
|
||||||
int128_t* values, int128_t n1, int print_mode) {
|
int128_t* values, int128_t n1, int print_mode, FILE* output) {
|
||||||
if (prime_index == NUM_PRIMES) {
|
if (prime_index == NUM_PRIMES) {
|
||||||
int128_t n2 = values[0] / n1;
|
int128_t n2 = values[0] / n1;
|
||||||
if (n1 > n2) return;
|
if (n1 > n2) return;
|
||||||
@@ -89,12 +89,12 @@ void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponent
|
|||||||
if (is_new_root(r1a, r1b, r1c)) {
|
if (is_new_root(r1a, r1b, r1c)) {
|
||||||
int128_t greater = max2(r1b, r1c);
|
int128_t greater = max2(r1b, r1c);
|
||||||
LIMIT = r1a >= greater ? r1a * greater / 2 : r1b * r1c / 2;
|
LIMIT = r1a >= greater ? r1a * greater / 2 : r1b * r1c / 2;
|
||||||
solve(sum, r1a, r1b, r1c, print_mode);
|
solve(sum, r1a, r1b, r1c, print_mode, output);
|
||||||
}
|
}
|
||||||
if (val2 >= 0 && is_new_root(r2a, r2b, r2c)) {
|
if (val2 >= 0 && is_new_root(r2a, r2b, r2c)) {
|
||||||
int128_t greater = max2(r2b, r2c);
|
int128_t greater = max2(r2b, r2c);
|
||||||
LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2;
|
LIMIT = r2a >= greater ? r2a * greater / 2 : r2b * r2c / 2;
|
||||||
solve(sum, r2a, r2b, r2c, print_mode);
|
solve(sum, r2a, r2b, r2c, print_mode, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -106,7 +106,7 @@ void generate_factor_pairs(int128_t* sum, int32_t prime_index, int32_t* exponent
|
|||||||
|
|
||||||
for (int j = 0; j <= exponent; j++) {
|
for (int j = 0; j <= exponent; j++) {
|
||||||
int128_t next_n1 = n1 * power_of_prime;
|
int128_t next_n1 = n1 * power_of_prime;
|
||||||
generate_factor_pairs(sum, prime_index+1, exponents, values, next_n1, print_mode);
|
generate_factor_pairs(sum, prime_index+1, exponents, values, next_n1, print_mode, output);
|
||||||
if (j < exponent) power_of_prime *= prime;
|
if (j < exponent) power_of_prime *= prime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,14 +31,6 @@ void node_pool_init(int32_t max_nodes) {
|
|||||||
for (size_t i = 0; i < HASH_SIZE; ++i) hash_table[i] = NULL;
|
for (size_t i = 0; i < HASH_SIZE; ++i) hash_table[i] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* node_pool_alloc() {
|
|
||||||
if (pool_ptr >= pool_capacity) {
|
|
||||||
fprintf(stderr, "\nNode Pool Overflow! Need more than %i nodes.\n", pool_capacity);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return &pool[pool_ptr++];
|
|
||||||
}
|
|
||||||
|
|
||||||
void node_pool_reset() {
|
void node_pool_reset() {
|
||||||
pool_ptr = 0;
|
pool_ptr = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-16
@@ -52,12 +52,12 @@ void try_push_children(Node* curr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
static bool bfs(int128_t* sum, 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();
|
||||||
|
|
||||||
Node* root = node_pool_alloc();
|
Node* root = node_pool_next();
|
||||||
if (!root) return false;
|
if (!root) return false;
|
||||||
|
|
||||||
root->a = a0; root->b = b0; root->c = c0;
|
root->a = a0; root->b = b0; root->c = c0;
|
||||||
@@ -100,21 +100,28 @@ static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_
|
|||||||
p = p->parent;
|
p = p->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nSolution found!\n");
|
if (output) {
|
||||||
printf("Initial: (");
|
fprintf(output, "\nSolution found!\n");
|
||||||
print_int128(a0); printf(", ");
|
fprintf(output, "Initial: (");
|
||||||
print_int128(b0); printf(", ");
|
fprint_int128(output, a0); fprintf(output, ", ");
|
||||||
print_int128(c0); printf(")\n");
|
fprint_int128(output, b0); fprintf(output, ", ");
|
||||||
|
fprint_int128(output, c0); fprintf(output, ")\n");
|
||||||
|
|
||||||
printf("Final: (");
|
fprintf(output, "Final: (");
|
||||||
print_int128(found->a); printf(", ");
|
fprint_int128(output, found->a); fprintf(output, ", ");
|
||||||
print_int128(found->b); printf(", ");
|
fprint_int128(output, found->b); fprintf(output, ", ");
|
||||||
print_int128(found->c); printf(")\n");
|
fprint_int128(output, found->c); fprintf(output, ")\n");
|
||||||
|
|
||||||
printf("Moves (%i): %s\n", max_moves, moves);
|
fprintf(output, "Moves (%i): %s\n", max_moves, moves);
|
||||||
|
}
|
||||||
} else if (print_mode == 2) {
|
} else if (print_mode == 2) {
|
||||||
print_int128(c0);
|
if (output) {
|
||||||
printf(" the required amount of moves is: %i\n", max_moves);
|
fprintf(output, "For (");
|
||||||
|
fprint_int128(output, a0); fprintf(output, ", ");
|
||||||
|
fprint_int128(output, b0); fprintf(output, ", ");
|
||||||
|
fprint_int128(output, c0);
|
||||||
|
fprintf(output, ") the required amount of moves is: %i\n", max_moves);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*sum += max_moves;
|
*sum += max_moves;
|
||||||
|
|
||||||
@@ -125,7 +132,7 @@ static bool bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool solve(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
bool solve(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode, FILE* output) {
|
||||||
/* Fallback to BFS */
|
/* Fallback to BFS */
|
||||||
return bfs(sum, a0, b0, c0, print_mode);
|
return bfs(sum, a0, b0, c0, print_mode, output);
|
||||||
}
|
}
|
||||||
+64
-26
@@ -8,8 +8,6 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <readline/readline.h>
|
|
||||||
#include <readline/history.h>
|
|
||||||
|
|
||||||
int128_t LIMIT = 0;
|
int128_t LIMIT = 0;
|
||||||
|
|
||||||
@@ -30,7 +28,7 @@ bool is_new_root(int128_t a, int128_t b, int128_t c) {
|
|||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalEntry* e = malloc(sizeof(GlobalEntry));
|
GlobalEntry* e = malloc(sizeof(*e));
|
||||||
if (!e) return true; /* Fallback to running the solver if malloc fails */
|
if (!e) return true; /* Fallback to running the solver if malloc fails */
|
||||||
e->a = a; e->b = b; e->c = c;
|
e->a = a; e->b = b; e->c = c;
|
||||||
e->next = global_root_table[h];
|
e->next = global_root_table[h];
|
||||||
@@ -60,7 +58,8 @@ int main() {
|
|||||||
int128_t a0 = 10, b0 = 6, c0 = 0, sum = 0;
|
int128_t a0 = 10, b0 = 6, c0 = 0, sum = 0;
|
||||||
|
|
||||||
/* File Specific Settings */
|
/* File Specific Settings */
|
||||||
FILE* file = NULL;
|
FILE* f_input = NULL;
|
||||||
|
FILE* f_output = stdout;
|
||||||
char* input = NULL;
|
char* input = NULL;
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
|
|
||||||
@@ -68,39 +67,66 @@ int main() {
|
|||||||
if (printf("1: manual\n") < 0) return 1;
|
if (printf("1: manual\n") < 0) return 1;
|
||||||
if (printf("2: auto\n") < 0) return 1;
|
if (printf("2: auto\n") < 0) return 1;
|
||||||
if (printf("3: Read file\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;
|
char* line = prompt_line("Choose what mode you want to enter: ");
|
||||||
if (scanf(" %i", &mode) != 1) {
|
if (!line) {
|
||||||
fprintf(stderr, "Input error.\n");
|
fprintf(stderr, "Input error.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
mode = atoi(line);
|
||||||
|
free(line);
|
||||||
if (mode != 1 && mode != 2 && mode != 3) mode = 0;
|
if (mode != 1 && mode != 2 && mode != 3) mode = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == 3) {
|
if (mode == 3) {
|
||||||
char* file_path = NULL;
|
char* file_path = NULL;
|
||||||
while (!file_path || *file_path == '\0') {
|
while (!file_path || *file_path == '\0') {
|
||||||
file_path = readline("Enter file path: ");
|
file_path = prompt_line("Enter the file from which to take data from: ");
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (file_path[i] != '\0') i++;
|
while (file_path[i] != '\0') i++;
|
||||||
if (file_path[i-1] == ' ') file_path[i-1] = '\0';
|
if (file_path[i-1] == ' ') file_path[i-1] = '\0';
|
||||||
if (!(file = fopen(file_path, "r"))) {
|
if (!(f_input = fopen(file_path, "r"))) {
|
||||||
fprintf(stderr, "File not found.\n");
|
fprintf(stderr, "File not found.\n");
|
||||||
free(file_path);
|
free(file_path);
|
||||||
|
file_path = NULL;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
free(file_path);
|
free(file_path);
|
||||||
|
file_path = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode == 2 || mode == 3) {
|
||||||
|
char* file_path = NULL;
|
||||||
|
while (!file_path) {
|
||||||
|
file_path = prompt_line("Enter the file from which write data to (leave empty for console): ");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*file_path != '\0') {
|
||||||
|
int i = 0;
|
||||||
|
while (file_path[i] != '\0') i++;
|
||||||
|
if (file_path[i-1] == ' ') file_path[i-1] = '\0';
|
||||||
|
if (!(f_output = fopen(file_path, "w"))) {
|
||||||
|
fprintf(stderr, "File not found.\n");
|
||||||
|
free(file_path);
|
||||||
|
file_path = NULL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
free(file_path);
|
||||||
|
file_path = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (print_mode == 0) {
|
while (print_mode == 0) {
|
||||||
if (printf("1: Print full?\n") < 0) return 1;
|
if (printf("1: Print full?\n") < 0) return 1;
|
||||||
if (printf("2: Print minimal?\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;
|
char* line = prompt_line("Choose what mode you want to enter: ");
|
||||||
if (scanf(" %i", &print_mode) != 1) {
|
if (!line) {
|
||||||
fprintf(stderr, "Input error.\n");
|
fprintf(stderr, "Input error.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
print_mode = atoi(line);
|
||||||
|
free(line);
|
||||||
if (print_mode != 1 && print_mode != 2) print_mode = 0;
|
if (print_mode != 1 && print_mode != 2) print_mode = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,23 +149,23 @@ int main() {
|
|||||||
int128_t greater = max2(b, c);
|
int128_t greater = max2(b, c);
|
||||||
LIMIT = a >= greater ? a * greater : b * c;
|
LIMIT = a >= greater ? a * greater : b * c;
|
||||||
|
|
||||||
solve(&sum, a, b, c, print_mode);
|
solve(&sum, a, b, c, print_mode, f_output);
|
||||||
} else if (mode == 2) {
|
} else if (mode == 2) {
|
||||||
clear_global_table(); /* Reset unique root tracker */
|
clear_global_table(); /* Reset unique root tracker */
|
||||||
int32_t p = 7;
|
int32_t p = 7;
|
||||||
int32_t exponents[3] = {2*p + 2, p, p};
|
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)};
|
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);
|
generate_factor_pairs(&sum, 0, exponents, values, 1, print_mode, f_output);
|
||||||
|
|
||||||
printf("Total sum is: ");
|
fprintf(f_output, "Total sum is: ");
|
||||||
print_int128(sum); printf("\n");
|
fprint_int128(f_output, sum); fprintf(f_output, "\n");
|
||||||
} else if (mode == 3) {
|
} else if (mode == 3) {
|
||||||
clear_global_table(); /* Reset unique root tracker */
|
clear_global_table(); /* Reset unique root tracker */
|
||||||
if (read_line(file, &input, &size)) return 1;
|
if (read_line(f_input, &input, &size)) return 1;
|
||||||
int p = atoi(input);
|
int p = atoi(input);
|
||||||
a0 = power(a0, p);
|
a0 = power(a0, p);
|
||||||
b0 = power(b0, p);
|
b0 = power(b0, p);
|
||||||
while (!read_line(file, &input, &size)) {
|
while (!read_line(f_input, &input, &size)) {
|
||||||
c0 = str_to_int128(input);
|
c0 = str_to_int128(input);
|
||||||
int128_t gcd = gcd3(a0, b0, c0);
|
int128_t gcd = gcd3(a0, b0, c0);
|
||||||
int128_t ra = a0 / gcd, rb = b0 / gcd, rc = c0 / gcd;
|
int128_t ra = a0 / gcd, rb = b0 / gcd, rc = c0 / gcd;
|
||||||
@@ -147,13 +173,25 @@ int main() {
|
|||||||
if (is_new_root(ra, rb, rc)) {
|
if (is_new_root(ra, rb, rc)) {
|
||||||
int128_t greater = max2(rb, rc);
|
int128_t greater = max2(rb, rc);
|
||||||
LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2;
|
LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2;
|
||||||
solve(&sum, ra, rb, rc, print_mode);
|
solve(&sum, ra, rb, rc, print_mode, f_output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Total sum is: ");
|
fprintf(f_output, "Total sum is: ");
|
||||||
print_int128(sum); printf("\n");
|
fprint_int128(f_output, sum); fprintf(f_output, "\n");
|
||||||
fclose(file);
|
if (f_input) {
|
||||||
|
fclose(f_input);
|
||||||
|
f_input = NULL;
|
||||||
|
}
|
||||||
|
if (f_output && f_output != stdin) {
|
||||||
|
fclose(f_output);
|
||||||
|
f_output = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input) {
|
||||||
|
free(input);
|
||||||
|
input = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
queue_destroy();
|
queue_destroy();
|
||||||
|
|||||||
Reference in new issue
Block a user