Problem 163
Cross-hatched triangles in size-36 triangular grid (closed form).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(1) | O(n^2) |
| Space complexity | O(1) | O(n^2) |
| Approach | Flow solution | Bottom-up DP |
| Verdict | Optimal |
Flow source
# Project Euler 163
# Cross-hatched triangles in size-36 triangular grid (closed form).
function fun(n: i64) -> i64 {
let n2: i64 = n * n
let n3: i64 = n2 * n
let t1: i64 = (2 * n3 + 5 * n2 + 2 * n) / 8
let t2: i64 = 2 * (n3 / 2 - n / 6)
let s1: i64 = (n * (n + 1) * (n + 2)) / 6
let s2: i64 = (2 * n3 + 5 * n2 + 2 * n) / 8
let s3: i64 = (2 * n3 + 3 * n2 - 3 * n) / 18
let s4: i64 = (2 * n3 + 3 * n2 - 3 * n) / 10
let t3: i64 = 6 * (s1 + s2 + s3 + s4)
let t4: i64 = 3 * ((22 * n3 + 45 * n2 - 4 * n) / 48)
return t1 + t2 + t3 + t4
}
function main() -> i32 {
printf("%lld\n", fun(36))
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 fun_i64(int64_t n);
int32_t main(void);
int64_t fun_i64(int64_t n) {
int64_t n2 = (n * n);
int64_t n3 = (n2 * n);
int64_t t1 = FLOW_CHECKED_DIV(((((2 * n3) + (5 * n2)) + (2 * n))), (8));
int64_t t2 = (2 * (FLOW_CHECKED_DIV((n3), (2)) - FLOW_CHECKED_DIV((n), (6))));
int64_t s1 = FLOW_CHECKED_DIV((((n * (n + 1)) * (n + 2))), (6));
int64_t s2 = FLOW_CHECKED_DIV(((((2 * n3) + (5 * n2)) + (2 * n))), (8));
int64_t s3 = FLOW_CHECKED_DIV(((((2 * n3) + (3 * n2)) - (3 * n))), (18));
int64_t s4 = FLOW_CHECKED_DIV(((((2 * n3) + (3 * n2)) - (3 * n))), (10));
int64_t t3 = (6 * (((s1 + s2) + s3) + s4));
int64_t t4 = (3 * FLOW_CHECKED_DIV(((((22 * n3) + (45 * n2)) - (4 * n))), (48)));
return (((t1 + t2) + t3) + t4);
}
int32_t main(void) {
printf("%lld\n", fun_i64(36));
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 @fun(%arg0: i64) -> i64 {
%0 = arith.muli %arg0, %arg0 : i64
%1 = arith.muli %0, %arg0 : i64
%2 = arith.constant 2 : i32
%4 = arith.extsi %2 : i32 to i64
%3 = arith.muli %4, %1 : i64
%5 = arith.constant 5 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.muli %7, %0 : i64
%8 = arith.addi %3, %6 : i64
%9 = arith.constant 2 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.muli %11, %arg0 : i64
%12 = arith.addi %8, %10 : i64
%13 = arith.constant 8 : i32
%15 = arith.extsi %13 : i32 to i64
%14 = arith.divsi %12, %15 : i64
%16 = arith.constant 2 : i32
%17 = arith.constant 2 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.divsi %1, %19 : i64
%20 = arith.constant 6 : i32
%22 = arith.extsi %20 : i32 to i64
%21 = arith.divsi %arg0, %22 : i64
%23 = arith.subi %18, %21 : i64
%25 = arith.extsi %16 : i32 to i64
%24 = arith.muli %25, %23 : i64
%26 = arith.constant 1 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.addi %arg0, %28 : i64
%29 = arith.muli %arg0, %27 : i64
%30 = arith.constant 2 : i32
%32 = arith.extsi %30 : i32 to i64
%31 = arith.addi %arg0, %32 : i64
%33 = arith.muli %29, %31 : i64
%34 = arith.constant 6 : i32
%36 = arith.extsi %34 : i32 to i64
%35 = arith.divsi %33, %36 : i64
%37 = arith.constant 2 : i32
%39 = arith.extsi %37 : i32 to i64
%38 = arith.muli %39, %1 : i64
%40 = arith.constant 5 : i32
%42 = arith.extsi %40 : i32 to i64
%41 = arith.muli %42, %0 : i64
%43 = arith.addi %38, %41 : i64
%44 = arith.constant 2 : i32
%46 = arith.extsi %44 : i32 to i64
%45 = arith.muli %46, %arg0 : i64
%47 = arith.addi %43, %45 : i64
%48 = arith.constant 8 : i32
%50 = arith.extsi %48 : i32 to i64
%49 = arith.divsi %47, %50 : i64
%51 = arith.constant 2 : i32
%53 = arith.extsi %51 : i32 to i64
%52 = arith.muli %53, %1 : i64
%54 = arith.constant 3 : i32
%56 = arith.extsi %54 : i32 to i64
%55 = arith.muli %56, %0 : i64
%57 = arith.addi %52, %55 : i64
%58 = arith.constant 3 : i32
%60 = arith.extsi %58 : i32 to i64
%59 = arith.muli %60, %arg0 : i64
%61 = arith.subi %57, %59 : i64
%62 = arith.constant 18 : i32
%64 = arith.extsi %62 : i32 to i64
%63 = arith.divsi %61, %64 : i64
%65 = arith.constant 2 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.muli %67, %1 : i64
%68 = arith.constant 3 : i32
%70 = arith.extsi %68 : i32 to i64
%69 = arith.muli %70, %0 : i64
%71 = arith.addi %66, %69 : i64
%72 = arith.constant 3 : i32
%74 = arith.extsi %72 : i32 to i64
%73 = arith.muli %74, %arg0 : i64
%75 = arith.subi %71, %73 : i64
%76 = arith.constant 10 : i32
%78 = arith.extsi %76 : i32 to i64
%77 = arith.divsi %75, %78 : i64
%79 = arith.constant 6 : i32
%80 = arith.addi %35, %49 : i64
%81 = arith.addi %80, %63 : i64
%82 = arith.addi %81, %77 : i64
%84 = arith.extsi %79 : i32 to i64
%83 = arith.muli %84, %82 : i64
%85 = arith.constant 3 : i32
%86 = arith.constant 22 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.muli %88, %1 : i64
%89 = arith.constant 45 : i32
%91 = arith.extsi %89 : i32 to i64
%90 = arith.muli %91, %0 : i64
%92 = arith.addi %87, %90 : i64
%93 = arith.constant 4 : i32
%95 = arith.extsi %93 : i32 to i64
%94 = arith.muli %95, %arg0 : i64
%96 = arith.subi %92, %94 : i64
%97 = arith.constant 48 : i32
%99 = arith.extsi %97 : i32 to i64
%98 = arith.divsi %96, %99 : i64
%101 = arith.extsi %85 : i32 to i64
%100 = arith.muli %101, %98 : i64
%102 = arith.addi %14, %24 : i64
%103 = arith.addi %102, %83 : i64
%104 = arith.addi %103, %100 : i64
func.return %104 : i64
}
func.func @main() -> i32 {
%105 = llvm.mlir.addressof @str_0 : !llvm.ptr
%107 = arith.constant 36 : i32
%108 = arith.extsi %107 : i32 to i64
%106 = func.call @fun(%108) : (i64) -> i64
%109 = llvm.call @printf(%105, %106) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%110 = arith.constant 0 : i32
func.return %110 : i32
}
}