← All problems
Problem 938
Exhausting a Colour — P(R,B) via log-gamma closed form. P(24690, 12345).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(1)?
Space complexity O(1)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 938
# Exhausting a Colour — P(R,B) via log-gamma closed form. P(24690, 12345).
extern {
function lgamma(x: f64) -> f64
function log(x: f64) -> f64
function exp(x: f64) -> f64
function log1p(x: f64) -> f64
}
function log_add_exp(log_x: f64, log_y: f64, has_x: i32) -> f64 {
if has_x == 0 { return log_y }
let mut a: f64 = log_x
let mut b: f64 = log_y
if b > a {
let t: f64 = a
a = b
b = t
}
return a + log1p(exp(b - a))
}
function probability_black(R: i64, B: i64) -> f64 {
if B <= 0 { return 0.0 }
if R <= 0 { return 1.0 }
if (R & 1) == 1 { return 0.0 }
let a: i64 = R / 2
let b: i64 = B
if a == 0 { return 1.0 }
let log4: f64 = log(4.0)
let m: i64 = a - 1
let mut log_u: f64 = 0.0
let mut has: i32 = 0
let mut k: i64 = 1
while k <= b {
let log_g: f64 = lgamma((2 * k + 1) as f64) - 2.0 * lgamma((k + 1) as f64) - (k as f64) * log4
let n: i64 = a + b - k - 1
let log_binom: f64 = lgamma((n + 1) as f64) - lgamma((m + 1) as f64) - lgamma((n - m + 1) as f64)
log_u = log_add_exp(log_u, log_g + log_binom, has)
has = 1
k = k + 1
}
let log_C: f64 = lgamma((a + b) as f64 + 0.5) - lgamma(a as f64 + 0.5) - lgamma((b + 1) as f64)
return exp(log_u - log_C)
}
function main() -> i32 {
printf("%.10f\n", probability_black(24690, 12345))
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 lgamma(double x);
double log1p(double x);
double log_add_exp_f64_f64_i32(double log_x, double log_y, int32_t has_x);
double probability_black_i64_i64(int64_t R, int64_t B);
int32_t main(void);
double log_add_exp_f64_f64_i32(double log_x, double log_y, int32_t has_x) {
if (has_x == 0) {
return log_y;
}
double a = log_x;
double b = log_y;
if (b > a) {
double t = a;
a = b;
b = t;
}
return (a + log1p(exp((b - a))));
}
double probability_black_i64_i64(int64_t R, int64_t B) {
if (B <= 0) {
return 0.0;
}
if (R <= 0) {
return 1.0;
}
if ((R & 1) == 1) {
return 0.0;
}
int64_t a = FLOW_CHECKED_DIV((R), (2));
int64_t b = B;
if (a == 0) {
return 1.0;
}
double log4 = log(4.0);
int64_t m = (a - 1);
double log_u = 0.0;
int32_t has = 0;
int64_t k = 1;
while (k <= b) {
double log_g = ((lgamma(((double)(((2 * k) + 1)))) - (2.0 * lgamma(((double)((k + 1)))))) - (((double)(k)) * log4));
int64_t n = (((a + b) - k) - 1);
double log_binom = ((lgamma(((double)((n + 1)))) - lgamma(((double)((m + 1))))) - lgamma(((double)(((n - m) + 1)))));
log_u = log_add_exp_f64_f64_i32(log_u, (log_g + log_binom), has);
has = 1;
k = (k + 1);
}
double log_C = ((lgamma((((double)((a + b))) + 0.5)) - lgamma((((double)(a)) + 0.5))) - lgamma(((double)((b + 1)))));
return exp((log_u - log_C));
}
int32_t main(void) {
printf("%.10f\n", probability_black_i64_i64(24690, 12345));
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 @lgamma(f64) -> f64
func.func private @log(f64) -> f64
func.func private @exp(f64) -> f64
func.func private @log1p(f64) -> f64
func.func @log_add_exp(%arg0: f64, %arg1: f64, %arg2: i32) -> f64 {
%0 = arith.constant 0 : i32
%1 = arith.cmpi eq, %arg2, %0 : i32
cf.cond_br %1, ^bb0, ^bb1
^bb0:
func.return %arg1 : f64
^bb1:
cf.br ^bb2
^bb2:
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x f64 : (i64) -> !llvm.ptr
llvm.store %arg0, %3 : f64, !llvm.ptr
%4 = llvm.mlir.constant(1 : i64) : i64
%5 = llvm.alloca %4 x f64 : (i64) -> !llvm.ptr
llvm.store %arg1, %5 : f64, !llvm.ptr
%6 = llvm.load %5 : !llvm.ptr -> f64
%7 = llvm.load %3 : !llvm.ptr -> f64
%8 = arith.cmpf ogt, %6, %7 : f64
cf.cond_br %8, ^bb3, ^bb4
^bb3:
%9 = llvm.load %3 : !llvm.ptr -> f64
%10 = llvm.load %5 : !llvm.ptr -> f64
llvm.store %10, %3 : f64, !llvm.ptr
llvm.store %9, %5 : f64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%11 = llvm.load %3 : !llvm.ptr -> f64
%13 = llvm.load %5 : !llvm.ptr -> f64
%14 = llvm.load %3 : !llvm.ptr -> f64
%15 = arith.subf %13, %14 : f64
%16 = math.exp %15 : f64
%12 = func.call @log1p(%16) : (f64) -> f64
%17 = arith.addf %11, %12 : f64
func.return %17 : f64
}
func.func @probability_black(%arg0: i64, %arg1: i64) -> f64 {
%18 = arith.constant 0 : i32
%20 = arith.extsi %18 : i32 to i64
%19 = arith.cmpi sle, %arg1, %20 : i64
cf.cond_br %19, ^bb6, ^bb7
^bb6:
%21 = arith.constant 0.0 : f32
%22 = arith.extf %21 : f32 to f64
func.return %22 : f64
^bb7:
cf.br ^bb8
^bb8:
%23 = arith.constant 0 : i32
%25 = arith.extsi %23 : i32 to i64
%24 = arith.cmpi sle, %arg0, %25 : i64
cf.cond_br %24, ^bb9, ^bb10
^bb9:
%26 = arith.constant 1.0 : f32
%27 = arith.extf %26 : f32 to f64
func.return %27 : f64
^bb10:
cf.br ^bb11
^bb11:
%28 = arith.constant 1 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.andi %arg0, %30 : i64
%31 = arith.constant 1 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.cmpi eq, %29, %33 : i64
cf.cond_br %32, ^bb12, ^bb13
^bb12:
%34 = arith.constant 0.0 : f32
%35 = arith.extf %34 : f32 to f64
func.return %35 : f64
^bb13:
cf.br ^bb14
^bb14:
%36 = arith.constant 2 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.divsi %arg0, %38 : i64
%39 = arith.constant 0 : i32
%41 = arith.extsi %39 : i32 to i64
%40 = arith.cmpi eq, %37, %41 : i64
cf.cond_br %40, ^bb15, ^bb16
^bb15:
%42 = arith.constant 1.0 : f32
%43 = arith.extf %42 : f32 to f64
func.return %43 : f64
^bb16:
cf.br ^bb17
^bb17:
%44 = arith.constant 4.0 : f32
%45 = math.log %44 : f32
%46 = arith.extf %45 : f32 to f64
%47 = arith.constant 1 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.subi %37, %49 : i64
%50 = arith.constant 0.0 : f32
%51 = arith.extf %50 : f32 to f64
%52 = llvm.mlir.constant(1 : i64) : i64
%53 = llvm.alloca %52 x f64 : (i64) -> !llvm.ptr
llvm.store %51, %53 : f64, !llvm.ptr
%54 = arith.constant 0 : i32
%55 = llvm.mlir.constant(1 : i64) : i64
%56 = llvm.alloca %55 x i32 : (i64) -> !llvm.ptr
llvm.store %54, %56 : i32, !llvm.ptr
%57 = arith.constant 1 : i32
%58 = arith.extsi %57 : i32 to i64
%59 = llvm.mlir.constant(1 : i64) : i64
%60 = llvm.alloca %59 x i64 : (i64) -> !llvm.ptr
llvm.store %58, %60 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%61 = llvm.load %60 : !llvm.ptr -> i64
%62 = arith.cmpi sle, %61, %arg1 : i64
cf.cond_br %62, ^bb19, ^bb20
^bb19:
%64 = arith.constant 2 : i32
%65 = llvm.load %60 : !llvm.ptr -> i64
%67 = arith.extsi %64 : i32 to i64
%66 = arith.muli %67, %65 : i64
%68 = arith.constant 1 : i32
%70 = arith.extsi %68 : i32 to i64
%69 = arith.addi %66, %70 : i64
%71 = arith.sitofp %69 : i64 to f64
%63 = func.call @lgamma(%71) : (f64) -> f64
%72 = arith.constant 2.0 : f32
%74 = llvm.load %60 : !llvm.ptr -> i64
%75 = arith.constant 1 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.addi %74, %77 : i64
%78 = arith.sitofp %76 : i64 to f64
%73 = func.call @lgamma(%78) : (f64) -> f64
%80 = arith.extf %72 : f32 to f64
%79 = arith.mulf %80, %73 : f64
%81 = arith.subf %63, %79 : f64
%82 = llvm.load %60 : !llvm.ptr -> i64
%83 = arith.sitofp %82 : i64 to f64
%84 = arith.mulf %83, %46 : f64
%85 = arith.subf %81, %84 : f64
%86 = arith.addi %37, %arg1 : i64
%87 = llvm.load %60 : !llvm.ptr -> i64
%88 = arith.subi %86, %87 : i64
%89 = arith.constant 1 : i32
%91 = arith.extsi %89 : i32 to i64
%90 = arith.subi %88, %91 : i64
%93 = arith.constant 1 : i32
%95 = arith.extsi %93 : i32 to i64
%94 = arith.addi %90, %95 : i64
%96 = arith.sitofp %94 : i64 to f64
%92 = func.call @lgamma(%96) : (f64) -> f64
%98 = arith.constant 1 : i32
%100 = arith.extsi %98 : i32 to i64
%99 = arith.addi %48, %100 : i64
%101 = arith.sitofp %99 : i64 to f64
%97 = func.call @lgamma(%101) : (f64) -> f64
%102 = arith.subf %92, %97 : f64
%104 = arith.subi %90, %48 : i64
%105 = arith.constant 1 : i32
%107 = arith.extsi %105 : i32 to i64
%106 = arith.addi %104, %107 : i64
%108 = arith.sitofp %106 : i64 to f64
%103 = func.call @lgamma(%108) : (f64) -> f64
%109 = arith.subf %102, %103 : f64
%111 = llvm.load %53 : !llvm.ptr -> f64
%112 = arith.addf %85, %109 : f64
%113 = llvm.load %56 : !llvm.ptr -> i32
%110 = func.call @log_add_exp(%111, %112, %113) : (f64, f64, i32) -> f64
llvm.store %110, %53 : f64, !llvm.ptr
%114 = arith.constant 1 : i32
llvm.store %114, %56 : i32, !llvm.ptr
%115 = llvm.load %60 : !llvm.ptr -> i64
%116 = arith.constant 1 : i32
%118 = arith.extsi %116 : i32 to i64
%117 = arith.addi %115, %118 : i64
llvm.store %117, %60 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%120 = arith.addi %37, %arg1 : i64
%121 = arith.sitofp %120 : i64 to f64
%122 = arith.constant 0.5 : f32
%124 = arith.extf %122 : f32 to f64
%123 = arith.addf %121, %124 : f64
%119 = func.call @lgamma(%123) : (f64) -> f64
%126 = arith.sitofp %37 : i64 to f64
%127 = arith.constant 0.5 : f32
%129 = arith.extf %127 : f32 to f64
%128 = arith.addf %126, %129 : f64
%125 = func.call @lgamma(%128) : (f64) -> f64
%130 = arith.subf %119, %125 : f64
%132 = arith.constant 1 : i32
%134 = arith.extsi %132 : i32 to i64
%133 = arith.addi %arg1, %134 : i64
%135 = arith.sitofp %133 : i64 to f64
%131 = func.call @lgamma(%135) : (f64) -> f64
%136 = arith.subf %130, %131 : f64
%137 = llvm.load %53 : !llvm.ptr -> f64
%138 = arith.subf %137, %136 : f64
%139 = math.exp %138 : f64
func.return %139 : f64
}
func.func @main() -> i32 {
%140 = llvm.mlir.addressof @str_0 : !llvm.ptr
%142 = arith.constant 24690 : i32
%143 = arith.constant 12345 : i32
%144 = arith.extsi %142 : i32 to i64
%145 = arith.extsi %143 : i32 to i64
%141 = func.call @probability_black(%144, %145) : (i64, i64) -> f64
%146 = llvm.call @printf(%140, %141) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%147 = arith.constant 0 : i32
func.return %147 : i32
}
}