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
@@ -6,13 +6,13 @@
|
||||
#include <stdio.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();
|
||||
queue_clear();
|
||||
clear_hash_table();
|
||||
|
||||
Node* root = node_pool_alloc();
|
||||
if (!root) return 0;
|
||||
if (!root) return false;
|
||||
|
||||
root->a = a0; root->b = b0; root->c = c0;
|
||||
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 (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++;
|
||||
|
||||
char* moves;
|
||||
if (print_mode == 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';
|
||||
|
||||
Node* p = found;
|
||||
int idx = max_moves - 1;
|
||||
int32_t idx = max_moves - 1;
|
||||
while (p && p->move != '\0') {
|
||||
moves[idx--] = p->move;
|
||||
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;
|
||||
|
||||
if (print_mode == 1) free(moves);
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
+2
-2
@@ -1,10 +1,10 @@
|
||||
#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);
|
||||
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);
|
||||
return (unsigned int)(x % HASH_SIZE);
|
||||
return (uint32_t)(x % HASH_SIZE);
|
||||
}
|
||||
|
||||
void sort_triplet(int128_t* a, int128_t* b, int128_t* c) {
|
||||
|
||||
+8
-7
@@ -1,14 +1,15 @@
|
||||
#include "maths.h"
|
||||
#include "node.h"
|
||||
#include "bfs.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern int128_t LIMIT;
|
||||
extern int run_bfs(int128_t* sum, int128_t a0, int128_t b0, int128_t c0, int print_mode);
|
||||
extern 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);
|
||||
|
||||
const int NUM_PRIMES = 3;
|
||||
const int primes[] = {2, 3, 5};
|
||||
const int32_t NUM_PRIMES = 3;
|
||||
const int32_t primes[] = {2, 3, 5};
|
||||
|
||||
bool is_prime(int128_t n) {
|
||||
if (n < 2) return false;
|
||||
@@ -54,7 +55,7 @@ int128_t power(int128_t b, int128_t ex) {
|
||||
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) {
|
||||
if (prime_index == NUM_PRIMES) {
|
||||
int128_t n2 = values[0] / n1;
|
||||
@@ -83,8 +84,8 @@ void generate_factor_pairs(int128_t* sum, int prime_index, int* exponents,
|
||||
return;
|
||||
}
|
||||
|
||||
int prime = primes[prime_index];
|
||||
int exponent = exponents[prime_index];
|
||||
int32_t prime = primes[prime_index];
|
||||
int32_t exponent = exponents[prime_index];
|
||||
int128_t power_of_prime = 1;
|
||||
|
||||
for (int j = 0; j <= exponent; j++) {
|
||||
|
||||
+12
-7
@@ -6,14 +6,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct VisitedEntry {
|
||||
int128_t a, b, c;
|
||||
struct VisitedEntry* next;
|
||||
} VisitedEntry;
|
||||
|
||||
static Node* pool = NULL;
|
||||
static VisitedEntry* visited_pool = NULL;
|
||||
static VisitedEntry* hash_table[HASH_SIZE] = {0};
|
||||
static int pool_ptr = 0;
|
||||
static int pool_capacity = 0;
|
||||
static int visited_ptr = 0;
|
||||
static int32_t pool_ptr = 0;
|
||||
static int32_t pool_capacity = 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 = malloc(pool_capacity * sizeof(Node));
|
||||
visited_pool = malloc(pool_capacity * sizeof(VisitedEntry));
|
||||
@@ -44,7 +49,7 @@ void node_pool_destroy() {
|
||||
queue_destroy();
|
||||
}
|
||||
|
||||
int node_pool_usage() {
|
||||
int32_t node_pool_usage() {
|
||||
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);
|
||||
unsigned int h = hash_three128(a,b,c);
|
||||
uint32_t h = hash_three128(a,b,c);
|
||||
VisitedEntry* cur = hash_table[h];
|
||||
while (cur) {
|
||||
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_back = NULL;
|
||||
static int current_size = 0;
|
||||
static int capacity = 0;
|
||||
static int32_t current_size = 0;
|
||||
static int32_t capacity = 0;
|
||||
|
||||
void queue_init(int max_elements) {
|
||||
void queue_init(int32_t max_elements) {
|
||||
capacity = max_elements;
|
||||
queue_front = NULL;
|
||||
queue_back = NULL;
|
||||
@@ -45,7 +45,7 @@ Node* queue_pop() {
|
||||
return r;
|
||||
}
|
||||
|
||||
int queue_size() {
|
||||
int32_t queue_size() {
|
||||
return current_size;
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -9,7 +9,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int LIMIT = 0;
|
||||
int32_t LIMIT = 0;
|
||||
|
||||
typedef struct GlobalEntry {
|
||||
int128_t a, b, c;
|
||||
@@ -18,22 +18,22 @@ typedef struct GlobalEntry {
|
||||
|
||||
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);
|
||||
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];
|
||||
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;
|
||||
}
|
||||
|
||||
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->next = global_root_table[h];
|
||||
global_root_table[h] = e;
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void clear_global_table() {
|
||||
@@ -100,8 +100,8 @@ int main() {
|
||||
}
|
||||
else if (mode == 2) {
|
||||
clear_global_table(); /* Reset unique root tracker */
|
||||
int p = 7;
|
||||
int exponents[3] = {2*p + 2, p, p};
|
||||
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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user