Problem 147
Rectangles in cross-hatched grids; sum for 1≤w≤47, 1≤h≤43.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^3) | O(n^2) |
| Space complexity | O(1) | O(1) |
| Approach | Flow solution | Closed-form or combinatorial counting |
| Verdict | Suboptimal |
Flow source
# Project Euler 147
# Rectangles in cross-hatched grids; sum for 1≤w≤47, 1≤h≤43.
function grid(width: i64, height: i64) -> i64 {
return (width * (width + 1) / 2) * (height * (height + 1) / 2)
}
function capped_descending_sum(cap: i64, start: i64, last_index: i64) -> i64 {
let mut first_desc: i64 = last_index
if start - cap < first_desc { first_desc = start - cap }
let mut total: i64 = 0
if first_desc >= 0 {
total = total + (first_desc + 1) * cap
}
if last_index > first_desc {
let count: i64 = last_index - first_desc
let lo: i64 = first_desc + 1
let hi: i64 = last_index
total = total + count * start - (lo + hi) * count / 2
}
return total
}
function diagonal(width0: i64, height0: i64) -> i64 {
let mut a: i64 = width0
let mut b: i64 = height0
if a < b {
let t: i64 = a
a = b
b = t
}
let mut count: i64 = 0
let mut i: i64 = 0
while i < a {
let mut j: i64 = 0
while j < b {
let mut parity: i64 = 0
while parity <= 1 {
let start_x: i64 = 2 * i + 1 + parity
let start_y: i64 = 2 * j + 2 - parity
let x_limit: i64 = 2 * a - start_x
let y_limit: i64 = 2 * b - start_y
let mut cap: i64 = x_limit
if y_limit < cap { cap = y_limit }
let mut last_width: i64 = start_y - 1
if x_limit - 1 < last_width { last_width = x_limit - 1 }
if cap > 0 && last_width >= 0 {
count = count + capped_descending_sum(cap, x_limit, last_width)
}
parity = parity + 1
}
j = j + 1
}
i = i + 1
}
return count
}
function main() -> i32 {
let mut total: i64 = 0
let mut w: i64 = 1
while w <= 47 {
let mut h: i64 = 1
while h <= 43 {
total = total + grid(w, h) + diagonal(w, h)
h = h + 1
}
w = w + 1
}
printf("%lld\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; }
int64_t grid_i64_i64(int64_t width, int64_t height);
int64_t capped_descending_sum_i64_i64_i64(int64_t cap, int64_t start, int64_t last_index);
int64_t diagonal_i64_i64(int64_t width0, int64_t height0);
int32_t main(void);
int64_t grid_i64_i64(int64_t width, int64_t height) {
return (FLOW_CHECKED_DIV(((width * (width + 1))), (2)) * FLOW_CHECKED_DIV(((height * (height + 1))), (2)));
}
int64_t capped_descending_sum_i64_i64_i64(int64_t cap, int64_t start, int64_t last_index) {
int64_t first_desc = last_index;
if ((start - cap) < first_desc) {
first_desc = (start - cap);
}
int64_t total = 0;
if (first_desc >= 0) {
total = (total + ((first_desc + 1) * cap));
}
if (last_index > first_desc) {
int64_t count = (last_index - first_desc);
int64_t lo = (first_desc + 1);
int64_t hi = last_index;
total = ((total + (count * start)) - FLOW_CHECKED_DIV((((lo + hi) * count)), (2)));
}
return total;
}
int64_t diagonal_i64_i64(int64_t width0, int64_t height0) {
int64_t a = width0;
int64_t b = height0;
if (a < b) {
int64_t t = a;
a = b;
b = t;
}
int64_t count = 0;
int64_t i = 0;
while (i < a) {
int64_t j = 0;
while (j < b) {
int64_t parity = 0;
while (parity <= 1) {
int64_t start_x = (((2 * i) + 1) + parity);
int64_t start_y = (((2 * j) + 2) - parity);
int64_t x_limit = ((2 * a) - start_x);
int64_t y_limit = ((2 * b) - start_y);
int64_t cap = x_limit;
if (y_limit < cap) {
cap = y_limit;
}
int64_t last_width = (start_y - 1);
if ((x_limit - 1) < last_width) {
last_width = (x_limit - 1);
}
if ((cap > 0 && last_width >= 0)) {
count = (count + capped_descending_sum_i64_i64_i64(cap, x_limit, last_width));
}
parity = (parity + 1);
}
j = (j + 1);
}
i = (i + 1);
}
return count;
}
int32_t main(void) {
int64_t total = 0;
int64_t w = 1;
while (w <= 47) {
int64_t h = 1;
while (h <= 43) {
total = ((total + grid_i64_i64(w, h)) + diagonal_i64_i64(w, h));
h = (h + 1);
}
w = (w + 1);
}
printf("%lld\n", total);
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 @grid(%arg0: i64, %arg1: i64) -> i64 {
%0 = arith.constant 1 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.addi %arg0, %2 : i64
%3 = arith.muli %arg0, %1 : i64
%4 = arith.constant 2 : i32
%6 = arith.extsi %4 : i32 to i64
%5 = arith.divsi %3, %6 : i64
%7 = arith.constant 1 : i32
%9 = arith.extsi %7 : i32 to i64
%8 = arith.addi %arg1, %9 : i64
%10 = arith.muli %arg1, %8 : i64
%11 = arith.constant 2 : i32
%13 = arith.extsi %11 : i32 to i64
%12 = arith.divsi %10, %13 : i64
%14 = arith.muli %5, %12 : i64
func.return %14 : i64
}
func.func @capped_descending_sum(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%15 = llvm.mlir.constant(1 : i64) : i64
%16 = llvm.alloca %15 x i64 : (i64) -> !llvm.ptr
llvm.store %arg2, %16 : i64, !llvm.ptr
%17 = arith.subi %arg1, %arg0 : i64
%18 = llvm.load %16 : !llvm.ptr -> i64
%19 = arith.cmpi slt, %17, %18 : i64
cf.cond_br %19, ^bb0, ^bb1
^bb0:
%20 = arith.subi %arg1, %arg0 : i64
llvm.store %20, %16 : i64, !llvm.ptr
cf.br ^bb2
^bb1:
cf.br ^bb2
^bb2:
%21 = arith.constant 0 : i32
%22 = arith.extsi %21 : i32 to i64
%23 = llvm.mlir.constant(1 : i64) : i64
%24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
llvm.store %22, %24 : i64, !llvm.ptr
%25 = llvm.load %16 : !llvm.ptr -> i64
%26 = arith.constant 0 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.cmpi sge, %25, %28 : i64
cf.cond_br %27, ^bb3, ^bb4
^bb3:
%29 = llvm.load %24 : !llvm.ptr -> i64
%30 = llvm.load %16 : !llvm.ptr -> i64
%31 = arith.constant 1 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.addi %30, %33 : i64
%34 = arith.muli %32, %arg0 : i64
%35 = arith.addi %29, %34 : i64
llvm.store %35, %24 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%36 = llvm.load %16 : !llvm.ptr -> i64
%37 = arith.cmpi sgt, %arg2, %36 : i64
cf.cond_br %37, ^bb6, ^bb7
^bb6:
%38 = llvm.load %16 : !llvm.ptr -> i64
%39 = arith.subi %arg2, %38 : i64
%40 = llvm.load %16 : !llvm.ptr -> i64
%41 = arith.constant 1 : i32
%43 = arith.extsi %41 : i32 to i64
%42 = arith.addi %40, %43 : i64
%44 = llvm.load %24 : !llvm.ptr -> i64
%45 = arith.muli %39, %arg1 : i64
%46 = arith.addi %44, %45 : i64
%47 = arith.addi %42, %arg2 : i64
%48 = arith.muli %47, %39 : i64
%49 = arith.constant 2 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.divsi %48, %51 : i64
%52 = arith.subi %46, %50 : i64
llvm.store %52, %24 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%53 = llvm.load %24 : !llvm.ptr -> i64
func.return %53 : i64
}
func.func @diagonal(%arg0: i64, %arg1: i64) -> i64 {
%54 = llvm.mlir.constant(1 : i64) : i64
%55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %55 : i64, !llvm.ptr
%56 = llvm.mlir.constant(1 : i64) : i64
%57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %57 : i64, !llvm.ptr
%58 = llvm.load %55 : !llvm.ptr -> i64
%59 = llvm.load %57 : !llvm.ptr -> i64
%60 = arith.cmpi slt, %58, %59 : i64
cf.cond_br %60, ^bb9, ^bb10
^bb9:
%61 = llvm.load %55 : !llvm.ptr -> i64
%62 = llvm.load %57 : !llvm.ptr -> i64
llvm.store %62, %55 : i64, !llvm.ptr
llvm.store %61, %57 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%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
%67 = arith.constant 0 : i32
%68 = arith.extsi %67 : i32 to i64
%69 = llvm.mlir.constant(1 : i64) : i64
%70 = llvm.alloca %69 x i64 : (i64) -> !llvm.ptr
llvm.store %68, %70 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%71 = llvm.load %70 : !llvm.ptr -> i64
%72 = llvm.load %55 : !llvm.ptr -> i64
%73 = arith.cmpi slt, %71, %72 : i64
cf.cond_br %73, ^bb13, ^bb14
^bb13:
%74 = arith.constant 0 : i32
%75 = arith.extsi %74 : i32 to i64
%76 = llvm.mlir.constant(1 : i64) : i64
%77 = llvm.alloca %76 x i64 : (i64) -> !llvm.ptr
llvm.store %75, %77 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%78 = llvm.load %77 : !llvm.ptr -> i64
%79 = llvm.load %57 : !llvm.ptr -> i64
%80 = arith.cmpi slt, %78, %79 : i64
cf.cond_br %80, ^bb16, ^bb17
^bb16:
%81 = arith.constant 0 : i32
%82 = arith.extsi %81 : i32 to i64
%83 = llvm.mlir.constant(1 : i64) : i64
%84 = llvm.alloca %83 x i64 : (i64) -> !llvm.ptr
llvm.store %82, %84 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%85 = llvm.load %84 : !llvm.ptr -> i64
%86 = arith.constant 1 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.cmpi sle, %85, %88 : i64
cf.cond_br %87, ^bb19, ^bb20
^bb19:
%89 = arith.constant 2 : i32
%90 = llvm.load %70 : !llvm.ptr -> i64
%92 = arith.extsi %89 : i32 to i64
%91 = arith.muli %92, %90 : i64
%93 = arith.constant 1 : i32
%95 = arith.extsi %93 : i32 to i64
%94 = arith.addi %91, %95 : i64
%96 = llvm.load %84 : !llvm.ptr -> i64
%97 = arith.addi %94, %96 : i64
%98 = arith.constant 2 : i32
%99 = llvm.load %77 : !llvm.ptr -> i64
%101 = arith.extsi %98 : i32 to i64
%100 = arith.muli %101, %99 : i64
%102 = arith.constant 2 : i32
%104 = arith.extsi %102 : i32 to i64
%103 = arith.addi %100, %104 : i64
%105 = llvm.load %84 : !llvm.ptr -> i64
%106 = arith.subi %103, %105 : i64
%107 = arith.constant 2 : i32
%108 = llvm.load %55 : !llvm.ptr -> i64
%110 = arith.extsi %107 : i32 to i64
%109 = arith.muli %110, %108 : i64
%111 = arith.subi %109, %97 : i64
%112 = arith.constant 2 : i32
%113 = llvm.load %57 : !llvm.ptr -> i64
%115 = arith.extsi %112 : i32 to i64
%114 = arith.muli %115, %113 : i64
%116 = arith.subi %114, %106 : i64
%117 = llvm.mlir.constant(1 : i64) : i64
%118 = llvm.alloca %117 x i64 : (i64) -> !llvm.ptr
llvm.store %111, %118 : i64, !llvm.ptr
%119 = llvm.load %118 : !llvm.ptr -> i64
%120 = arith.cmpi slt, %116, %119 : i64
cf.cond_br %120, ^bb21, ^bb22
^bb21:
llvm.store %116, %118 : i64, !llvm.ptr
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%121 = arith.constant 1 : i32
%123 = arith.extsi %121 : i32 to i64
%122 = arith.subi %106, %123 : i64
%124 = llvm.mlir.constant(1 : i64) : i64
%125 = llvm.alloca %124 x i64 : (i64) -> !llvm.ptr
llvm.store %122, %125 : i64, !llvm.ptr
%126 = arith.constant 1 : i32
%128 = arith.extsi %126 : i32 to i64
%127 = arith.subi %111, %128 : i64
%129 = llvm.load %125 : !llvm.ptr -> i64
%130 = arith.cmpi slt, %127, %129 : i64
cf.cond_br %130, ^bb24, ^bb25
^bb24:
%131 = arith.constant 1 : i32
%133 = arith.extsi %131 : i32 to i64
%132 = arith.subi %111, %133 : i64
llvm.store %132, %125 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%134 = llvm.load %118 : !llvm.ptr -> i64
%135 = arith.constant 0 : i32
%137 = arith.extsi %135 : i32 to i64
%136 = arith.cmpi sgt, %134, %137 : i64
%138 = scf.if %136 -> (i1) {
%139 = llvm.load %125 : !llvm.ptr -> i64
%140 = arith.constant 0 : i32
%142 = arith.extsi %140 : i32 to i64
%141 = arith.cmpi sge, %139, %142 : i64
scf.yield %141 : i1
} else {
%143 = arith.constant false
scf.yield %143 : i1
}
cf.cond_br %138, ^bb27, ^bb28
^bb27:
%144 = llvm.load %66 : !llvm.ptr -> i64
%146 = llvm.load %118 : !llvm.ptr -> i64
%147 = llvm.load %125 : !llvm.ptr -> i64
%145 = func.call @capped_descending_sum(%146, %111, %147) : (i64, i64, i64) -> i64
%148 = arith.addi %144, %145 : i64
llvm.store %148, %66 : i64, !llvm.ptr
cf.br ^bb29
^bb28:
cf.br ^bb29
^bb29:
%149 = llvm.load %84 : !llvm.ptr -> i64
%150 = arith.constant 1 : i32
%152 = arith.extsi %150 : i32 to i64
%151 = arith.addi %149, %152 : i64
llvm.store %151, %84 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%153 = llvm.load %77 : !llvm.ptr -> i64
%154 = arith.constant 1 : i32
%156 = arith.extsi %154 : i32 to i64
%155 = arith.addi %153, %156 : i64
llvm.store %155, %77 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%157 = llvm.load %70 : !llvm.ptr -> i64
%158 = arith.constant 1 : i32
%160 = arith.extsi %158 : i32 to i64
%159 = arith.addi %157, %160 : i64
llvm.store %159, %70 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%161 = llvm.load %66 : !llvm.ptr -> i64
func.return %161 : i64
}
func.func @main() -> i32 {
%162 = arith.constant 0 : i32
%163 = arith.extsi %162 : i32 to i64
%164 = llvm.mlir.constant(1 : i64) : i64
%165 = llvm.alloca %164 x i64 : (i64) -> !llvm.ptr
llvm.store %163, %165 : i64, !llvm.ptr
%166 = arith.constant 1 : i32
%167 = arith.extsi %166 : i32 to i64
%168 = llvm.mlir.constant(1 : i64) : i64
%169 = llvm.alloca %168 x i64 : (i64) -> !llvm.ptr
llvm.store %167, %169 : i64, !llvm.ptr
cf.br ^bb30
^bb30:
%170 = llvm.load %169 : !llvm.ptr -> i64
%171 = arith.constant 47 : i32
%173 = arith.extsi %171 : i32 to i64
%172 = arith.cmpi sle, %170, %173 : i64
cf.cond_br %172, ^bb31, ^bb32
^bb31:
%174 = arith.constant 1 : i32
%175 = arith.extsi %174 : i32 to i64
%176 = llvm.mlir.constant(1 : i64) : i64
%177 = llvm.alloca %176 x i64 : (i64) -> !llvm.ptr
llvm.store %175, %177 : i64, !llvm.ptr
cf.br ^bb33
^bb33:
%178 = llvm.load %177 : !llvm.ptr -> i64
%179 = arith.constant 43 : i32
%181 = arith.extsi %179 : i32 to i64
%180 = arith.cmpi sle, %178, %181 : i64
cf.cond_br %180, ^bb34, ^bb35
^bb34:
%182 = llvm.load %165 : !llvm.ptr -> i64
%184 = llvm.load %169 : !llvm.ptr -> i64
%185 = llvm.load %177 : !llvm.ptr -> i64
%183 = func.call @grid(%184, %185) : (i64, i64) -> i64
%186 = arith.addi %182, %183 : i64
%188 = llvm.load %169 : !llvm.ptr -> i64
%189 = llvm.load %177 : !llvm.ptr -> i64
%187 = func.call @diagonal(%188, %189) : (i64, i64) -> i64
%190 = arith.addi %186, %187 : i64
llvm.store %190, %165 : i64, !llvm.ptr
%191 = llvm.load %177 : !llvm.ptr -> i64
%192 = arith.constant 1 : i32
%194 = arith.extsi %192 : i32 to i64
%193 = arith.addi %191, %194 : i64
llvm.store %193, %177 : i64, !llvm.ptr
cf.br ^bb33
^bb35:
%195 = llvm.load %169 : !llvm.ptr -> i64
%196 = arith.constant 1 : i32
%198 = arith.extsi %196 : i32 to i64
%197 = arith.addi %195, %198 : i64
llvm.store %197, %169 : i64, !llvm.ptr
cf.br ^bb30
^bb32:
%199 = llvm.mlir.addressof @str_0 : !llvm.ptr
%200 = llvm.load %165 : !llvm.ptr -> i64
%201 = llvm.call @printf(%199, %200) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%202 = arith.constant 0 : i32
func.return %202 : i32
}
}