Problem 389
Variance of nested Platonic dice: 1d4→d6→d8→d12→d20, 4 decimals.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n * s) |
| Space complexity | O(1) | O(s) |
| Approach | Flow solution | Dice distribution DP |
| Verdict | Unknown |
Flow source
# Project Euler 389
# Variance of nested Platonic dice: 1d4→d6→d8→d12→d20, 4 decimals.
function main() -> i32 {
# mean = (s+1)/2, var = (s^2-1)/12 as rationals tracked by numer/denom
# Use f64 with enough precision for final 4 decimals (exact fractions are small).
let mut mean: f64 = (4.0 + 1.0) / 2.0
let mut var: f64 = (16.0 - 1.0) / 12.0
let sides: ptr<i32> = calloc(4, 4)
if sides == null { return 1 }
sides[0] = 6; sides[1] = 8; sides[2] = 12; sides[3] = 20
let mut i: i32 = 0
while i < 4 {
let s: f64 = sides[i] as f64
let y_mean: f64 = (s + 1.0) / 2.0
let y_var: f64 = (s * s - 1.0) / 12.0
let n_mean: f64 = mean
let n_var: f64 = var
mean = n_mean * y_mean
var = n_mean * y_var + n_var * y_mean * y_mean
i = i + 1
}
printf("%.4f\n", var)
free(sides)
return 0
}
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
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) {
double mean = ((4.0 + 1.0) / 2.0);
double var = ((16.0 - 1.0) / 12.0);
int32_t* sides = (int32_t*)(calloc(4, 4));
if (sides == NULL) {
return 1;
}
sides[0] = 6;
sides[1] = 8;
sides[2] = 12;
sides[3] = 20;
int32_t i = 0;
while (i < 4) {
double s = ((double)(sides[i]));
double y_mean = ((s + 1.0) / 2.0);
double y_var = (((s * s) - 1.0) / 12.0);
double n_mean = mean;
double n_var = var;
mean = (n_mean * y_mean);
var = ((n_mean * y_var) + ((n_var * y_mean) * y_mean));
i = (i + 1);
}
printf("%.4f\n", var);
free(sides);
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 @main() -> i32 {
%0 = arith.constant 4.0 : f32
%1 = arith.constant 1.0 : f32
%2 = arith.addf %0, %1 : f32
%3 = arith.constant 2.0 : f32
%4 = arith.divf %2, %3 : f32
%5 = arith.extf %4 : f32 to f64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x f64 : (i64) -> !llvm.ptr
llvm.store %5, %7 : f64, !llvm.ptr
%8 = arith.constant 16.0 : f32
%9 = arith.constant 1.0 : f32
%10 = arith.subf %8, %9 : f32
%11 = arith.constant 12.0 : f32
%12 = arith.divf %10, %11 : f32
%13 = arith.extf %12 : f32 to f64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x f64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : f64, !llvm.ptr
%17 = arith.constant 4 : i32
%18 = arith.constant 4 : i32
%19 = arith.extsi %17 : i32 to i64
%20 = arith.extsi %18 : i32 to i64
%16 = func.call @calloc(%19, %20) : (i64, i64) -> !llvm.ptr
%21 = llvm.mlir.zero : !llvm.ptr
%22 = llvm.icmp "eq" %16, %21 : !llvm.ptr
cf.cond_br %22, ^bb0, ^bb1
^bb0:
%23 = arith.constant 1 : i32
func.return %23 : i32
^bb1:
cf.br ^bb2
^bb2:
%24 = arith.constant 6 : i32
%25 = arith.constant 0 : i32
%26 = arith.extsi %25 : i32 to i64
%27 = llvm.getelementptr %16[%26] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %24, %27 : i32, !llvm.ptr
%28 = arith.constant 8 : i32
%29 = arith.constant 1 : i32
%30 = arith.extsi %29 : i32 to i64
%31 = llvm.getelementptr %16[%30] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %28, %31 : i32, !llvm.ptr
%32 = arith.constant 12 : i32
%33 = arith.constant 2 : i32
%34 = arith.extsi %33 : i32 to i64
%35 = llvm.getelementptr %16[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %32, %35 : i32, !llvm.ptr
%36 = arith.constant 20 : i32
%37 = arith.constant 3 : i32
%38 = arith.extsi %37 : i32 to i64
%39 = llvm.getelementptr %16[%38] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %36, %39 : i32, !llvm.ptr
%40 = arith.constant 0 : i32
%41 = llvm.mlir.constant(1 : i64) : i64
%42 = llvm.alloca %41 x i32 : (i64) -> !llvm.ptr
llvm.store %40, %42 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%43 = llvm.load %42 : !llvm.ptr -> i32
%44 = arith.constant 4 : i32
%45 = arith.cmpi slt, %43, %44 : i32
cf.cond_br %45, ^bb4, ^bb5
^bb4:
%47 = llvm.load %42 : !llvm.ptr -> i32
%48 = arith.extsi %47 : i32 to i64
%49 = llvm.getelementptr %16[%48] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%46 = llvm.load %49 : !llvm.ptr -> i32
%50 = arith.sitofp %46 : i32 to f64
%51 = arith.constant 1.0 : f32
%53 = arith.extf %51 : f32 to f64
%52 = arith.addf %50, %53 : f64
%54 = arith.constant 2.0 : f32
%56 = arith.extf %54 : f32 to f64
%55 = arith.divf %52, %56 : f64
%57 = arith.mulf %50, %50 : f64
%58 = arith.constant 1.0 : f32
%60 = arith.extf %58 : f32 to f64
%59 = arith.subf %57, %60 : f64
%61 = arith.constant 12.0 : f32
%63 = arith.extf %61 : f32 to f64
%62 = arith.divf %59, %63 : f64
%64 = llvm.load %7 : !llvm.ptr -> f64
%65 = llvm.load %15 : !llvm.ptr -> f64
%66 = arith.mulf %64, %55 : f64
llvm.store %66, %7 : f64, !llvm.ptr
%67 = arith.mulf %64, %62 : f64
%68 = arith.mulf %65, %55 : f64
%69 = arith.mulf %68, %55 : f64
%70 = arith.addf %67, %69 : f64
llvm.store %70, %15 : f64, !llvm.ptr
%71 = llvm.load %42 : !llvm.ptr -> i32
%72 = arith.constant 1 : i32
%73 = arith.addi %71, %72 : i32
llvm.store %73, %42 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%74 = llvm.mlir.addressof @str_0 : !llvm.ptr
%75 = llvm.load %15 : !llvm.ptr -> f64
%76 = llvm.call @printf(%74, %75) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
func.call @free(%16) : (!llvm.ptr) -> ()
%78 = arith.constant 0 : i32
func.return %78 : i32
}
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
}