← All problems
Problem 021
Sum of amicable numbers under 10000.
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-based sum of proper divisors
Verdict Suboptimal
Flow source
# Project Euler 021
# Sum of amicable numbers under 10000.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let limit: i64 = 10000
let sumdiv: ptr<i64> = calloc(limit, 8)
if sumdiv == null {
return 1
}
for i in 1..limit {
let mut j: i64 = 2 * i
while j < limit {
sumdiv[j] = sumdiv[j] + i
j = j + i
}
}
let mut total: i64 = 0
for i in 2..limit {
let b: i64 = sumdiv[i]
if b > i && b < limit && sumdiv[b] == i {
total = total + i + b
} elif b >= limit {
# need sum of divisors of b if b is outside table
let mut sb: i64 = 1
let mut d: i64 = 2
while d * d <= b {
if b % d == 0 {
sb = sb + d
if d * d != b {
sb = sb + b / d
}
}
d = d + 1
}
if sb == i {
total = total + i
}
}
}
printf("%lld\n", total)
free(sumdiv)
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 limit = 10000;
int64_t* sumdiv = (int64_t*)(calloc(limit, 8));
if (sumdiv == NULL) {
return 1;
}
int32_t __flow_step_1 = 1;
for (int32_t i = 1; (1 <= limit) ? i < limit : i > limit; i += (1 <= limit) ? 1 : -1) {
int64_t j = (2 * i);
while (j < limit) {
sumdiv[j] = (sumdiv[j] + i);
j = (j + i);
}
}
int64_t total = 0;
int32_t __flow_step_2 = 1;
for (int32_t i = 2; (2 <= limit) ? i < limit : i > limit; i += (2 <= limit) ? 1 : -1) {
int64_t b = sumdiv[i];
if (((b > i && b < limit) && sumdiv[b] == i)) {
total = ((total + i) + b);
} else if (b >= limit) {
int64_t sb = 1;
int64_t d = 2;
while ((d * d) <= b) {
if (FLOW_CHECKED_MOD((b), (d)) == 0) {
sb = (sb + d);
if ((d * d) != b) {
sb = (sb + FLOW_CHECKED_DIV((b), (d)));
}
}
d = (d + 1);
}
if (sb == i) {
total = (total + i);
}
}
}
printf("%lld\n", total);
free(sumdiv);
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 10000 : i32
%1 = arith.extsi %0 : i32 to i64
%3 = arith.constant 8 : i32
%4 = arith.extsi %3 : i32 to i64
%2 = func.call @calloc(%1, %4) : (i64, i64) -> !llvm.ptr
%5 = llvm.mlir.zero : !llvm.ptr
%6 = llvm.icmp "eq" %2, %5 : !llvm.ptr
cf.cond_br %6, ^bb0, ^bb1
^bb0:
%7 = arith.constant 1 : i32
func.return %7 : i32
^bb1:
cf.br ^bb2
^bb2:
%8 = arith.constant 1 : i32
%9 = arith.index_cast %8 : i32 to index
%10 = arith.index_cast %1 : i32 to index
%12 = arith.constant 1 : index
%13 = arith.constant -1 : index
%14 = arith.cmpi sle, %9, %10 : index
%11 = arith.select %14, %12, %13 : index
cf.br ^bb3(%9 : index)
^bb3(%15: index):
%16 = arith.cmpi slt, %15, %10 : index
%17 = arith.cmpi sgt, %15, %10 : index
%18 = arith.select %14, %16, %17 : i1
cf.cond_br %18, ^bb4(%15 : index), ^bb5(%15 : index)
^bb4(%19: index):
%20 = arith.constant 2 : i32
%22 = arith.index_cast %19 : index to i32
%21 = arith.muli %20, %22 : i32
%23 = arith.extsi %21 : i32 to i64
%24 = llvm.mlir.constant(1 : i64) : i64
%25 = llvm.alloca %24 x i64 : (i64) -> !llvm.ptr
llvm.store %23, %25 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%26 = llvm.load %25 : !llvm.ptr -> i64
%27 = arith.cmpi slt, %26, %1 : i64
cf.cond_br %27, ^bb7, ^bb8
^bb7:
%29 = llvm.load %25 : !llvm.ptr -> i64
%30 = llvm.getelementptr %2[%29] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%28 = llvm.load %30 : !llvm.ptr -> i64
%32 = arith.trunci %28 : i64 to i32
%33 = arith.index_cast %19 : index to i32
%31 = arith.addi %32, %33 : i32
%34 = llvm.load %25 : !llvm.ptr -> i64
%35 = arith.extsi %31 : i32 to i64
%36 = llvm.getelementptr %2[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %35, %36 : i64, !llvm.ptr
%37 = llvm.load %25 : !llvm.ptr -> i64
%39 = arith.trunci %37 : i64 to i32
%40 = arith.index_cast %19 : index to i32
%38 = arith.addi %39, %40 : i32
%41 = arith.extsi %38 : i32 to i64
llvm.store %41, %25 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%42 = arith.addi %19, %11 : index
cf.br ^bb3(%42 : index)
^bb5(%43: index):
%44 = arith.constant 0 : i32
%45 = arith.extsi %44 : i32 to i64
%46 = llvm.mlir.constant(1 : i64) : i64
%47 = llvm.alloca %46 x i64 : (i64) -> !llvm.ptr
llvm.store %45, %47 : i64, !llvm.ptr
%48 = arith.constant 2 : i32
%49 = arith.index_cast %48 : i32 to index
%50 = arith.index_cast %1 : i32 to index
%52 = arith.constant 1 : index
%53 = arith.constant -1 : index
%54 = arith.cmpi sle, %49, %50 : index
%51 = arith.select %54, %52, %53 : index
cf.br ^bb9(%49 : index)
^bb9(%55: index):
%56 = arith.cmpi slt, %55, %50 : index
%57 = arith.cmpi sgt, %55, %50 : index
%58 = arith.select %54, %56, %57 : i1
cf.cond_br %58, ^bb10(%55 : index), ^bb11(%55 : index)
^bb10(%59: index):
%61 = arith.index_cast %59 : index to i64
%62 = llvm.getelementptr %2[%61] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%60 = llvm.load %62 : !llvm.ptr -> i64
%64 = arith.trunci %60 : i64 to i32
%65 = arith.index_cast %59 : index to i32
%63 = arith.cmpi sgt, %64, %65 : i32
%66 = scf.if %63 -> (i1) {
%67 = arith.cmpi slt, %60, %1 : i64
scf.yield %67 : i1
} else {
%68 = arith.constant false
scf.yield %68 : i1
}
%69 = scf.if %66 -> (i1) {
%71 = llvm.getelementptr %2[%60] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%70 = llvm.load %71 : !llvm.ptr -> i64
%73 = arith.trunci %70 : i64 to i32
%74 = arith.index_cast %59 : index to i32
%72 = arith.cmpi eq, %73, %74 : i32
scf.yield %72 : i1
} else {
%75 = arith.constant false
scf.yield %75 : i1
}
cf.cond_br %69, ^bb12, ^bb13
^bb12:
%76 = llvm.load %47 : !llvm.ptr -> i64
%78 = arith.trunci %76 : i64 to i32
%79 = arith.index_cast %59 : index to i32
%77 = arith.addi %78, %79 : i32
%81 = arith.extsi %77 : i32 to i64
%80 = arith.addi %81, %60 : i64
llvm.store %80, %47 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
%82 = arith.cmpi sge, %60, %1 : i64
cf.cond_br %82, ^bb15, ^bb14
^bb15:
%83 = arith.constant 1 : i32
%84 = arith.extsi %83 : i32 to i64
%85 = llvm.mlir.constant(1 : i64) : i64
%86 = llvm.alloca %85 x i64 : (i64) -> !llvm.ptr
llvm.store %84, %86 : i64, !llvm.ptr
%87 = arith.constant 2 : i32
%88 = arith.extsi %87 : i32 to i64
%89 = llvm.mlir.constant(1 : i64) : i64
%90 = llvm.alloca %89 x i64 : (i64) -> !llvm.ptr
llvm.store %88, %90 : i64, !llvm.ptr
cf.br ^bb16
^bb16:
%91 = llvm.load %90 : !llvm.ptr -> i64
%92 = llvm.load %90 : !llvm.ptr -> i64
%93 = arith.muli %91, %92 : i64
%94 = arith.cmpi sle, %93, %60 : i64
cf.cond_br %94, ^bb17, ^bb18
^bb17:
%95 = llvm.load %90 : !llvm.ptr -> i64
%96 = arith.remsi %60, %95 : i64
%97 = arith.constant 0 : i32
%99 = arith.extsi %97 : i32 to i64
%98 = arith.cmpi eq, %96, %99 : i64
cf.cond_br %98, ^bb19, ^bb20
^bb19:
%100 = llvm.load %86 : !llvm.ptr -> i64
%101 = llvm.load %90 : !llvm.ptr -> i64
%102 = arith.addi %100, %101 : i64
llvm.store %102, %86 : i64, !llvm.ptr
%103 = llvm.load %90 : !llvm.ptr -> i64
%104 = llvm.load %90 : !llvm.ptr -> i64
%105 = arith.muli %103, %104 : i64
%106 = arith.cmpi ne, %105, %60 : i64
cf.cond_br %106, ^bb22, ^bb23
^bb22:
%107 = llvm.load %86 : !llvm.ptr -> i64
%108 = llvm.load %90 : !llvm.ptr -> i64
%109 = arith.divsi %60, %108 : i64
%110 = arith.addi %107, %109 : i64
llvm.store %110, %86 : i64, !llvm.ptr
cf.br ^bb24
^bb23:
cf.br ^bb24
^bb24:
cf.br ^bb21
^bb20:
cf.br ^bb21
^bb21:
%111 = llvm.load %90 : !llvm.ptr -> i64
%112 = arith.constant 1 : i32
%114 = arith.extsi %112 : i32 to i64
%113 = arith.addi %111, %114 : i64
llvm.store %113, %90 : i64, !llvm.ptr
cf.br ^bb16
^bb18:
%115 = llvm.load %86 : !llvm.ptr -> i64
%117 = arith.trunci %115 : i64 to i32
%118 = arith.index_cast %59 : index to i32
%116 = arith.cmpi eq, %117, %118 : i32
cf.cond_br %116, ^bb25, ^bb26
^bb25:
%119 = llvm.load %47 : !llvm.ptr -> i64
%121 = arith.trunci %119 : i64 to i32
%122 = arith.index_cast %59 : index to i32
%120 = arith.addi %121, %122 : i32
%123 = arith.extsi %120 : i32 to i64
llvm.store %123, %47 : i64, !llvm.ptr
cf.br ^bb27
^bb26:
cf.br ^bb27
^bb27:
cf.br ^bb14
^bb14:
%124 = arith.addi %59, %51 : index
cf.br ^bb9(%124 : index)
^bb11(%125: index):
%126 = llvm.mlir.addressof @str_0 : !llvm.ptr
%127 = llvm.load %47 : !llvm.ptr -> i64
%128 = llvm.call @printf(%126, %127) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%2) : (!llvm.ptr) -> ()
%130 = arith.constant 0 : i32
func.return %130 : i32
}
}