← All problems
Problem 023
Sum of all positive integers which cannot be written as the sum of two abundant numbers.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n log log n)
Space complexity O(n)O(n)
Approach Flow solution Sieve of abundant sums
Verdict Suboptimal
Flow source
# Project Euler 023
# Sum of all positive integers which cannot be written as the sum of two
# abundant numbers.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
struct NumInfo {
sumdiv: i32,
abundant: i32,
can: i8
}
function main() -> i32 {
let limit: i32 = 28123
let info: ptr<NumInfo> = calloc((limit + 1) as i64, 9) as ptr<NumInfo>
if info == null {
return 1
}
for i in 1..(limit + 1) {
let mut j: i32 = 2 * i
while j <= limit {
info[j].sumdiv = info[j].sumdiv + i
j = j + i
}
}
let mut acount: i32 = 0
for i in 1..(limit + 1) {
if info[i].sumdiv > i {
info[acount].abundant = i
acount = acount + 1
}
}
for a in 0..acount {
for b in a..acount {
let s: i32 = info[a].abundant + info[b].abundant
if s > limit {
break
}
info[s].can = 1
}
}
let mut total: i64 = 0
for i in 1..(limit + 1) {
if info[i].can == 0 {
total = total + (i as i64)
}
}
printf("%lld\n", total)
free(info)
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; }
typedef struct NumInfo NumInfo;
struct NumInfo {
int32_t sumdiv;
int32_t abundant;
int8_t can;
};
int32_t main(void);
int32_t main(void) {
int32_t limit = 28123;
NumInfo* info = (NumInfo*)(((NumInfo*)(calloc(((int64_t)((limit + 1))), 9))));
if (info == NULL) {
return 1;
}
int32_t __flow_step_1 = 1;
for (int32_t i = 1; (1 <= (limit + 1)) ? i < (limit + 1) : i > (limit + 1); i += (1 <= (limit + 1)) ? 1 : -1) {
int32_t j = (2 * i);
while (j <= limit) {
info[j].sumdiv = (info[j].sumdiv + i);
j = (j + i);
}
}
int32_t acount = 0;
int32_t __flow_step_2 = 1;
for (int32_t i = 1; (1 <= (limit + 1)) ? i < (limit + 1) : i > (limit + 1); i += (1 <= (limit + 1)) ? 1 : -1) {
if (info[i].sumdiv > i) {
info[acount].abundant = i;
acount = (acount + 1);
}
}
int32_t __flow_step_3 = 1;
for (int32_t a = 0; (0 <= acount) ? a < acount : a > acount; a += (0 <= acount) ? 1 : -1) {
int32_t __flow_step_4 = 1;
for (int32_t b = a; (a <= acount) ? b < acount : b > acount; b += (a <= acount) ? 1 : -1) {
int32_t s = (info[a].abundant + info[b].abundant);
if (s > limit) {
break;
}
info[s].can = 1;
}
}
int64_t total = 0;
int32_t __flow_step_5 = 1;
for (int32_t i = 1; (1 <= (limit + 1)) ? i < (limit + 1) : i > (limit + 1); i += (1 <= (limit + 1)) ? 1 : -1) {
if (info[i].can == 0) {
total = (total + ((int64_t)(i)));
}
}
printf("%lld\n", total);
free(info);
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) -> ()
// Struct: NumInfo
// Fields:
// sumdiv: i32
// abundant: i32
// can: i8
func.func @main() -> i32 {
%0 = arith.constant 28123 : i32
%2 = arith.constant 1 : i32
%3 = arith.addi %0, %2 : i32
%4 = arith.extsi %3 : i32 to i64
%5 = arith.constant 9 : i32
%6 = arith.extsi %5 : i32 to i64
%1 = func.call @calloc(%4, %6) : (i64, i64) -> !llvm.ptr
%7 = llvm.mlir.zero : !llvm.ptr
%8 = llvm.icmp "eq" %1, %7 : !llvm.ptr
cf.cond_br %8, ^bb0, ^bb1
^bb0:
%9 = arith.constant 1 : i32
func.return %9 : i32
^bb1:
cf.br ^bb2
^bb2:
%10 = arith.constant 1 : i32
%11 = arith.constant 1 : i32
%12 = arith.addi %0, %11 : i32
%13 = arith.index_cast %10 : i32 to index
%14 = arith.index_cast %12 : i32 to index
%16 = arith.constant 1 : index
%17 = arith.constant -1 : index
%18 = arith.cmpi sle, %13, %14 : index
%15 = arith.select %18, %16, %17 : index
cf.br ^bb3(%13 : index)
^bb3(%19: index):
%20 = arith.cmpi slt, %19, %14 : index
%21 = arith.cmpi sgt, %19, %14 : index
%22 = arith.select %18, %20, %21 : i1
cf.cond_br %22, ^bb4(%19 : index), ^bb5(%19 : index)
^bb4(%23: index):
%24 = arith.constant 2 : i32
%26 = arith.index_cast %23 : index to i32
%25 = arith.muli %24, %26 : i32
%27 = llvm.mlir.constant(1 : i64) : i64
%28 = llvm.alloca %27 x i32 : (i64) -> !llvm.ptr
llvm.store %25, %28 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%29 = llvm.load %28 : !llvm.ptr -> i32
%30 = arith.cmpi sle, %29, %0 : i32
cf.cond_br %30, ^bb7, ^bb8
^bb7:
%32 = llvm.load %28 : !llvm.ptr -> i32
%33 = arith.extsi %32 : i32 to i64
%34 = llvm.getelementptr %1[%33] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%31 = llvm.load %34 : !llvm.ptr -> !llvm.struct<(i32, i32, i8)>
%35 = llvm.load %28 : !llvm.ptr -> i32
%36 = arith.extsi %35 : i32 to i64
%37 = llvm.getelementptr %1[%36] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%38 = llvm.getelementptr %37[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%39 = llvm.load %38 : !llvm.ptr -> i32
%41 = arith.index_cast %23 : index to i32
%40 = arith.addi %39, %41 : i32
%42 = llvm.load %28 : !llvm.ptr -> i32
%43 = arith.extsi %42 : i32 to i64
%44 = llvm.getelementptr %1[%43] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%45 = llvm.getelementptr %44[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
llvm.store %40, %45 : i32, !llvm.ptr
%46 = llvm.load %28 : !llvm.ptr -> i32
%48 = arith.index_cast %23 : index to i32
%47 = arith.addi %46, %48 : i32
llvm.store %47, %28 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%49 = arith.addi %23, %15 : index
cf.br ^bb3(%49 : index)
^bb5(%50: index):
%51 = arith.constant 0 : i32
%52 = llvm.mlir.constant(1 : i64) : i64
%53 = llvm.alloca %52 x i32 : (i64) -> !llvm.ptr
llvm.store %51, %53 : i32, !llvm.ptr
%54 = arith.constant 1 : i32
%55 = arith.constant 1 : i32
%56 = arith.addi %0, %55 : i32
%57 = arith.index_cast %54 : i32 to index
%58 = arith.index_cast %56 : i32 to index
%60 = arith.constant 1 : index
%61 = arith.constant -1 : index
%62 = arith.cmpi sle, %57, %58 : index
%59 = arith.select %62, %60, %61 : index
cf.br ^bb9(%57 : index)
^bb9(%63: index):
%64 = arith.cmpi slt, %63, %58 : index
%65 = arith.cmpi sgt, %63, %58 : index
%66 = arith.select %62, %64, %65 : i1
cf.cond_br %66, ^bb10(%63 : index), ^bb11(%63 : index)
^bb10(%67: index):
%69 = arith.index_cast %67 : index to i64
%70 = llvm.getelementptr %1[%69] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%68 = llvm.load %70 : !llvm.ptr -> !llvm.struct<(i32, i32, i8)>
%71 = arith.index_cast %67 : index to i64
%72 = llvm.getelementptr %1[%71] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%73 = llvm.getelementptr %72[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%74 = llvm.load %73 : !llvm.ptr -> i32
%76 = arith.index_cast %67 : index to i32
%75 = arith.cmpi sgt, %74, %76 : i32
cf.cond_br %75, ^bb12, ^bb13
^bb12:
%77 = llvm.load %53 : !llvm.ptr -> i32
%78 = arith.extsi %77 : i32 to i64
%79 = llvm.getelementptr %1[%78] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%80 = arith.index_cast %67 : index to i32
%81 = llvm.getelementptr %79[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
llvm.store %80, %81 : i32, !llvm.ptr
%82 = llvm.load %53 : !llvm.ptr -> i32
%83 = arith.constant 1 : i32
%84 = arith.addi %82, %83 : i32
llvm.store %84, %53 : i32, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%85 = arith.addi %67, %59 : index
cf.br ^bb9(%85 : index)
^bb11(%86: index):
%87 = arith.constant 0 : i32
%88 = llvm.load %53 : !llvm.ptr -> i32
%89 = arith.index_cast %87 : i32 to index
%90 = arith.index_cast %88 : i32 to index
%92 = arith.constant 1 : index
%93 = arith.constant -1 : index
%94 = arith.cmpi sle, %89, %90 : index
%91 = arith.select %94, %92, %93 : index
cf.br ^bb15(%89 : index)
^bb15(%95: index):
%96 = arith.cmpi slt, %95, %90 : index
%97 = arith.cmpi sgt, %95, %90 : index
%98 = arith.select %94, %96, %97 : i1
cf.cond_br %98, ^bb16(%95 : index), ^bb17(%95 : index)
^bb16(%99: index):
%100 = llvm.load %53 : !llvm.ptr -> i32
%101 = arith.index_cast %99 : i32 to index
%102 = arith.index_cast %100 : i32 to index
%104 = arith.constant 1 : index
%105 = arith.constant -1 : index
%106 = arith.cmpi sle, %101, %102 : index
%103 = arith.select %106, %104, %105 : index
cf.br ^bb18(%101 : index)
^bb18(%107: index):
%108 = arith.cmpi slt, %107, %102 : index
%109 = arith.cmpi sgt, %107, %102 : index
%110 = arith.select %106, %108, %109 : i1
cf.cond_br %110, ^bb19(%107 : index), ^bb20(%107 : index)
^bb19(%111: index):
%113 = arith.index_cast %99 : index to i64
%114 = llvm.getelementptr %1[%113] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%112 = llvm.load %114 : !llvm.ptr -> !llvm.struct<(i32, i32, i8)>
%115 = arith.index_cast %99 : index to i64
%116 = llvm.getelementptr %1[%115] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%117 = llvm.getelementptr %116[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%118 = llvm.load %117 : !llvm.ptr -> i32
%120 = arith.index_cast %111 : index to i64
%121 = llvm.getelementptr %1[%120] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%119 = llvm.load %121 : !llvm.ptr -> !llvm.struct<(i32, i32, i8)>
%122 = arith.index_cast %111 : index to i64
%123 = llvm.getelementptr %1[%122] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%124 = llvm.getelementptr %123[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%125 = llvm.load %124 : !llvm.ptr -> i32
%126 = arith.addi %118, %125 : i32
%127 = arith.cmpi sgt, %126, %0 : i32
cf.cond_br %127, ^bb21, ^bb22
^bb21:
cf.br ^bb20(%111 : index)
^bb22:
cf.br ^bb23
^bb23:
%128 = arith.constant 1 : i32
%129 = arith.extsi %126 : i32 to i64
%130 = llvm.getelementptr %1[%129] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%131 = arith.trunci %128 : i32 to i8
%132 = llvm.getelementptr %130[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
llvm.store %131, %132 : i8, !llvm.ptr
%133 = arith.addi %111, %103 : index
cf.br ^bb18(%133 : index)
^bb20(%134: index):
%135 = arith.addi %99, %91 : index
cf.br ^bb15(%135 : index)
^bb17(%136: index):
%137 = arith.constant 0 : i32
%138 = arith.extsi %137 : i32 to i64
%139 = llvm.mlir.constant(1 : i64) : i64
%140 = llvm.alloca %139 x i64 : (i64) -> !llvm.ptr
llvm.store %138, %140 : i64, !llvm.ptr
%141 = arith.constant 1 : i32
%142 = arith.constant 1 : i32
%143 = arith.addi %0, %142 : i32
%144 = arith.index_cast %141 : i32 to index
%145 = arith.index_cast %143 : i32 to index
%147 = arith.constant 1 : index
%148 = arith.constant -1 : index
%149 = arith.cmpi sle, %144, %145 : index
%146 = arith.select %149, %147, %148 : index
cf.br ^bb24(%144 : index)
^bb24(%150: index):
%151 = arith.cmpi slt, %150, %145 : index
%152 = arith.cmpi sgt, %150, %145 : index
%153 = arith.select %149, %151, %152 : i1
cf.cond_br %153, ^bb25(%150 : index), ^bb26(%150 : index)
^bb25(%154: index):
%156 = arith.index_cast %154 : index to i64
%157 = llvm.getelementptr %1[%156] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%155 = llvm.load %157 : !llvm.ptr -> !llvm.struct<(i32, i32, i8)>
%158 = arith.index_cast %154 : index to i64
%159 = llvm.getelementptr %1[%158] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%160 = llvm.getelementptr %159[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32, i8)>
%161 = llvm.load %160 : !llvm.ptr -> i8
%162 = arith.constant 0 : i32
%164 = arith.extsi %161 : i8 to i32
%163 = arith.cmpi eq, %164, %162 : i32
cf.cond_br %163, ^bb27, ^bb28
^bb27:
%165 = llvm.load %140 : !llvm.ptr -> i64
%166 = arith.index_cast %154 : index to i64
%167 = arith.addi %165, %166 : i64
llvm.store %167, %140 : i64, !llvm.ptr
cf.br ^bb29
^bb28:
cf.br ^bb29
^bb29:
%168 = arith.addi %154, %146 : index
cf.br ^bb24(%168 : index)
^bb26(%169: index):
%170 = llvm.mlir.addressof @str_0 : !llvm.ptr
%171 = llvm.load %140 : !llvm.ptr -> i64
%172 = llvm.call @printf(%170, %171) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%1) : (!llvm.ptr) -> ()
%174 = arith.constant 0 : i32
func.return %174 : i32
}
}