Problem 219
Minimal cost for 10^9 binary codes with costs 1 and 4.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n log n) |
| Space complexity | O(1) | O(n) |
| Approach | Flow solution | Huffman coding |
| Verdict | Suboptimal |
Flow source
# Project Euler 219
# Minimal cost for 10^9 binary codes with costs 1 and 4.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let LIMIT: i64 = 1000000000
let costs: ptr<i64> = calloc(80, 8)
if costs == null { return 1 }
costs[1] = 1
costs[4] = 1
let mut total: i64 = 5
let mut current: i64 = 1
let mut remaining: i64 = LIMIT - 2
while remaining > 0 {
while costs[current] == 0 { current = current + 1 }
let mut block: i64 = costs[current]
if block > remaining { block = remaining }
remaining = remaining - block
costs[current] = costs[current] - block
costs[current + 1] = costs[current + 1] + block
costs[current + 4] = costs[current + 4] + block
total = total + block * (current + 5)
}
printf("%lld\n", total)
free(costs)
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) {
int64_t LIMIT = 1000000000;
int64_t* costs = (int64_t*)(calloc(80, 8));
if (costs == NULL) {
return 1;
}
costs[1] = 1;
costs[4] = 1;
int64_t total = 5;
int64_t current = 1;
int64_t remaining = (LIMIT - 2);
while (remaining > 0) {
while (costs[current] == 0) {
current = (current + 1);
}
int64_t block = costs[current];
if (block > remaining) {
block = remaining;
}
remaining = (remaining - block);
costs[current] = (costs[current] - block);
costs[(current + 1)] = (costs[(current + 1)] + block);
costs[(current + 4)] = (costs[(current + 4)] + block);
total = (total + (block * (current + 5)));
}
printf("%lld\n", total);
free(costs);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%lld\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 1000000000 : i32
%1 = arith.extsi %0 : i32 to i64
%3 = arith.constant 80 : i32
%4 = arith.constant 8 : i32
%5 = arith.extsi %3 : i32 to i64
%6 = arith.extsi %4 : i32 to i64
%2 = func.call @calloc(%5, %6) : (i64, i64) -> !llvm.ptr
%7 = llvm.mlir.zero : !llvm.ptr
%8 = llvm.icmp "eq" %2, %7 : !llvm.ptr
cf.cond_br %8, ^bb0, ^bb1
^bb0:
%9 = arith.constant 1 : i32
func.return %9 : i32
^bb1:
cf.br ^bb2
^bb2:
%10 = arith.constant 1 : i32
%11 = arith.constant 1 : i32
%12 = arith.extsi %10 : i32 to i64
%13 = arith.extsi %11 : i32 to i64
%14 = llvm.getelementptr %2[%13] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %12, %14 : i64, !llvm.ptr
%15 = arith.constant 1 : i32
%16 = arith.constant 4 : i32
%17 = arith.extsi %15 : i32 to i64
%18 = arith.extsi %16 : i32 to i64
%19 = llvm.getelementptr %2[%18] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %17, %19 : i64, !llvm.ptr
%20 = arith.constant 5 : i32
%21 = arith.extsi %20 : i32 to i64
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
llvm.store %21, %23 : i64, !llvm.ptr
%24 = arith.constant 1 : i32
%25 = arith.extsi %24 : i32 to i64
%26 = llvm.mlir.constant(1 : i64) : i64
%27 = llvm.alloca %26 x i64 : (i64) -> !llvm.ptr
llvm.store %25, %27 : i64, !llvm.ptr
%28 = arith.constant 2 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.subi %1, %30 : i64
%31 = llvm.mlir.constant(1 : i64) : i64
%32 = llvm.alloca %31 x i64 : (i64) -> !llvm.ptr
llvm.store %29, %32 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%33 = llvm.load %32 : !llvm.ptr -> i64
%34 = arith.constant 0 : i32
%36 = arith.extsi %34 : i32 to i64
%35 = arith.cmpi sgt, %33, %36 : i64
cf.cond_br %35, ^bb4, ^bb5
^bb4:
cf.br ^bb6
^bb6:
%38 = llvm.load %27 : !llvm.ptr -> i64
%39 = llvm.getelementptr %2[%38] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%37 = llvm.load %39 : !llvm.ptr -> i64
%40 = arith.constant 0 : i32
%42 = arith.extsi %40 : i32 to i64
%41 = arith.cmpi eq, %37, %42 : i64
cf.cond_br %41, ^bb7, ^bb8
^bb7:
%43 = llvm.load %27 : !llvm.ptr -> i64
%44 = arith.constant 1 : i32
%46 = arith.extsi %44 : i32 to i64
%45 = arith.addi %43, %46 : i64
llvm.store %45, %27 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%48 = llvm.load %27 : !llvm.ptr -> i64
%49 = llvm.getelementptr %2[%48] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%47 = llvm.load %49 : !llvm.ptr -> i64
%50 = llvm.mlir.constant(1 : i64) : i64
%51 = llvm.alloca %50 x i64 : (i64) -> !llvm.ptr
llvm.store %47, %51 : i64, !llvm.ptr
%52 = llvm.load %51 : !llvm.ptr -> i64
%53 = llvm.load %32 : !llvm.ptr -> i64
%54 = arith.cmpi sgt, %52, %53 : i64
cf.cond_br %54, ^bb9, ^bb10
^bb9:
%55 = llvm.load %32 : !llvm.ptr -> i64
llvm.store %55, %51 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%56 = llvm.load %32 : !llvm.ptr -> i64
%57 = llvm.load %51 : !llvm.ptr -> i64
%58 = arith.subi %56, %57 : i64
llvm.store %58, %32 : i64, !llvm.ptr
%60 = llvm.load %27 : !llvm.ptr -> i64
%61 = llvm.getelementptr %2[%60] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%59 = llvm.load %61 : !llvm.ptr -> i64
%62 = llvm.load %51 : !llvm.ptr -> i64
%63 = arith.subi %59, %62 : i64
%64 = llvm.load %27 : !llvm.ptr -> i64
%65 = llvm.getelementptr %2[%64] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %63, %65 : i64, !llvm.ptr
%67 = llvm.load %27 : !llvm.ptr -> i64
%68 = arith.constant 1 : i32
%70 = arith.extsi %68 : i32 to i64
%69 = arith.addi %67, %70 : i64
%71 = llvm.getelementptr %2[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%66 = llvm.load %71 : !llvm.ptr -> i64
%72 = llvm.load %51 : !llvm.ptr -> i64
%73 = arith.addi %66, %72 : i64
%74 = llvm.load %27 : !llvm.ptr -> i64
%75 = arith.constant 1 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.addi %74, %77 : i64
%78 = llvm.getelementptr %2[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %73, %78 : i64, !llvm.ptr
%80 = llvm.load %27 : !llvm.ptr -> i64
%81 = arith.constant 4 : i32
%83 = arith.extsi %81 : i32 to i64
%82 = arith.addi %80, %83 : i64
%84 = llvm.getelementptr %2[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%79 = llvm.load %84 : !llvm.ptr -> i64
%85 = llvm.load %51 : !llvm.ptr -> i64
%86 = arith.addi %79, %85 : i64
%87 = llvm.load %27 : !llvm.ptr -> i64
%88 = arith.constant 4 : i32
%90 = arith.extsi %88 : i32 to i64
%89 = arith.addi %87, %90 : i64
%91 = llvm.getelementptr %2[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %86, %91 : i64, !llvm.ptr
%92 = llvm.load %23 : !llvm.ptr -> i64
%93 = llvm.load %51 : !llvm.ptr -> i64
%94 = llvm.load %27 : !llvm.ptr -> i64
%95 = arith.constant 5 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.addi %94, %97 : i64
%98 = arith.muli %93, %96 : i64
%99 = arith.addi %92, %98 : i64
llvm.store %99, %23 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%100 = llvm.mlir.addressof @str_0 : !llvm.ptr
%101 = llvm.load %23 : !llvm.ptr -> i64
%102 = llvm.call @printf(%100, %101) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%2) : (!llvm.ptr) -> ()
%104 = arith.constant 0 : i32
func.return %104 : i32
}
}