Attempted to separate the snippets of code to small functions and each set of functions have its own file. Also made some improvements by using pools instead of constantly mallocing space.

This commit is contained in:
2026-05-07 02:00:15 +03:00
parent 00396f86fe
commit e3239bf564
15 changed files with 561 additions and 289 deletions
+17
View File
@@ -0,0 +1,17 @@
#ifndef BFS_H
#define BFS_H
#include "utils.h"
/**
* @brief Runs a BFS to find the minimum moves to reach a triplet with zero
* @param sum Pointer to store the sum of moves for the found solution
* @param a0 The initial value of a
* @param b0 The initial value of b
* @param c0 The initial value of c
* @param print_mode The mode for printing the results (1 for full, 2 for minimal)
* @return 1 if a solution is found, 0 otherwise
*/
int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode);
#endif /* BFS_H */
-16
View File
@@ -1,16 +0,0 @@
#ifndef CORE_H
#define CORE_H
__extension__ typedef __int128 int128_t;
__extension__ typedef unsigned __int128 uint128_t;
// Read 128 bit integers as string
int read_int128_triplet(const char* prompt, int128_t* a, int128_t* b, int128_t* c);
// Convert the read string into 128 bit integers
int128_t str_to_int128(const char* s);
// Print 128 but integers
void print_int128(int128_t n);
#endif
+25
View File
@@ -0,0 +1,25 @@
#ifndef HASH_H
#define HASH_H
#include "utils.h"
#define HASH_SIZE 2000003
/**
* @brief Hashes three 128-bit integers into a single unsigned int
* @param a The first integer
* @param b The second integer
* @param c The third integer
* @return A hash value for the three integers
*/
unsigned int hash_three128(uint128_t a, uint128_t b, uint128_t c);
/**
* @brief Sorts a triplet of 128-bit integers in ascending order
* @param a Pointer to the first integer
* @param b Pointer to the second integer
* @param c Pointer to the third integer
*/
void sort_triplet(int128_t* a, int128_t* b, int128_t* c);
#endif /* HASH_H */
Executable
+29
View File
@@ -0,0 +1,29 @@
#ifndef IO_H
#define IO_H
#include "utils.h"
/*
* @brief Reads three 128 bit integers from the user with a prompt
* @param prompt The message to display to the user before reading input
* @param a Pointer to store the first integer
* @param b Pointer to store the second integer
* @param c Pointer to store the third integer
* @return 0 on success, -1 on printf error, -2 on scanf error
*/
int read_int128_triplet(const char* prompt, int128_t* a, int128_t* b, int128_t* c);
/*
* @brief Converts a string to a 128 bit integer
* @param s The string to convert
* @return The converted 128 bit integer
*/
int128_t str_to_int128(const char* s);
/**
* @brief Prints a 128 bit integer
* @param n The integer to print
*/
void print_int128(int128_t n);
#endif
+53 -10
View File
@@ -1,28 +1,71 @@
#ifndef MATHS_H
#define MATHS_H
#include "core.h"
#include "utils.h"
// Check if a 128 bit integer is a prime number or not
int is_prime(int128_t n);
/**
* @brief Checks if a 128 bit integer is a prime number
* @param n The integer to check
* @return true if the integer is prime, false otherwise
*/
bool is_prime(int128_t n);
// Calculate the GCD for 3 128 bit integers
/**
* @brief Checks if a 128 bit integer is within the safe limit
* @param x The integer to check
* @return true if the integer is within the safe limit, false otherwise
*/
bool safe_val(int128_t x);
/**
* @brief Calculates the GCD for 3 128 bit integers
* @param a The first integer
* @param b The second integer
* @param c The third integer
* @return The GCD of the three integers
*/
int128_t gcd3(int128_t a, int128_t b, int128_t c);
// Calculate the GCD for 2 128 bit integers
/**
* @brief Calculates the GCD for 2 128 bit integers
* @param a The first integer
* @param b The second integer
* @return The GCD of the two integers
*/
int128_t gcd2(int128_t a, int128_t b);
// Return the absolute value of a 128 bit integer
/**
* @brief Returns the absolute value of a 128 bit integer
* @param x The integer to get the absolute value of
* @return The absolute value of the integer
*/
int128_t abs128(int128_t x);
// Raise a number a certain exponent
/**
* @brief Calculates b raised to the power of ex for 128 bit integers
* @param b The base integer
* @param ex The exponent integer
* @return The result of b raised to the power of ex
*/
int128_t power(int128_t b, int128_t ex);
// Generate the factor pairs
/**
* @brief Generates factor pairs for a 128 bit integer
* @param sum Pointer to store the sum of the factor pairs
* @param prime_index The index of the prime number to use
* @param exponents Pointer to the array of exponents
* @param values Pointer to the array of values
* @param n1 The first integer
* @param print_mode The mode for printing the results
*/
void generate_factor_pairs(int128_t* sum, int prime_index, int* exponents,
int128_t* values, int128_t n1, int print_mode);
int128_t* values, int128_t n1, int print_mode);
// Calculate the square root of an unsigned 128 bit integer
/**
* @brief Calculates the square root of an unsigned 128 bit integer
* @param number The integer to calculate the square root of
* @return The square root of the integer
*/
uint128_t mysqrt_uint128(uint128_t number);
#endif
+66
View File
@@ -0,0 +1,66 @@
#ifndef NODES_H
#define NODES_H
#include "utils.h"
typedef struct Node {
int128_t a, b, c;
char move;
struct Node* parent;
struct Node* next;
} Node;
/**
* @brief Initializes the node pool with a specified maximum number of nodes
* @param max_nodes The maximum number of nodes the pool can hold
*/
void node_pool_init(int 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
*/
void node_pool_reset();
/**
* @brief Frees the node pool memory
*/
void node_pool_destroy();
/**
* @brief Returns the current usage of the node pool
* @return The number of nodes currently allocated from the pool
*/
int node_pool_usage();
/**
* @brief Tries to push child nodes based on the current node's move
* @param curr The current node
*/
void try_push_children(Node* curr);
typedef struct VisitedEntry {
int128_t a, b, c;
struct VisitedEntry* next;
} VisitedEntry;
/**
* @brief Marks a triplet as visited and checks if it's new
* @param a The first integer of the triplet
* @param b The second integer of the triplet
* @param c The third integer of the triplet
* @return 1 if the triplet is new and marked, 0 if it was already visited
*/
int visit_and_mark(int128_t a, int128_t b, int128_t c);
/**
* @brief Clears the hash table
*/
void clear_hash_table();
#endif /* NODES_H */
+41
View File
@@ -0,0 +1,41 @@
#ifndef QUEUE_H
#define QUEUE_H
#include "utils.h"
#include "node.h"
/**
* @brief Initializes the queue with a maximum number of elements
* @param max_elements The maximum number of elements the queue can hold
*/
void q_init(int max_elements);
/**
* @brief Pushes a node into the queue
* @param n The node to push into the queue
*/
void q_push(Node* n);
/**
* @brief Returns the node from the front of the queue
* @return The node from the front of the queue
*/
Node* q_pop();
/**
* @brief Returns the current size of the queue
* @return The current size of the queue
*/
int q_size();
/**
* @brief Clears the queue without freeing memory
*/
void q_clear();
/**
* @brief Reset the queue memory
*/
void q_destroy();
#endif
+9
View File
@@ -0,0 +1,9 @@
#ifndef UTILS_H
#define UTILS_H
#include <stdbool.h>
__extension__ typedef __int128 int128_t;
__extension__ typedef unsigned __int128 uint128_t;
#endif /* UTILS_H */