# Project Euler 227
# Expected turns for The Chase with 100 players.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function fabs(x: f64) -> f64
}
function main() -> i32 {
let N: i32 = 100
let n: i32 = N - 1
let mut i: i32 = 0
let A: ptr<f64> = calloc((n as i64) * (n as i64), 8)
let b: ptr<f64> = calloc(n as i64, 8)
if A == null || b == null { return 1 }
let p_m2: f64 = 1.0 / 36.0
let p_m1: f64 = 2.0 / 9.0
let p_0: f64 = 0.5
let p_p1: f64 = 2.0 / 9.0
let p_p2: f64 = 1.0 / 36.0
let mut d: i32 = 1
while d < N {
let r: i32 = d - 1
A[(r as i64) * (n as i64) + (r as i64)] = 1.0
b[r] = 1.0
let mut k: i32 = 0
while k < 5 {
let mut delta: i32 = 0
let mut p: f64 = 0.0
match k {
0 => { delta = 0 - 2; p = p_m2 }
1 => { delta = 0 - 1; p = p_m1 }
2 => { delta = 0; p = p_0 }
3 => { delta = 1; p = p_p1 }
_ => { delta = 2; p = p_p2 }
}
let mut d2: i32 = (d + delta) % N
if d2 < 0 { d2 = d2 + N }
if d2 != 0 {
let c: i32 = d2 - 1
let idx: i64 = (r as i64) * (n as i64) + (c as i64)
A[idx] = A[idx] - p
}
k = k + 1
}
d = d + 1
}
let mut col: i32 = 0
while col < n {
let mut piv: i32 = col
let mut best: f64 = fabs(A[(col as i64) * (n as i64) + (col as i64)])
i = col + 1
while i < n {
let v: f64 = fabs(A[(i as i64) * (n as i64) + (col as i64)])
if v > best { best = v; piv = i }
i = i + 1
}
if piv != col {
let mut j: i32 = 0
while j < n {
let tmp: f64 = A[(col as i64) * (n as i64) + (j as i64)]
A[(col as i64) * (n as i64) + (j as i64)] = A[(piv as i64) * (n as i64) + (j as i64)]
A[(piv as i64) * (n as i64) + (j as i64)] = tmp
j = j + 1
}
let tb: f64 = b[col]
b[col] = b[piv]
b[piv] = tb
}
let diag: f64 = A[(col as i64) * (n as i64) + (col as i64)]
i = col + 1
while i < n {
let f: f64 = A[(i as i64) * (n as i64) + (col as i64)] / diag
let mut j: i32 = col
while j < n {
let ij: i64 = (i as i64) * (n as i64) + (j as i64)
let cj: i64 = (col as i64) * (n as i64) + (j as i64)
A[ij] = A[ij] - f * A[cj]
j = j + 1
}
b[i] = b[i] - f * b[col]
i = i + 1
}
col = col + 1
}
let x: ptr<f64> = calloc(n as i64, 8)
i = n - 1
while i >= 0 {
let mut s: f64 = b[i]
let mut j: i32 = i + 1
while j < n {
s = s - A[(i as i64) * (n as i64) + (j as i64)] * x[j]
j = j + 1
}
x[i] = s / A[(i as i64) * (n as i64) + (i as i64)]
i = i - 1
}
printf("%.6f\n", x[N / 2 - 1])
free(A); free(b); free(x)
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; }
int32_t main(void);
int32_t main(void) {
int32_t N = 100;
int32_t n = (N - 1);
int32_t i = 0;
double* A = (double*)(calloc((((int64_t)(n)) * ((int64_t)(n))), 8));
double* b = (double*)(calloc(((int64_t)(n)), 8));
if ((A == NULL || b == NULL)) {
return 1;
}
double p_m2 = (1.0 / 36.0);
double p_m1 = (2.0 / 9.0);
double p_0 = 0.5;
double p_p1 = (2.0 / 9.0);
double p_p2 = (1.0 / 36.0);
int32_t d = 1;
while (d < N) {
int32_t r = (d - 1);
A[((((int64_t)(r)) * ((int64_t)(n))) + ((int64_t)(r)))] = 1.0;
b[r] = 1.0;
int32_t k = 0;
while (k < 5) {
int32_t delta = 0;
double p = 0.0;
{ // match block
if ((k) == 0) {
delta = (0 - 2);
p = p_m2;
} else if ((k) == 1) {
delta = (0 - 1);
p = p_m1;
} else if ((k) == 2) {
delta = 0;
p = p_0;
} else if ((k) == 3) {
delta = 1;
p = p_p1;
} else { // exhaustive
delta = 2;
p = p_p2;
}
} // end match
int32_t d2 = FLOW_CHECKED_MOD(((d + delta)), (N));
if (d2 < 0) {
d2 = (d2 + N);
}
if (d2 != 0) {
int32_t c = (d2 - 1);
int64_t idx = ((((int64_t)(r)) * ((int64_t)(n))) + ((int64_t)(c)));
A[idx] = (A[idx] - p);
}
k = (k + 1);
}
d = (d + 1);
}
int32_t col = 0;
while (col < n) {
int32_t piv = col;
double best = fabs(A[((((int64_t)(col)) * ((int64_t)(n))) + ((int64_t)(col)))]);
i = (col + 1);
while (i < n) {
double v = fabs(A[((((int64_t)(i)) * ((int64_t)(n))) + ((int64_t)(col)))]);
if (v > best) {
best = v;
piv = i;
}
i = (i + 1);
}
if (piv != col) {
int32_t j = 0;
while (j < n) {
double tmp = A[((((int64_t)(col)) * ((int64_t)(n))) + ((int64_t)(j)))];
A[((((int64_t)(col)) * ((int64_t)(n))) + ((int64_t)(j)))] = A[((((int64_t)(piv)) * ((int64_t)(n))) + ((int64_t)(j)))];
A[((((int64_t)(piv)) * ((int64_t)(n))) + ((int64_t)(j)))] = tmp;
j = (j + 1);
}
double tb = b[col];
b[col] = b[piv];
b[piv] = tb;
}
double diag = A[((((int64_t)(col)) * ((int64_t)(n))) + ((int64_t)(col)))];
i = (col + 1);
while (i < n) {
double f = (A[((((int64_t)(i)) * ((int64_t)(n))) + ((int64_t)(col)))] / diag);
int32_t j = col;
while (j < n) {
int64_t ij = ((((int64_t)(i)) * ((int64_t)(n))) + ((int64_t)(j)));
int64_t cj = ((((int64_t)(col)) * ((int64_t)(n))) + ((int64_t)(j)));
A[ij] = (A[ij] - (f * A[cj]));
j = (j + 1);
}
b[i] = (b[i] - (f * b[col]));
i = (i + 1);
}
col = (col + 1);
}
double* x = (double*)(calloc(((int64_t)(n)), 8));
i = (n - 1);
while (i >= 0) {
double s = b[i];
int32_t j = (i + 1);
while (j < n) {
s = (s - (A[((((int64_t)(i)) * ((int64_t)(n))) + ((int64_t)(j)))] * x[j]));
j = (j + 1);
}
x[i] = (s / A[((((int64_t)(i)) * ((int64_t)(n))) + ((int64_t)(i)))]);
i = (i - 1);
}
printf("%.6f\n", x[(FLOW_CHECKED_DIV((N), (2)) - 1)]);
free(A);
free(b);
free(x);
return 0;
}