# Project Euler 295
# L(100000) from precomputed radius multiplicity tables.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function fopen(path: string, mode: string) -> ptr<void>
function fclose(f: ptr<void>) -> i32
function fgetc(f: ptr<void>) -> i32
}
function read_i64(f: ptr<void>, out: ptr<i64>) -> bool {
let mut c: i32 = fgetc(f)
while c == 32 || c == 10 || c == 13 || c == 9 {
c = fgetc(f)
}
if c < 0 { return false }
let mut sign: i64 = 1
if c == 45 {
sign = -1
c = fgetc(f)
}
if c < 48 || c > 57 { return false }
let mut v: i64 = 0
while c >= 48 && c <= 57 {
v = v * 10 + (c - 48)
c = fgetc(f)
}
out[0] = v * sign
return true
}
function main() -> i32 {
let CAP: i64 = 200003
let su: ptr<i8> = calloc(CAP, 1)
let sk: ptr<i64> = calloc(CAP, 8)
let sv: ptr<i64> = calloc(CAP, 8)
let tmp: ptr<i64> = calloc(1, 8)
let f: ptr<void> = fopen("data/p295_single.txt", "r")
while read_i64(f, tmp) {
let s: i64 = tmp[0]
if !read_i64(f, tmp) { break }
let c: i64 = tmp[0]
let mut h: i64 = s % CAP
if h < 0 { h = -h }
while su[h] != 0 {
h = h + 1
if h == CAP { h = 0 }
}
su[h] = 1
sk[h] = s
sv[h] = c
}
fclose(f)
let ms: ptr<i64> = calloc(20000 * 8, 8)
let mc: ptr<i64> = calloc(20000, 8)
let mn: ptr<i32> = calloc(20000, 4)
let mut nmulti: i64 = 0
f = fopen("data/p295_multi.txt", "r")
while read_i64(f, tmp) {
let k: i64 = tmp[0]
mn[nmulti] = k as i32
let mut i: i64 = 0
while i < k {
read_i64(f, tmp)
ms[nmulti * 8 + i] = tmp[0]
i = i + 1
}
read_i64(f, tmp)
mc[nmulti] = tmp[0]
nmulti = nmulti + 1
}
fclose(f)
let mut total: i64 = 0
let mut h: i64 = 0
while h < CAP {
if su[h] != 0 {
let c: i64 = sv[h]
total = total + c * (c + 1) / 2
}
h = h + 1
}
let mut i: i64 = 0
while i < nmulti {
let c: i64 = mc[i]
total = total + c * (c + 1) / 2
i = i + 1
}
i = 0
while i < nmulti {
let c: i64 = mc[i]
let k: i64 = mn[i] as i64
let mut sum_single: i64 = 0
let mut j: i64 = 0
while j < k {
let s: i64 = ms[i * 8 + j]
let mut hh: i64 = s % CAP
if hh < 0 { hh = -hh }
while su[hh] != 0 {
if sk[hh] == s {
sum_single = sum_single + sv[hh]
break
}
hh = hh + 1
if hh == CAP { hh = 0 }
}
j = j + 1
}
total = total + c * sum_single
i = i + 1
}
i = 0
while i < nmulti {
let mut j: i64 = i + 1
while j < nmulti {
let ki: i64 = mn[i] as i64
let kj: i64 = mn[j] as i64
let mut inter: bool = false
let mut a: i64 = 0
while a < ki {
let mut b: i64 = 0
while b < kj {
if ms[i * 8 + a] == ms[j * 8 + b] {
inter = true
break
}
b = b + 1
}
if inter { break }
a = a + 1
}
if inter {
total = total + mc[i] * mc[j]
}
j = j + 1
}
i = i + 1
}
printf("%lld\n", total)
return 0
}
Generated C
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
while (flow_temp_head) {
flow_temp_node* n = flow_temp_head;
flow_temp_head = n->next;
free(n);
}
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
if (!node) return NULL;
node->next = flow_temp_head;
flow_temp_head = node;
if (!flow_temp_atexit_set) {
flow_temp_atexit_set = 1;
atexit(flow_temp_free_all);
}
return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
char* r = (char*)flow_temp_alloc(la + lb + 1);
if (!r) return NULL;
if (la) memcpy(r, a, la);
if (lb) memcpy(r + la, b, lb);
r[la + lb] = '\0';
return r;
}
#define __flow_in_arr(arr, val) __extension__ ({ \
int _found = 0; \
size_t _n = sizeof(arr)/sizeof((arr)[0]); \
for (size_t _i = 0; _i < _n; _i++) { \
if ((arr)[_i] == (val)) { _found = 1; break; } \
} _found; })
/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
abort();
#if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")
#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#include <math.h>
void* _ui_state = NULL;
static inline float i32_to_f32(int32_t v) { return (float)v; }
/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }
bool read_i64_ptr_void_ptr_i64(void* f, int64_t* out);
int32_t main(void);
bool read_i64_ptr_void_ptr_i64(void* f, int64_t* out) {
int32_t c = fgetc(f);
while ((((c == 32 || c == 10) || c == 13) || c == 9)) {
c = fgetc(f);
}
if (c < 0) {
return 0;
}
int64_t sign = 1;
if (c == 45) {
sign = (-1);
c = fgetc(f);
}
if ((c < 48 || c > 57)) {
return 0;
}
int64_t v = 0;
while ((c >= 48 && c <= 57)) {
v = ((v * 10) + (c - 48));
c = fgetc(f);
}
out[0] = (v * sign);
return 1;
}
int32_t main(void) {
int64_t CAP = 200003;
int8_t* su = (int8_t*)(calloc(CAP, 1));
int64_t* sk = (int64_t*)(calloc(CAP, 8));
int64_t* sv = (int64_t*)(calloc(CAP, 8));
int64_t* tmp = (int64_t*)(calloc(1, 8));
void* f = (void*)(fopen("data/p295_single.txt", "r"));
while (read_i64_ptr_void_ptr_i64(f, tmp)) {
int64_t s = tmp[0];
if ((!(read_i64_ptr_void_ptr_i64(f, tmp)))) {
break;
}
int64_t c = tmp[0];
int64_t h = FLOW_CHECKED_MOD((s), (CAP));
if (h < 0) {
h = (-h);
}
while (su[h] != 0) {
h = (h + 1);
if (h == CAP) {
h = 0;
}
}
su[h] = 1;
sk[h] = s;
sv[h] = c;
}
fclose(f);
int64_t* ms = (int64_t*)(calloc((20000 * 8), 8));
int64_t* mc = (int64_t*)(calloc(20000, 8));
int32_t* mn = (int32_t*)(calloc(20000, 4));
int64_t nmulti = 0;
f = fopen("data/p295_multi.txt", "r");
while (read_i64_ptr_void_ptr_i64(f, tmp)) {
int64_t k = tmp[0];
mn[nmulti] = ((int32_t)(k));
int64_t i = 0;
while (i < k) {
read_i64_ptr_void_ptr_i64(f, tmp);
ms[((nmulti * 8) + i)] = tmp[0];
i = (i + 1);
}
read_i64_ptr_void_ptr_i64(f, tmp);
mc[nmulti] = tmp[0];
nmulti = (nmulti + 1);
}
fclose(f);
int64_t total = 0;
int64_t h = 0;
while (h < CAP) {
if (su[h] != 0) {
int64_t c = sv[h];
total = (total + FLOW_CHECKED_DIV(((c * (c + 1))), (2)));
}
h = (h + 1);
}
int64_t i = 0;
while (i < nmulti) {
int64_t c = mc[i];
total = (total + FLOW_CHECKED_DIV(((c * (c + 1))), (2)));
i = (i + 1);
}
i = 0;
while (i < nmulti) {
int64_t c = mc[i];
int64_t k = ((int64_t)(mn[i]));
int64_t sum_single = 0;
int64_t j = 0;
while (j < k) {
int64_t s = ms[((i * 8) + j)];
int64_t hh = FLOW_CHECKED_MOD((s), (CAP));
if (hh < 0) {
hh = (-hh);
}
while (su[hh] != 0) {
if (sk[hh] == s) {
sum_single = (sum_single + sv[hh]);
break;
}
hh = (hh + 1);
if (hh == CAP) {
hh = 0;
}
}
j = (j + 1);
}
total = (total + (c * sum_single));
i = (i + 1);
}
i = 0;
while (i < nmulti) {
int64_t j = (i + 1);
while (j < nmulti) {
int64_t ki = ((int64_t)(mn[i]));
int64_t kj = ((int64_t)(mn[j]));
bool inter = 0;
int64_t a = 0;
while (a < ki) {
int64_t b = 0;
while (b < kj) {
if (ms[((i * 8) + a)] == ms[((j * 8) + b)]) {
inter = 1;
break;
}
b = (b + 1);
}
if (inter) {
break;
}
a = (a + 1);
}
if (inter) {
total = (total + (mc[i] * mc[j]));
}
j = (j + 1);
}
i = (i + 1);
}
printf("%lld\n", total);
return 0;
}