← All problems
Problem 150
Minimum sub-triangle sum in a 1000-row LCG triangle.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n^2)
Space complexity O(n)O(n^2)
Approach Flow solution Bottom-up DP
Verdict Optimal
Flow source
# Project Euler 150
# Minimum sub-triangle sum in a 1000-row LCG triangle.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let size: i64 = 1000
let total_n: i64 = size * (size + 1) / 2
let tri: ptr<i64> = calloc(total_n, 8)
let row_start: ptr<i64> = calloc(size + 1, 8)
if tri == null || row_start == null { return 1 }
let mut seed: i64 = 0
let mask: i64 = 1048576 # 2^20
let half: i64 = 524288 # 2^19
let mut idx: i64 = 0
let mut y: i64 = 0
while y < size {
row_start[y] = idx
let mut x: i64 = 0
while x <= y {
seed = (615949 * seed + 797807) % mask
tri[idx] = seed - half
idx = idx + 1
x = x + 1
}
y = y + 1
}
# prefix sums per row: sums[y][x] = sum of first x+1 entries of row y
let sums: ptr<i64> = calloc(total_n, 8)
if sums == null { return 1 }
y = 0
while y < size {
let mut run: i64 = 0
let mut x: i64 = 0
while x <= y {
run = run + tri[row_start[y] + x]
sums[row_start[y] + x] = run
x = x + 1
}
y = y + 1
}
let mut result: i64 = tri[0]
y = 0
while y < size {
let mut x: i64 = 0
while x <= y {
let mut total: i64 = tri[row_start[y] + x]
if result > total { result = total }
let max_len: i64 = size - y
let mut cur: i64 = 1
while cur < max_len {
let ry: i64 = y + cur
let mut row_sum: i64 = sums[row_start[ry] + x + cur]
if x > 0 {
row_sum = row_sum - sums[row_start[ry] + x - 1]
}
total = total + row_sum
if result > total { result = total }
cur = cur + 1
}
x = x + 1
}
y = y + 1
}
printf("%lld\n", result)
free(sums)
free(row_start)
free(tri)
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 size = 1000;
int64_t total_n = FLOW_CHECKED_DIV(((size * (size + 1))), (2));
int64_t* tri = (int64_t*)(calloc(total_n, 8));
int64_t* row_start = (int64_t*)(calloc((size + 1), 8));
if ((tri == NULL || row_start == NULL)) {
return 1;
}
int64_t seed = 0;
int64_t mask = 1048576;
int64_t half = 524288;
int64_t idx = 0;
int64_t y = 0;
while (y < size) {
row_start[y] = idx;
int64_t x = 0;
while (x <= y) {
seed = FLOW_CHECKED_MOD((((615949 * seed) + 797807)), (mask));
tri[idx] = (seed - half);
idx = (idx + 1);
x = (x + 1);
}
y = (y + 1);
}
int64_t* sums = (int64_t*)(calloc(total_n, 8));
if (sums == NULL) {
return 1;
}
y = 0;
while (y < size) {
int64_t run = 0;
int64_t x = 0;
while (x <= y) {
run = (run + tri[(row_start[y] + x)]);
sums[(row_start[y] + x)] = run;
x = (x + 1);
}
y = (y + 1);
}
int64_t result = tri[0];
y = 0;
while (y < size) {
int64_t x = 0;
while (x <= y) {
int64_t total = tri[(row_start[y] + x)];
if (result > total) {
result = total;
}
int64_t max_len = (size - y);
int64_t cur = 1;
while (cur < max_len) {
int64_t ry = (y + cur);
int64_t row_sum = sums[((row_start[ry] + x) + cur)];
if (x > 0) {
row_sum = (row_sum - sums[((row_start[ry] + x) - 1)]);
}
total = (total + row_sum);
if (result > total) {
result = total;
}
cur = (cur + 1);
}
x = (x + 1);
}
y = (y + 1);
}
printf("%lld\n", result);
free(sums);
free(row_start);
free(tri);
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 1000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 1 : i32
%4 = arith.extsi %2 : i32 to i64
%3 = arith.addi %1, %4 : i64
%5 = arith.muli %1, %3 : i64
%6 = arith.constant 2 : i32
%8 = arith.extsi %6 : i32 to i64
%7 = arith.divsi %5, %8 : i64
%10 = arith.constant 8 : i32
%11 = arith.extsi %10 : i32 to i64
%9 = func.call @calloc(%7, %11) : (i64, i64) -> !llvm.ptr
%13 = arith.constant 1 : i32
%15 = arith.extsi %13 : i32 to i64
%14 = arith.addi %1, %15 : i64
%16 = arith.constant 8 : i32
%17 = arith.extsi %16 : i32 to i64
%12 = func.call @calloc(%14, %17) : (i64, i64) -> !llvm.ptr
%18 = llvm.mlir.zero : !llvm.ptr
%19 = llvm.icmp "eq" %9, %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.constant 0 : i32
%26 = arith.extsi %25 : i32 to i64
%27 = llvm.mlir.constant(1 : i64) : i64
%28 = llvm.alloca %27 x i64 : (i64) -> !llvm.ptr
llvm.store %26, %28 : i64, !llvm.ptr
%29 = arith.constant 1048576 : i32
%30 = arith.extsi %29 : i32 to i64
%31 = arith.constant 524288 : i32
%32 = arith.extsi %31 : i32 to i64
%33 = arith.constant 0 : i32
%34 = arith.extsi %33 : i32 to i64
%35 = llvm.mlir.constant(1 : i64) : i64
%36 = llvm.alloca %35 x i64 : (i64) -> !llvm.ptr
llvm.store %34, %36 : i64, !llvm.ptr
%37 = arith.constant 0 : i32
%38 = arith.extsi %37 : i32 to i64
%39 = llvm.mlir.constant(1 : i64) : i64
%40 = llvm.alloca %39 x i64 : (i64) -> !llvm.ptr
llvm.store %38, %40 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%41 = llvm.load %40 : !llvm.ptr -> i64
%42 = arith.cmpi slt, %41, %1 : i64
cf.cond_br %42, ^bb4, ^bb5
^bb4:
%43 = llvm.load %36 : !llvm.ptr -> i64
%44 = llvm.load %40 : !llvm.ptr -> i64
%45 = llvm.getelementptr %12[%44] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %43, %45 : i64, !llvm.ptr
%46 = arith.constant 0 : i32
%47 = arith.extsi %46 : i32 to i64
%48 = llvm.mlir.constant(1 : i64) : i64
%49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
llvm.store %47, %49 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%50 = llvm.load %49 : !llvm.ptr -> i64
%51 = llvm.load %40 : !llvm.ptr -> i64
%52 = arith.cmpi sle, %50, %51 : i64
cf.cond_br %52, ^bb7, ^bb8
^bb7:
%53 = arith.constant 615949 : i32
%54 = llvm.load %28 : !llvm.ptr -> i64
%56 = arith.extsi %53 : i32 to i64
%55 = arith.muli %56, %54 : i64
%57 = arith.constant 797807 : i32
%59 = arith.extsi %57 : i32 to i64
%58 = arith.addi %55, %59 : i64
%60 = arith.remsi %58, %30 : i64
llvm.store %60, %28 : i64, !llvm.ptr
%61 = llvm.load %28 : !llvm.ptr -> i64
%62 = arith.subi %61, %32 : i64
%63 = llvm.load %36 : !llvm.ptr -> i64
%64 = llvm.getelementptr %9[%63] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %62, %64 : i64, !llvm.ptr
%65 = llvm.load %36 : !llvm.ptr -> i64
%66 = arith.constant 1 : i32
%68 = arith.extsi %66 : i32 to i64
%67 = arith.addi %65, %68 : i64
llvm.store %67, %36 : i64, !llvm.ptr
%69 = llvm.load %49 : !llvm.ptr -> i64
%70 = arith.constant 1 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.addi %69, %72 : i64
llvm.store %71, %49 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%73 = llvm.load %40 : !llvm.ptr -> i64
%74 = arith.constant 1 : i32
%76 = arith.extsi %74 : i32 to i64
%75 = arith.addi %73, %76 : i64
llvm.store %75, %40 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%78 = arith.constant 8 : i32
%79 = arith.extsi %78 : i32 to i64
%77 = func.call @calloc(%7, %79) : (i64, i64) -> !llvm.ptr
%80 = llvm.mlir.zero : !llvm.ptr
%81 = llvm.icmp "eq" %77, %80 : !llvm.ptr
cf.cond_br %81, ^bb9, ^bb10
^bb9:
%82 = arith.constant 1 : i32
func.return %82 : i32
^bb10:
cf.br ^bb11
^bb11:
%83 = arith.constant 0 : i32
%84 = arith.extsi %83 : i32 to i64
llvm.store %84, %40 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%85 = llvm.load %40 : !llvm.ptr -> i64
%86 = arith.cmpi slt, %85, %1 : i64
cf.cond_br %86, ^bb13, ^bb14
^bb13:
%87 = arith.constant 0 : i32
%88 = arith.extsi %87 : i32 to i64
%89 = llvm.mlir.constant(1 : i64) : i64
%90 = llvm.alloca %89 x i64 : (i64) -> !llvm.ptr
llvm.store %88, %90 : i64, !llvm.ptr
%91 = arith.constant 0 : i32
%92 = arith.extsi %91 : i32 to i64
%93 = llvm.mlir.constant(1 : i64) : i64
%94 = llvm.alloca %93 x i64 : (i64) -> !llvm.ptr
llvm.store %92, %94 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%95 = llvm.load %94 : !llvm.ptr -> i64
%96 = llvm.load %40 : !llvm.ptr -> i64
%97 = arith.cmpi sle, %95, %96 : i64
cf.cond_br %97, ^bb16, ^bb17
^bb16:
%98 = llvm.load %90 : !llvm.ptr -> i64
%101 = llvm.load %40 : !llvm.ptr -> i64
%102 = llvm.getelementptr %12[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%100 = llvm.load %102 : !llvm.ptr -> i64
%103 = llvm.load %94 : !llvm.ptr -> i64
%104 = arith.addi %100, %103 : i64
%105 = llvm.getelementptr %9[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%99 = llvm.load %105 : !llvm.ptr -> i64
%106 = arith.addi %98, %99 : i64
llvm.store %106, %90 : i64, !llvm.ptr
%107 = llvm.load %90 : !llvm.ptr -> i64
%109 = llvm.load %40 : !llvm.ptr -> i64
%110 = llvm.getelementptr %12[%109] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%108 = llvm.load %110 : !llvm.ptr -> i64
%111 = llvm.load %94 : !llvm.ptr -> i64
%112 = arith.addi %108, %111 : i64
%113 = llvm.getelementptr %77[%112] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %107, %113 : i64, !llvm.ptr
%114 = llvm.load %94 : !llvm.ptr -> i64
%115 = arith.constant 1 : i32
%117 = arith.extsi %115 : i32 to i64
%116 = arith.addi %114, %117 : i64
llvm.store %116, %94 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%118 = llvm.load %40 : !llvm.ptr -> i64
%119 = arith.constant 1 : i32
%121 = arith.extsi %119 : i32 to i64
%120 = arith.addi %118, %121 : i64
llvm.store %120, %40 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%123 = arith.constant 0 : i32
%124 = arith.extsi %123 : i32 to i64
%125 = llvm.getelementptr %9[%124] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%122 = llvm.load %125 : !llvm.ptr -> i64
%126 = llvm.mlir.constant(1 : i64) : i64
%127 = llvm.alloca %126 x i64 : (i64) -> !llvm.ptr
llvm.store %122, %127 : i64, !llvm.ptr
%128 = arith.constant 0 : i32
%129 = arith.extsi %128 : i32 to i64
llvm.store %129, %40 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%130 = llvm.load %40 : !llvm.ptr -> i64
%131 = arith.cmpi slt, %130, %1 : i64
cf.cond_br %131, ^bb19, ^bb20
^bb19:
%132 = arith.constant 0 : i32
%133 = arith.extsi %132 : i32 to i64
%134 = llvm.mlir.constant(1 : i64) : i64
%135 = llvm.alloca %134 x i64 : (i64) -> !llvm.ptr
llvm.store %133, %135 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%136 = llvm.load %135 : !llvm.ptr -> i64
%137 = llvm.load %40 : !llvm.ptr -> i64
%138 = arith.cmpi sle, %136, %137 : i64
cf.cond_br %138, ^bb22, ^bb23
^bb22:
%141 = llvm.load %40 : !llvm.ptr -> i64
%142 = llvm.getelementptr %12[%141] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%140 = llvm.load %142 : !llvm.ptr -> i64
%143 = llvm.load %135 : !llvm.ptr -> i64
%144 = arith.addi %140, %143 : i64
%145 = llvm.getelementptr %9[%144] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%139 = llvm.load %145 : !llvm.ptr -> i64
%146 = llvm.mlir.constant(1 : i64) : i64
%147 = llvm.alloca %146 x i64 : (i64) -> !llvm.ptr
llvm.store %139, %147 : i64, !llvm.ptr
%148 = llvm.load %127 : !llvm.ptr -> i64
%149 = llvm.load %147 : !llvm.ptr -> i64
%150 = arith.cmpi sgt, %148, %149 : i64
cf.cond_br %150, ^bb24, ^bb25
^bb24:
%151 = llvm.load %147 : !llvm.ptr -> i64
llvm.store %151, %127 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%152 = llvm.load %40 : !llvm.ptr -> i64
%153 = arith.subi %1, %152 : i64
%154 = arith.constant 1 : i32
%155 = arith.extsi %154 : i32 to i64
%156 = llvm.mlir.constant(1 : i64) : i64
%157 = llvm.alloca %156 x i64 : (i64) -> !llvm.ptr
llvm.store %155, %157 : i64, !llvm.ptr
cf.br ^bb27
^bb27:
%158 = llvm.load %157 : !llvm.ptr -> i64
%159 = arith.cmpi slt, %158, %153 : i64
cf.cond_br %159, ^bb28, ^bb29
^bb28:
%160 = llvm.load %40 : !llvm.ptr -> i64
%161 = llvm.load %157 : !llvm.ptr -> i64
%162 = arith.addi %160, %161 : i64
%165 = llvm.getelementptr %12[%162] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%164 = llvm.load %165 : !llvm.ptr -> i64
%166 = llvm.load %135 : !llvm.ptr -> i64
%167 = arith.addi %164, %166 : i64
%168 = llvm.load %157 : !llvm.ptr -> i64
%169 = arith.addi %167, %168 : i64
%170 = llvm.getelementptr %77[%169] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%163 = llvm.load %170 : !llvm.ptr -> i64
%171 = llvm.mlir.constant(1 : i64) : i64
%172 = llvm.alloca %171 x i64 : (i64) -> !llvm.ptr
llvm.store %163, %172 : i64, !llvm.ptr
%173 = llvm.load %135 : !llvm.ptr -> i64
%174 = arith.constant 0 : i32
%176 = arith.extsi %174 : i32 to i64
%175 = arith.cmpi sgt, %173, %176 : i64
cf.cond_br %175, ^bb30, ^bb31
^bb30:
%177 = llvm.load %172 : !llvm.ptr -> i64
%180 = llvm.getelementptr %12[%162] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%179 = llvm.load %180 : !llvm.ptr -> i64
%181 = llvm.load %135 : !llvm.ptr -> i64
%182 = arith.addi %179, %181 : i64
%183 = arith.constant 1 : i32
%185 = arith.extsi %183 : i32 to i64
%184 = arith.subi %182, %185 : i64
%186 = llvm.getelementptr %77[%184] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%178 = llvm.load %186 : !llvm.ptr -> i64
%187 = arith.subi %177, %178 : i64
llvm.store %187, %172 : i64, !llvm.ptr
cf.br ^bb32
^bb31:
cf.br ^bb32
^bb32:
%188 = llvm.load %147 : !llvm.ptr -> i64
%189 = llvm.load %172 : !llvm.ptr -> i64
%190 = arith.addi %188, %189 : i64
llvm.store %190, %147 : i64, !llvm.ptr
%191 = llvm.load %127 : !llvm.ptr -> i64
%192 = llvm.load %147 : !llvm.ptr -> i64
%193 = arith.cmpi sgt, %191, %192 : i64
cf.cond_br %193, ^bb33, ^bb34
^bb33:
%194 = llvm.load %147 : !llvm.ptr -> i64
llvm.store %194, %127 : i64, !llvm.ptr
cf.br ^bb35
^bb34:
cf.br ^bb35
^bb35:
%195 = llvm.load %157 : !llvm.ptr -> i64
%196 = arith.constant 1 : i32
%198 = arith.extsi %196 : i32 to i64
%197 = arith.addi %195, %198 : i64
llvm.store %197, %157 : i64, !llvm.ptr
cf.br ^bb27
^bb29:
%199 = llvm.load %135 : !llvm.ptr -> i64
%200 = arith.constant 1 : i32
%202 = arith.extsi %200 : i32 to i64
%201 = arith.addi %199, %202 : i64
llvm.store %201, %135 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%203 = llvm.load %40 : !llvm.ptr -> i64
%204 = arith.constant 1 : i32
%206 = arith.extsi %204 : i32 to i64
%205 = arith.addi %203, %206 : i64
llvm.store %205, %40 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%207 = llvm.mlir.addressof @str_0 : !llvm.ptr
%208 = llvm.load %127 : !llvm.ptr -> i64
%209 = llvm.call @printf(%207, %208) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%77) : (!llvm.ptr) -> ()
func.call @free(%12) : (!llvm.ptr) -> ()
func.call @free(%9) : (!llvm.ptr) -> ()
%213 = arith.constant 0 : i32
func.return %213 : i32
}
}