Problem 088
Sum of all minimal product-sum numbers for 2≤k≤12000.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n * m) |
| Space complexity | O(n) | O(n) |
| Approach | Flow solution | Dynamic programming or generating function |
| Verdict | Unknown |
Flow source
# Project Euler 088
# Sum of all minimal product-sum numbers for 2≤k≤12000.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function search(prod: i32, sum: i32, cnt: i32, start: i32, limit: i32, kmax: i32, min_n: ptr<i32>) -> void {
if cnt > 0 {
let k: i32 = prod - sum + cnt
if k >= 2 && k <= kmax {
if prod < min_n[k] {
min_n[k] = prod
}
}
}
let mut f: i32 = start
while prod * f <= limit {
search(prod * f, sum + f, cnt + 1, f, limit, kmax, min_n)
f = f + 1
}
}
function main() -> i32 {
let kmax: i32 = 12000
let limit: i32 = 2 * kmax
let min_n: ptr<i32> = calloc((kmax + 1) as i64, 4)
if min_n == null { return 1 }
let mut k: i32 = 0
while k <= kmax {
min_n[k] = limit + 1
k = k + 1
}
search(1, 0, 0, 2, limit, kmax, min_n)
let used: ptr<i8> = calloc((limit + 1) as i64, 1)
if used == null { return 1 }
let mut total: i64 = 0
k = 2
while k <= kmax {
let n: i32 = min_n[k]
if used[n] == 0 {
used[n] = 1
total = total + (n as i64)
}
k = k + 1
}
printf("%lld\n", total)
free(min_n)
free(used)
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; }
void search_i32_i32_i32_i32_i32_i32_ptr_i32(int32_t prod, int32_t sum, int32_t cnt, int32_t start, int32_t limit, int32_t kmax, int32_t* min_n);
int32_t main(void);
void search_i32_i32_i32_i32_i32_i32_ptr_i32(int32_t prod, int32_t sum, int32_t cnt, int32_t start, int32_t limit, int32_t kmax, int32_t* min_n) {
if (cnt > 0) {
int32_t k = ((prod - sum) + cnt);
if ((k >= 2 && k <= kmax)) {
if (prod < min_n[k]) {
min_n[k] = prod;
}
}
}
int32_t f = start;
while ((prod * f) <= limit) {
search_i32_i32_i32_i32_i32_i32_ptr_i32((prod * f), (sum + f), (cnt + 1), f, limit, kmax, min_n);
f = (f + 1);
}
}
int32_t main(void) {
int32_t kmax = 12000;
int32_t limit = (2 * kmax);
int32_t* min_n = (int32_t*)(calloc(((int64_t)((kmax + 1))), 4));
if (min_n == NULL) {
return 1;
}
int32_t k = 0;
while (k <= kmax) {
min_n[k] = (limit + 1);
k = (k + 1);
}
search_i32_i32_i32_i32_i32_i32_ptr_i32(1, 0, 0, 2, limit, kmax, min_n);
int8_t* used = (int8_t*)(calloc(((int64_t)((limit + 1))), 1));
if (used == NULL) {
return 1;
}
int64_t total = 0;
k = 2;
while (k <= kmax) {
int32_t n = min_n[k];
if (used[n] == 0) {
used[n] = 1;
total = (total + ((int64_t)(n)));
}
k = (k + 1);
}
printf("%lld\n", total);
free(min_n);
free(used);
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 @search(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32, %arg4: i32, %arg5: i32, %arg6: !llvm.ptr) -> () {
%0 = arith.constant 0 : i32
%1 = arith.cmpi sgt, %arg2, %0 : i32
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%2 = arith.subi %arg0, %arg1 : i32
%3 = arith.addi %2, %arg2 : i32
%4 = arith.constant 2 : i32
%5 = arith.cmpi sge, %3, %4 : i32
%6 = scf.if %5 -> (i1) {
%7 = arith.cmpi sle, %3, %arg5 : i32
scf.yield %7 : i1
} else {
%8 = arith.constant false
scf.yield %8 : i1
}
cf.cond_br %6, ^bb3, ^bb4
^bb3:
%10 = arith.extsi %3 : i32 to i64
%11 = llvm.getelementptr %arg6[%10] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%9 = llvm.load %11 : !llvm.ptr -> i32
%12 = arith.cmpi slt, %arg0, %9 : i32
cf.cond_br %12, ^bb6, ^bb7
^bb6:
%13 = arith.extsi %3 : i32 to i64
%14 = llvm.getelementptr %arg6[%13] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %arg0, %14 : i32, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
cf.br ^bb2
^bb1:
cf.br ^bb2
^bb2:
%15 = llvm.mlir.constant(1 : i64) : i64
%16 = llvm.alloca %15 x i32 : (i64) -> !llvm.ptr
llvm.store %arg3, %16 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%17 = llvm.load %16 : !llvm.ptr -> i32
%18 = arith.muli %arg0, %17 : i32
%19 = arith.cmpi sle, %18, %arg4 : i32
cf.cond_br %19, ^bb10, ^bb11
^bb10:
%21 = llvm.load %16 : !llvm.ptr -> i32
%22 = arith.muli %arg0, %21 : i32
%23 = llvm.load %16 : !llvm.ptr -> i32
%24 = arith.addi %arg1, %23 : i32
%25 = arith.constant 1 : i32
%26 = arith.addi %arg2, %25 : i32
%27 = llvm.load %16 : !llvm.ptr -> i32
func.call @search(%22, %24, %26, %27, %arg4, %arg5, %arg6) : (i32, i32, i32, i32, i32, i32, !llvm.ptr) -> ()
%28 = llvm.load %16 : !llvm.ptr -> i32
%29 = arith.constant 1 : i32
%30 = arith.addi %28, %29 : i32
llvm.store %30, %16 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
func.return
}
func.func @main() -> i32 {
%31 = arith.constant 12000 : i32
%32 = arith.constant 2 : i32
%33 = arith.muli %32, %31 : i32
%35 = arith.constant 1 : i32
%36 = arith.addi %31, %35 : i32
%37 = arith.extsi %36 : i32 to i64
%38 = arith.constant 4 : i32
%39 = arith.extsi %38 : i32 to i64
%34 = func.call @calloc(%37, %39) : (i64, i64) -> !llvm.ptr
%40 = llvm.mlir.zero : !llvm.ptr
%41 = llvm.icmp "eq" %34, %40 : !llvm.ptr
cf.cond_br %41, ^bb12, ^bb13
^bb12:
%42 = arith.constant 1 : i32
func.return %42 : i32
^bb13:
cf.br ^bb14
^bb14:
%43 = arith.constant 0 : i32
%44 = llvm.mlir.constant(1 : i64) : i64
%45 = llvm.alloca %44 x i32 : (i64) -> !llvm.ptr
llvm.store %43, %45 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%46 = llvm.load %45 : !llvm.ptr -> i32
%47 = arith.cmpi sle, %46, %31 : i32
cf.cond_br %47, ^bb16, ^bb17
^bb16:
%48 = arith.constant 1 : i32
%49 = arith.addi %33, %48 : i32
%50 = llvm.load %45 : !llvm.ptr -> i32
%51 = arith.extsi %50 : i32 to i64
%52 = llvm.getelementptr %34[%51] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %49, %52 : i32, !llvm.ptr
%53 = llvm.load %45 : !llvm.ptr -> i32
%54 = arith.constant 1 : i32
%55 = arith.addi %53, %54 : i32
llvm.store %55, %45 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%57 = arith.constant 1 : i32
%58 = arith.constant 0 : i32
%59 = arith.constant 0 : i32
%60 = arith.constant 2 : i32
func.call @search(%57, %58, %59, %60, %33, %31, %34) : (i32, i32, i32, i32, i32, i32, !llvm.ptr) -> ()
%62 = arith.constant 1 : i32
%63 = arith.addi %33, %62 : i32
%64 = arith.extsi %63 : i32 to i64
%65 = arith.constant 1 : i32
%66 = arith.extsi %65 : i32 to i64
%61 = func.call @calloc(%64, %66) : (i64, i64) -> !llvm.ptr
%67 = llvm.mlir.zero : !llvm.ptr
%68 = llvm.icmp "eq" %61, %67 : !llvm.ptr
cf.cond_br %68, ^bb18, ^bb19
^bb18:
%69 = arith.constant 1 : i32
func.return %69 : i32
^bb19:
cf.br ^bb20
^bb20:
%70 = arith.constant 0 : i32
%71 = arith.extsi %70 : i32 to i64
%72 = llvm.mlir.constant(1 : i64) : i64
%73 = llvm.alloca %72 x i64 : (i64) -> !llvm.ptr
llvm.store %71, %73 : i64, !llvm.ptr
%74 = arith.constant 2 : i32
llvm.store %74, %45 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%75 = llvm.load %45 : !llvm.ptr -> i32
%76 = arith.cmpi sle, %75, %31 : i32
cf.cond_br %76, ^bb22, ^bb23
^bb22:
%78 = llvm.load %45 : !llvm.ptr -> i32
%79 = arith.extsi %78 : i32 to i64
%80 = llvm.getelementptr %34[%79] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%77 = llvm.load %80 : !llvm.ptr -> i32
%82 = arith.extsi %77 : i32 to i64
%83 = llvm.getelementptr %61[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%81 = llvm.load %83 : !llvm.ptr -> i8
%84 = arith.constant 0 : i32
%86 = arith.extsi %81 : i8 to i32
%85 = arith.cmpi eq, %86, %84 : i32
cf.cond_br %85, ^bb24, ^bb25
^bb24:
%87 = arith.constant 1 : i32
%88 = arith.trunci %87 : i32 to i8
%89 = arith.extsi %77 : i32 to i64
%90 = llvm.getelementptr %61[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %88, %90 : i8, !llvm.ptr
%91 = llvm.load %73 : !llvm.ptr -> i64
%92 = arith.extsi %77 : i32 to i64
%93 = arith.addi %91, %92 : i64
llvm.store %93, %73 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%94 = llvm.load %45 : !llvm.ptr -> i32
%95 = arith.constant 1 : i32
%96 = arith.addi %94, %95 : i32
llvm.store %96, %45 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%97 = llvm.mlir.addressof @str_0 : !llvm.ptr
%98 = llvm.load %73 : !llvm.ptr -> i64
%99 = llvm.call @printf(%97, %98) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%34) : (!llvm.ptr) -> ()
func.call @free(%61) : (!llvm.ptr) -> ()
%102 = arith.constant 0 : i32
func.return %102 : i32
}
}