Converting from int to int32_t and some cases bool.
This commit is contained in:
12 files changed
+51
-48
No files matched your search
+1
-1
@@ -12,6 +12,6 @@
|
|||||||
* @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)
|
||||||
* @return 1 if a solution is found, 0 otherwise
|
* @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);
|
bool run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode);
|
||||||
|
|
||||||
#endif /* BFS_H */
|
#endif /* BFS_H */
|
||||||
+1
-1
@@ -12,7 +12,7 @@
|
|||||||
* @param c The third integer
|
* @param c The third integer
|
||||||
* @return A hash value for the three integers
|
* @return A hash value for the three integers
|
||||||
*/
|
*/
|
||||||
unsigned int hash_three128(uint128_t a, uint128_t b, uint128_t c);
|
uint32_t hash_three128(uint128_t a, uint128_t b, uint128_t c);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sorts a triplet of 128-bit integers in ascending order
|
* @brief Sorts a triplet of 128-bit integers in ascending order
|
||||||
|
|||||||
+1
-1
@@ -58,7 +58,7 @@ int128_t power(int128_t b, int128_t ex);
|
|||||||
* @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
|
||||||
*/
|
*/
|
||||||
void generate_factor_pairs(int128_t* sum, int prime_index, int* 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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+3
-8
@@ -14,7 +14,7 @@ typedef struct Node {
|
|||||||
* @brief Initializes the node pool with a specified maximum number of nodes
|
* @brief Initializes the node pool with a specified maximum number of nodes
|
||||||
* @param max_nodes The maximum number of nodes the pool can hold
|
* @param max_nodes The maximum number of nodes the pool can hold
|
||||||
*/
|
*/
|
||||||
void node_pool_init(int max_nodes);
|
void node_pool_init(int32_t max_nodes);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Allocates a node from the pool
|
* @brief Allocates a node from the pool
|
||||||
@@ -36,7 +36,7 @@ void node_pool_destroy();
|
|||||||
* @brief Returns the current usage of the node pool
|
* @brief Returns the current usage of the node pool
|
||||||
* @return The number of nodes currently allocated from the pool
|
* @return The number of nodes currently allocated from the pool
|
||||||
*/
|
*/
|
||||||
int node_pool_usage();
|
int32_t node_pool_usage();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Tries to push child nodes based on the current node's move
|
* @brief Tries to push child nodes based on the current node's move
|
||||||
@@ -44,11 +44,6 @@ int node_pool_usage();
|
|||||||
*/
|
*/
|
||||||
void try_push_children(Node* curr);
|
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
|
* @brief Marks a triplet as visited and checks if it's new
|
||||||
* @param a The first integer of the triplet
|
* @param a The first integer of the triplet
|
||||||
@@ -56,7 +51,7 @@ typedef struct VisitedEntry {
|
|||||||
* @param c The third 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
|
* @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);
|
bool visit_and_mark(int128_t a, int128_t b, int128_t c);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Clears the hash table
|
* @brief Clears the hash table
|
||||||
|
|||||||
+2
-2
@@ -8,7 +8,7 @@
|
|||||||
* @brief Initializes the queue with a maximum number of elements
|
* @brief Initializes the queue with a maximum number of elements
|
||||||
* @param max_elements The maximum number of elements the queue can hold
|
* @param max_elements The maximum number of elements the queue can hold
|
||||||
*/
|
*/
|
||||||
void queue_init(int max_elements);
|
void queue_init(int32_t max_elements);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Pushes a node into the queue
|
* @brief Pushes a node into the queue
|
||||||
@@ -26,7 +26,7 @@ Node* queue_pop();
|
|||||||
* @brief Returns the current size of the queue
|
* @brief Returns the current size of the queue
|
||||||
* @return The current size of the queue
|
* @return The current size of the queue
|
||||||
*/
|
*/
|
||||||
int queue_size();
|
int32_t queue_size();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Clears the queue without freeing memory
|
* @brief Clears the queue without freeing memory
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
#define UTILS_H
|
#define UTILS_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
__extension__ typedef __int128 int128_t;
|
__extension__ typedef __int128 int128_t;
|
||||||
__extension__ typedef unsigned __int128 uint128_t;
|
__extension__ typedef unsigned __int128 uint128_t;
|
||||||
|
|||||||
@@ -6,13 +6,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
bool run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode) {
|
||||||
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_alloc();
|
||||||
if (!root) return 0;
|
if (!root) return false;
|
||||||
|
|
||||||
root->a = a0; root->b = b0; root->c = c0;
|
root->a = a0; root->b = b0; root->c = c0;
|
||||||
root->move = '\0'; root->parent = NULL;
|
root->move = '\0'; root->parent = NULL;
|
||||||
@@ -35,20 +35,20 @@ int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode
|
|||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
if (print_mode == 1) printf("No solution found.\n");
|
if (print_mode == 1) printf("No solution found.\n");
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int max_moves = 0;
|
int32_t max_moves = 0;
|
||||||
for (Node* p = found; p && p->move != '\0'; p = p->parent) max_moves++;
|
for (Node* p = found; p && p->move != '\0'; p = p->parent) max_moves++;
|
||||||
|
|
||||||
char* moves;
|
char* moves;
|
||||||
if (print_mode == 1) {
|
if (print_mode == 1) {
|
||||||
moves = malloc(max_moves + 1);
|
moves = malloc(max_moves + 1);
|
||||||
if (!moves) { fprintf(stderr, "Out of memory\n"); return 0; }
|
if (!moves) { fprintf(stderr, "Out of memory\n"); return false; }
|
||||||
moves[max_moves] = '\0';
|
moves[max_moves] = '\0';
|
||||||
|
|
||||||
Node* p = found;
|
Node* p = found;
|
||||||
int idx = max_moves - 1;
|
int32_t idx = max_moves - 1;
|
||||||
while (p && p->move != '\0') {
|
while (p && p->move != '\0') {
|
||||||
moves[idx--] = p->move;
|
moves[idx--] = p->move;
|
||||||
p = p->parent;
|
p = p->parent;
|
||||||
@@ -73,5 +73,5 @@ int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode
|
|||||||
*sum+=max_moves;
|
*sum+=max_moves;
|
||||||
|
|
||||||
if (print_mode == 1) free(moves);
|
if (print_mode == 1) free(moves);
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1,10 +1,10 @@
|
|||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
|
||||||
unsigned int hash_three128(uint128_t a, uint128_t b, uint128_t c) {
|
uint32_t hash_three128(uint128_t a, uint128_t b, uint128_t c) {
|
||||||
uint128_t x = a + (uint128_t)0x9e3779b97f4a7c15ULL + ((uint128_t)0x9e3779b97f4a7c15ULL << 64);
|
uint128_t x = a + (uint128_t)0x9e3779b97f4a7c15ULL + ((uint128_t)0x9e3779b97f4a7c15ULL << 64);
|
||||||
x ^= b + ((uint128_t)0xbf58476d1ce4e5b9ULL + ((uint128_t)0xbf58476d1ce4e5b9ULL << 64)) + (x << 6) + (x >> 2);
|
x ^= b + ((uint128_t)0xbf58476d1ce4e5b9ULL + ((uint128_t)0xbf58476d1ce4e5b9ULL << 64)) + (x << 6) + (x >> 2);
|
||||||
x ^= c + ((uint128_t)0x94d049bb133111ebULL + ((uint128_t)0x94d049bb133111ebULL << 64)) + (x << 6) + (x >> 2);
|
x ^= c + ((uint128_t)0x94d049bb133111ebULL + ((uint128_t)0x94d049bb133111ebULL << 64)) + (x << 6) + (x >> 2);
|
||||||
return (unsigned int)(x % HASH_SIZE);
|
return (uint32_t)(x % HASH_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sort_triplet(int128_t* a, int128_t* b, int128_t* c) {
|
void sort_triplet(int128_t* a, int128_t* b, int128_t* c) {
|
||||||
|
|||||||
+8
-7
@@ -1,14 +1,15 @@
|
|||||||
#include "maths.h"
|
#include "maths.h"
|
||||||
|
#include "node.h"
|
||||||
|
#include "bfs.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
extern int128_t LIMIT;
|
extern int128_t LIMIT;
|
||||||
extern int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode);
|
bool is_new_root(int128_t a, int128_t b, int128_t c);
|
||||||
extern int is_new_root(int128_t a, int128_t b, int128_t c);
|
|
||||||
|
|
||||||
const int NUM_PRIMES = 3;
|
const int32_t NUM_PRIMES = 3;
|
||||||
const int primes[] = {2, 3, 5};
|
const int32_t primes[] = {2, 3, 5};
|
||||||
|
|
||||||
bool is_prime(int128_t n) {
|
bool is_prime(int128_t n) {
|
||||||
if (n < 2) return false;
|
if (n < 2) return false;
|
||||||
@@ -54,7 +55,7 @@ int128_t power(int128_t b, int128_t ex) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate_factor_pairs(int128_t* sum, int prime_index, int* 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) {
|
||||||
if (prime_index == NUM_PRIMES) {
|
if (prime_index == NUM_PRIMES) {
|
||||||
int128_t n2 = values[0] / n1;
|
int128_t n2 = values[0] / n1;
|
||||||
@@ -83,8 +84,8 @@ void generate_factor_pairs(int128_t* sum, int prime_index, int* exponents,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int prime = primes[prime_index];
|
int32_t prime = primes[prime_index];
|
||||||
int exponent = exponents[prime_index];
|
int32_t exponent = exponents[prime_index];
|
||||||
int128_t power_of_prime = 1;
|
int128_t power_of_prime = 1;
|
||||||
|
|
||||||
for (int j = 0; j <= exponent; j++) {
|
for (int j = 0; j <= exponent; j++) {
|
||||||
|
|||||||
+12
-7
@@ -6,14 +6,19 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct VisitedEntry {
|
||||||
|
int128_t a, b, c;
|
||||||
|
struct VisitedEntry* next;
|
||||||
|
} VisitedEntry;
|
||||||
|
|
||||||
static Node* pool = NULL;
|
static Node* pool = NULL;
|
||||||
static VisitedEntry* visited_pool = NULL;
|
static VisitedEntry* visited_pool = NULL;
|
||||||
static VisitedEntry* hash_table[HASH_SIZE] = {0};
|
static VisitedEntry* hash_table[HASH_SIZE] = {0};
|
||||||
static int pool_ptr = 0;
|
static int32_t pool_ptr = 0;
|
||||||
static int pool_capacity = 0;
|
static int32_t pool_capacity = 0;
|
||||||
static int visited_ptr = 0;
|
static int32_t visited_ptr = 0;
|
||||||
|
|
||||||
void node_pool_init(int max_nodes) {
|
void node_pool_init(int32_t max_nodes) {
|
||||||
pool_capacity = max_nodes;
|
pool_capacity = max_nodes;
|
||||||
pool = malloc(pool_capacity * sizeof(Node));
|
pool = malloc(pool_capacity * sizeof(Node));
|
||||||
visited_pool = malloc(pool_capacity * sizeof(VisitedEntry));
|
visited_pool = malloc(pool_capacity * sizeof(VisitedEntry));
|
||||||
@@ -44,7 +49,7 @@ void node_pool_destroy() {
|
|||||||
queue_destroy();
|
queue_destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
int node_pool_usage() {
|
int32_t node_pool_usage() {
|
||||||
return pool_ptr;
|
return pool_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,9 +103,9 @@ void try_push_children(Node* curr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int visit_and_mark(int128_t a, int128_t b, int128_t c) {
|
bool visit_and_mark(int128_t a, int128_t b, int128_t c) {
|
||||||
sort_triplet(&a, &b, &c);
|
sort_triplet(&a, &b, &c);
|
||||||
unsigned int h = hash_three128(a,b,c);
|
uint32_t h = hash_three128(a,b,c);
|
||||||
VisitedEntry* cur = hash_table[h];
|
VisitedEntry* cur = hash_table[h];
|
||||||
while (cur) {
|
while (cur) {
|
||||||
if (cur->a == a && cur->b == b && cur->c == c) return 0;
|
if (cur->a == a && cur->b == b && cur->c == c) return 0;
|
||||||
|
|||||||
+4
-4
@@ -4,10 +4,10 @@
|
|||||||
|
|
||||||
static Node* queue_front = NULL;
|
static Node* queue_front = NULL;
|
||||||
static Node* queue_back = NULL;
|
static Node* queue_back = NULL;
|
||||||
static int current_size = 0;
|
static int32_t current_size = 0;
|
||||||
static int capacity = 0;
|
static int32_t capacity = 0;
|
||||||
|
|
||||||
void queue_init(int max_elements) {
|
void queue_init(int32_t max_elements) {
|
||||||
capacity = max_elements;
|
capacity = max_elements;
|
||||||
queue_front = NULL;
|
queue_front = NULL;
|
||||||
queue_back = NULL;
|
queue_back = NULL;
|
||||||
@@ -45,7 +45,7 @@ Node* queue_pop() {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int queue_size() {
|
int32_t queue_size() {
|
||||||
return current_size;
|
return current_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-8
@@ -9,7 +9,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int LIMIT = 0;
|
int32_t LIMIT = 0;
|
||||||
|
|
||||||
typedef struct GlobalEntry {
|
typedef struct GlobalEntry {
|
||||||
int128_t a, b, c;
|
int128_t a, b, c;
|
||||||
@@ -18,22 +18,22 @@ typedef struct GlobalEntry {
|
|||||||
|
|
||||||
static GlobalEntry* global_root_table[HASH_SIZE] = {0};
|
static GlobalEntry* global_root_table[HASH_SIZE] = {0};
|
||||||
|
|
||||||
int is_new_root(int128_t a, int128_t b, int128_t c) {
|
bool is_new_root(int128_t a, int128_t b, int128_t c) {
|
||||||
sort_triplet(&a, &b, &c);
|
sort_triplet(&a, &b, &c);
|
||||||
unsigned int h = hash_three128((uint128_t)a, (uint128_t)b, (uint128_t)c);
|
uint32_t h = hash_three128((uint128_t)a, (uint128_t)b, (uint128_t)c);
|
||||||
|
|
||||||
GlobalEntry* cur = global_root_table[h];
|
GlobalEntry* cur = global_root_table[h];
|
||||||
while (cur) {
|
while (cur) {
|
||||||
if (cur->a == a && cur->b == b && cur->c == c) return 0;
|
if (cur->a == a && cur->b == b && cur->c == c) return false;
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalEntry* e = malloc(sizeof(GlobalEntry));
|
GlobalEntry* e = malloc(sizeof(GlobalEntry));
|
||||||
if (!e) return 1; /* Fallback to running BFS if malloc fails */
|
if (!e) return true; /* Fallback to running BFS 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];
|
||||||
global_root_table[h] = e;
|
global_root_table[h] = e;
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear_global_table() {
|
void clear_global_table() {
|
||||||
@@ -100,8 +100,8 @@ int main() {
|
|||||||
}
|
}
|
||||||
else if (mode == 2) {
|
else if (mode == 2) {
|
||||||
clear_global_table(); /* Reset unique root tracker */
|
clear_global_table(); /* Reset unique root tracker */
|
||||||
int p = 7;
|
int32_t p = 7;
|
||||||
int 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);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user