Problem 371
Expected number of plates until 000/500 pair (mod 1000).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n * s^2) |
| Space complexity | O(n) | O(s^2) |
| Approach | Flow solution | Markov chain or DP over states |
| Verdict | Unknown |
Flow source
# Project Euler 371
# Expected number of plates until 000/500 pair (mod 1000).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let num_plates: i32 = 1000
let max_have: i32 = num_plates / 2 - 1
let plates: f64 = num_plates as f64
let have500: ptr<f64> = calloc((max_have + 1) as i64, 8)
let no500: ptr<f64> = calloc((max_have + 1) as i64, 8)
if have500 == null || no500 == null { return 1 }
let mut prob_duplicate: f64 = (max_have as f64) / plates
let prob_zero: f64 = 1.0 / plates
let prob_500: f64 = 1.0 / plates
let mut prob_unchanged: f64 = prob_duplicate + prob_zero
have500[max_have] = 1.0 / (1.0 - prob_unchanged)
no500[max_have] = (1.0 + prob_500 * have500[max_have]) / (1.0 - prob_unchanged)
let mut have: i32 = max_have - 1
while have >= 0 {
let num_new: f64 = plates - 2.0 * (have as f64) - 2.0
let prob_new: f64 = num_new / plates
prob_duplicate = (have as f64) / plates
prob_unchanged = prob_duplicate + prob_zero
have500[have] = (1.0 + prob_new * have500[have + 1]) / (1.0 - prob_unchanged)
no500[have] = (1.0 + prob_500 * have500[have] + prob_new * no500[have + 1]) / (1.0 - prob_unchanged)
have = have - 1
}
printf("%.8f\n", no500[0])
free(no500)
free(have500)
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; }
int32_t main(void);
int32_t main(void) {
int32_t num_plates = 1000;
int32_t max_have = (FLOW_CHECKED_DIV((num_plates), (2)) - 1);
double plates = ((double)(num_plates));
double* have500 = (double*)(calloc(((int64_t)((max_have + 1))), 8));
double* no500 = (double*)(calloc(((int64_t)((max_have + 1))), 8));
if ((have500 == NULL || no500 == NULL)) {
return 1;
}
double prob_duplicate = (((double)(max_have)) / plates);
double prob_zero = (1.0 / plates);
double prob_500 = (1.0 / plates);
double prob_unchanged = (prob_duplicate + prob_zero);
have500[max_have] = (1.0 / (1.0 - prob_unchanged));
no500[max_have] = ((1.0 + (prob_500 * have500[max_have])) / (1.0 - prob_unchanged));
int32_t have = (max_have - 1);
while (have >= 0) {
double num_new = ((plates - (2.0 * ((double)(have)))) - 2.0);
double prob_new = (num_new / plates);
prob_duplicate = (((double)(have)) / plates);
prob_unchanged = (prob_duplicate + prob_zero);
have500[have] = ((1.0 + (prob_new * have500[(have + 1)])) / (1.0 - prob_unchanged));
no500[have] = (((1.0 + (prob_500 * have500[have])) + (prob_new * no500[(have + 1)])) / (1.0 - prob_unchanged));
have = (have - 1);
}
printf("%.8f\n", no500[0]);
free(no500);
free(have500);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.8f\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 @main() -> i32 {
%0 = arith.constant 1000 : i32
%1 = arith.constant 2 : i32
%2 = arith.divsi %0, %1 : i32
%3 = arith.constant 1 : i32
%4 = arith.subi %2, %3 : i32
%5 = arith.sitofp %0 : i32 to f64
%7 = arith.constant 1 : i32
%8 = arith.addi %4, %7 : i32
%9 = arith.extsi %8 : i32 to i64
%10 = arith.constant 8 : i32
%11 = arith.extsi %10 : i32 to i64
%6 = func.call @calloc(%9, %11) : (i64, i64) -> !llvm.ptr
%13 = arith.constant 1 : i32
%14 = arith.addi %4, %13 : i32
%15 = arith.extsi %14 : i32 to i64
%16 = arith.constant 8 : i32
%17 = arith.extsi %16 : i32 to i64
%12 = func.call @calloc(%15, %17) : (i64, i64) -> !llvm.ptr
%18 = llvm.mlir.zero : !llvm.ptr
%19 = llvm.icmp "eq" %6, %18 : !llvm.ptr
%20 = scf.if %19 -> (i1) {
%21 = arith.constant true
scf.yield %21 : i1
} else {
%22 = llvm.mlir.zero : !llvm.ptr
%23 = llvm.icmp "eq" %12, %22 : !llvm.ptr
scf.yield %23 : i1
}
cf.cond_br %20, ^bb0, ^bb1
^bb0:
%24 = arith.constant 1 : i32
func.return %24 : i32
^bb1:
cf.br ^bb2
^bb2:
%25 = arith.sitofp %4 : i32 to f64
%26 = arith.divf %25, %5 : f64
%27 = llvm.mlir.constant(1 : i64) : i64
%28 = llvm.alloca %27 x f64 : (i64) -> !llvm.ptr
llvm.store %26, %28 : f64, !llvm.ptr
%29 = arith.constant 1.0 : f32
%31 = arith.extf %29 : f32 to f64
%30 = arith.divf %31, %5 : f64
%32 = arith.constant 1.0 : f32
%34 = arith.extf %32 : f32 to f64
%33 = arith.divf %34, %5 : f64
%35 = llvm.load %28 : !llvm.ptr -> f64
%36 = arith.addf %35, %30 : f64
%37 = llvm.mlir.constant(1 : i64) : i64
%38 = llvm.alloca %37 x f64 : (i64) -> !llvm.ptr
llvm.store %36, %38 : f64, !llvm.ptr
%39 = arith.constant 1.0 : f32
%40 = arith.constant 1.0 : f32
%41 = llvm.load %38 : !llvm.ptr -> f64
%43 = arith.extf %40 : f32 to f64
%42 = arith.subf %43, %41 : f64
%45 = arith.extf %39 : f32 to f64
%44 = arith.divf %45, %42 : f64
%46 = arith.extsi %4 : i32 to i64
%47 = llvm.getelementptr %6[%46] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %44, %47 : f64, !llvm.ptr
%48 = arith.constant 1.0 : f32
%50 = arith.extsi %4 : i32 to i64
%51 = llvm.getelementptr %6[%50] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%49 = llvm.load %51 : !llvm.ptr -> f64
%52 = arith.mulf %33, %49 : f64
%54 = arith.extf %48 : f32 to f64
%53 = arith.addf %54, %52 : f64
%55 = arith.constant 1.0 : f32
%56 = llvm.load %38 : !llvm.ptr -> f64
%58 = arith.extf %55 : f32 to f64
%57 = arith.subf %58, %56 : f64
%59 = arith.divf %53, %57 : f64
%60 = arith.extsi %4 : i32 to i64
%61 = llvm.getelementptr %12[%60] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %59, %61 : f64, !llvm.ptr
%62 = arith.constant 1 : i32
%63 = arith.subi %4, %62 : i32
%64 = llvm.mlir.constant(1 : i64) : i64
%65 = llvm.alloca %64 x i32 : (i64) -> !llvm.ptr
llvm.store %63, %65 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%66 = llvm.load %65 : !llvm.ptr -> i32
%67 = arith.constant 0 : i32
%68 = arith.cmpi sge, %66, %67 : i32
cf.cond_br %68, ^bb4, ^bb5
^bb4:
%69 = arith.constant 2.0 : f32
%70 = llvm.load %65 : !llvm.ptr -> i32
%71 = arith.sitofp %70 : i32 to f64
%73 = arith.extf %69 : f32 to f64
%72 = arith.mulf %73, %71 : f64
%74 = arith.subf %5, %72 : f64
%75 = arith.constant 2.0 : f32
%77 = arith.extf %75 : f32 to f64
%76 = arith.subf %74, %77 : f64
%78 = arith.divf %76, %5 : f64
%79 = llvm.load %65 : !llvm.ptr -> i32
%80 = arith.sitofp %79 : i32 to f64
%81 = arith.divf %80, %5 : f64
llvm.store %81, %28 : f64, !llvm.ptr
%82 = llvm.load %28 : !llvm.ptr -> f64
%83 = arith.addf %82, %30 : f64
llvm.store %83, %38 : f64, !llvm.ptr
%84 = arith.constant 1.0 : f32
%86 = llvm.load %65 : !llvm.ptr -> i32
%87 = arith.constant 1 : i32
%88 = arith.addi %86, %87 : i32
%89 = arith.extsi %88 : i32 to i64
%90 = llvm.getelementptr %6[%89] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%85 = llvm.load %90 : !llvm.ptr -> f64
%91 = arith.mulf %78, %85 : f64
%93 = arith.extf %84 : f32 to f64
%92 = arith.addf %93, %91 : f64
%94 = arith.constant 1.0 : f32
%95 = llvm.load %38 : !llvm.ptr -> f64
%97 = arith.extf %94 : f32 to f64
%96 = arith.subf %97, %95 : f64
%98 = arith.divf %92, %96 : f64
%99 = llvm.load %65 : !llvm.ptr -> i32
%100 = arith.extsi %99 : i32 to i64
%101 = llvm.getelementptr %6[%100] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %98, %101 : f64, !llvm.ptr
%102 = arith.constant 1.0 : f32
%104 = llvm.load %65 : !llvm.ptr -> i32
%105 = arith.extsi %104 : i32 to i64
%106 = llvm.getelementptr %6[%105] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%103 = llvm.load %106 : !llvm.ptr -> f64
%107 = arith.mulf %33, %103 : f64
%109 = arith.extf %102 : f32 to f64
%108 = arith.addf %109, %107 : f64
%111 = llvm.load %65 : !llvm.ptr -> i32
%112 = arith.constant 1 : i32
%113 = arith.addi %111, %112 : i32
%114 = arith.extsi %113 : i32 to i64
%115 = llvm.getelementptr %12[%114] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%110 = llvm.load %115 : !llvm.ptr -> f64
%116 = arith.mulf %78, %110 : f64
%117 = arith.addf %108, %116 : f64
%118 = arith.constant 1.0 : f32
%119 = llvm.load %38 : !llvm.ptr -> f64
%121 = arith.extf %118 : f32 to f64
%120 = arith.subf %121, %119 : f64
%122 = arith.divf %117, %120 : f64
%123 = llvm.load %65 : !llvm.ptr -> i32
%124 = arith.extsi %123 : i32 to i64
%125 = llvm.getelementptr %12[%124] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %122, %125 : f64, !llvm.ptr
%126 = llvm.load %65 : !llvm.ptr -> i32
%127 = arith.constant 1 : i32
%128 = arith.subi %126, %127 : i32
llvm.store %128, %65 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%129 = llvm.mlir.addressof @str_0 : !llvm.ptr
%131 = arith.constant 0 : i32
%132 = arith.extsi %131 : i32 to i64
%133 = llvm.getelementptr %12[%132] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%130 = llvm.load %133 : !llvm.ptr -> f64
%134 = llvm.call @printf(%129, %130) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
func.call @free(%12) : (!llvm.ptr) -> ()
func.call @free(%6) : (!llvm.ptr) -> ()
%137 = arith.constant 0 : i32
func.return %137 : i32
}
}