← All problems
Problem 313
Sliding game on prime squares: count primes p≤10^6 with search cost = p^2.
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 313
# Sliding game on prime squares: count primes p≤10^6 with search cost = p^2.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let limit: i64 = 1000000
let half: i64 = (limit >> 1) + 1
let sieve: ptr<i8> = calloc(half, 1)
if sieve == null { return 1 }
let mut i: i64 = 0
while i < half {
sieve[i] = 1
i = i + 1
}
if half > 0 { sieve[0] = 0 }
i = 1
while 2 * i * i < half {
if sieve[i] == 1 {
let mut current: i64 = 3 * i + 1
let step: i64 = 2 * i + 1
while current < half {
sieve[current] = 0
current = current + step
}
}
i = i + 1
}
let mut total: i64 = 0
# p = 2
# (4-1)/12 = 0 — skip; formula for odd primes
let mut p: i64 = 3
while p <= limit {
let odd: i64 = p >> 1
if sieve[odd] == 1 {
if p == 3 {
total = total + 2
} else {
total = total + (p * p - 1) / 12
}
}
p = p + 2
}
printf("%lld\n", total)
free(sieve)
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 = 1000000;
int64_t half = (FLOW_CHECKED_SHR((limit), (1)) + 1);
int8_t* sieve = (int8_t*)(calloc(half, 1));
if (sieve == NULL) {
return 1;
}
int64_t i = 0;
while (i < half) {
sieve[i] = 1;
i = (i + 1);
}
if (half > 0) {
sieve[0] = 0;
}
i = 1;
while (((2 * i) * i) < half) {
if (sieve[i] == 1) {
int64_t current = ((3 * i) + 1);
int64_t step = ((2 * i) + 1);
while (current < half) {
sieve[current] = 0;
current = (current + step);
}
}
i = (i + 1);
}
int64_t total = 0;
int64_t p = 3;
while (p <= limit) {
int64_t odd = FLOW_CHECKED_SHR((p), (1));
if (sieve[odd] == 1) {
if (p == 3) {
total = (total + 2);
} else {
total = (total + FLOW_CHECKED_DIV((((p * p) - 1)), (12)));
}
}
p = (p + 2);
}
printf("%lld\n", total);
free(sieve);
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 1000000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 1 : i32
%4 = arith.extsi %2 : i32 to i64
%3 = arith.shrsi %1, %4 : i64
%5 = arith.constant 1 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.addi %3, %7 : i64
%9 = arith.constant 1 : i32
%10 = arith.extsi %9 : i32 to i64
%8 = func.call @calloc(%6, %10) : (i64, i64) -> !llvm.ptr
%11 = llvm.mlir.zero : !llvm.ptr
%12 = llvm.icmp "eq" %8, %11 : !llvm.ptr
cf.cond_br %12, ^bb0, ^bb1
^bb0:
%13 = arith.constant 1 : i32
func.return %13 : i32
^bb1:
cf.br ^bb2
^bb2:
%14 = arith.constant 0 : i32
%15 = arith.extsi %14 : i32 to i64
%16 = llvm.mlir.constant(1 : i64) : i64
%17 = llvm.alloca %16 x i64 : (i64) -> !llvm.ptr
llvm.store %15, %17 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%18 = llvm.load %17 : !llvm.ptr -> i64
%19 = arith.cmpi slt, %18, %6 : i64
cf.cond_br %19, ^bb4, ^bb5
^bb4:
%20 = arith.constant 1 : i32
%21 = llvm.load %17 : !llvm.ptr -> i64
%22 = arith.trunci %20 : i32 to i8
%23 = llvm.getelementptr %8[%21] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %22, %23 : i8, !llvm.ptr
%24 = llvm.load %17 : !llvm.ptr -> i64
%25 = arith.constant 1 : i32
%27 = arith.extsi %25 : i32 to i64
%26 = arith.addi %24, %27 : i64
llvm.store %26, %17 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%28 = arith.constant 0 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.cmpi sgt, %6, %30 : i64
cf.cond_br %29, ^bb6, ^bb7
^bb6:
%31 = arith.constant 0 : i32
%32 = arith.constant 0 : i32
%33 = arith.trunci %31 : i32 to i8
%34 = arith.extsi %32 : i32 to i64
%35 = llvm.getelementptr %8[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %33, %35 : i8, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%36 = arith.constant 1 : i32
%37 = arith.extsi %36 : i32 to i64
llvm.store %37, %17 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%38 = arith.constant 2 : i32
%39 = llvm.load %17 : !llvm.ptr -> i64
%41 = arith.extsi %38 : i32 to i64
%40 = arith.muli %41, %39 : i64
%42 = llvm.load %17 : !llvm.ptr -> i64
%43 = arith.muli %40, %42 : i64
%44 = arith.cmpi slt, %43, %6 : i64
cf.cond_br %44, ^bb10, ^bb11
^bb10:
%46 = llvm.load %17 : !llvm.ptr -> i64
%47 = llvm.getelementptr %8[%46] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%45 = llvm.load %47 : !llvm.ptr -> i8
%48 = arith.constant 1 : i32
%50 = arith.extsi %45 : i8 to i32
%49 = arith.cmpi eq, %50, %48 : i32
cf.cond_br %49, ^bb12, ^bb13
^bb12:
%51 = arith.constant 3 : i32
%52 = llvm.load %17 : !llvm.ptr -> i64
%54 = arith.extsi %51 : i32 to i64
%53 = arith.muli %54, %52 : i64
%55 = arith.constant 1 : i32
%57 = arith.extsi %55 : i32 to i64
%56 = arith.addi %53, %57 : i64
%58 = llvm.mlir.constant(1 : i64) : i64
%59 = llvm.alloca %58 x i64 : (i64) -> !llvm.ptr
llvm.store %56, %59 : i64, !llvm.ptr
%60 = arith.constant 2 : i32
%61 = llvm.load %17 : !llvm.ptr -> i64
%63 = arith.extsi %60 : i32 to i64
%62 = arith.muli %63, %61 : i64
%64 = arith.constant 1 : i32
%66 = arith.extsi %64 : i32 to i64
%65 = arith.addi %62, %66 : i64
cf.br ^bb15
^bb15:
%67 = llvm.load %59 : !llvm.ptr -> i64
%68 = arith.cmpi slt, %67, %6 : i64
cf.cond_br %68, ^bb16, ^bb17
^bb16:
%69 = arith.constant 0 : i32
%70 = llvm.load %59 : !llvm.ptr -> i64
%71 = arith.trunci %69 : i32 to i8
%72 = llvm.getelementptr %8[%70] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %71, %72 : i8, !llvm.ptr
%73 = llvm.load %59 : !llvm.ptr -> i64
%74 = arith.addi %73, %65 : i64
llvm.store %74, %59 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%75 = llvm.load %17 : !llvm.ptr -> i64
%76 = arith.constant 1 : i32
%78 = arith.extsi %76 : i32 to i64
%77 = arith.addi %75, %78 : i64
llvm.store %77, %17 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%79 = arith.constant 0 : i32
%80 = arith.extsi %79 : i32 to i64
%81 = llvm.mlir.constant(1 : i64) : i64
%82 = llvm.alloca %81 x i64 : (i64) -> !llvm.ptr
llvm.store %80, %82 : i64, !llvm.ptr
%83 = arith.constant 3 : 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
cf.br ^bb18
^bb18:
%87 = llvm.load %86 : !llvm.ptr -> i64
%88 = arith.cmpi sle, %87, %1 : i64
cf.cond_br %88, ^bb19, ^bb20
^bb19:
%89 = llvm.load %86 : !llvm.ptr -> i64
%90 = arith.constant 1 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.shrsi %89, %92 : i64
%94 = llvm.getelementptr %8[%91] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%93 = llvm.load %94 : !llvm.ptr -> i8
%95 = arith.constant 1 : i32
%97 = arith.extsi %93 : i8 to i32
%96 = arith.cmpi eq, %97, %95 : i32
cf.cond_br %96, ^bb21, ^bb22
^bb21:
%98 = llvm.load %86 : !llvm.ptr -> i64
%99 = arith.constant 3 : i32
%101 = arith.extsi %99 : i32 to i64
%100 = arith.cmpi eq, %98, %101 : i64
cf.cond_br %100, ^bb24, ^bb25
^bb24:
%102 = llvm.load %82 : !llvm.ptr -> i64
%103 = arith.constant 2 : i32
%105 = arith.extsi %103 : i32 to i64
%104 = arith.addi %102, %105 : i64
llvm.store %104, %82 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
%106 = llvm.load %82 : !llvm.ptr -> i64
%107 = llvm.load %86 : !llvm.ptr -> i64
%108 = llvm.load %86 : !llvm.ptr -> i64
%109 = arith.muli %107, %108 : i64
%110 = arith.constant 1 : i32
%112 = arith.extsi %110 : i32 to i64
%111 = arith.subi %109, %112 : i64
%113 = arith.constant 12 : i32
%115 = arith.extsi %113 : i32 to i64
%114 = arith.divsi %111, %115 : i64
%116 = arith.addi %106, %114 : i64
llvm.store %116, %82 : i64, !llvm.ptr
cf.br ^bb26
^bb26:
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%117 = llvm.load %86 : !llvm.ptr -> i64
%118 = arith.constant 2 : i32
%120 = arith.extsi %118 : i32 to i64
%119 = arith.addi %117, %120 : i64
llvm.store %119, %86 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%121 = llvm.mlir.addressof @str_0 : !llvm.ptr
%122 = llvm.load %82 : !llvm.ptr -> i64
%123 = llvm.call @printf(%121, %122) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%8) : (!llvm.ptr) -> ()
%125 = arith.constant 0 : i32
func.return %125 : i32
}
}