# Project Euler 259
# Sum of positive reachable integers from digits 1..9.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function gcd_abs(a0: i64, b0: i64) -> i64 {
let mut a: i64 = a0
let mut b: i64 = b0
if a < 0 { a = -a }
if b < 0 { b = -b }
while b != 0 {
let t: i64 = a % b
a = b
b = t
}
return a
}
function concat_val(l: i64, r: i64) -> i64 {
let mut v: i64 = 0
let mut i: i64 = l
while i <= r {
v = v * 10 + i
i = i + 1
}
return v
}
function pack(num: i64, den: i64) -> i64 {
# mix into one key for hash; store separately
return num * 1000003 + den
}
function main() -> i32 {
# 10x10 ranges; for each range a hashset
let R: i64 = 10
let CAP: i64 = 8000009
# Too big for all ranges - use one working set built bottom-up by span
# Store results in jagged: offsets[100], nums/dens big arrays
let max_total: i64 = 6000000
let all_num: ptr<i64> = calloc(max_total, 8)
let all_den: ptr<i64> = calloc(max_total, 8)
let off: ptr<i32> = calloc(101, 4)
let cnt: ptr<i32> = calloc(100, 4)
let ready: ptr<i8> = calloc(100, 1)
let mut total: i64 = 0
# hash helpers for building one range
let HCAP: i64 = 8388608
let hused: ptr<i8> = calloc(HCAP, 1)
let hnum: ptr<i64> = calloc(HCAP, 8)
let hden: ptr<i64> = calloc(HCAP, 8)
let mut span: i64 = 1
while span <= 9 {
let mut l: i64 = 1
while l + span - 1 <= 9 {
let r: i64 = l + span - 1
let idx: i64 = l * 10 + r
# clear hash
let mut hi: i64 = 0
while hi < HCAP {
hused[hi] = 0
hi = hi + 1
}
let mut nlocal: i64 = 0
# insert helper via inline
# concat
let cv: i64 = concat_val(l, r)
let mut hn: i64 = cv % HCAP
if hn < 0 { hn = -hn }
while hused[hn] != 0 {
if hnum[hn] == cv && hden[hn] == 1 { break }
hn = hn + 1
if hn == HCAP { hn = 0 }
}
if hused[hn] == 0 {
hused[hn] = 1
hnum[hn] = cv
hden[hn] = 1
nlocal = nlocal + 1
}
if span > 1 {
let mut mid: i64 = l
while mid < r {
let li: i64 = l * 10 + mid
let ri: i64 = (mid + 1) * 10 + r
let l0: i64 = off[li] as i64
let lc: i64 = cnt[li] as i64
let r0: i64 = off[ri] as i64
let rc: i64 = cnt[ri] as i64
let mut i: i64 = 0
while i < lc {
let an: i64 = all_num[l0 + i]
let ad: i64 = all_den[l0 + i]
let mut j: i64 = 0
while j < rc {
let bn: i64 = all_num[r0 + j]
let bd: i64 = all_den[r0 + j]
# four ops
let mut op: i64 = 0
while op < 4 {
let mut num: i64 = 0
let mut den: i64 = 1
let mut ok: bool = true
if op == 0 {
num = an * bd + bn * ad
den = ad * bd
} elif op == 1 {
num = an * bd - bn * ad
den = ad * bd
} elif op == 2 {
num = an * bn
den = ad * bd
} else {
if bn == 0 { ok = false }
else {
num = an * bd
den = ad * bn
}
}
if ok {
if den < 0 {
num = 0 - num
den = 0 - den
}
if den != 0 {
let g: i64 = gcd_abs(num, den)
num = num / g
den = den / g
hn = (num * 1000003 + den) % HCAP
if hn < 0 { hn = -hn }
while hused[hn] != 0 {
if hnum[hn] == num && hden[hn] == den { break }
hn = hn + 1
if hn == HCAP { hn = 0 }
}
if hused[hn] == 0 {
hused[hn] = 1
hnum[hn] = num
hden[hn] = den
nlocal = nlocal + 1
}
}
}
op = op + 1
}
j = j + 1
}
i = i + 1
}
mid = mid + 1
}
}
off[idx] = total as i32
cnt[idx] = nlocal as i32
# dump hash to all_*
hi = 0
while hi < HCAP {
if hused[hi] != 0 {
all_num[total] = hnum[hi]
all_den[total] = hden[hi]
total = total + 1
}
hi = hi + 1
}
ready[idx] = 1
l = l + 1
}
span = span + 1
}
let idx: i64 = 19
let base: i64 = off[idx] as i64
let n: i64 = cnt[idx] as i64
let mut sum: i64 = 0
let mut i: i64 = 0
while i < n {
if all_den[base + i] == 1 && all_num[base + i] > 0 {
sum = sum + all_num[base + i]
}
i = i + 1
}
printf("%lld\n", sum)
free(all_num); free(all_den); free(off); free(cnt); free(ready); free(hused); free(hnum); free(hden)
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; }
int64_t gcd_abs_i64_i64(int64_t a0, int64_t b0);
int64_t concat_val_i64_i64(int64_t l, int64_t r);
int64_t pack_i64_i64(int64_t num, int64_t den);
int32_t main(void);
int64_t gcd_abs_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
if (a < 0) {
a = (-a);
}
if (b < 0) {
b = (-b);
}
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t concat_val_i64_i64(int64_t l, int64_t r) {
int64_t v = 0;
int64_t i = l;
while (i <= r) {
v = ((v * 10) + i);
i = (i + 1);
}
return v;
}
int64_t pack_i64_i64(int64_t num, int64_t den) {
return ((num * 1000003) + den);
}
int32_t main(void) {
int64_t R = 10;
int64_t CAP = 8000009;
int64_t max_total = 6000000;
int64_t* all_num = (int64_t*)(calloc(max_total, 8));
int64_t* all_den = (int64_t*)(calloc(max_total, 8));
int32_t* off = (int32_t*)(calloc(101, 4));
int32_t* cnt = (int32_t*)(calloc(100, 4));
int8_t* ready = (int8_t*)(calloc(100, 1));
int64_t total = 0;
int64_t HCAP = 8388608;
int8_t* hused = (int8_t*)(calloc(HCAP, 1));
int64_t* hnum = (int64_t*)(calloc(HCAP, 8));
int64_t* hden = (int64_t*)(calloc(HCAP, 8));
int64_t span = 1;
while (span <= 9) {
int64_t l = 1;
while (((l + span) - 1) <= 9) {
int64_t r = ((l + span) - 1);
int64_t idx = ((l * 10) + r);
int64_t hi = 0;
while (hi < HCAP) {
hused[hi] = 0;
hi = (hi + 1);
}
int64_t nlocal = 0;
int64_t cv = concat_val_i64_i64(l, r);
int64_t hn = FLOW_CHECKED_MOD((cv), (HCAP));
if (hn < 0) {
hn = (-hn);
}
while (hused[hn] != 0) {
if ((hnum[hn] == cv && hden[hn] == 1)) {
break;
}
hn = (hn + 1);
if (hn == HCAP) {
hn = 0;
}
}
if (hused[hn] == 0) {
hused[hn] = 1;
hnum[hn] = cv;
hden[hn] = 1;
nlocal = (nlocal + 1);
}
if (span > 1) {
int64_t mid = l;
while (mid < r) {
int64_t li = ((l * 10) + mid);
int64_t ri = (((mid + 1) * 10) + r);
int64_t l0 = ((int64_t)(off[li]));
int64_t lc = ((int64_t)(cnt[li]));
int64_t r0 = ((int64_t)(off[ri]));
int64_t rc = ((int64_t)(cnt[ri]));
int64_t i = 0;
while (i < lc) {
int64_t an = all_num[(l0 + i)];
int64_t ad = all_den[(l0 + i)];
int64_t j = 0;
while (j < rc) {
int64_t bn = all_num[(r0 + j)];
int64_t bd = all_den[(r0 + j)];
int64_t op = 0;
while (op < 4) {
int64_t num = 0;
int64_t den = 1;
bool ok = 1;
if (op == 0) {
num = ((an * bd) + (bn * ad));
den = (ad * bd);
} else if (op == 1) {
num = ((an * bd) - (bn * ad));
den = (ad * bd);
} else if (op == 2) {
num = (an * bn);
den = (ad * bd);
} else {
if (bn == 0) {
ok = 0;
} else {
num = (an * bd);
den = (ad * bn);
}
}
if (ok) {
if (den < 0) {
num = (0 - num);
den = (0 - den);
}
if (den != 0) {
int64_t g = gcd_abs_i64_i64(num, den);
num = FLOW_CHECKED_DIV((num), (g));
den = FLOW_CHECKED_DIV((den), (g));
hn = FLOW_CHECKED_MOD((((num * 1000003) + den)), (HCAP));
if (hn < 0) {
hn = (-hn);
}
while (hused[hn] != 0) {
if ((hnum[hn] == num && hden[hn] == den)) {
break;
}
hn = (hn + 1);
if (hn == HCAP) {
hn = 0;
}
}
if (hused[hn] == 0) {
hused[hn] = 1;
hnum[hn] = num;
hden[hn] = den;
nlocal = (nlocal + 1);
}
}
}
op = (op + 1);
}
j = (j + 1);
}
i = (i + 1);
}
mid = (mid + 1);
}
}
off[idx] = ((int32_t)(total));
cnt[idx] = ((int32_t)(nlocal));
hi = 0;
while (hi < HCAP) {
if (hused[hi] != 0) {
all_num[total] = hnum[hi];
all_den[total] = hden[hi];
total = (total + 1);
}
hi = (hi + 1);
}
ready[idx] = 1;
l = (l + 1);
}
span = (span + 1);
}
int64_t idx = 19;
int64_t base = ((int64_t)(off[idx]));
int64_t n = ((int64_t)(cnt[idx]));
int64_t sum = 0;
int64_t i = 0;
while (i < n) {
if ((all_den[(base + i)] == 1 && all_num[(base + i)] > 0)) {
sum = (sum + all_num[(base + i)]);
}
i = (i + 1);
}
printf("%lld\n", sum);
free(all_num);
free(all_den);
free(off);
free(cnt);
free(ready);
free(hused);
free(hnum);
free(hden);
return 0;
}