← All problems
Problem 193
Count squarefree numbers below 2^50 via Mobius inversion. Uses a struct to pair mu and prime flags, and for-ranges for the sieve.
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 Mobius sieve
Verdict Suboptimal
Flow source
# Project Euler 193
# Count squarefree numbers below 2^50 via Mobius inversion.
#
# Uses a struct to pair mu and prime flags, and for-ranges for the sieve.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
struct SieveEntry {
mu: i8,
prime: i8
}
function main() -> i32 {
let N: i64 = 1
N = N << 50
# lim = floor(sqrt(N)) = 2^25
let lim: i64 = 1
lim = lim << 25
let entries: ptr<SieveEntry> = calloc(lim + 1, 2) as ptr<SieveEntry>
if entries == null { return 1 }
let mut i: i64 = 0
while i <= lim {
entries[i] = SieveEntry { mu: 1, prime: 1 }
i = i + 1
}
entries[0].prime = 0
entries[1].prime = 0
i = 2
while i <= lim {
if entries[i].prime == 1 {
let mut j: i64 = i * i
while j <= lim {
entries[j].prime = 0
j = j + i
}
j = i
while j <= lim {
entries[j].mu = (0 - entries[j].mu) as i8
j = j + i
}
let sq: i64 = i * i
j = sq
while j <= lim {
entries[j].mu = 0
j = j + sq
}
}
i = i + 1
}
let mut S: i64 = 0
let mut d: i64 = 1
while d <= lim {
let m: i64 = entries[d].mu as i64
if m != 0 {
S = S + m * (N / (d * d))
}
d = d + 1
}
printf("%lld\n", S)
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 SieveEntry SieveEntry;
struct SieveEntry {
int8_t mu;
int8_t prime;
};
int32_t main(void);
int32_t main(void) {
int64_t N = 1;
N = FLOW_CHECKED_SHL((N), (50));
int64_t lim = 1;
lim = FLOW_CHECKED_SHL((lim), (25));
SieveEntry* entries = (SieveEntry*)(((SieveEntry*)(calloc((lim + 1), 2))));
if (entries == NULL) {
return 1;
}
int64_t i = 0;
while (i <= lim) {
entries[i] = (SieveEntry){ .mu = 1, .prime = 1 };
i = (i + 1);
}
entries[0].prime = 0;
entries[1].prime = 0;
i = 2;
while (i <= lim) {
if (entries[i].prime == 1) {
int64_t j = (i * i);
while (j <= lim) {
entries[j].prime = 0;
j = (j + i);
}
j = i;
while (j <= lim) {
entries[j].mu = ((int8_t)((0 - entries[j].mu)));
j = (j + i);
}
int64_t sq = (i * i);
j = sq;
while (j <= lim) {
entries[j].mu = 0;
j = (j + sq);
}
}
i = (i + 1);
}
int64_t S = 0;
int64_t d = 1;
while (d <= lim) {
int64_t m = ((int64_t)(entries[d].mu));
if (m != 0) {
S = (S + (m * FLOW_CHECKED_DIV((N), ((d * d)))));
}
d = (d + 1);
}
printf("%lld\n", S);
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: SieveEntry
// Fields:
// mu: i8
// prime: i8
func.func @main() -> i32 {
%0 = arith.constant 1 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 50 : i32
%4 = arith.extsi %2 : i32 to i64
%3 = arith.shli %1, %4 : i64
%5 = arith.constant 1 : i32
%6 = arith.extsi %5 : i32 to i64
%7 = arith.constant 25 : i32
%9 = arith.extsi %7 : i32 to i64
%8 = arith.shli %6, %9 : i64
%11 = arith.constant 1 : i32
%13 = arith.extsi %11 : i32 to i64
%12 = arith.addi %8, %13 : i64
%14 = arith.constant 2 : i32
%15 = arith.extsi %14 : i32 to i64
%10 = func.call @calloc(%12, %15) : (i64, i64) -> !llvm.ptr
%16 = llvm.mlir.zero : !llvm.ptr
%17 = llvm.icmp "eq" %10, %16 : !llvm.ptr
cf.cond_br %17, ^bb0, ^bb1
^bb0:
%18 = arith.constant 1 : i32
func.return %18 : i32
^bb1:
cf.br ^bb2
^bb2:
%19 = arith.constant 0 : i32
%20 = arith.extsi %19 : i32 to i64
%21 = llvm.mlir.constant(1 : i64) : i64
%22 = llvm.alloca %21 x i64 : (i64) -> !llvm.ptr
llvm.store %20, %22 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%23 = llvm.load %22 : !llvm.ptr -> i64
%24 = arith.cmpi sle, %23, %8 : i64
cf.cond_br %24, ^bb4, ^bb5
^bb4:
%25 = llvm.mlir.undef : !llvm.struct<(i8, i8)>
%26 = arith.constant 1 : i32
%27 = arith.trunci %26 : i32 to i8
%28 = llvm.insertvalue %27, %25[0] : !llvm.struct<(i8, i8)>
%29 = arith.constant 1 : i32
%30 = arith.trunci %29 : i32 to i8
%31 = llvm.insertvalue %30, %28[1] : !llvm.struct<(i8, i8)>
%32 = llvm.load %22 : !llvm.ptr -> i64
%33 = llvm.getelementptr %10[%32] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
llvm.store %31, %33 : !llvm.struct<(i8, i8)>, !llvm.ptr
%34 = llvm.load %22 : !llvm.ptr -> i64
%35 = arith.constant 1 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.addi %34, %37 : i64
llvm.store %36, %22 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%38 = arith.constant 0 : i32
%39 = arith.constant 0 : i32
%40 = arith.extsi %39 : i32 to i64
%41 = llvm.getelementptr %10[%40] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%42 = arith.trunci %38 : i32 to i8
%43 = llvm.getelementptr %41[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i8, i8)>
llvm.store %42, %43 : i8, !llvm.ptr
%44 = arith.constant 0 : i32
%45 = arith.constant 1 : i32
%46 = arith.extsi %45 : i32 to i64
%47 = llvm.getelementptr %10[%46] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%48 = arith.trunci %44 : i32 to i8
%49 = llvm.getelementptr %47[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i8, i8)>
llvm.store %48, %49 : i8, !llvm.ptr
%50 = arith.constant 2 : i32
%51 = arith.extsi %50 : i32 to i64
llvm.store %51, %22 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%52 = llvm.load %22 : !llvm.ptr -> i64
%53 = arith.cmpi sle, %52, %8 : i64
cf.cond_br %53, ^bb7, ^bb8
^bb7:
%55 = llvm.load %22 : !llvm.ptr -> i64
%56 = llvm.getelementptr %10[%55] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%54 = llvm.load %56 : !llvm.ptr -> !llvm.struct<(i8, i8)>
%57 = llvm.load %22 : !llvm.ptr -> i64
%58 = llvm.getelementptr %10[%57] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%59 = llvm.getelementptr %58[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%60 = llvm.load %59 : !llvm.ptr -> i8
%61 = arith.constant 1 : i32
%63 = arith.extsi %60 : i8 to i32
%62 = arith.cmpi eq, %63, %61 : i32
cf.cond_br %62, ^bb9, ^bb10
^bb9:
%64 = llvm.load %22 : !llvm.ptr -> i64
%65 = llvm.load %22 : !llvm.ptr -> i64
%66 = arith.muli %64, %65 : i64
%67 = llvm.mlir.constant(1 : i64) : i64
%68 = llvm.alloca %67 x i64 : (i64) -> !llvm.ptr
llvm.store %66, %68 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%69 = llvm.load %68 : !llvm.ptr -> i64
%70 = arith.cmpi sle, %69, %8 : i64
cf.cond_br %70, ^bb13, ^bb14
^bb13:
%71 = arith.constant 0 : i32
%72 = llvm.load %68 : !llvm.ptr -> i64
%73 = llvm.getelementptr %10[%72] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%74 = arith.trunci %71 : i32 to i8
%75 = llvm.getelementptr %73[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i8, i8)>
llvm.store %74, %75 : i8, !llvm.ptr
%76 = llvm.load %68 : !llvm.ptr -> i64
%77 = llvm.load %22 : !llvm.ptr -> i64
%78 = arith.addi %76, %77 : i64
llvm.store %78, %68 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%79 = llvm.load %22 : !llvm.ptr -> i64
llvm.store %79, %68 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%80 = llvm.load %68 : !llvm.ptr -> i64
%81 = arith.cmpi sle, %80, %8 : i64
cf.cond_br %81, ^bb16, ^bb17
^bb16:
%82 = arith.constant 0 : i32
%84 = llvm.load %68 : !llvm.ptr -> i64
%85 = llvm.getelementptr %10[%84] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%83 = llvm.load %85 : !llvm.ptr -> !llvm.struct<(i8, i8)>
%86 = llvm.load %68 : !llvm.ptr -> i64
%87 = llvm.getelementptr %10[%86] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%88 = llvm.getelementptr %87[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%89 = llvm.load %88 : !llvm.ptr -> i8
%91 = arith.extsi %89 : i8 to i32
%90 = arith.subi %82, %91 : i32
%92 = arith.trunci %90 : i32 to i8
%93 = llvm.load %68 : !llvm.ptr -> i64
%94 = llvm.getelementptr %10[%93] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%95 = llvm.getelementptr %94[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i8, i8)>
llvm.store %92, %95 : i8, !llvm.ptr
%96 = llvm.load %68 : !llvm.ptr -> i64
%97 = llvm.load %22 : !llvm.ptr -> i64
%98 = arith.addi %96, %97 : i64
llvm.store %98, %68 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%99 = llvm.load %22 : !llvm.ptr -> i64
%100 = llvm.load %22 : !llvm.ptr -> i64
%101 = arith.muli %99, %100 : i64
llvm.store %101, %68 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%102 = llvm.load %68 : !llvm.ptr -> i64
%103 = arith.cmpi sle, %102, %8 : i64
cf.cond_br %103, ^bb19, ^bb20
^bb19:
%104 = arith.constant 0 : i32
%105 = llvm.load %68 : !llvm.ptr -> i64
%106 = llvm.getelementptr %10[%105] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%107 = arith.trunci %104 : i32 to i8
%108 = llvm.getelementptr %106[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i8, i8)>
llvm.store %107, %108 : i8, !llvm.ptr
%109 = llvm.load %68 : !llvm.ptr -> i64
%110 = arith.addi %109, %101 : i64
llvm.store %110, %68 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%111 = llvm.load %22 : !llvm.ptr -> i64
%112 = arith.constant 1 : i32
%114 = arith.extsi %112 : i32 to i64
%113 = arith.addi %111, %114 : i64
llvm.store %113, %22 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%115 = arith.constant 0 : i32
%116 = arith.extsi %115 : i32 to i64
%117 = llvm.mlir.constant(1 : i64) : i64
%118 = llvm.alloca %117 x i64 : (i64) -> !llvm.ptr
llvm.store %116, %118 : i64, !llvm.ptr
%119 = arith.constant 1 : i32
%120 = arith.extsi %119 : i32 to i64
%121 = llvm.mlir.constant(1 : i64) : i64
%122 = llvm.alloca %121 x i64 : (i64) -> !llvm.ptr
llvm.store %120, %122 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%123 = llvm.load %122 : !llvm.ptr -> i64
%124 = arith.cmpi sle, %123, %8 : i64
cf.cond_br %124, ^bb22, ^bb23
^bb22:
%126 = llvm.load %122 : !llvm.ptr -> i64
%127 = llvm.getelementptr %10[%126] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%125 = llvm.load %127 : !llvm.ptr -> !llvm.struct<(i8, i8)>
%128 = llvm.load %122 : !llvm.ptr -> i64
%129 = llvm.getelementptr %10[%128] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%130 = llvm.getelementptr %129[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i8, i8)>
%131 = llvm.load %130 : !llvm.ptr -> i8
%132 = arith.extsi %131 : i8 to i64
%133 = arith.constant 0 : i32
%135 = arith.extsi %133 : i32 to i64
%134 = arith.cmpi ne, %132, %135 : i64
cf.cond_br %134, ^bb24, ^bb25
^bb24:
%136 = llvm.load %118 : !llvm.ptr -> i64
%137 = llvm.load %122 : !llvm.ptr -> i64
%138 = llvm.load %122 : !llvm.ptr -> i64
%139 = arith.muli %137, %138 : i64
%140 = arith.divsi %3, %139 : i64
%141 = arith.muli %132, %140 : i64
%142 = arith.addi %136, %141 : i64
llvm.store %142, %118 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%143 = llvm.load %122 : !llvm.ptr -> i64
%144 = arith.constant 1 : i32
%146 = arith.extsi %144 : i32 to i64
%145 = arith.addi %143, %146 : i64
llvm.store %145, %122 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%147 = llvm.mlir.addressof @str_0 : !llvm.ptr
%148 = llvm.load %118 : !llvm.ptr -> i64
%149 = llvm.call @printf(%147, %148) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%10) : (!llvm.ptr) -> ()
%151 = arith.constant 0 : i32
func.return %151 : i32
}
}