← All problems
Problem 870
Transition values T(i) computed via g-base generation. Uses logarithmic representation of b values to avoid bignum, since b values grow exponentially. The ratio b_k/b[i_ptr-1] converges and f64 precision suffices for 10 decimal places.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)?
Space complexity O(n)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 870: Stone Game IV
# Transition values T(i) computed via g-base generation.
# Uses logarithmic representation of b values to avoid bignum,
# since b values grow exponentially. The ratio b_k/b[i_ptr-1]
# converges and f64 precision suffices for 10 decimal places.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>)
function printf(fmt: ptr<i8>, ...) -> i32
function log(x: f64) -> f64
function exp(x: f64) -> f64
function log1p(x: f64) -> f64
}
const MAX_STEPS: i32 = 3000
function next_transition(q: f64) -> f64 {
let log_b: ptr<f64> = calloc((MAX_STEPS + 2) as i64, 8)
log_b[0] = 0.0
let mut len_: i32 = 1
let mut i_ptr: i32 = 0
let mut best_ratio: f64 = 0.0
let mut have_best: i32 = 0
let log_q: f64 = log(q)
let mut step: i32 = 0
while step < MAX_STEPS {
let log_bk: f64 = log_b[len_ - 1]
let target: f64 = log_bk - log_q
# advance i_ptr until log_b[i_ptr] >= target
while i_ptr < len_ {
if log_b[i_ptr] >= target - 1e-10 { break }
i_ptr = i_ptr + 1
}
if i_ptr >= len_ { break }
if i_ptr > 0 {
let ratio: f64 = exp(log_bk - log_b[i_ptr - 1])
if have_best == 0 || ratio < best_ratio {
best_ratio = ratio
have_best = 1
}
}
# b[len] = bk + b[i_ptr] => log = log_bk + log1p(exp(log_b[i_ptr] - log_bk))
let diff: f64 = log_b[i_ptr] - log_bk
log_b[len_] = log_bk + log1p(exp(diff))
len_ = len_ + 1
step = step + 1
}
free(log_b as ptr<void>)
if have_best == 0 { return 1.0 }
return best_ratio
}
function main() -> i32 {
let mut q: f64 = 1.0
let mut i: i32 = 0
while i < 123455 {
q = next_transition(q)
i = i + 1
}
printf("%.10f\n", q)
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 log1p(double x);
double next_transition_f64(double q);
int32_t main(void);
static const int32_t MAX_STEPS = 3000;
double next_transition_f64(double q) {
double* log_b = (double*)(calloc(((int64_t)((MAX_STEPS + 2))), 8));
log_b[0] = 0.0;
int32_t len_ = 1;
int32_t i_ptr = 0;
double best_ratio = 0.0;
int32_t have_best = 0;
double log_q = log(q);
int32_t step = 0;
while (step < MAX_STEPS) {
double log_bk = log_b[(len_ - 1)];
double target = (log_bk - log_q);
while (i_ptr < len_) {
if (log_b[i_ptr] >= (target - 1e-10)) {
break;
}
i_ptr = (i_ptr + 1);
}
if (i_ptr >= len_) {
break;
}
if (i_ptr > 0) {
double ratio = exp((log_bk - log_b[(i_ptr - 1)]));
if ((have_best == 0 || ratio < best_ratio)) {
best_ratio = ratio;
have_best = 1;
}
}
double diff = (log_b[i_ptr] - log_bk);
log_b[len_] = (log_bk + log1p(exp(diff)));
len_ = (len_ + 1);
step = (step + 1);
}
free(((void*)(log_b)));
if (have_best == 0) {
return 1.0;
}
return best_ratio;
}
int32_t main(void) {
double q = 1.0;
int32_t i = 0;
while (i < 123455) {
q = next_transition_f64(q);
i = (i + 1);
}
printf("%.10f\n", q);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.10f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func private @log(f64) -> f64
func.func private @exp(f64) -> f64
func.func private @log1p(f64) -> f64
// Constant: MAX_STEPS
llvm.mlir.global internal constant @MAX_STEPS(3000 : i32) : i32
func.func @next_transition(%arg0: f64) -> f64 {
%1 = llvm.mlir.addressof @MAX_STEPS : !llvm.ptr
%2 = llvm.load %1 : !llvm.ptr -> i32
%3 = arith.constant 2 : i32
%4 = arith.addi %2, %3 : i32
%5 = arith.extsi %4 : i32 to i64
%6 = arith.constant 8 : i32
%7 = arith.extsi %6 : i32 to i64
%0 = func.call @calloc(%5, %7) : (i64, i64) -> !llvm.ptr
%8 = arith.constant 0.0 : f32
%9 = arith.constant 0 : i32
%10 = arith.extf %8 : f32 to f64
%11 = arith.extsi %9 : i32 to i64
%12 = llvm.getelementptr %0[%11] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %10, %12 : f64, !llvm.ptr
%13 = arith.constant 1 : i32
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i32 : (i64) -> !llvm.ptr
llvm.store %13, %15 : i32, !llvm.ptr
%16 = arith.constant 0 : i32
%17 = llvm.mlir.constant(1 : i64) : i64
%18 = llvm.alloca %17 x i32 : (i64) -> !llvm.ptr
llvm.store %16, %18 : i32, !llvm.ptr
%19 = arith.constant 0.0 : f32
%20 = arith.extf %19 : f32 to f64
%21 = llvm.mlir.constant(1 : i64) : i64
%22 = llvm.alloca %21 x f64 : (i64) -> !llvm.ptr
llvm.store %20, %22 : f64, !llvm.ptr
%23 = arith.constant 0 : i32
%24 = llvm.mlir.constant(1 : i64) : i64
%25 = llvm.alloca %24 x i32 : (i64) -> !llvm.ptr
llvm.store %23, %25 : i32, !llvm.ptr
%26 = math.log %arg0 : f64
%27 = arith.constant 0 : i32
%28 = llvm.mlir.constant(1 : i64) : i64
%29 = llvm.alloca %28 x i32 : (i64) -> !llvm.ptr
llvm.store %27, %29 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%30 = llvm.load %29 : !llvm.ptr -> i32
%31 = llvm.mlir.addressof @MAX_STEPS : !llvm.ptr
%32 = llvm.load %31 : !llvm.ptr -> i32
%33 = arith.cmpi slt, %30, %32 : i32
cf.cond_br %33, ^bb1, ^bb2
^bb1:
%35 = llvm.load %15 : !llvm.ptr -> i32
%36 = arith.constant 1 : i32
%37 = arith.subi %35, %36 : i32
%38 = arith.extsi %37 : i32 to i64
%39 = llvm.getelementptr %0[%38] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%34 = llvm.load %39 : !llvm.ptr -> f64
%40 = arith.subf %34, %26 : f64
cf.br ^bb3
^bb3:
%41 = llvm.load %18 : !llvm.ptr -> i32
%42 = llvm.load %15 : !llvm.ptr -> i32
%43 = arith.cmpi slt, %41, %42 : i32
cf.cond_br %43, ^bb4, ^bb5
^bb4:
%45 = llvm.load %18 : !llvm.ptr -> i32
%46 = arith.extsi %45 : i32 to i64
%47 = llvm.getelementptr %0[%46] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%44 = llvm.load %47 : !llvm.ptr -> f64
%48 = arith.constant 0.0000000001 : f32
%50 = arith.extf %48 : f32 to f64
%49 = arith.subf %40, %50 : f64
%51 = arith.cmpf oge, %44, %49 : f64
cf.cond_br %51, ^bb6, ^bb7
^bb6:
cf.br ^bb5
^bb7:
cf.br ^bb8
^bb8:
%52 = llvm.load %18 : !llvm.ptr -> i32
%53 = arith.constant 1 : i32
%54 = arith.addi %52, %53 : i32
llvm.store %54, %18 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%55 = llvm.load %18 : !llvm.ptr -> i32
%56 = llvm.load %15 : !llvm.ptr -> i32
%57 = arith.cmpi sge, %55, %56 : i32
cf.cond_br %57, ^bb9, ^bb10
^bb9:
cf.br ^bb2
^bb10:
cf.br ^bb11
^bb11:
%58 = llvm.load %18 : !llvm.ptr -> i32
%59 = arith.constant 0 : i32
%60 = arith.cmpi sgt, %58, %59 : i32
cf.cond_br %60, ^bb12, ^bb13
^bb12:
%62 = llvm.load %18 : !llvm.ptr -> i32
%63 = arith.constant 1 : i32
%64 = arith.subi %62, %63 : i32
%65 = arith.extsi %64 : i32 to i64
%66 = llvm.getelementptr %0[%65] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%61 = llvm.load %66 : !llvm.ptr -> f64
%67 = arith.subf %34, %61 : f64
%68 = math.exp %67 : f64
%69 = llvm.load %25 : !llvm.ptr -> i32
%70 = arith.constant 0 : i32
%71 = arith.cmpi eq, %69, %70 : i32
%72 = scf.if %71 -> (i1) {
%73 = arith.constant true
scf.yield %73 : i1
} else {
%74 = llvm.load %22 : !llvm.ptr -> f64
%75 = arith.cmpf olt, %68, %74 : f64
scf.yield %75 : i1
}
cf.cond_br %72, ^bb15, ^bb16
^bb15:
llvm.store %68, %22 : f64, !llvm.ptr
%76 = arith.constant 1 : i32
llvm.store %76, %25 : i32, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%78 = llvm.load %18 : !llvm.ptr -> i32
%79 = arith.extsi %78 : i32 to i64
%80 = llvm.getelementptr %0[%79] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%77 = llvm.load %80 : !llvm.ptr -> f64
%81 = arith.subf %77, %34 : f64
%83 = math.exp %81 : f64
%82 = func.call @log1p(%83) : (f64) -> f64
%84 = arith.addf %34, %82 : f64
%85 = llvm.load %15 : !llvm.ptr -> i32
%86 = arith.extsi %85 : i32 to i64
%87 = llvm.getelementptr %0[%86] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %84, %87 : f64, !llvm.ptr
%88 = llvm.load %15 : !llvm.ptr -> i32
%89 = arith.constant 1 : i32
%90 = arith.addi %88, %89 : i32
llvm.store %90, %15 : i32, !llvm.ptr
%91 = llvm.load %29 : !llvm.ptr -> i32
%92 = arith.constant 1 : i32
%93 = arith.addi %91, %92 : i32
llvm.store %93, %29 : i32, !llvm.ptr
cf.br ^bb0
^bb2:
func.call @free(%0) : (!llvm.ptr) -> ()
%95 = llvm.load %25 : !llvm.ptr -> i32
%96 = arith.constant 0 : i32
%97 = arith.cmpi eq, %95, %96 : i32
cf.cond_br %97, ^bb18, ^bb19
^bb18:
%98 = arith.constant 1.0 : f32
%99 = arith.extf %98 : f32 to f64
func.return %99 : f64
^bb19:
cf.br ^bb20
^bb20:
%100 = llvm.load %22 : !llvm.ptr -> f64
func.return %100 : f64
}
func.func @main() -> i32 {
%101 = arith.constant 1.0 : f32
%102 = arith.extf %101 : f32 to f64
%103 = llvm.mlir.constant(1 : i64) : i64
%104 = llvm.alloca %103 x f64 : (i64) -> !llvm.ptr
llvm.store %102, %104 : f64, !llvm.ptr
%105 = arith.constant 0 : i32
%106 = llvm.mlir.constant(1 : i64) : i64
%107 = llvm.alloca %106 x i32 : (i64) -> !llvm.ptr
llvm.store %105, %107 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%108 = llvm.load %107 : !llvm.ptr -> i32
%109 = arith.constant 123455 : i32
%110 = arith.cmpi slt, %108, %109 : i32
cf.cond_br %110, ^bb22, ^bb23
^bb22:
%112 = llvm.load %104 : !llvm.ptr -> f64
%111 = func.call @next_transition(%112) : (f64) -> f64
llvm.store %111, %104 : f64, !llvm.ptr
%113 = llvm.load %107 : !llvm.ptr -> i32
%114 = arith.constant 1 : i32
%115 = arith.addi %113, %114 : i32
llvm.store %115, %107 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%116 = llvm.mlir.addressof @str_0 : !llvm.ptr
%117 = llvm.load %104 : !llvm.ptr -> f64
%118 = llvm.call @printf(%116, %117) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%119 = arith.constant 0 : i32
func.return %119 : i32
}
}