Problem 224
Almost right-angled triangles II: a^2+b^2=c^2-1, perimeter <= 75e6.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n^2) |
| Space complexity | O(n) | O(n^2) |
| Approach | Flow solution | Bottom-up DP |
| Verdict | Optimal |
Flow source
# Project Euler 224
# Almost right-angled triangles II: a^2+b^2=c^2-1, perimeter <= 75e6.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let LIM: i64 = 75000000
let CAP: i64 = 20000000
let sa: ptr<i32> = calloc(CAP, 4)
let sb: ptr<i32> = calloc(CAP, 4)
let sc: ptr<i32> = calloc(CAP, 4)
if sa == null || sb == null || sc == null { return 1 }
let mut sp: i64 = 0
sa[0] = 2; sb[0] = 2; sc[0] = 3; sp = 1
let mut count: i64 = 0
while sp > 0 {
sp = sp - 1
let a: i64 = sa[sp] as i64
let b: i64 = sb[sp] as i64
let c: i64 = sc[sp] as i64
let p: i64 = a + b + c
if p > LIM { continue }
count = count + 1
let x1: i64 = a - 2 * b + 2 * c
let y1: i64 = 2 * a - b + 2 * c
let z1: i64 = 2 * a - 2 * b + 3 * c
if x1 + y1 + z1 <= LIM {
sa[sp] = x1 as i32; sb[sp] = y1 as i32; sc[sp] = z1 as i32; sp = sp + 1
}
if a != b {
let x2: i64 = 0 - a + 2 * b + 2 * c
let y2: i64 = 0 - 2 * a + b + 2 * c
let z2: i64 = 0 - 2 * a + 2 * b + 3 * c
if x2 + y2 + z2 <= LIM {
sa[sp] = x2 as i32; sb[sp] = y2 as i32; sc[sp] = z2 as i32; sp = sp + 1
}
}
let x3: i64 = 2 * a + b + 2 * c
let y3: i64 = a + 2 * b + 2 * c
let z3: i64 = 2 * a + 2 * b + 3 * c
if x3 + y3 + z3 <= LIM {
sa[sp] = x3 as i32; sb[sp] = y3 as i32; sc[sp] = z3 as i32; sp = sp + 1
}
}
printf("%lld\n", count)
free(sa); free(sb); free(sc)
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 LIM = 75000000;
int64_t CAP = 20000000;
int32_t* sa = (int32_t*)(calloc(CAP, 4));
int32_t* sb = (int32_t*)(calloc(CAP, 4));
int32_t* sc = (int32_t*)(calloc(CAP, 4));
if (((sa == NULL || sb == NULL) || sc == NULL)) {
return 1;
}
int64_t sp = 0;
sa[0] = 2;
sb[0] = 2;
sc[0] = 3;
sp = 1;
int64_t count = 0;
while (sp > 0) {
sp = (sp - 1);
int64_t a = ((int64_t)(sa[sp]));
int64_t b = ((int64_t)(sb[sp]));
int64_t c = ((int64_t)(sc[sp]));
int64_t p = ((a + b) + c);
if (p > LIM) {
continue;
}
count = (count + 1);
int64_t x1 = ((a - (2 * b)) + (2 * c));
int64_t y1 = (((2 * a) - b) + (2 * c));
int64_t z1 = (((2 * a) - (2 * b)) + (3 * c));
if (((x1 + y1) + z1) <= LIM) {
sa[sp] = ((int32_t)(x1));
sb[sp] = ((int32_t)(y1));
sc[sp] = ((int32_t)(z1));
sp = (sp + 1);
}
if (a != b) {
int64_t x2 = (((0 - a) + (2 * b)) + (2 * c));
int64_t y2 = (((0 - (2 * a)) + b) + (2 * c));
int64_t z2 = (((0 - (2 * a)) + (2 * b)) + (3 * c));
if (((x2 + y2) + z2) <= LIM) {
sa[sp] = ((int32_t)(x2));
sb[sp] = ((int32_t)(y2));
sc[sp] = ((int32_t)(z2));
sp = (sp + 1);
}
}
int64_t x3 = (((2 * a) + b) + (2 * c));
int64_t y3 = ((a + (2 * b)) + (2 * c));
int64_t z3 = (((2 * a) + (2 * b)) + (3 * c));
if (((x3 + y3) + z3) <= LIM) {
sa[sp] = ((int32_t)(x3));
sb[sp] = ((int32_t)(y3));
sc[sp] = ((int32_t)(z3));
sp = (sp + 1);
}
}
printf("%lld\n", count);
free(sa);
free(sb);
free(sc);
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 75000000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 20000000 : i32
%3 = arith.extsi %2 : i32 to i64
%5 = arith.constant 4 : i32
%6 = arith.extsi %5 : i32 to i64
%4 = func.call @calloc(%3, %6) : (i64, i64) -> !llvm.ptr
%8 = arith.constant 4 : i32
%9 = arith.extsi %8 : i32 to i64
%7 = func.call @calloc(%3, %9) : (i64, i64) -> !llvm.ptr
%11 = arith.constant 4 : i32
%12 = arith.extsi %11 : i32 to i64
%10 = func.call @calloc(%3, %12) : (i64, i64) -> !llvm.ptr
%13 = llvm.mlir.zero : !llvm.ptr
%14 = llvm.icmp "eq" %4, %13 : !llvm.ptr
%15 = scf.if %14 -> (i1) {
%16 = arith.constant true
scf.yield %16 : i1
} else {
%17 = llvm.mlir.zero : !llvm.ptr
%18 = llvm.icmp "eq" %7, %17 : !llvm.ptr
scf.yield %18 : i1
}
%19 = scf.if %15 -> (i1) {
%20 = arith.constant true
scf.yield %20 : i1
} else {
%21 = llvm.mlir.zero : !llvm.ptr
%22 = llvm.icmp "eq" %10, %21 : !llvm.ptr
scf.yield %22 : i1
}
cf.cond_br %19, ^bb0, ^bb1
^bb0:
%23 = arith.constant 1 : i32
func.return %23 : i32
^bb1:
cf.br ^bb2
^bb2:
%24 = arith.constant 0 : 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
%29 = arith.constant 0 : i32
%30 = arith.extsi %29 : i32 to i64
%31 = llvm.getelementptr %4[%30] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %28, %31 : i32, !llvm.ptr
%32 = arith.constant 2 : i32
%33 = arith.constant 0 : i32
%34 = arith.extsi %33 : i32 to i64
%35 = llvm.getelementptr %7[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %32, %35 : i32, !llvm.ptr
%36 = arith.constant 3 : i32
%37 = arith.constant 0 : i32
%38 = arith.extsi %37 : i32 to i64
%39 = llvm.getelementptr %10[%38] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %36, %39 : i32, !llvm.ptr
%40 = arith.constant 1 : i32
%41 = arith.extsi %40 : i32 to i64
llvm.store %41, %27 : i64, !llvm.ptr
%42 = arith.constant 0 : i32
%43 = arith.extsi %42 : i32 to i64
%44 = llvm.mlir.constant(1 : i64) : i64
%45 = llvm.alloca %44 x i64 : (i64) -> !llvm.ptr
llvm.store %43, %45 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%46 = llvm.load %27 : !llvm.ptr -> i64
%47 = arith.constant 0 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.cmpi sgt, %46, %49 : i64
cf.cond_br %48, ^bb4, ^bb5
^bb4:
%50 = llvm.load %27 : !llvm.ptr -> i64
%51 = arith.constant 1 : i32
%53 = arith.extsi %51 : i32 to i64
%52 = arith.subi %50, %53 : i64
llvm.store %52, %27 : i64, !llvm.ptr
%55 = llvm.load %27 : !llvm.ptr -> i64
%56 = llvm.getelementptr %4[%55] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%54 = llvm.load %56 : !llvm.ptr -> i32
%57 = arith.extsi %54 : i32 to i64
%59 = llvm.load %27 : !llvm.ptr -> i64
%60 = llvm.getelementptr %7[%59] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%58 = llvm.load %60 : !llvm.ptr -> i32
%61 = arith.extsi %58 : i32 to i64
%63 = llvm.load %27 : !llvm.ptr -> i64
%64 = llvm.getelementptr %10[%63] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%62 = llvm.load %64 : !llvm.ptr -> i32
%65 = arith.extsi %62 : i32 to i64
%66 = arith.addi %57, %61 : i64
%67 = arith.addi %66, %65 : i64
%68 = arith.cmpi sgt, %67, %1 : i64
cf.cond_br %68, ^bb6, ^bb7
^bb6:
cf.br ^bb3
^bb7:
cf.br ^bb8
^bb8:
%69 = llvm.load %45 : !llvm.ptr -> i64
%70 = arith.constant 1 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.addi %69, %72 : i64
llvm.store %71, %45 : i64, !llvm.ptr
%73 = arith.constant 2 : i32
%75 = arith.extsi %73 : i32 to i64
%74 = arith.muli %75, %61 : i64
%76 = arith.subi %57, %74 : i64
%77 = arith.constant 2 : i32
%79 = arith.extsi %77 : i32 to i64
%78 = arith.muli %79, %65 : i64
%80 = arith.addi %76, %78 : i64
%81 = arith.constant 2 : i32
%83 = arith.extsi %81 : i32 to i64
%82 = arith.muli %83, %57 : i64
%84 = arith.subi %82, %61 : i64
%85 = arith.constant 2 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.muli %87, %65 : i64
%88 = arith.addi %84, %86 : i64
%89 = arith.constant 2 : i32
%91 = arith.extsi %89 : i32 to i64
%90 = arith.muli %91, %57 : i64
%92 = arith.constant 2 : i32
%94 = arith.extsi %92 : i32 to i64
%93 = arith.muli %94, %61 : i64
%95 = arith.subi %90, %93 : i64
%96 = arith.constant 3 : i32
%98 = arith.extsi %96 : i32 to i64
%97 = arith.muli %98, %65 : i64
%99 = arith.addi %95, %97 : i64
%100 = arith.addi %80, %88 : i64
%101 = arith.addi %100, %99 : i64
%102 = arith.cmpi sle, %101, %1 : i64
cf.cond_br %102, ^bb9, ^bb10
^bb9:
%103 = arith.trunci %80 : i64 to i32
%104 = llvm.load %27 : !llvm.ptr -> i64
%105 = llvm.getelementptr %4[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %103, %105 : i32, !llvm.ptr
%106 = arith.trunci %88 : i64 to i32
%107 = llvm.load %27 : !llvm.ptr -> i64
%108 = llvm.getelementptr %7[%107] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %106, %108 : i32, !llvm.ptr
%109 = arith.trunci %99 : i64 to i32
%110 = llvm.load %27 : !llvm.ptr -> i64
%111 = llvm.getelementptr %10[%110] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %109, %111 : i32, !llvm.ptr
%112 = llvm.load %27 : !llvm.ptr -> i64
%113 = arith.constant 1 : i32
%115 = arith.extsi %113 : i32 to i64
%114 = arith.addi %112, %115 : i64
llvm.store %114, %27 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%116 = arith.cmpi ne, %57, %61 : i64
cf.cond_br %116, ^bb12, ^bb13
^bb12:
%117 = arith.constant 0 : i32
%119 = arith.extsi %117 : i32 to i64
%118 = arith.subi %119, %57 : i64
%120 = arith.constant 2 : i32
%122 = arith.extsi %120 : i32 to i64
%121 = arith.muli %122, %61 : i64
%123 = arith.addi %118, %121 : i64
%124 = arith.constant 2 : i32
%126 = arith.extsi %124 : i32 to i64
%125 = arith.muli %126, %65 : i64
%127 = arith.addi %123, %125 : i64
%128 = arith.constant 0 : i32
%129 = arith.constant 2 : i32
%131 = arith.extsi %129 : i32 to i64
%130 = arith.muli %131, %57 : i64
%133 = arith.extsi %128 : i32 to i64
%132 = arith.subi %133, %130 : i64
%134 = arith.addi %132, %61 : i64
%135 = arith.constant 2 : i32
%137 = arith.extsi %135 : i32 to i64
%136 = arith.muli %137, %65 : i64
%138 = arith.addi %134, %136 : i64
%139 = arith.constant 0 : i32
%140 = arith.constant 2 : i32
%142 = arith.extsi %140 : i32 to i64
%141 = arith.muli %142, %57 : i64
%144 = arith.extsi %139 : i32 to i64
%143 = arith.subi %144, %141 : i64
%145 = arith.constant 2 : i32
%147 = arith.extsi %145 : i32 to i64
%146 = arith.muli %147, %61 : i64
%148 = arith.addi %143, %146 : i64
%149 = arith.constant 3 : i32
%151 = arith.extsi %149 : i32 to i64
%150 = arith.muli %151, %65 : i64
%152 = arith.addi %148, %150 : i64
%153 = arith.addi %127, %138 : i64
%154 = arith.addi %153, %152 : i64
%155 = arith.cmpi sle, %154, %1 : i64
cf.cond_br %155, ^bb15, ^bb16
^bb15:
%156 = arith.trunci %127 : i64 to i32
%157 = llvm.load %27 : !llvm.ptr -> i64
%158 = llvm.getelementptr %4[%157] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %156, %158 : i32, !llvm.ptr
%159 = arith.trunci %138 : i64 to i32
%160 = llvm.load %27 : !llvm.ptr -> i64
%161 = llvm.getelementptr %7[%160] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %159, %161 : i32, !llvm.ptr
%162 = arith.trunci %152 : i64 to i32
%163 = llvm.load %27 : !llvm.ptr -> i64
%164 = llvm.getelementptr %10[%163] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %162, %164 : i32, !llvm.ptr
%165 = llvm.load %27 : !llvm.ptr -> i64
%166 = arith.constant 1 : i32
%168 = arith.extsi %166 : i32 to i64
%167 = arith.addi %165, %168 : i64
llvm.store %167, %27 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%169 = arith.constant 2 : i32
%171 = arith.extsi %169 : i32 to i64
%170 = arith.muli %171, %57 : i64
%172 = arith.addi %170, %61 : i64
%173 = arith.constant 2 : i32
%175 = arith.extsi %173 : i32 to i64
%174 = arith.muli %175, %65 : i64
%176 = arith.addi %172, %174 : i64
%177 = arith.constant 2 : i32
%179 = arith.extsi %177 : i32 to i64
%178 = arith.muli %179, %61 : i64
%180 = arith.addi %57, %178 : i64
%181 = arith.constant 2 : i32
%183 = arith.extsi %181 : i32 to i64
%182 = arith.muli %183, %65 : i64
%184 = arith.addi %180, %182 : i64
%185 = arith.constant 2 : i32
%187 = arith.extsi %185 : i32 to i64
%186 = arith.muli %187, %57 : i64
%188 = arith.constant 2 : i32
%190 = arith.extsi %188 : i32 to i64
%189 = arith.muli %190, %61 : i64
%191 = arith.addi %186, %189 : i64
%192 = arith.constant 3 : i32
%194 = arith.extsi %192 : i32 to i64
%193 = arith.muli %194, %65 : i64
%195 = arith.addi %191, %193 : i64
%196 = arith.addi %176, %184 : i64
%197 = arith.addi %196, %195 : i64
%198 = arith.cmpi sle, %197, %1 : i64
cf.cond_br %198, ^bb18, ^bb19
^bb18:
%199 = arith.trunci %176 : i64 to i32
%200 = llvm.load %27 : !llvm.ptr -> i64
%201 = llvm.getelementptr %4[%200] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %199, %201 : i32, !llvm.ptr
%202 = arith.trunci %184 : i64 to i32
%203 = llvm.load %27 : !llvm.ptr -> i64
%204 = llvm.getelementptr %7[%203] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %202, %204 : i32, !llvm.ptr
%205 = arith.trunci %195 : i64 to i32
%206 = llvm.load %27 : !llvm.ptr -> i64
%207 = llvm.getelementptr %10[%206] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %205, %207 : i32, !llvm.ptr
%208 = llvm.load %27 : !llvm.ptr -> i64
%209 = arith.constant 1 : i32
%211 = arith.extsi %209 : i32 to i64
%210 = arith.addi %208, %211 : i64
llvm.store %210, %27 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
cf.br ^bb3
^bb5:
%212 = llvm.mlir.addressof @str_0 : !llvm.ptr
%213 = llvm.load %45 : !llvm.ptr -> i64
%214 = llvm.call @printf(%212, %213) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%4) : (!llvm.ptr) -> ()
func.call @free(%7) : (!llvm.ptr) -> ()
func.call @free(%10) : (!llvm.ptr) -> ()
%218 = arith.constant 0 : i32
func.return %218 : i32
}
}