← All problems
Problem 114
Ways to fill a row of length 50 with red blocks of length ≥3 and black tiles.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n * m)
Space complexity O(n)O(n)
Approach Flow solution Dynamic programming or generating function
Verdict Unknown
Flow source
# Project Euler 114
# Ways to fill a row of length 50 with red blocks of length ≥3 and black tiles.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let n: i32 = 50
let m: i32 = 3
let a: ptr<i64> = calloc((n + 1) as i64, 8)
if a == null { return 1 }
a[0] = 1
let mut i: i32 = 1
while i <= n {
# black tile at end
a[i] = a[i - 1]
# red block of length k>=m ending here
let mut k: i32 = m
while k <= i {
if k == i {
a[i] = a[i] + 1
} else {
# black before the block
a[i] = a[i] + a[i - k - 1]
}
k = k + 1
}
i = i + 1
}
printf("%lld\n", a[n])
free(a)
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 n = 50;
int32_t m = 3;
int64_t* a = (int64_t*)(calloc(((int64_t)((n + 1))), 8));
if (a == NULL) {
return 1;
}
a[0] = 1;
int32_t i = 1;
while (i <= n) {
a[i] = a[(i - 1)];
int32_t k = m;
while (k <= i) {
if (k == i) {
a[i] = (a[i] + 1);
} else {
a[i] = (a[i] + a[((i - k) - 1)]);
}
k = (k + 1);
}
i = (i + 1);
}
printf("%lld\n", a[n]);
free(a);
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 50 : i32
%1 = arith.constant 3 : i32
%3 = arith.constant 1 : i32
%4 = arith.addi %0, %3 : i32
%5 = arith.extsi %4 : i32 to i64
%6 = arith.constant 8 : i32
%7 = arith.extsi %6 : i32 to i64
%2 = func.call @calloc(%5, %7) : (i64, i64) -> !llvm.ptr
%8 = llvm.mlir.zero : !llvm.ptr
%9 = llvm.icmp "eq" %2, %8 : !llvm.ptr
cf.cond_br %9, ^bb0, ^bb1
^bb0:
%10 = arith.constant 1 : i32
func.return %10 : i32
^bb1:
cf.br ^bb2
^bb2:
%11 = arith.constant 1 : i32
%12 = arith.constant 0 : i32
%13 = arith.extsi %11 : i32 to i64
%14 = arith.extsi %12 : i32 to i64
%15 = llvm.getelementptr %2[%14] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %13, %15 : i64, !llvm.ptr
%16 = arith.constant 1 : i32
%17 = llvm.mlir.constant(1 : i64) : i64
%18 = llvm.alloca %17 x i32 : (i64) -> !llvm.ptr
llvm.store %16, %18 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%19 = llvm.load %18 : !llvm.ptr -> i32
%20 = arith.cmpi sle, %19, %0 : i32
cf.cond_br %20, ^bb4, ^bb5
^bb4:
%22 = llvm.load %18 : !llvm.ptr -> i32
%23 = arith.constant 1 : i32
%24 = arith.subi %22, %23 : i32
%25 = arith.extsi %24 : i32 to i64
%26 = llvm.getelementptr %2[%25] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%21 = llvm.load %26 : !llvm.ptr -> i64
%27 = llvm.load %18 : !llvm.ptr -> i32
%28 = arith.extsi %27 : i32 to i64
%29 = llvm.getelementptr %2[%28] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %21, %29 : i64, !llvm.ptr
%30 = llvm.mlir.constant(1 : i64) : i64
%31 = llvm.alloca %30 x i32 : (i64) -> !llvm.ptr
llvm.store %1, %31 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%32 = llvm.load %31 : !llvm.ptr -> i32
%33 = llvm.load %18 : !llvm.ptr -> i32
%34 = arith.cmpi sle, %32, %33 : i32
cf.cond_br %34, ^bb7, ^bb8
^bb7:
%35 = llvm.load %31 : !llvm.ptr -> i32
%36 = llvm.load %18 : !llvm.ptr -> i32
%37 = arith.cmpi eq, %35, %36 : i32
cf.cond_br %37, ^bb9, ^bb10
^bb9:
%39 = llvm.load %18 : !llvm.ptr -> i32
%40 = arith.extsi %39 : i32 to i64
%41 = llvm.getelementptr %2[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%38 = llvm.load %41 : !llvm.ptr -> i64
%42 = arith.constant 1 : i32
%44 = arith.extsi %42 : i32 to i64
%43 = arith.addi %38, %44 : i64
%45 = llvm.load %18 : !llvm.ptr -> i32
%46 = arith.extsi %45 : i32 to i64
%47 = llvm.getelementptr %2[%46] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %43, %47 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
%49 = llvm.load %18 : !llvm.ptr -> i32
%50 = arith.extsi %49 : i32 to i64
%51 = llvm.getelementptr %2[%50] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%48 = llvm.load %51 : !llvm.ptr -> i64
%53 = llvm.load %18 : !llvm.ptr -> i32
%54 = llvm.load %31 : !llvm.ptr -> i32
%55 = arith.subi %53, %54 : i32
%56 = arith.constant 1 : i32
%57 = arith.subi %55, %56 : i32
%58 = arith.extsi %57 : i32 to i64
%59 = llvm.getelementptr %2[%58] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%52 = llvm.load %59 : !llvm.ptr -> i64
%60 = arith.addi %48, %52 : i64
%61 = llvm.load %18 : !llvm.ptr -> i32
%62 = arith.extsi %61 : i32 to i64
%63 = llvm.getelementptr %2[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %60, %63 : i64, !llvm.ptr
cf.br ^bb11
^bb11:
%64 = llvm.load %31 : !llvm.ptr -> i32
%65 = arith.constant 1 : i32
%66 = arith.addi %64, %65 : i32
llvm.store %66, %31 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%67 = llvm.load %18 : !llvm.ptr -> i32
%68 = arith.constant 1 : i32
%69 = arith.addi %67, %68 : i32
llvm.store %69, %18 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%70 = llvm.mlir.addressof @str_0 : !llvm.ptr
%72 = arith.extsi %0 : i32 to i64
%73 = llvm.getelementptr %2[%72] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%71 = llvm.load %73 : !llvm.ptr -> i64
%74 = llvm.call @printf(%70, %71) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%2) : (!llvm.ptr) -> ()
%76 = arith.constant 0 : i32
func.return %76 : i32
}
}