← All problems
Problem 047
First of four consecutive integers each with four distinct prime factors.
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 + distinct prime factor count
Verdict Suboptimal
Flow source
# Project Euler 047
# First of four consecutive integers each with four distinct prime factors.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let limit: i64 = 200000
let factors: ptr<i32> = calloc(limit, 4)
if factors == null { return 1 }
let mut i: i64 = 2
while i < limit {
if factors[i] == 0 {
# i is prime
let mut m: i64 = i
while m < limit {
factors[m] = factors[m] + 1
m = m + i
}
}
i = i + 1
}
let mut n: i64 = 2
while n + 3 < limit {
if factors[n] == 4 && factors[n + 1] == 4 && factors[n + 2] == 4 && factors[n + 3] == 4 {
printf("%lld\n", n)
free(factors)
return 0
}
n = n + 1
}
free(factors)
return 1
}
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 = 200000;
int32_t* factors = (int32_t*)(calloc(limit, 4));
if (factors == NULL) {
return 1;
}
int64_t i = 2;
while (i < limit) {
if (factors[i] == 0) {
int64_t m = i;
while (m < limit) {
factors[m] = (factors[m] + 1);
m = (m + i);
}
}
i = (i + 1);
}
int64_t n = 2;
while ((n + 3) < limit) {
if ((((factors[n] == 4 && factors[(n + 1)] == 4) && factors[(n + 2)] == 4) && factors[(n + 3)] == 4)) {
printf("%lld\n", n);
free(factors);
return 0;
}
n = (n + 1);
}
free(factors);
return 1;
}
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 200000 : i32
%1 = arith.extsi %0 : i32 to i64
%3 = arith.constant 4 : i32
%4 = arith.extsi %3 : i32 to i64
%2 = func.call @calloc(%1, %4) : (i64, i64) -> !llvm.ptr
%5 = llvm.mlir.zero : !llvm.ptr
%6 = llvm.icmp "eq" %2, %5 : !llvm.ptr
cf.cond_br %6, ^bb0, ^bb1
^bb0:
%7 = arith.constant 1 : i32
func.return %7 : i32
^bb1:
cf.br ^bb2
^bb2:
%8 = arith.constant 2 : i32
%9 = arith.extsi %8 : i32 to i64
%10 = llvm.mlir.constant(1 : i64) : i64
%11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
llvm.store %9, %11 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%12 = llvm.load %11 : !llvm.ptr -> i64
%13 = arith.cmpi slt, %12, %1 : i64
cf.cond_br %13, ^bb4, ^bb5
^bb4:
%15 = llvm.load %11 : !llvm.ptr -> i64
%16 = llvm.getelementptr %2[%15] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%14 = llvm.load %16 : !llvm.ptr -> i32
%17 = arith.constant 0 : i32
%18 = arith.cmpi eq, %14, %17 : i32
cf.cond_br %18, ^bb6, ^bb7
^bb6:
%19 = llvm.load %11 : !llvm.ptr -> i64
%20 = llvm.mlir.constant(1 : i64) : i64
%21 = llvm.alloca %20 x i64 : (i64) -> !llvm.ptr
llvm.store %19, %21 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%22 = llvm.load %21 : !llvm.ptr -> i64
%23 = arith.cmpi slt, %22, %1 : i64
cf.cond_br %23, ^bb10, ^bb11
^bb10:
%25 = llvm.load %21 : !llvm.ptr -> i64
%26 = llvm.getelementptr %2[%25] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%24 = llvm.load %26 : !llvm.ptr -> i32
%27 = arith.constant 1 : i32
%28 = arith.addi %24, %27 : i32
%29 = llvm.load %21 : !llvm.ptr -> i64
%30 = llvm.getelementptr %2[%29] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %28, %30 : i32, !llvm.ptr
%31 = llvm.load %21 : !llvm.ptr -> i64
%32 = llvm.load %11 : !llvm.ptr -> i64
%33 = arith.addi %31, %32 : i64
llvm.store %33, %21 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%34 = llvm.load %11 : !llvm.ptr -> i64
%35 = arith.constant 1 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.addi %34, %37 : i64
llvm.store %36, %11 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%38 = arith.constant 2 : i32
%39 = arith.extsi %38 : i32 to i64
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
llvm.store %39, %41 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%42 = llvm.load %41 : !llvm.ptr -> i64
%43 = arith.constant 3 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.addi %42, %45 : i64
%46 = arith.cmpi slt, %44, %1 : i64
cf.cond_br %46, ^bb13, ^bb14
^bb13:
%48 = llvm.load %41 : !llvm.ptr -> i64
%49 = llvm.getelementptr %2[%48] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%47 = llvm.load %49 : !llvm.ptr -> i32
%50 = arith.constant 4 : i32
%51 = arith.cmpi eq, %47, %50 : i32
%52 = scf.if %51 -> (i1) {
%54 = llvm.load %41 : !llvm.ptr -> i64
%55 = arith.constant 1 : i32
%57 = arith.extsi %55 : i32 to i64
%56 = arith.addi %54, %57 : i64
%58 = llvm.getelementptr %2[%56] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%53 = llvm.load %58 : !llvm.ptr -> i32
%59 = arith.constant 4 : i32
%60 = arith.cmpi eq, %53, %59 : i32
scf.yield %60 : i1
} else {
%61 = arith.constant false
scf.yield %61 : i1
}
%62 = scf.if %52 -> (i1) {
%64 = llvm.load %41 : !llvm.ptr -> i64
%65 = arith.constant 2 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.addi %64, %67 : i64
%68 = llvm.getelementptr %2[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%63 = llvm.load %68 : !llvm.ptr -> i32
%69 = arith.constant 4 : i32
%70 = arith.cmpi eq, %63, %69 : i32
scf.yield %70 : i1
} else {
%71 = arith.constant false
scf.yield %71 : i1
}
%72 = scf.if %62 -> (i1) {
%74 = llvm.load %41 : !llvm.ptr -> i64
%75 = arith.constant 3 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.addi %74, %77 : i64
%78 = llvm.getelementptr %2[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%73 = llvm.load %78 : !llvm.ptr -> i32
%79 = arith.constant 4 : i32
%80 = arith.cmpi eq, %73, %79 : i32
scf.yield %80 : i1
} else {
%81 = arith.constant false
scf.yield %81 : i1
}
cf.cond_br %72, ^bb15, ^bb16
^bb15:
%82 = llvm.mlir.addressof @str_0 : !llvm.ptr
%83 = llvm.load %41 : !llvm.ptr -> i64
%84 = llvm.call @printf(%82, %83) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%2) : (!llvm.ptr) -> ()
%86 = arith.constant 0 : i32
func.return %86 : i32
^bb16:
cf.br ^bb17
^bb17:
%87 = llvm.load %41 : !llvm.ptr -> i64
%88 = arith.constant 1 : i32
%90 = arith.extsi %88 : i32 to i64
%89 = arith.addi %87, %90 : i64
llvm.store %89, %41 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
func.call @free(%2) : (!llvm.ptr) -> ()
%92 = arith.constant 1 : i32
func.return %92 : i32
}
}