← All problems
Problem 203
Sum of distinct squarefree numbers in first 51 Pascal rows.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^3)O(n log log n)
Space complexity O(n)O(n)
Approach Flow solution Mobius sieve
Verdict Suboptimal
Flow source
# Project Euler 203
# Sum of distinct squarefree numbers in first 51 Pascal rows.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function is_squarefree(x: i64, max_p: i64) -> bool {
let mut p: i64 = 2
while p <= max_p {
if x % (p * p) == 0 { return false }
p = p + 1
}
return true
}
function main() -> i32 {
let ROWS: i32 = 51
let vals: ptr<i64> = calloc(2000, 8)
let mut vc: i64 = 0
vals[0] = 1
vc = 1
let cur: ptr<i64> = calloc(ROWS + 2, 8)
let nxt: ptr<i64> = calloc(ROWS + 2, 8)
if cur == null || nxt == null || vals == null { return 1 }
cur[0] = 1
let mut len: i32 = 1
let mut row: i32 = 1
while row < ROWS {
nxt[0] = 1
let mut col: i32 = 1
while col < len {
nxt[col] = cur[col - 1] + cur[col]
col = col + 1
}
nxt[len] = 1
len = len + 1
col = 1
while col <= len / 2 {
let x: i64 = nxt[col]
if is_squarefree(x, ROWS as i64) {
let mut found: bool = false
let mut k: i64 = 0
while k < vc {
if vals[k] == x { found = true; break }
k = k + 1
}
if !found {
vals[vc] = x
vc = vc + 1
}
}
col = col + 1
}
col = 0
while col < len {
cur[col] = nxt[col]
col = col + 1
}
row = row + 1
}
let mut total: i64 = 0
let mut k: i64 = 0
while k < vc {
total = total + vals[k]
k = k + 1
}
printf("%lld\n", total)
free(vals); free(cur); free(nxt)
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; }
bool is_squarefree_i64_i64(int64_t x, int64_t max_p);
int32_t main(void);
bool is_squarefree_i64_i64(int64_t x, int64_t max_p) {
int64_t p = 2;
while (p <= max_p) {
if (FLOW_CHECKED_MOD((x), ((p * p))) == 0) {
return 0;
}
p = (p + 1);
}
return 1;
}
int32_t main(void) {
int32_t ROWS = 51;
int64_t* vals = (int64_t*)(calloc(2000, 8));
int64_t vc = 0;
vals[0] = 1;
vc = 1;
int64_t* cur = (int64_t*)(calloc((ROWS + 2), 8));
int64_t* nxt = (int64_t*)(calloc((ROWS + 2), 8));
if (((cur == NULL || nxt == NULL) || vals == NULL)) {
return 1;
}
cur[0] = 1;
int32_t len = 1;
int32_t row = 1;
while (row < ROWS) {
nxt[0] = 1;
int32_t col = 1;
while (col < len) {
nxt[col] = (cur[(col - 1)] + cur[col]);
col = (col + 1);
}
nxt[len] = 1;
len = (len + 1);
col = 1;
while (col <= FLOW_CHECKED_DIV((len), (2))) {
int64_t x = nxt[col];
if (is_squarefree_i64_i64(x, ((int64_t)(ROWS)))) {
bool found = 0;
int64_t k = 0;
while (k < vc) {
if (vals[k] == x) {
found = 1;
break;
}
k = (k + 1);
}
if ((!(found))) {
vals[vc] = x;
vc = (vc + 1);
}
}
col = (col + 1);
}
col = 0;
while (col < len) {
cur[col] = nxt[col];
col = (col + 1);
}
row = (row + 1);
}
int64_t total = 0;
int64_t k = 0;
while (k < vc) {
total = (total + vals[k]);
k = (k + 1);
}
printf("%lld\n", total);
free(vals);
free(cur);
free(nxt);
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 @is_squarefree(%arg0: i64, %arg1: i64) -> i1 {
%0 = arith.constant 2 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%4 = llvm.load %3 : !llvm.ptr -> i64
%5 = arith.cmpi sle, %4, %arg1 : i64
cf.cond_br %5, ^bb1, ^bb2
^bb1:
%6 = llvm.load %3 : !llvm.ptr -> i64
%7 = llvm.load %3 : !llvm.ptr -> i64
%8 = arith.muli %6, %7 : i64
%9 = arith.remsi %arg0, %8 : i64
%10 = arith.constant 0 : i32
%12 = arith.extsi %10 : i32 to i64
%11 = arith.cmpi eq, %9, %12 : i64
cf.cond_br %11, ^bb3, ^bb4
^bb3:
%13 = arith.constant 0 : i1
func.return %13 : i1
^bb4:
cf.br ^bb5
^bb5:
%14 = llvm.load %3 : !llvm.ptr -> i64
%15 = arith.constant 1 : i32
%17 = arith.extsi %15 : i32 to i64
%16 = arith.addi %14, %17 : i64
llvm.store %16, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%18 = arith.constant 1 : i1
func.return %18 : i1
}
func.func @main() -> i32 {
%19 = arith.constant 51 : i32
%21 = arith.constant 2000 : i32
%22 = arith.constant 8 : i32
%23 = arith.extsi %21 : i32 to i64
%24 = arith.extsi %22 : i32 to i64
%20 = func.call @calloc(%23, %24) : (i64, i64) -> !llvm.ptr
%25 = arith.constant 0 : i32
%26 = arith.extsi %25 : i32 to i64
%27 = llvm.mlir.constant(1 : i64) : i64
%28 = llvm.alloca %27 x i64 : (i64) -> !llvm.ptr
llvm.store %26, %28 : i64, !llvm.ptr
%29 = arith.constant 1 : i32
%30 = arith.constant 0 : i32
%31 = arith.extsi %29 : i32 to i64
%32 = arith.extsi %30 : i32 to i64
%33 = llvm.getelementptr %20[%32] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %31, %33 : i64, !llvm.ptr
%34 = arith.constant 1 : i32
%35 = arith.extsi %34 : i32 to i64
llvm.store %35, %28 : i64, !llvm.ptr
%37 = arith.constant 2 : i32
%38 = arith.addi %19, %37 : i32
%39 = arith.constant 8 : i32
%40 = arith.extsi %38 : i32 to i64
%41 = arith.extsi %39 : i32 to i64
%36 = func.call @calloc(%40, %41) : (i64, i64) -> !llvm.ptr
%43 = arith.constant 2 : i32
%44 = arith.addi %19, %43 : i32
%45 = arith.constant 8 : i32
%46 = arith.extsi %44 : i32 to i64
%47 = arith.extsi %45 : i32 to i64
%42 = func.call @calloc(%46, %47) : (i64, i64) -> !llvm.ptr
%48 = llvm.mlir.zero : !llvm.ptr
%49 = llvm.icmp "eq" %36, %48 : !llvm.ptr
%50 = scf.if %49 -> (i1) {
%51 = arith.constant true
scf.yield %51 : i1
} else {
%52 = llvm.mlir.zero : !llvm.ptr
%53 = llvm.icmp "eq" %42, %52 : !llvm.ptr
scf.yield %53 : i1
}
%54 = scf.if %50 -> (i1) {
%55 = arith.constant true
scf.yield %55 : i1
} else {
%56 = llvm.mlir.zero : !llvm.ptr
%57 = llvm.icmp "eq" %20, %56 : !llvm.ptr
scf.yield %57 : i1
}
cf.cond_br %54, ^bb6, ^bb7
^bb6:
%58 = arith.constant 1 : i32
func.return %58 : i32
^bb7:
cf.br ^bb8
^bb8:
%59 = arith.constant 1 : i32
%60 = arith.constant 0 : i32
%61 = arith.extsi %59 : i32 to i64
%62 = arith.extsi %60 : i32 to i64
%63 = llvm.getelementptr %36[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %61, %63 : i64, !llvm.ptr
%64 = arith.constant 1 : i32
%65 = llvm.mlir.constant(1 : i64) : i64
%66 = llvm.alloca %65 x i32 : (i64) -> !llvm.ptr
llvm.store %64, %66 : i32, !llvm.ptr
%67 = arith.constant 1 : i32
%68 = llvm.mlir.constant(1 : i64) : i64
%69 = llvm.alloca %68 x i32 : (i64) -> !llvm.ptr
llvm.store %67, %69 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%70 = llvm.load %69 : !llvm.ptr -> i32
%71 = arith.cmpi slt, %70, %19 : i32
cf.cond_br %71, ^bb10, ^bb11
^bb10:
%72 = arith.constant 1 : i32
%73 = arith.constant 0 : i32
%74 = arith.extsi %72 : i32 to i64
%75 = arith.extsi %73 : i32 to i64
%76 = llvm.getelementptr %42[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %74, %76 : i64, !llvm.ptr
%77 = arith.constant 1 : i32
%78 = llvm.mlir.constant(1 : i64) : i64
%79 = llvm.alloca %78 x i32 : (i64) -> !llvm.ptr
llvm.store %77, %79 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%80 = llvm.load %79 : !llvm.ptr -> i32
%81 = llvm.load %66 : !llvm.ptr -> i32
%82 = arith.cmpi slt, %80, %81 : i32
cf.cond_br %82, ^bb13, ^bb14
^bb13:
%84 = llvm.load %79 : !llvm.ptr -> i32
%85 = arith.constant 1 : i32
%86 = arith.subi %84, %85 : i32
%87 = arith.extsi %86 : i32 to i64
%88 = llvm.getelementptr %36[%87] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%83 = llvm.load %88 : !llvm.ptr -> i64
%90 = llvm.load %79 : !llvm.ptr -> i32
%91 = arith.extsi %90 : i32 to i64
%92 = llvm.getelementptr %36[%91] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%89 = llvm.load %92 : !llvm.ptr -> i64
%93 = arith.addi %83, %89 : i64
%94 = llvm.load %79 : !llvm.ptr -> i32
%95 = arith.extsi %94 : i32 to i64
%96 = llvm.getelementptr %42[%95] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %93, %96 : i64, !llvm.ptr
%97 = llvm.load %79 : !llvm.ptr -> i32
%98 = arith.constant 1 : i32
%99 = arith.addi %97, %98 : i32
llvm.store %99, %79 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%100 = arith.constant 1 : i32
%101 = llvm.load %66 : !llvm.ptr -> i32
%102 = arith.extsi %100 : i32 to i64
%103 = arith.extsi %101 : i32 to i64
%104 = llvm.getelementptr %42[%103] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %102, %104 : i64, !llvm.ptr
%105 = llvm.load %66 : !llvm.ptr -> i32
%106 = arith.constant 1 : i32
%107 = arith.addi %105, %106 : i32
llvm.store %107, %66 : i32, !llvm.ptr
%108 = arith.constant 1 : i32
llvm.store %108, %79 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%109 = llvm.load %79 : !llvm.ptr -> i32
%110 = llvm.load %66 : !llvm.ptr -> i32
%111 = arith.constant 2 : i32
%112 = arith.divsi %110, %111 : i32
%113 = arith.cmpi sle, %109, %112 : i32
cf.cond_br %113, ^bb16, ^bb17
^bb16:
%115 = llvm.load %79 : !llvm.ptr -> i32
%116 = arith.extsi %115 : i32 to i64
%117 = llvm.getelementptr %42[%116] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%114 = llvm.load %117 : !llvm.ptr -> i64
%119 = arith.extsi %19 : i32 to i64
%118 = func.call @is_squarefree(%114, %119) : (i64, i64) -> i1
cf.cond_br %118, ^bb18, ^bb19
^bb18:
%120 = arith.constant 0 : i1
%121 = llvm.mlir.constant(1 : i64) : i64
%122 = llvm.alloca %121 x i1 : (i64) -> !llvm.ptr
llvm.store %120, %122 : i1, !llvm.ptr
%123 = arith.constant 0 : i32
%124 = arith.extsi %123 : i32 to i64
%125 = llvm.mlir.constant(1 : i64) : i64
%126 = llvm.alloca %125 x i64 : (i64) -> !llvm.ptr
llvm.store %124, %126 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%127 = llvm.load %126 : !llvm.ptr -> i64
%128 = llvm.load %28 : !llvm.ptr -> i64
%129 = arith.cmpi slt, %127, %128 : i64
cf.cond_br %129, ^bb22, ^bb23
^bb22:
%131 = llvm.load %126 : !llvm.ptr -> i64
%132 = llvm.getelementptr %20[%131] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%130 = llvm.load %132 : !llvm.ptr -> i64
%133 = arith.cmpi eq, %130, %114 : i64
cf.cond_br %133, ^bb24, ^bb25
^bb24:
%134 = arith.constant 1 : i1
llvm.store %134, %122 : i1, !llvm.ptr
cf.br ^bb23
^bb25:
cf.br ^bb26
^bb26:
%135 = llvm.load %126 : !llvm.ptr -> i64
%136 = arith.constant 1 : i32
%138 = arith.extsi %136 : i32 to i64
%137 = arith.addi %135, %138 : i64
llvm.store %137, %126 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%139 = llvm.load %122 : !llvm.ptr -> i1
%141 = arith.constant 1 : i1
%140 = arith.xori %139, %141 : i1
cf.cond_br %140, ^bb27, ^bb28
^bb27:
%143 = llvm.load %28 : !llvm.ptr -> i64
%144 = llvm.getelementptr %20[%143] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %114, %144 : i64, !llvm.ptr
%145 = llvm.load %28 : !llvm.ptr -> i64
%146 = arith.constant 1 : i32
%148 = arith.extsi %146 : i32 to i64
%147 = arith.addi %145, %148 : i64
llvm.store %147, %28 : i64, !llvm.ptr
cf.br ^bb29
^bb28:
cf.br ^bb29
^bb29:
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%149 = llvm.load %79 : !llvm.ptr -> i32
%150 = arith.constant 1 : i32
%151 = arith.addi %149, %150 : i32
llvm.store %151, %79 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%152 = arith.constant 0 : i32
llvm.store %152, %79 : i32, !llvm.ptr
cf.br ^bb30
^bb30:
%153 = llvm.load %79 : !llvm.ptr -> i32
%154 = llvm.load %66 : !llvm.ptr -> i32
%155 = arith.cmpi slt, %153, %154 : i32
cf.cond_br %155, ^bb31, ^bb32
^bb31:
%157 = llvm.load %79 : !llvm.ptr -> i32
%158 = arith.extsi %157 : i32 to i64
%159 = llvm.getelementptr %42[%158] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%156 = llvm.load %159 : !llvm.ptr -> i64
%160 = llvm.load %79 : !llvm.ptr -> i32
%161 = arith.extsi %160 : i32 to i64
%162 = llvm.getelementptr %36[%161] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %156, %162 : i64, !llvm.ptr
%163 = llvm.load %79 : !llvm.ptr -> i32
%164 = arith.constant 1 : i32
%165 = arith.addi %163, %164 : i32
llvm.store %165, %79 : i32, !llvm.ptr
cf.br ^bb30
^bb32:
%166 = llvm.load %69 : !llvm.ptr -> i32
%167 = arith.constant 1 : i32
%168 = arith.addi %166, %167 : i32
llvm.store %168, %69 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
%169 = arith.constant 0 : i32
%170 = arith.extsi %169 : i32 to i64
%171 = llvm.mlir.constant(1 : i64) : i64
%172 = llvm.alloca %171 x i64 : (i64) -> !llvm.ptr
llvm.store %170, %172 : i64, !llvm.ptr
%173 = arith.constant 0 : i32
%174 = arith.extsi %173 : i32 to i64
%175 = llvm.mlir.constant(1 : i64) : i64
%176 = llvm.alloca %175 x i64 : (i64) -> !llvm.ptr
llvm.store %174, %176 : i64, !llvm.ptr
cf.br ^bb33
^bb33:
%177 = llvm.load %176 : !llvm.ptr -> i64
%178 = llvm.load %28 : !llvm.ptr -> i64
%179 = arith.cmpi slt, %177, %178 : i64
cf.cond_br %179, ^bb34, ^bb35
^bb34:
%180 = llvm.load %172 : !llvm.ptr -> i64
%182 = llvm.load %176 : !llvm.ptr -> i64
%183 = llvm.getelementptr %20[%182] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%181 = llvm.load %183 : !llvm.ptr -> i64
%184 = arith.addi %180, %181 : i64
llvm.store %184, %172 : i64, !llvm.ptr
%185 = llvm.load %176 : !llvm.ptr -> i64
%186 = arith.constant 1 : i32
%188 = arith.extsi %186 : i32 to i64
%187 = arith.addi %185, %188 : i64
llvm.store %187, %176 : i64, !llvm.ptr
cf.br ^bb33
^bb35:
%189 = llvm.mlir.addressof @str_0 : !llvm.ptr
%190 = llvm.load %172 : !llvm.ptr -> i64
%191 = llvm.call @printf(%189, %190) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%20) : (!llvm.ptr) -> ()
func.call @free(%36) : (!llvm.ptr) -> ()
func.call @free(%42) : (!llvm.ptr) -> ()
%195 = arith.constant 0 : i32
func.return %195 : i32
}
}