Problem 321
Sum of first 40 n where M(n)=n(n+2) is triangular (Diophantine recurrence).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n log n) |
| Space complexity | O(1) | O(1) |
| Approach | Flow solution | Divisor-based Diophantine solver |
| Verdict | Optimal |
Flow source
# Project Euler 321
# Sum of first 40 n where M(n)=n(n+2) is triangular (Diophantine recurrence).
function main() -> i32 {
let num_values: i64 = 40
let mut x1: i64 = 2
let mut y1: i64 = 1
let mut x2: i64 = 5
let mut y2: i64 = 3
let mut sum: i64 = y1 + y2
let mut n: i64 = 3
while n <= num_values {
let next_x1: i64 = 3 * x1 + 4 * y1 + 5
let next_y1: i64 = 2 * x1 + 3 * y1 + 3
x1 = next_x1
y1 = next_y1
sum = sum + y1
n = n + 1
if n > num_values { break }
let next_x2: i64 = 3 * x2 + 4 * y2 + 5
let next_y2: i64 = 2 * x2 + 3 * y2 + 3
x2 = next_x2
y2 = next_y2
sum = sum + y2
n = n + 1
}
printf("%lld\n", sum)
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 num_values = 40;
int64_t x1 = 2;
int64_t y1 = 1;
int64_t x2 = 5;
int64_t y2 = 3;
int64_t sum = (y1 + y2);
int64_t n = 3;
while (n <= num_values) {
int64_t next_x1 = (((3 * x1) + (4 * y1)) + 5);
int64_t next_y1 = (((2 * x1) + (3 * y1)) + 3);
x1 = next_x1;
y1 = next_y1;
sum = (sum + y1);
n = (n + 1);
if (n > num_values) {
break;
}
int64_t next_x2 = (((3 * x2) + (4 * y2)) + 5);
int64_t next_y2 = (((2 * x2) + (3 * y2)) + 3);
x2 = next_x2;
y2 = next_y2;
sum = (sum + y2);
n = (n + 1);
}
printf("%lld\n", sum);
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 40 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 2 : i32
%3 = arith.extsi %2 : i32 to i64
%4 = llvm.mlir.constant(1 : i64) : i64
%5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
llvm.store %3, %5 : i64, !llvm.ptr
%6 = arith.constant 1 : i32
%7 = arith.extsi %6 : i32 to i64
%8 = llvm.mlir.constant(1 : i64) : i64
%9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
llvm.store %7, %9 : i64, !llvm.ptr
%10 = arith.constant 5 : i32
%11 = arith.extsi %10 : i32 to i64
%12 = llvm.mlir.constant(1 : i64) : i64
%13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
llvm.store %11, %13 : i64, !llvm.ptr
%14 = arith.constant 3 : i32
%15 = arith.extsi %14 : i32 to i64
%16 = llvm.mlir.constant(1 : i64) : i64
%17 = llvm.alloca %16 x i64 : (i64) -> !llvm.ptr
llvm.store %15, %17 : i64, !llvm.ptr
%18 = llvm.load %9 : !llvm.ptr -> i64
%19 = llvm.load %17 : !llvm.ptr -> i64
%20 = arith.addi %18, %19 : i64
%21 = llvm.mlir.constant(1 : i64) : i64
%22 = llvm.alloca %21 x i64 : (i64) -> !llvm.ptr
llvm.store %20, %22 : i64, !llvm.ptr
%23 = arith.constant 3 : i32
%24 = arith.extsi %23 : i32 to i64
%25 = llvm.mlir.constant(1 : i64) : i64
%26 = llvm.alloca %25 x i64 : (i64) -> !llvm.ptr
llvm.store %24, %26 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%27 = llvm.load %26 : !llvm.ptr -> i64
%28 = arith.cmpi sle, %27, %1 : i64
cf.cond_br %28, ^bb1, ^bb2
^bb1:
%29 = arith.constant 3 : i32
%30 = llvm.load %5 : !llvm.ptr -> i64
%32 = arith.extsi %29 : i32 to i64
%31 = arith.muli %32, %30 : i64
%33 = arith.constant 4 : i32
%34 = llvm.load %9 : !llvm.ptr -> i64
%36 = arith.extsi %33 : i32 to i64
%35 = arith.muli %36, %34 : i64
%37 = arith.addi %31, %35 : i64
%38 = arith.constant 5 : i32
%40 = arith.extsi %38 : i32 to i64
%39 = arith.addi %37, %40 : i64
%41 = arith.constant 2 : i32
%42 = llvm.load %5 : !llvm.ptr -> i64
%44 = arith.extsi %41 : i32 to i64
%43 = arith.muli %44, %42 : i64
%45 = arith.constant 3 : i32
%46 = llvm.load %9 : !llvm.ptr -> i64
%48 = arith.extsi %45 : i32 to i64
%47 = arith.muli %48, %46 : i64
%49 = arith.addi %43, %47 : i64
%50 = arith.constant 3 : i32
%52 = arith.extsi %50 : i32 to i64
%51 = arith.addi %49, %52 : i64
llvm.store %39, %5 : i64, !llvm.ptr
llvm.store %51, %9 : i64, !llvm.ptr
%53 = llvm.load %22 : !llvm.ptr -> i64
%54 = llvm.load %9 : !llvm.ptr -> i64
%55 = arith.addi %53, %54 : i64
llvm.store %55, %22 : i64, !llvm.ptr
%56 = llvm.load %26 : !llvm.ptr -> i64
%57 = arith.constant 1 : i32
%59 = arith.extsi %57 : i32 to i64
%58 = arith.addi %56, %59 : i64
llvm.store %58, %26 : i64, !llvm.ptr
%60 = llvm.load %26 : !llvm.ptr -> i64
%61 = arith.cmpi sgt, %60, %1 : i64
cf.cond_br %61, ^bb3, ^bb4
^bb3:
cf.br ^bb2
^bb4:
cf.br ^bb5
^bb5:
%62 = arith.constant 3 : i32
%63 = llvm.load %13 : !llvm.ptr -> i64
%65 = arith.extsi %62 : i32 to i64
%64 = arith.muli %65, %63 : i64
%66 = arith.constant 4 : i32
%67 = llvm.load %17 : !llvm.ptr -> i64
%69 = arith.extsi %66 : i32 to i64
%68 = arith.muli %69, %67 : i64
%70 = arith.addi %64, %68 : i64
%71 = arith.constant 5 : i32
%73 = arith.extsi %71 : i32 to i64
%72 = arith.addi %70, %73 : i64
%74 = arith.constant 2 : i32
%75 = llvm.load %13 : !llvm.ptr -> i64
%77 = arith.extsi %74 : i32 to i64
%76 = arith.muli %77, %75 : i64
%78 = arith.constant 3 : i32
%79 = llvm.load %17 : !llvm.ptr -> i64
%81 = arith.extsi %78 : i32 to i64
%80 = arith.muli %81, %79 : i64
%82 = arith.addi %76, %80 : i64
%83 = arith.constant 3 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.addi %82, %85 : i64
llvm.store %72, %13 : i64, !llvm.ptr
llvm.store %84, %17 : i64, !llvm.ptr
%86 = llvm.load %22 : !llvm.ptr -> i64
%87 = llvm.load %17 : !llvm.ptr -> i64
%88 = arith.addi %86, %87 : i64
llvm.store %88, %22 : i64, !llvm.ptr
%89 = llvm.load %26 : !llvm.ptr -> i64
%90 = arith.constant 1 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.addi %89, %92 : i64
llvm.store %91, %26 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%93 = llvm.mlir.addressof @str_0 : !llvm.ptr
%94 = llvm.load %22 : !llvm.ptr -> i64
%95 = llvm.call @printf(%93, %94) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%96 = arith.constant 0 : i32
func.return %96 : i32
}
}