← All problems
Problem 124
E(10000) where E is rad(n) sorted, then n: the 10000th. Uses a struct to pair rad and index, and for-ranges for the sieve.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^3)O(sqrt(n))
Space complexity O(n)O(1)
Approach Flow solution Trial division or Pollard rho
Verdict Suboptimal
Flow source
# Project Euler 124
# E(10000) where E is rad(n) sorted, then n: the 10000th.
#
# Uses a struct to pair rad and index, and for-ranges for the sieve.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
struct RadEntry {
rad: i32,
idx: i32
}
function main() -> i32 {
let limit: i32 = 100000
let entries: ptr<RadEntry> = calloc((limit + 1) as i64, 8) as ptr<RadEntry>
if entries == null { return 1 }
for i in 1..(limit + 1) {
entries[i] = RadEntry { rad: 1, idx: i }
}
let mut p: i32 = 2
while p <= limit {
if entries[p].rad == 1 {
for m in p..(limit + 1) step p {
entries[m].rad = entries[m].rad * p
}
}
p = p + 1
}
# shell sort by (rad, n)
let mut gap: i32 = limit / 2
while gap > 0 {
for i2 in (gap + 1)..(limit + 1) {
let mut j: i32 = i2
while j - gap >= 1 {
let a: i32 = entries[j].idx
let b: i32 = entries[j - gap].idx
if entries[a].rad > entries[b].rad || (entries[a].rad == entries[b].rad && a > b) {
break
}
entries[j].idx = b
entries[j - gap].idx = a
j = j - gap
}
}
gap = gap / 2
}
printf("%lld\n", entries[10000].idx as i64)
free(entries)
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 RadEntry RadEntry;
struct RadEntry {
int32_t rad;
int32_t idx;
};
int32_t main(void);
int32_t main(void) {
int32_t limit = 100000;
RadEntry* entries = (RadEntry*)(((RadEntry*)(calloc(((int64_t)((limit + 1))), 8))));
if (entries == 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) {
entries[i] = (RadEntry){ .rad = 1, .idx = i };
}
int32_t p = 2;
while (p <= limit) {
if (entries[p].rad == 1) {
int32_t __flow_step_2 = p;
#pragma clang loop vectorize(enable) interleave(enable)
#pragma GCC ivdep
for (int32_t m = p; (__flow_step_2 > 0) ? m < (limit + 1) : m > (limit + 1); m += __flow_step_2) {
entries[m].rad = (entries[m].rad * p);
}
}
p = (p + 1);
}
int32_t gap = FLOW_CHECKED_DIV((limit), (2));
while (gap > 0) {
int32_t __flow_step_3 = 1;
for (int32_t i2 = (gap + 1); ((gap + 1) <= (limit + 1)) ? i2 < (limit + 1) : i2 > (limit + 1); i2 += ((gap + 1) <= (limit + 1)) ? 1 : -1) {
int32_t j = i2;
while ((j - gap) >= 1) {
int32_t a = entries[j].idx;
int32_t b = entries[(j - gap)].idx;
if ((entries[a].rad > entries[b].rad || (entries[a].rad == entries[b].rad && a > b))) {
break;
}
entries[j].idx = b;
entries[(j - gap)].idx = a;
j = (j - gap);
}
}
gap = FLOW_CHECKED_DIV((gap), (2));
}
printf("%lld\n", ((int64_t)(entries[10000].idx)));
free(entries);
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: RadEntry
// Fields:
// rad: i32
// idx: i32
func.func @main() -> i32 {
%0 = arith.constant 100000 : i32
%2 = arith.constant 1 : i32
%3 = arith.addi %0, %2 : i32
%4 = arith.extsi %3 : i32 to i64
%5 = arith.constant 8 : 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 = llvm.mlir.undef : !llvm.struct<(i32, i32)>
%25 = arith.constant 1 : i32
%26 = llvm.insertvalue %25, %24[0] : !llvm.struct<(i32, i32)>
%27 = arith.index_cast %23 : index to i32
%28 = llvm.insertvalue %27, %26[1] : !llvm.struct<(i32, i32)>
%29 = arith.index_cast %23 : index to i64
%30 = llvm.getelementptr %1[%29] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
llvm.store %28, %30 : !llvm.struct<(i32, i32)>, !llvm.ptr
%31 = arith.addi %23, %15 : index
cf.br ^bb3(%31 : index)
^bb5(%32: index):
%33 = arith.constant 2 : i32
%34 = llvm.mlir.constant(1 : i64) : i64
%35 = llvm.alloca %34 x i32 : (i64) -> !llvm.ptr
llvm.store %33, %35 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%36 = llvm.load %35 : !llvm.ptr -> i32
%37 = arith.cmpi sle, %36, %0 : i32
cf.cond_br %37, ^bb7, ^bb8
^bb7:
%39 = llvm.load %35 : !llvm.ptr -> i32
%40 = arith.extsi %39 : i32 to i64
%41 = llvm.getelementptr %1[%40] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%38 = llvm.load %41 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%42 = llvm.load %35 : !llvm.ptr -> i32
%43 = arith.extsi %42 : i32 to i64
%44 = llvm.getelementptr %1[%43] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%45 = llvm.getelementptr %44[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%46 = llvm.load %45 : !llvm.ptr -> i32
%47 = arith.constant 1 : i32
%48 = arith.cmpi eq, %46, %47 : i32
cf.cond_br %48, ^bb9, ^bb10
^bb9:
%49 = llvm.load %35 : !llvm.ptr -> i32
%50 = arith.constant 1 : i32
%51 = arith.addi %0, %50 : i32
%52 = arith.index_cast %49 : i32 to index
%53 = arith.index_cast %51 : i32 to index
%55 = llvm.load %35 : !llvm.ptr -> i32
%54 = arith.index_cast %55 : i32 to index
%56 = arith.constant 0 : index
%57 = arith.cmpi sgt, %54, %56 : index
cf.br ^bb12(%52 : index)
^bb12(%58: index):
%59 = arith.cmpi slt, %58, %53 : index
%60 = arith.cmpi sgt, %58, %53 : index
%61 = arith.select %57, %59, %60 : i1
cf.cond_br %61, ^bb13(%58 : index), ^bb14(%58 : index)
^bb13(%62: index):
%64 = arith.index_cast %62 : index to i64
%65 = llvm.getelementptr %1[%64] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%63 = llvm.load %65 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%66 = arith.index_cast %62 : index to i64
%67 = llvm.getelementptr %1[%66] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%68 = llvm.getelementptr %67[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%69 = llvm.load %68 : !llvm.ptr -> i32
%70 = llvm.load %35 : !llvm.ptr -> i32
%71 = arith.muli %69, %70 : i32
%72 = arith.index_cast %62 : index to i64
%73 = llvm.getelementptr %1[%72] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%74 = llvm.getelementptr %73[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
llvm.store %71, %74 : i32, !llvm.ptr
%75 = arith.addi %62, %54 : index
cf.br ^bb12(%75 : index)
^bb14(%76: index):
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%77 = llvm.load %35 : !llvm.ptr -> i32
%78 = arith.constant 1 : i32
%79 = arith.addi %77, %78 : i32
llvm.store %79, %35 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%80 = arith.constant 2 : i32
%81 = arith.divsi %0, %80 : i32
%82 = llvm.mlir.constant(1 : i64) : i64
%83 = llvm.alloca %82 x i32 : (i64) -> !llvm.ptr
llvm.store %81, %83 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%84 = llvm.load %83 : !llvm.ptr -> i32
%85 = arith.constant 0 : i32
%86 = arith.cmpi sgt, %84, %85 : i32
cf.cond_br %86, ^bb16, ^bb17
^bb16:
%87 = llvm.load %83 : !llvm.ptr -> i32
%88 = arith.constant 1 : i32
%89 = arith.addi %87, %88 : i32
%90 = arith.constant 1 : i32
%91 = arith.addi %0, %90 : i32
%92 = arith.index_cast %89 : i32 to index
%93 = arith.index_cast %91 : i32 to index
%95 = arith.constant 1 : index
%96 = arith.constant -1 : index
%97 = arith.cmpi sle, %92, %93 : index
%94 = arith.select %97, %95, %96 : index
cf.br ^bb18(%92 : index)
^bb18(%98: index):
%99 = arith.cmpi slt, %98, %93 : index
%100 = arith.cmpi sgt, %98, %93 : index
%101 = arith.select %97, %99, %100 : i1
cf.cond_br %101, ^bb19(%98 : index), ^bb20(%98 : index)
^bb19(%102: index):
%103 = arith.index_cast %102 : index to i32
%104 = llvm.mlir.constant(1 : i64) : i64
%105 = llvm.alloca %104 x i32 : (i64) -> !llvm.ptr
llvm.store %103, %105 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%106 = llvm.load %105 : !llvm.ptr -> i32
%107 = llvm.load %83 : !llvm.ptr -> i32
%108 = arith.subi %106, %107 : i32
%109 = arith.constant 1 : i32
%110 = arith.cmpi sge, %108, %109 : i32
cf.cond_br %110, ^bb22, ^bb23
^bb22:
%112 = llvm.load %105 : !llvm.ptr -> i32
%113 = arith.extsi %112 : i32 to i64
%114 = llvm.getelementptr %1[%113] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%111 = llvm.load %114 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%115 = llvm.load %105 : !llvm.ptr -> i32
%116 = arith.extsi %115 : i32 to i64
%117 = llvm.getelementptr %1[%116] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%118 = llvm.getelementptr %117[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%119 = llvm.load %118 : !llvm.ptr -> i32
%121 = llvm.load %105 : !llvm.ptr -> i32
%122 = llvm.load %83 : !llvm.ptr -> i32
%123 = arith.subi %121, %122 : i32
%124 = arith.extsi %123 : i32 to i64
%125 = llvm.getelementptr %1[%124] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%120 = llvm.load %125 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%126 = llvm.load %105 : !llvm.ptr -> i32
%127 = llvm.load %83 : !llvm.ptr -> i32
%128 = arith.subi %126, %127 : i32
%129 = arith.extsi %128 : i32 to i64
%130 = llvm.getelementptr %1[%129] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%131 = llvm.getelementptr %130[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%132 = llvm.load %131 : !llvm.ptr -> i32
%134 = arith.extsi %119 : i32 to i64
%135 = llvm.getelementptr %1[%134] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%133 = llvm.load %135 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%136 = arith.extsi %119 : i32 to i64
%137 = llvm.getelementptr %1[%136] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%138 = llvm.getelementptr %137[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%139 = llvm.load %138 : !llvm.ptr -> i32
%141 = arith.extsi %132 : i32 to i64
%142 = llvm.getelementptr %1[%141] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%140 = llvm.load %142 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%143 = arith.extsi %132 : i32 to i64
%144 = llvm.getelementptr %1[%143] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%145 = llvm.getelementptr %144[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%146 = llvm.load %145 : !llvm.ptr -> i32
%147 = arith.cmpi sgt, %139, %146 : i32
%148 = scf.if %147 -> (i1) {
%149 = arith.constant true
scf.yield %149 : i1
} else {
%151 = arith.extsi %119 : i32 to i64
%152 = llvm.getelementptr %1[%151] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%150 = llvm.load %152 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%153 = arith.extsi %119 : i32 to i64
%154 = llvm.getelementptr %1[%153] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%155 = llvm.getelementptr %154[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%156 = llvm.load %155 : !llvm.ptr -> i32
%158 = arith.extsi %132 : i32 to i64
%159 = llvm.getelementptr %1[%158] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%157 = llvm.load %159 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%160 = arith.extsi %132 : i32 to i64
%161 = llvm.getelementptr %1[%160] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%162 = llvm.getelementptr %161[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%163 = llvm.load %162 : !llvm.ptr -> i32
%164 = arith.cmpi eq, %156, %163 : i32
%165 = scf.if %164 -> (i1) {
%166 = arith.cmpi sgt, %119, %132 : i32
scf.yield %166 : i1
} else {
%167 = arith.constant false
scf.yield %167 : i1
}
scf.yield %165 : i1
}
cf.cond_br %148, ^bb24, ^bb25
^bb24:
cf.br ^bb23
^bb25:
cf.br ^bb26
^bb26:
%168 = llvm.load %105 : !llvm.ptr -> i32
%169 = arith.extsi %168 : i32 to i64
%170 = llvm.getelementptr %1[%169] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%171 = llvm.getelementptr %170[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
llvm.store %132, %171 : i32, !llvm.ptr
%172 = llvm.load %105 : !llvm.ptr -> i32
%173 = llvm.load %83 : !llvm.ptr -> i32
%174 = arith.subi %172, %173 : i32
%175 = arith.extsi %174 : i32 to i64
%176 = llvm.getelementptr %1[%175] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%177 = llvm.getelementptr %176[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
llvm.store %119, %177 : i32, !llvm.ptr
%178 = llvm.load %105 : !llvm.ptr -> i32
%179 = llvm.load %83 : !llvm.ptr -> i32
%180 = arith.subi %178, %179 : i32
llvm.store %180, %105 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%181 = arith.addi %102, %94 : index
cf.br ^bb18(%181 : index)
^bb20(%182: index):
%183 = llvm.load %83 : !llvm.ptr -> i32
%184 = arith.constant 2 : i32
%185 = arith.divsi %183, %184 : i32
llvm.store %185, %83 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%186 = llvm.mlir.addressof @str_0 : !llvm.ptr
%188 = arith.constant 10000 : i32
%189 = arith.extsi %188 : i32 to i64
%190 = llvm.getelementptr %1[%189] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%187 = llvm.load %190 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%191 = arith.constant 10000 : i32
%192 = arith.extsi %191 : i32 to i64
%193 = llvm.getelementptr %1[%192] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%194 = llvm.getelementptr %193[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%195 = llvm.load %194 : !llvm.ptr -> i32
%196 = arith.extsi %195 : i32 to i64
%197 = llvm.call @printf(%186, %196) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%1) : (!llvm.ptr) -> ()
%199 = arith.constant 0 : i32
func.return %199 : i32
}
}