← All problems
Problem 039
Perimeter p ≤ 1000 maximizing number of integer right triangles {a,b,c}.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^3)O(n)
Space complexity O(1)O(1)
Approach Flow solution Pythagorean triple perimeter count
Verdict Suboptimal
Flow source
# Project Euler 039
# Perimeter p ≤ 1000 maximizing number of integer right triangles {a,b,c}.
function main() -> i32 {
let mut best_p: i64 = 0
let mut best_c: i64 = 0
let mut p: i64 = 12
while p <= 1000 {
let mut count: i64 = 0
let mut a: i64 = 1
while a < p / 3 {
let mut b: i64 = a
while b < p / 2 {
let c: i64 = p - a - b
if b > c { break }
if a * a + b * b == c * c {
count = count + 1
}
b = b + 1
}
a = a + 1
}
if count > best_c {
best_c = count
best_p = p
}
p = p + 1
}
printf("%lld\n", best_p)
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 best_p = 0;
int64_t best_c = 0;
int64_t p = 12;
while (p <= 1000) {
int64_t count = 0;
int64_t a = 1;
while (a < FLOW_CHECKED_DIV((p), (3))) {
int64_t b = a;
while (b < FLOW_CHECKED_DIV((p), (2))) {
int64_t c = ((p - a) - b);
if (b > c) {
break;
}
if (((a * a) + (b * b)) == (c * c)) {
count = (count + 1);
}
b = (b + 1);
}
a = (a + 1);
}
if (count > best_c) {
best_c = count;
best_p = p;
}
p = (p + 1);
}
printf("%lld\n", best_p);
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 @main() -> i32 {
%0 = arith.constant 0 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
%4 = arith.constant 0 : i32
%5 = arith.extsi %4 : i32 to i64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
llvm.store %5, %7 : i64, !llvm.ptr
%8 = arith.constant 12 : i32
%9 = arith.extsi %8 : i32 to i64
%10 = llvm.mlir.constant(1 : i64) : i64
%11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
llvm.store %9, %11 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%12 = llvm.load %11 : !llvm.ptr -> i64
%13 = arith.constant 1000 : i32
%15 = arith.extsi %13 : i32 to i64
%14 = arith.cmpi sle, %12, %15 : i64
cf.cond_br %14, ^bb1, ^bb2
^bb1:
%16 = arith.constant 0 : i32
%17 = arith.extsi %16 : i32 to i64
%18 = llvm.mlir.constant(1 : i64) : i64
%19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
llvm.store %17, %19 : i64, !llvm.ptr
%20 = arith.constant 1 : i32
%21 = arith.extsi %20 : i32 to i64
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
llvm.store %21, %23 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%24 = llvm.load %23 : !llvm.ptr -> i64
%25 = llvm.load %11 : !llvm.ptr -> i64
%26 = arith.constant 3 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.divsi %25, %28 : i64
%29 = arith.cmpi slt, %24, %27 : i64
cf.cond_br %29, ^bb4, ^bb5
^bb4:
%30 = llvm.load %23 : !llvm.ptr -> i64
%31 = llvm.mlir.constant(1 : i64) : i64
%32 = llvm.alloca %31 x i64 : (i64) -> !llvm.ptr
llvm.store %30, %32 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%33 = llvm.load %32 : !llvm.ptr -> i64
%34 = llvm.load %11 : !llvm.ptr -> i64
%35 = arith.constant 2 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.divsi %34, %37 : i64
%38 = arith.cmpi slt, %33, %36 : i64
cf.cond_br %38, ^bb7, ^bb8
^bb7:
%39 = llvm.load %11 : !llvm.ptr -> i64
%40 = llvm.load %23 : !llvm.ptr -> i64
%41 = arith.subi %39, %40 : i64
%42 = llvm.load %32 : !llvm.ptr -> i64
%43 = arith.subi %41, %42 : i64
%44 = llvm.load %32 : !llvm.ptr -> i64
%45 = arith.cmpi sgt, %44, %43 : i64
cf.cond_br %45, ^bb9, ^bb10
^bb9:
cf.br ^bb8
^bb10:
cf.br ^bb11
^bb11:
%46 = llvm.load %23 : !llvm.ptr -> i64
%47 = llvm.load %23 : !llvm.ptr -> i64
%48 = arith.muli %46, %47 : i64
%49 = llvm.load %32 : !llvm.ptr -> i64
%50 = llvm.load %32 : !llvm.ptr -> i64
%51 = arith.muli %49, %50 : i64
%52 = arith.addi %48, %51 : i64
%53 = arith.muli %43, %43 : i64
%54 = arith.cmpi eq, %52, %53 : i64
cf.cond_br %54, ^bb12, ^bb13
^bb12:
%55 = llvm.load %19 : !llvm.ptr -> i64
%56 = arith.constant 1 : i32
%58 = arith.extsi %56 : i32 to i64
%57 = arith.addi %55, %58 : i64
llvm.store %57, %19 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%59 = llvm.load %32 : !llvm.ptr -> i64
%60 = arith.constant 1 : i32
%62 = arith.extsi %60 : i32 to i64
%61 = arith.addi %59, %62 : i64
llvm.store %61, %32 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%63 = llvm.load %23 : !llvm.ptr -> i64
%64 = arith.constant 1 : i32
%66 = arith.extsi %64 : i32 to i64
%65 = arith.addi %63, %66 : i64
llvm.store %65, %23 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%67 = llvm.load %19 : !llvm.ptr -> i64
%68 = llvm.load %7 : !llvm.ptr -> i64
%69 = arith.cmpi sgt, %67, %68 : i64
cf.cond_br %69, ^bb15, ^bb16
^bb15:
%70 = llvm.load %19 : !llvm.ptr -> i64
llvm.store %70, %7 : i64, !llvm.ptr
%71 = llvm.load %11 : !llvm.ptr -> i64
llvm.store %71, %3 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%72 = llvm.load %11 : !llvm.ptr -> i64
%73 = arith.constant 1 : i32
%75 = arith.extsi %73 : i32 to i64
%74 = arith.addi %72, %75 : i64
llvm.store %74, %11 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%76 = llvm.mlir.addressof @str_0 : !llvm.ptr
%77 = llvm.load %3 : !llvm.ptr -> i64
%78 = llvm.call @printf(%76, %77) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%79 = arith.constant 0 : i32
func.return %79 : i32
}
}