← All problems
Problem 863
Different Dice — expected rolls to emulate n-sided die with d5/d6.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n * s^2)
Space complexity O(n^2)O(s^2)
Approach Flow solution Markov chain or DP over states
Verdict Unknown
Flow source
# Project Euler 863
# Different Dice — expected rolls to emulate n-sided die with d5/d6.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function fabs(x: f64) -> f64
}
function R(n: i64) -> f64 {
let nxt5: ptr<i64> = calloc(n, 8)
let nxt6: ptr<i64> = calloc(n, 8)
let c5: ptr<f64> = calloc(n, 8)
let c6: ptr<f64> = calloc(n, 8)
let E: ptr<f64> = calloc(n, 8)
let mut s: i64 = 1
while s < n {
let t5: i64 = 5 * s
let r5: i64 = t5 % n
nxt5[s] = r5
if r5 == 0 { c5[s] = 0.0 } else { c5[s] = (r5 as f64) / (t5 as f64) }
let t6: i64 = 6 * s
let r6: i64 = t6 % n
nxt6[s] = r6
if r6 == 0 { c6[s] = 0.0 } else { c6[s] = (r6 as f64) / (t6 as f64) }
s = s + 1
}
let mut iter: i64 = 0
while iter < 10000 {
let mut delta: f64 = 0.0
s = n - 1
while s > 0 {
let v0: f64 = 1.0 + c5[s] * E[nxt5[s]]
let v1: f64 = 1.0 + c6[s] * E[nxt6[s]]
let mut neu: f64 = v0
if v1 < v0 { neu = v1 }
let diff: f64 = fabs(neu - E[s])
if diff > delta { delta = diff }
E[s] = neu
s = s - 1
}
if delta < 1e-15 { break }
iter = iter + 1
}
let ans: f64 = E[1]
free(nxt5)
free(nxt6)
free(c5)
free(c6)
free(E)
return ans
}
function main() -> i32 {
let mut total: f64 = 0.0
let mut k: i64 = 2
while k <= 1000 {
total = total + R(k)
k = k + 1
}
printf("%.6f\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 R_i64(int64_t n);
int32_t main(void);
double R_i64(int64_t n) {
int64_t* nxt5 = (int64_t*)(calloc(n, 8));
int64_t* nxt6 = (int64_t*)(calloc(n, 8));
double* c5 = (double*)(calloc(n, 8));
double* c6 = (double*)(calloc(n, 8));
double* E = (double*)(calloc(n, 8));
int64_t s = 1;
while (s < n) {
int64_t t5 = (5 * s);
int64_t r5 = FLOW_CHECKED_MOD((t5), (n));
nxt5[s] = r5;
if (r5 == 0) {
c5[s] = 0.0;
} else {
c5[s] = (((double)(r5)) / ((double)(t5)));
}
int64_t t6 = (6 * s);
int64_t r6 = FLOW_CHECKED_MOD((t6), (n));
nxt6[s] = r6;
if (r6 == 0) {
c6[s] = 0.0;
} else {
c6[s] = (((double)(r6)) / ((double)(t6)));
}
s = (s + 1);
}
int64_t iter = 0;
while (iter < 10000) {
double delta = 0.0;
s = (n - 1);
while (s > 0) {
double v0 = (1.0 + (c5[s] * E[nxt5[s]]));
double v1 = (1.0 + (c6[s] * E[nxt6[s]]));
double neu = v0;
if (v1 < v0) {
neu = v1;
}
double diff = fabs((neu - E[s]));
if (diff > delta) {
delta = diff;
}
E[s] = neu;
s = (s - 1);
}
if (delta < 1e-15) {
break;
}
iter = (iter + 1);
}
double ans = E[1];
free(nxt5);
free(nxt6);
free(c5);
free(c6);
free(E);
return ans;
}
int32_t main(void) {
double total = 0.0;
int64_t k = 2;
while (k <= 1000) {
total = (total + R_i64(k));
k = (k + 1);
}
printf("%.6f\n", total);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.6f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func private @fabs(f64) -> f64
func.func @R(%arg0: i64) -> f64 {
%1 = arith.constant 8 : i32
%2 = arith.extsi %1 : i32 to i64
%0 = func.call @calloc(%arg0, %2) : (i64, i64) -> !llvm.ptr
%4 = arith.constant 8 : i32
%5 = arith.extsi %4 : i32 to i64
%3 = func.call @calloc(%arg0, %5) : (i64, i64) -> !llvm.ptr
%7 = arith.constant 8 : i32
%8 = arith.extsi %7 : i32 to i64
%6 = func.call @calloc(%arg0, %8) : (i64, i64) -> !llvm.ptr
%10 = arith.constant 8 : i32
%11 = arith.extsi %10 : i32 to i64
%9 = func.call @calloc(%arg0, %11) : (i64, i64) -> !llvm.ptr
%13 = arith.constant 8 : i32
%14 = arith.extsi %13 : i32 to i64
%12 = func.call @calloc(%arg0, %14) : (i64, i64) -> !llvm.ptr
%15 = arith.constant 1 : i32
%16 = arith.extsi %15 : i32 to i64
%17 = llvm.mlir.constant(1 : i64) : i64
%18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
llvm.store %16, %18 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%19 = llvm.load %18 : !llvm.ptr -> i64
%20 = arith.cmpi slt, %19, %arg0 : i64
cf.cond_br %20, ^bb1, ^bb2
^bb1:
%21 = arith.constant 5 : i32
%22 = llvm.load %18 : !llvm.ptr -> i64
%24 = arith.extsi %21 : i32 to i64
%23 = arith.muli %24, %22 : i64
%25 = arith.remsi %23, %arg0 : i64
%26 = llvm.load %18 : !llvm.ptr -> i64
%27 = llvm.getelementptr %0[%26] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %25, %27 : i64, !llvm.ptr
%28 = arith.constant 0 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.cmpi eq, %25, %30 : i64
cf.cond_br %29, ^bb3, ^bb4
^bb3:
%31 = arith.constant 0.0 : f32
%32 = llvm.load %18 : !llvm.ptr -> i64
%33 = arith.extf %31 : f32 to f64
%34 = llvm.getelementptr %6[%32] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %33, %34 : f64, !llvm.ptr
cf.br ^bb5
^bb4:
%35 = arith.sitofp %25 : i64 to f64
%36 = arith.sitofp %23 : i64 to f64
%37 = arith.divf %35, %36 : f64
%38 = llvm.load %18 : !llvm.ptr -> i64
%39 = llvm.getelementptr %6[%38] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %37, %39 : f64, !llvm.ptr
cf.br ^bb5
^bb5:
%40 = arith.constant 6 : i32
%41 = llvm.load %18 : !llvm.ptr -> i64
%43 = arith.extsi %40 : i32 to i64
%42 = arith.muli %43, %41 : i64
%44 = arith.remsi %42, %arg0 : i64
%45 = llvm.load %18 : !llvm.ptr -> i64
%46 = llvm.getelementptr %3[%45] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %44, %46 : i64, !llvm.ptr
%47 = arith.constant 0 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.cmpi eq, %44, %49 : i64
cf.cond_br %48, ^bb6, ^bb7
^bb6:
%50 = arith.constant 0.0 : f32
%51 = llvm.load %18 : !llvm.ptr -> i64
%52 = arith.extf %50 : f32 to f64
%53 = llvm.getelementptr %9[%51] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %52, %53 : f64, !llvm.ptr
cf.br ^bb8
^bb7:
%54 = arith.sitofp %44 : i64 to f64
%55 = arith.sitofp %42 : i64 to f64
%56 = arith.divf %54, %55 : f64
%57 = llvm.load %18 : !llvm.ptr -> i64
%58 = llvm.getelementptr %9[%57] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %56, %58 : f64, !llvm.ptr
cf.br ^bb8
^bb8:
%59 = llvm.load %18 : !llvm.ptr -> i64
%60 = arith.constant 1 : i32
%62 = arith.extsi %60 : i32 to i64
%61 = arith.addi %59, %62 : i64
llvm.store %61, %18 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%63 = arith.constant 0 : i32
%64 = arith.extsi %63 : i32 to i64
%65 = llvm.mlir.constant(1 : i64) : i64
%66 = llvm.alloca %65 x i64 : (i64) -> !llvm.ptr
llvm.store %64, %66 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%67 = llvm.load %66 : !llvm.ptr -> i64
%68 = arith.constant 10000 : i32
%70 = arith.extsi %68 : i32 to i64
%69 = arith.cmpi slt, %67, %70 : i64
cf.cond_br %69, ^bb10, ^bb11
^bb10:
%71 = arith.constant 0.0 : f32
%72 = arith.extf %71 : f32 to f64
%73 = llvm.mlir.constant(1 : i64) : i64
%74 = llvm.alloca %73 x f64 : (i64) -> !llvm.ptr
llvm.store %72, %74 : f64, !llvm.ptr
%75 = arith.constant 1 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.subi %arg0, %77 : i64
llvm.store %76, %18 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%78 = llvm.load %18 : !llvm.ptr -> i64
%79 = arith.constant 0 : i32
%81 = arith.extsi %79 : i32 to i64
%80 = arith.cmpi sgt, %78, %81 : i64
cf.cond_br %80, ^bb13, ^bb14
^bb13:
%82 = arith.constant 1.0 : f32
%84 = llvm.load %18 : !llvm.ptr -> i64
%85 = llvm.getelementptr %6[%84] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%83 = llvm.load %85 : !llvm.ptr -> f64
%88 = llvm.load %18 : !llvm.ptr -> i64
%89 = llvm.getelementptr %0[%88] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%87 = llvm.load %89 : !llvm.ptr -> i64
%90 = llvm.getelementptr %12[%87] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%86 = llvm.load %90 : !llvm.ptr -> f64
%91 = arith.mulf %83, %86 : f64
%93 = arith.extf %82 : f32 to f64
%92 = arith.addf %93, %91 : f64
%94 = arith.constant 1.0 : f32
%96 = llvm.load %18 : !llvm.ptr -> i64
%97 = llvm.getelementptr %9[%96] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%95 = llvm.load %97 : !llvm.ptr -> f64
%100 = llvm.load %18 : !llvm.ptr -> i64
%101 = llvm.getelementptr %3[%100] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%99 = llvm.load %101 : !llvm.ptr -> i64
%102 = llvm.getelementptr %12[%99] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%98 = llvm.load %102 : !llvm.ptr -> f64
%103 = arith.mulf %95, %98 : f64
%105 = arith.extf %94 : f32 to f64
%104 = arith.addf %105, %103 : f64
%106 = llvm.mlir.constant(1 : i64) : i64
%107 = llvm.alloca %106 x f64 : (i64) -> !llvm.ptr
llvm.store %92, %107 : f64, !llvm.ptr
%108 = arith.cmpf olt, %104, %92 : f64
cf.cond_br %108, ^bb15, ^bb16
^bb15:
llvm.store %104, %107 : f64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%109 = llvm.load %107 : !llvm.ptr -> f64
%111 = llvm.load %18 : !llvm.ptr -> i64
%112 = llvm.getelementptr %12[%111] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%110 = llvm.load %112 : !llvm.ptr -> f64
%113 = arith.subf %109, %110 : f64
%114 = math.absf %113 : f64
%115 = llvm.load %74 : !llvm.ptr -> f64
%116 = arith.cmpf ogt, %114, %115 : f64
cf.cond_br %116, ^bb18, ^bb19
^bb18:
llvm.store %114, %74 : f64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%117 = llvm.load %107 : !llvm.ptr -> f64
%118 = llvm.load %18 : !llvm.ptr -> i64
%119 = llvm.getelementptr %12[%118] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %117, %119 : f64, !llvm.ptr
%120 = llvm.load %18 : !llvm.ptr -> i64
%121 = arith.constant 1 : i32
%123 = arith.extsi %121 : i32 to i64
%122 = arith.subi %120, %123 : i64
llvm.store %122, %18 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%124 = llvm.load %74 : !llvm.ptr -> f64
%125 = arith.constant 0 : f32
%127 = arith.extf %125 : f32 to f64
%126 = arith.cmpf olt, %124, %127 : f64
cf.cond_br %126, ^bb21, ^bb22
^bb21:
cf.br ^bb11
^bb22:
cf.br ^bb23
^bb23:
%128 = llvm.load %66 : !llvm.ptr -> i64
%129 = arith.constant 1 : i32
%131 = arith.extsi %129 : i32 to i64
%130 = arith.addi %128, %131 : i64
llvm.store %130, %66 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%133 = arith.constant 1 : i32
%134 = arith.extsi %133 : i32 to i64
%135 = llvm.getelementptr %12[%134] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%132 = llvm.load %135 : !llvm.ptr -> f64
func.call @free(%0) : (!llvm.ptr) -> ()
func.call @free(%3) : (!llvm.ptr) -> ()
func.call @free(%6) : (!llvm.ptr) -> ()
func.call @free(%9) : (!llvm.ptr) -> ()
func.call @free(%12) : (!llvm.ptr) -> ()
func.return %132 : f64
}
func.func @main() -> i32 {
%141 = arith.constant 0.0 : f32
%142 = arith.extf %141 : f32 to f64
%143 = llvm.mlir.constant(1 : i64) : i64
%144 = llvm.alloca %143 x f64 : (i64) -> !llvm.ptr
llvm.store %142, %144 : f64, !llvm.ptr
%145 = arith.constant 2 : i32
%146 = arith.extsi %145 : i32 to i64
%147 = llvm.mlir.constant(1 : i64) : i64
%148 = llvm.alloca %147 x i64 : (i64) -> !llvm.ptr
llvm.store %146, %148 : i64, !llvm.ptr
cf.br ^bb24
^bb24:
%149 = llvm.load %148 : !llvm.ptr -> i64
%150 = arith.constant 1000 : i32
%152 = arith.extsi %150 : i32 to i64
%151 = arith.cmpi sle, %149, %152 : i64
cf.cond_br %151, ^bb25, ^bb26
^bb25:
%153 = llvm.load %144 : !llvm.ptr -> f64
%155 = llvm.load %148 : !llvm.ptr -> i64
%154 = func.call @R(%155) : (i64) -> f64
%156 = arith.addf %153, %154 : f64
llvm.store %156, %144 : f64, !llvm.ptr
%157 = llvm.load %148 : !llvm.ptr -> i64
%158 = arith.constant 1 : i32
%160 = arith.extsi %158 : i32 to i64
%159 = arith.addi %157, %160 : i64
llvm.store %159, %148 : i64, !llvm.ptr
cf.br ^bb24
^bb26:
%161 = llvm.mlir.addressof @str_0 : !llvm.ptr
%162 = llvm.load %144 : !llvm.ptr -> f64
%163 = llvm.call @printf(%161, %162) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%164 = arith.constant 0 : i32
func.return %164 : i32
}
}