Problem 661
Long chess match: closed-form E_A for killed birth-death chain; H(50).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(1) |
| Space complexity | O(1) | O(1) |
| Approach | Flow solution | Closed-form formula |
| Verdict | Suboptimal |
Flow source
# Project Euler 661
# Long chess match: closed-form E_A for killed birth-death chain; H(50).
extern {
function sqrt(x: f64) -> f64
}
function expected_times_a_leading(p_a: f64, p_b: f64, p_stop: f64) -> f64 {
let q: f64 = 1.0 - p_stop
let a: f64 = q * p_a
let b: f64 = q * p_b
let c: f64 = p_stop + q * (p_a + p_b)
let mut disc: f64 = c * c - 4.0 * a * b
if disc < 0.0 && disc > (0.0 - 0.000000000000001) { disc = 0.0 }
let s: f64 = sqrt(disc)
let r2: f64 = (c + s) / (2.0 * a)
let r1: f64 = b / (a * r2)
return (1.0 / p_stop + 1.0 / q) * (1.0 - r1) / (r2 - r1)
}
function main() -> i32 {
let mut total: f64 = 0.0
let mut k: i64 = 3
while k <= 50 {
let kf: f64 = k as f64
let p_a: f64 = 1.0 / sqrt(kf + 3.0)
let p_b: f64 = p_a + 1.0 / (kf * kf)
let p_stop: f64 = 1.0 / (kf * kf * kf)
total = total + expected_times_a_leading(p_a, p_b, p_stop)
k = k + 1
}
printf("%.4f\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; }
double expected_times_a_leading_f64_f64_f64(double p_a, double p_b, double p_stop);
int32_t main(void);
double expected_times_a_leading_f64_f64_f64(double p_a, double p_b, double p_stop) {
double q = (1.0 - p_stop);
double a = (q * p_a);
double b = (q * p_b);
double c = (p_stop + (q * (p_a + p_b)));
double disc = ((c * c) - ((4.0 * a) * b));
if ((disc < 0.0 && disc > (0.0 - 0.000000000000001))) {
disc = 0.0;
}
double s = sqrt(disc);
double r2 = ((c + s) / (2.0 * a));
double r1 = (b / (a * r2));
return ((((1.0 / p_stop) + (1.0 / q)) * (1.0 - r1)) / (r2 - r1));
}
int32_t main(void) {
double total = 0.0;
int64_t k = 3;
while (k <= 50) {
double kf = ((double)(k));
double p_a = (1.0 / sqrt((kf + 3.0)));
double p_b = (p_a + (1.0 / (kf * kf)));
double p_stop = (1.0 / ((kf * kf) * kf));
total = (total + expected_times_a_leading_f64_f64_f64(p_a, p_b, p_stop));
k = (k + 1);
}
printf("%.4f\n", total);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.4f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @sqrt(f64) -> f64
func.func @expected_times_a_leading(%arg0: f64, %arg1: f64, %arg2: f64) -> f64 {
%0 = arith.constant 1.0 : f32
%2 = arith.extf %0 : f32 to f64
%1 = arith.subf %2, %arg2 : f64
%3 = arith.mulf %1, %arg0 : f64
%4 = arith.mulf %1, %arg1 : f64
%5 = arith.addf %arg0, %arg1 : f64
%6 = arith.mulf %1, %5 : f64
%7 = arith.addf %arg2, %6 : f64
%8 = arith.mulf %7, %7 : f64
%9 = arith.constant 4.0 : f32
%11 = arith.extf %9 : f32 to f64
%10 = arith.mulf %11, %3 : f64
%12 = arith.mulf %10, %4 : f64
%13 = arith.subf %8, %12 : f64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x f64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : f64, !llvm.ptr
%16 = llvm.load %15 : !llvm.ptr -> f64
%17 = arith.constant 0.0 : f32
%19 = arith.extf %17 : f32 to f64
%18 = arith.cmpf olt, %16, %19 : f64
%20 = scf.if %18 -> (i1) {
%21 = llvm.load %15 : !llvm.ptr -> f64
%22 = arith.constant 0.0 : f32
%23 = arith.constant 0.000000000000001 : f32
%24 = arith.subf %22, %23 : f32
%26 = arith.extf %24 : f32 to f64
%25 = arith.cmpf ogt, %21, %26 : f64
scf.yield %25 : i1
} else {
%27 = arith.constant false
scf.yield %27 : i1
}
cf.cond_br %20, ^bb0, ^bb1
^bb0:
%28 = arith.constant 0.0 : f32
%29 = arith.extf %28 : f32 to f64
llvm.store %29, %15 : f64, !llvm.ptr
cf.br ^bb2
^bb1:
cf.br ^bb2
^bb2:
%30 = llvm.load %15 : !llvm.ptr -> f64
%31 = math.sqrt %30 : f64
%32 = arith.addf %7, %31 : f64
%33 = arith.constant 2.0 : f32
%35 = arith.extf %33 : f32 to f64
%34 = arith.mulf %35, %3 : f64
%36 = arith.divf %32, %34 : f64
%37 = arith.mulf %3, %36 : f64
%38 = arith.divf %4, %37 : f64
%39 = arith.constant 1.0 : f32
%41 = arith.extf %39 : f32 to f64
%40 = arith.divf %41, %arg2 : f64
%42 = arith.constant 1.0 : f32
%44 = arith.extf %42 : f32 to f64
%43 = arith.divf %44, %1 : f64
%45 = arith.addf %40, %43 : f64
%46 = arith.constant 1.0 : f32
%48 = arith.extf %46 : f32 to f64
%47 = arith.subf %48, %38 : f64
%49 = arith.mulf %45, %47 : f64
%50 = arith.subf %36, %38 : f64
%51 = arith.divf %49, %50 : f64
func.return %51 : f64
}
func.func @main() -> i32 {
%52 = arith.constant 0.0 : f32
%53 = arith.extf %52 : f32 to f64
%54 = llvm.mlir.constant(1 : i64) : i64
%55 = llvm.alloca %54 x f64 : (i64) -> !llvm.ptr
llvm.store %53, %55 : f64, !llvm.ptr
%56 = arith.constant 3 : i32
%57 = arith.extsi %56 : i32 to i64
%58 = llvm.mlir.constant(1 : i64) : i64
%59 = llvm.alloca %58 x i64 : (i64) -> !llvm.ptr
llvm.store %57, %59 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%60 = llvm.load %59 : !llvm.ptr -> i64
%61 = arith.constant 50 : i32
%63 = arith.extsi %61 : i32 to i64
%62 = arith.cmpi sle, %60, %63 : i64
cf.cond_br %62, ^bb4, ^bb5
^bb4:
%64 = llvm.load %59 : !llvm.ptr -> i64
%65 = arith.sitofp %64 : i64 to f64
%66 = arith.constant 1.0 : f32
%67 = arith.constant 3.0 : f32
%69 = arith.extf %67 : f32 to f64
%68 = arith.addf %65, %69 : f64
%70 = math.sqrt %68 : f64
%72 = arith.extf %66 : f32 to f64
%71 = arith.divf %72, %70 : f64
%73 = arith.constant 1.0 : f32
%74 = arith.mulf %65, %65 : f64
%76 = arith.extf %73 : f32 to f64
%75 = arith.divf %76, %74 : f64
%77 = arith.addf %71, %75 : f64
%78 = arith.constant 1.0 : f32
%79 = arith.mulf %65, %65 : f64
%80 = arith.mulf %79, %65 : f64
%82 = arith.extf %78 : f32 to f64
%81 = arith.divf %82, %80 : f64
%83 = llvm.load %55 : !llvm.ptr -> f64
%84 = func.call @expected_times_a_leading(%71, %77, %81) : (f64, f64, f64) -> f64
%85 = arith.addf %83, %84 : f64
llvm.store %85, %55 : f64, !llvm.ptr
%86 = llvm.load %59 : !llvm.ptr -> i64
%87 = arith.constant 1 : i32
%89 = arith.extsi %87 : i32 to i64
%88 = arith.addi %86, %89 : i64
llvm.store %88, %59 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%90 = llvm.mlir.addressof @str_0 : !llvm.ptr
%91 = llvm.load %55 : !llvm.ptr -> f64
%92 = llvm.call @printf(%90, %91) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%93 = arith.constant 0 : i32
func.return %93 : i32
}
}