# Project Euler 986
# Sum of G(c,d) for 1<=c,d<=160 via cellular automaton thresholds.
# Port of the C reference solver to pure Flow.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function malloc(n: i64) -> ptr<void>
function memset(p: ptr<void>, c: i64, n: i64) -> ptr<void>
}
function extinct_for_k1(n: i64, k: i64) -> i64 {
if k == 0 { return 1 }
let size: i64 = n + 1
let last: i64 = size - 1
let cells: ptr<i64> = calloc(size, 8) as ptr<i64>
cells[last] = k
let mut zero_count: i64 = last
while 1 != 0 {
let mut i: i64 = 0
while i < last {
let old: i64 = cells[i]
let nxt: i64 = (old + cells[i + 1]) >> 1
cells[i] = nxt
if old != 0 {
if nxt == 0 { zero_count = zero_count + 1 }
} else {
if nxt != 0 { zero_count = zero_count - 1 }
}
i = i + 1
}
let old2: i64 = cells[last]
let nxt2: i64 = (old2 + cells[0]) >> 1
cells[last] = nxt2
if old2 != 0 {
if nxt2 == 0 { zero_count = zero_count + 1 }
} else {
if nxt2 != 0 { zero_count = zero_count - 1 }
}
if zero_count == size {
free(cells as ptr<void>)
return 1
}
if zero_count == 0 {
free(cells as ptr<void>)
return 0
}
}
return 0
}
function threshold_k1_plain(n: i64) -> i64 {
let mut lo: i64 = 0
let mut hi: i64 = 1
while extinct_for_k1(n, hi) != 0 {
lo = hi
hi = hi * 2
}
while lo + 1 < hi {
let mid: i64 = (lo + hi) / 2
if extinct_for_k1(n, mid) != 0 {
lo = mid
} else {
hi = mid
}
}
return lo
}
function predict_k1_from_previous(s: ptr<i64>, n: i64) -> i64 {
let a: i64 = s[n - 32]
let b: i64 = s[n - 24]
let c: i64 = s[n - 16]
let d: i64 = s[n - 8]
return d + (d - c) + (d - 2 * c + b) + (d - 3 * c + 3 * b - a)
}
function threshold_k1_with_guess(n: i64, guess: i64) -> i64 {
let mut lo: i64 = guess - 4096
if lo < 0 { lo = 0 }
let mut hi: i64 = guess + 4096
# while (lo > 0 && !extinct_for_k1(n, lo)) { hi = lo; lo /= 2; }
let mut done_lo: i64 = 0
while done_lo == 0 {
if lo == 0 {
done_lo = 1
} else {
if extinct_for_k1(n, lo) == 0 {
hi = lo
lo = lo / 2
} else {
done_lo = 1
}
}
}
while extinct_for_k1(n, hi) != 0 {
lo = hi
hi = hi * 2
}
while lo + 1 < hi {
let mid: i64 = (lo + hi) / 2
if extinct_for_k1(n, mid) != 0 {
lo = mid
} else {
hi = mid
}
}
return lo
}
function gcd_ll(a: i64, b: i64) -> i64 {
let mut aa: i64 = a
let mut bb: i64 = b
while bb != 0 {
let t: i64 = aa % bb
aa = bb
bb = t
}
return aa
}
function get_exception(c: i64) -> i64 {
# exc_c = {2,3,4,5,6,8,10}, exc_h = {3,5,7,11,13,21,31}
if c == 2 { return 3 }
if c == 3 { return 5 }
if c == 4 { return 7 }
if c == 5 { return 11 }
if c == 6 { return 13 }
if c == 8 { return 21 }
if c == 10 { return 31 }
return -1
}
function main() -> i32 {
let limit: i64 = 160
let max_n: i64 = limit + (limit - 1) / 2
let s: ptr<i64> = calloc(max_n + 1, 8) as ptr<i64>
let mut n: i64 = 1
while n <= max_n {
if n < 33 {
s[n] = threshold_k1_plain(n)
} else {
let guess: i64 = predict_k1_from_previous(s, n)
s[n] = threshold_k1_with_guess(n, guess)
}
n = n + 1
}
let dim: i64 = limit + 1
let memo: ptr<i64> = malloc(dim * dim * 8) as ptr<i64>
# memset to -1 (all bytes 0xFF = -1 for i64)
memset(memo as ptr<void>, -1, dim * dim * 8)
let mut total: i64 = 0
let mut c: i64 = 1
while c <= limit {
let mut d: i64 = 1
while d <= limit {
let g: i64 = gcd_ll(c, d)
let cr: i64 = c / g
let dr: i64 = d / g
let slot_idx: i64 = cr * dim + dr
let mut val: i64 = memo[slot_idx]
if val < 0 {
let mut h: i64 = 0
if dr == 1 {
let exc: i64 = get_exception(cr)
if exc >= 0 {
h = exc
} else {
h = s[dr + (cr - 1) / 2]
}
} else {
h = s[dr + (cr - 1) / 2]
}
val = 2 * h + 1
memo[slot_idx] = val
}
total = total + val
d = d + 1
}
c = c + 1
}
free(s as ptr<void>)
free(memo as ptr<void>)
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; }
int64_t extinct_for_k1_i64_i64(int64_t n, int64_t k);
int64_t threshold_k1_plain_i64(int64_t n);
int64_t predict_k1_from_previous_ptr_i64_i64(int64_t* s, int64_t n);
int64_t threshold_k1_with_guess_i64_i64(int64_t n, int64_t guess);
int64_t gcd_ll_i64_i64(int64_t a, int64_t b);
int64_t get_exception_i64(int64_t c);
int32_t main(void);
int64_t extinct_for_k1_i64_i64(int64_t n, int64_t k) {
if (k == 0) {
return 1;
}
int64_t size = (n + 1);
int64_t last = (size - 1);
int64_t* cells = (int64_t*)(((int64_t*)(calloc(size, 8))));
cells[last] = k;
int64_t zero_count = last;
while (1 != 0) {
int64_t i = 0;
while (i < last) {
int64_t old = cells[i];
int64_t nxt = FLOW_CHECKED_SHR(((old + cells[(i + 1)])), (1));
cells[i] = nxt;
if (old != 0) {
if (nxt == 0) {
zero_count = (zero_count + 1);
}
} else {
if (nxt != 0) {
zero_count = (zero_count - 1);
}
}
i = (i + 1);
}
int64_t old2 = cells[last];
int64_t nxt2 = FLOW_CHECKED_SHR(((old2 + cells[0])), (1));
cells[last] = nxt2;
if (old2 != 0) {
if (nxt2 == 0) {
zero_count = (zero_count + 1);
}
} else {
if (nxt2 != 0) {
zero_count = (zero_count - 1);
}
}
if (zero_count == size) {
free(((void*)(cells)));
return 1;
}
if (zero_count == 0) {
free(((void*)(cells)));
return 0;
}
}
return 0;
}
int64_t threshold_k1_plain_i64(int64_t n) {
int64_t lo = 0;
int64_t hi = 1;
while (extinct_for_k1_i64_i64(n, hi) != 0) {
lo = hi;
hi = (hi * 2);
}
while ((lo + 1) < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (extinct_for_k1_i64_i64(n, mid) != 0) {
lo = mid;
} else {
hi = mid;
}
}
return lo;
}
int64_t predict_k1_from_previous_ptr_i64_i64(int64_t* s, int64_t n) {
int64_t a = s[(n - 32)];
int64_t b = s[(n - 24)];
int64_t c = s[(n - 16)];
int64_t d = s[(n - 8)];
return (((d + (d - c)) + ((d - (2 * c)) + b)) + (((d - (3 * c)) + (3 * b)) - a));
}
int64_t threshold_k1_with_guess_i64_i64(int64_t n, int64_t guess) {
int64_t lo = (guess - 4096);
if (lo < 0) {
lo = 0;
}
int64_t hi = (guess + 4096);
int64_t done_lo = 0;
while (done_lo == 0) {
if (lo == 0) {
done_lo = 1;
} else {
if (extinct_for_k1_i64_i64(n, lo) == 0) {
hi = lo;
lo = FLOW_CHECKED_DIV((lo), (2));
} else {
done_lo = 1;
}
}
}
while (extinct_for_k1_i64_i64(n, hi) != 0) {
lo = hi;
hi = (hi * 2);
}
while ((lo + 1) < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (extinct_for_k1_i64_i64(n, mid) != 0) {
lo = mid;
} else {
hi = mid;
}
}
return lo;
}
int64_t gcd_ll_i64_i64(int64_t a, int64_t b) {
int64_t aa = a;
int64_t bb = b;
while (bb != 0) {
int64_t t = FLOW_CHECKED_MOD((aa), (bb));
aa = bb;
bb = t;
}
return aa;
}
int64_t get_exception_i64(int64_t c) {
if (c == 2) {
return 3;
}
if (c == 3) {
return 5;
}
if (c == 4) {
return 7;
}
if (c == 5) {
return 11;
}
if (c == 6) {
return 13;
}
if (c == 8) {
return 21;
}
if (c == 10) {
return 31;
}
return (-1);
}
int32_t main(void) {
int64_t limit = 160;
int64_t max_n = (limit + FLOW_CHECKED_DIV(((limit - 1)), (2)));
int64_t* s = (int64_t*)(((int64_t*)(calloc((max_n + 1), 8))));
int64_t n = 1;
while (n <= max_n) {
if (n < 33) {
s[n] = threshold_k1_plain_i64(n);
} else {
int64_t guess = predict_k1_from_previous_ptr_i64_i64(s, n);
s[n] = threshold_k1_with_guess_i64_i64(n, guess);
}
n = (n + 1);
}
int64_t dim = (limit + 1);
int64_t* memo = (int64_t*)(((int64_t*)(malloc(((dim * dim) * 8)))));
memset(((void*)(memo)), (-1), ((dim * dim) * 8));
int64_t total = 0;
int64_t c = 1;
while (c <= limit) {
int64_t d = 1;
while (d <= limit) {
int64_t g = gcd_ll_i64_i64(c, d);
int64_t cr = FLOW_CHECKED_DIV((c), (g));
int64_t dr = FLOW_CHECKED_DIV((d), (g));
int64_t slot_idx = ((cr * dim) + dr);
int64_t val = memo[slot_idx];
if (val < 0) {
int64_t h = 0;
if (dr == 1) {
int64_t exc = get_exception_i64(cr);
if (exc >= 0) {
h = exc;
} else {
h = s[(dr + FLOW_CHECKED_DIV(((cr - 1)), (2)))];
}
} else {
h = s[(dr + FLOW_CHECKED_DIV(((cr - 1)), (2)))];
}
val = ((2 * h) + 1);
memo[slot_idx] = val;
}
total = (total + val);
d = (d + 1);
}
c = (c + 1);
}
free(((void*)(s)));
free(((void*)(memo)));
printf("%lld\n", total);
return 0;
}