← All problems
Problem 010
Sum of all primes below two million. Linear sieve → portable C. Same algorithm you'd write in C, less noise.
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 Eratosthenes
Verdict Suboptimal
Flow source
# Project Euler 010
# Sum of all primes below two million.
#
# Linear sieve → portable C. Same algorithm you'd write in C, less noise.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function sum_primes_below(limit: i64) -> i64 {
let sieve: ptr<i8> = calloc(limit, 1)
if sieve == null {
return -1
}
# sieve[i] == 0 means prime (calloc zero-fills)
sieve[0] = 1
sieve[1] = 1
let mut p: i64 = 2
while p * p < limit {
if sieve[p] == 0 {
let mut m: i64 = p * p
while m < limit {
sieve[m] = 1
m = m + p
}
}
p = p + 1
}
let mut total: i64 = 0
for i in 2..limit {
if sieve[i] == 0 {
total = total + i
}
}
free(sieve)
return total
}
function main() -> i32 {
printf("%lld\n", sum_primes_below(2000000))
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; }
int64_t sum_primes_below_i64(int64_t limit);
int32_t main(void);
int64_t sum_primes_below_i64(int64_t limit) {
int8_t* sieve = (int8_t*)(calloc(limit, 1));
if (sieve == NULL) {
return (-1);
}
sieve[0] = 1;
sieve[1] = 1;
int64_t p = 2;
while ((p * p) < limit) {
if (sieve[p] == 0) {
int64_t m = (p * p);
while (m < limit) {
sieve[m] = 1;
m = (m + p);
}
}
p = (p + 1);
}
int64_t total = 0;
int32_t __flow_step_1 = 1;
for (int32_t i = 2; (2 <= limit) ? i < limit : i > limit; i += (2 <= limit) ? 1 : -1) {
if (sieve[i] == 0) {
total = (total + i);
}
}
free(sieve);
return total;
}
int32_t main(void) {
printf("%lld\n", sum_primes_below_i64(2000000));
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 @sum_primes_below(%arg0: i64) -> i64 {
%1 = arith.constant 1 : i32
%2 = arith.extsi %1 : i32 to i64
%0 = func.call @calloc(%arg0, %2) : (i64, i64) -> !llvm.ptr
%3 = llvm.mlir.zero : !llvm.ptr
%4 = llvm.icmp "eq" %0, %3 : !llvm.ptr
cf.cond_br %4, ^bb0, ^bb1
^bb0:
%5 = arith.constant 1 : i32
%7 = arith.constant 0 : i32
%6 = arith.subi %7, %5 : i32
%8 = arith.extsi %6 : i32 to i64
func.return %8 : i64
^bb1:
cf.br ^bb2
^bb2:
%9 = arith.constant 1 : i32
%10 = arith.constant 0 : i32
%11 = arith.trunci %9 : i32 to i8
%12 = arith.extsi %10 : i32 to i64
%13 = llvm.getelementptr %0[%12] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %11, %13 : i8, !llvm.ptr
%14 = arith.constant 1 : i32
%15 = arith.constant 1 : i32
%16 = arith.trunci %14 : i32 to i8
%17 = arith.extsi %15 : i32 to i64
%18 = llvm.getelementptr %0[%17] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %16, %18 : i8, !llvm.ptr
%19 = arith.constant 2 : 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 = llvm.load %22 : !llvm.ptr -> i64
%25 = arith.muli %23, %24 : i64
%26 = arith.cmpi slt, %25, %arg0 : i64
cf.cond_br %26, ^bb4, ^bb5
^bb4:
%28 = llvm.load %22 : !llvm.ptr -> i64
%29 = llvm.getelementptr %0[%28] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%27 = llvm.load %29 : !llvm.ptr -> i8
%30 = arith.constant 0 : i32
%32 = arith.extsi %27 : i8 to i32
%31 = arith.cmpi eq, %32, %30 : i32
cf.cond_br %31, ^bb6, ^bb7
^bb6:
%33 = llvm.load %22 : !llvm.ptr -> i64
%34 = llvm.load %22 : !llvm.ptr -> i64
%35 = arith.muli %33, %34 : i64
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x i64 : (i64) -> !llvm.ptr
llvm.store %35, %37 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%38 = llvm.load %37 : !llvm.ptr -> i64
%39 = arith.cmpi slt, %38, %arg0 : i64
cf.cond_br %39, ^bb10, ^bb11
^bb10:
%40 = arith.constant 1 : i32
%41 = llvm.load %37 : !llvm.ptr -> i64
%42 = arith.trunci %40 : i32 to i8
%43 = llvm.getelementptr %0[%41] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %42, %43 : i8, !llvm.ptr
%44 = llvm.load %37 : !llvm.ptr -> i64
%45 = llvm.load %22 : !llvm.ptr -> i64
%46 = arith.addi %44, %45 : i64
llvm.store %46, %37 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%47 = llvm.load %22 : !llvm.ptr -> i64
%48 = arith.constant 1 : i32
%50 = arith.extsi %48 : i32 to i64
%49 = arith.addi %47, %50 : i64
llvm.store %49, %22 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%51 = arith.constant 0 : i32
%52 = arith.extsi %51 : i32 to i64
%53 = llvm.mlir.constant(1 : i64) : i64
%54 = llvm.alloca %53 x i64 : (i64) -> !llvm.ptr
llvm.store %52, %54 : i64, !llvm.ptr
%55 = arith.constant 2 : i32
%56 = arith.index_cast %55 : i32 to index
%57 = arith.index_cast %arg0 : i32 to index
%59 = arith.constant 1 : index
%60 = arith.constant -1 : index
%61 = arith.cmpi sle, %56, %57 : index
%58 = arith.select %61, %59, %60 : index
cf.br ^bb12(%56 : index)
^bb12(%62: index):
%63 = arith.cmpi slt, %62, %57 : index
%64 = arith.cmpi sgt, %62, %57 : index
%65 = arith.select %61, %63, %64 : i1
cf.cond_br %65, ^bb13(%62 : index), ^bb14(%62 : index)
^bb13(%66: index):
%68 = arith.index_cast %66 : index to i64
%69 = llvm.getelementptr %0[%68] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%67 = llvm.load %69 : !llvm.ptr -> i8
%70 = arith.constant 0 : i32
%72 = arith.extsi %67 : i8 to i32
%71 = arith.cmpi eq, %72, %70 : i32
cf.cond_br %71, ^bb15, ^bb16
^bb15:
%73 = llvm.load %54 : !llvm.ptr -> i64
%75 = arith.trunci %73 : i64 to i32
%76 = arith.index_cast %66 : index to i32
%74 = arith.addi %75, %76 : i32
%77 = arith.extsi %74 : i32 to i64
llvm.store %77, %54 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%78 = arith.addi %66, %58 : index
cf.br ^bb12(%78 : index)
^bb14(%79: index):
func.call @free(%0) : (!llvm.ptr) -> ()
%81 = llvm.load %54 : !llvm.ptr -> i64
func.return %81 : i64
}
func.func @main() -> i32 {
%82 = llvm.mlir.addressof @str_0 : !llvm.ptr
%84 = arith.constant 2000000 : i32
%85 = arith.extsi %84 : i32 to i64
%83 = func.call @sum_primes_below(%85) : (i64) -> i64
%86 = llvm.call @printf(%82, %83) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%87 = arith.constant 0 : i32
func.return %87 : i32
}
}