← All problems
Problem 087
How many numbers below fifty million are prime power triples (p1^2+p2^3+p3^4).
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 087
# How many numbers below fifty million are prime power triples (p1^2+p2^3+p3^4).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let limit: i64 = 50000000
let plim: i32 = 8000 # sqrt(50e6)≈7071
let sieve: ptr<i8> = calloc(plim as i64, 1)
let primes: ptr<i32> = calloc(plim as i64, 4)
if sieve == null || primes == null { return 1 }
sieve[0] = 1
sieve[1] = 1
let mut p: i32 = 2
while p * p < plim {
if sieve[p] == 0 {
let mut m: i32 = p * p
while m < plim {
sieve[m] = 1
m = m + p
}
}
p = p + 1
}
let mut pc: i32 = 0
let mut i: i32 = 2
while i < plim {
if sieve[i] == 0 {
primes[pc] = i
pc = pc + 1
}
i = i + 1
}
let seen: ptr<i8> = calloc(limit, 1)
if seen == null { return 1 }
let mut count: i64 = 0
let mut i4: i32 = 0
while i4 < pc {
let a: i64 = primes[i4] as i64
let p4: i64 = a * a * a * a
if p4 >= limit { break }
let mut i3: i32 = 0
while i3 < pc {
let b: i64 = primes[i3] as i64
let p3: i64 = b * b * b
if p4 + p3 >= limit { break }
let mut i2: i32 = 0
while i2 < pc {
let c: i64 = primes[i2] as i64
let p2: i64 = c * c
let v: i64 = p4 + p3 + p2
if v >= limit { break }
if seen[v] == 0 {
seen[v] = 1
count = count + 1
}
i2 = i2 + 1
}
i3 = i3 + 1
}
i4 = i4 + 1
}
printf("%lld\n", count)
free(sieve)
free(primes)
free(seen)
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 = 50000000;
int32_t plim = 8000;
int8_t* sieve = (int8_t*)(calloc(((int64_t)(plim)), 1));
int32_t* primes = (int32_t*)(calloc(((int64_t)(plim)), 4));
if ((sieve == NULL || primes == NULL)) {
return 1;
}
sieve[0] = 1;
sieve[1] = 1;
int32_t p = 2;
while ((p * p) < plim) {
if (sieve[p] == 0) {
int32_t m = (p * p);
while (m < plim) {
sieve[m] = 1;
m = (m + p);
}
}
p = (p + 1);
}
int32_t pc = 0;
int32_t i = 2;
while (i < plim) {
if (sieve[i] == 0) {
primes[pc] = i;
pc = (pc + 1);
}
i = (i + 1);
}
int8_t* seen = (int8_t*)(calloc(limit, 1));
if (seen == NULL) {
return 1;
}
int64_t count = 0;
int32_t i4 = 0;
while (i4 < pc) {
int64_t a = ((int64_t)(primes[i4]));
int64_t p4 = (((a * a) * a) * a);
if (p4 >= limit) {
break;
}
int32_t i3 = 0;
while (i3 < pc) {
int64_t b = ((int64_t)(primes[i3]));
int64_t p3 = ((b * b) * b);
if ((p4 + p3) >= limit) {
break;
}
int32_t i2 = 0;
while (i2 < pc) {
int64_t c = ((int64_t)(primes[i2]));
int64_t p2 = (c * c);
int64_t v = ((p4 + p3) + p2);
if (v >= limit) {
break;
}
if (seen[v] == 0) {
seen[v] = 1;
count = (count + 1);
}
i2 = (i2 + 1);
}
i3 = (i3 + 1);
}
i4 = (i4 + 1);
}
printf("%lld\n", count);
free(sieve);
free(primes);
free(seen);
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 50000000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 8000 : i32
%4 = arith.extsi %2 : i32 to i64
%5 = arith.constant 1 : i32
%6 = arith.extsi %5 : i32 to i64
%3 = func.call @calloc(%4, %6) : (i64, i64) -> !llvm.ptr
%8 = arith.extsi %2 : i32 to i64
%9 = arith.constant 4 : i32
%10 = arith.extsi %9 : i32 to i64
%7 = func.call @calloc(%8, %10) : (i64, i64) -> !llvm.ptr
%11 = llvm.mlir.zero : !llvm.ptr
%12 = llvm.icmp "eq" %3, %11 : !llvm.ptr
%13 = scf.if %12 -> (i1) {
%14 = arith.constant true
scf.yield %14 : i1
} else {
%15 = llvm.mlir.zero : !llvm.ptr
%16 = llvm.icmp "eq" %7, %15 : !llvm.ptr
scf.yield %16 : i1
}
cf.cond_br %13, ^bb0, ^bb1
^bb0:
%17 = arith.constant 1 : i32
func.return %17 : i32
^bb1:
cf.br ^bb2
^bb2:
%18 = arith.constant 1 : i32
%19 = arith.constant 0 : i32
%20 = arith.trunci %18 : i32 to i8
%21 = arith.extsi %19 : i32 to i64
%22 = llvm.getelementptr %3[%21] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %20, %22 : i8, !llvm.ptr
%23 = arith.constant 1 : i32
%24 = arith.constant 1 : i32
%25 = arith.trunci %23 : i32 to i8
%26 = arith.extsi %24 : i32 to i64
%27 = llvm.getelementptr %3[%26] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %25, %27 : i8, !llvm.ptr
%28 = arith.constant 2 : i32
%29 = llvm.mlir.constant(1 : i64) : i64
%30 = llvm.alloca %29 x i32 : (i64) -> !llvm.ptr
llvm.store %28, %30 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%31 = llvm.load %30 : !llvm.ptr -> i32
%32 = llvm.load %30 : !llvm.ptr -> i32
%33 = arith.muli %31, %32 : i32
%34 = arith.cmpi slt, %33, %2 : i32
cf.cond_br %34, ^bb4, ^bb5
^bb4:
%36 = llvm.load %30 : !llvm.ptr -> i32
%37 = arith.extsi %36 : i32 to i64
%38 = llvm.getelementptr %3[%37] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%35 = llvm.load %38 : !llvm.ptr -> i8
%39 = arith.constant 0 : i32
%41 = arith.extsi %35 : i8 to i32
%40 = arith.cmpi eq, %41, %39 : i32
cf.cond_br %40, ^bb6, ^bb7
^bb6:
%42 = llvm.load %30 : !llvm.ptr -> i32
%43 = llvm.load %30 : !llvm.ptr -> i32
%44 = arith.muli %42, %43 : i32
%45 = llvm.mlir.constant(1 : i64) : i64
%46 = llvm.alloca %45 x i32 : (i64) -> !llvm.ptr
llvm.store %44, %46 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%47 = llvm.load %46 : !llvm.ptr -> i32
%48 = arith.cmpi slt, %47, %2 : i32
cf.cond_br %48, ^bb10, ^bb11
^bb10:
%49 = arith.constant 1 : i32
%50 = llvm.load %46 : !llvm.ptr -> i32
%51 = arith.trunci %49 : i32 to i8
%52 = arith.extsi %50 : i32 to i64
%53 = llvm.getelementptr %3[%52] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %51, %53 : i8, !llvm.ptr
%54 = llvm.load %46 : !llvm.ptr -> i32
%55 = llvm.load %30 : !llvm.ptr -> i32
%56 = arith.addi %54, %55 : i32
llvm.store %56, %46 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%57 = llvm.load %30 : !llvm.ptr -> i32
%58 = arith.constant 1 : i32
%59 = arith.addi %57, %58 : i32
llvm.store %59, %30 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%60 = arith.constant 0 : i32
%61 = llvm.mlir.constant(1 : i64) : i64
%62 = llvm.alloca %61 x i32 : (i64) -> !llvm.ptr
llvm.store %60, %62 : i32, !llvm.ptr
%63 = arith.constant 2 : i32
%64 = llvm.mlir.constant(1 : i64) : i64
%65 = llvm.alloca %64 x i32 : (i64) -> !llvm.ptr
llvm.store %63, %65 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%66 = llvm.load %65 : !llvm.ptr -> i32
%67 = arith.cmpi slt, %66, %2 : i32
cf.cond_br %67, ^bb13, ^bb14
^bb13:
%69 = llvm.load %65 : !llvm.ptr -> i32
%70 = arith.extsi %69 : i32 to i64
%71 = llvm.getelementptr %3[%70] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%68 = llvm.load %71 : !llvm.ptr -> i8
%72 = arith.constant 0 : i32
%74 = arith.extsi %68 : i8 to i32
%73 = arith.cmpi eq, %74, %72 : i32
cf.cond_br %73, ^bb15, ^bb16
^bb15:
%75 = llvm.load %65 : !llvm.ptr -> i32
%76 = llvm.load %62 : !llvm.ptr -> i32
%77 = arith.extsi %76 : i32 to i64
%78 = llvm.getelementptr %7[%77] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %75, %78 : i32, !llvm.ptr
%79 = llvm.load %62 : !llvm.ptr -> i32
%80 = arith.constant 1 : i32
%81 = arith.addi %79, %80 : i32
llvm.store %81, %62 : i32, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%82 = llvm.load %65 : !llvm.ptr -> i32
%83 = arith.constant 1 : i32
%84 = arith.addi %82, %83 : i32
llvm.store %84, %65 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%86 = arith.constant 1 : i32
%87 = arith.extsi %86 : i32 to i64
%85 = func.call @calloc(%1, %87) : (i64, i64) -> !llvm.ptr
%88 = llvm.mlir.zero : !llvm.ptr
%89 = llvm.icmp "eq" %85, %88 : !llvm.ptr
cf.cond_br %89, ^bb18, ^bb19
^bb18:
%90 = arith.constant 1 : i32
func.return %90 : i32
^bb19:
cf.br ^bb20
^bb20:
%91 = arith.constant 0 : i32
%92 = arith.extsi %91 : i32 to i64
%93 = llvm.mlir.constant(1 : i64) : i64
%94 = llvm.alloca %93 x i64 : (i64) -> !llvm.ptr
llvm.store %92, %94 : i64, !llvm.ptr
%95 = arith.constant 0 : i32
%96 = llvm.mlir.constant(1 : i64) : i64
%97 = llvm.alloca %96 x i32 : (i64) -> !llvm.ptr
llvm.store %95, %97 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%98 = llvm.load %97 : !llvm.ptr -> i32
%99 = llvm.load %62 : !llvm.ptr -> i32
%100 = arith.cmpi slt, %98, %99 : i32
cf.cond_br %100, ^bb22, ^bb23
^bb22:
%102 = llvm.load %97 : !llvm.ptr -> i32
%103 = arith.extsi %102 : i32 to i64
%104 = llvm.getelementptr %7[%103] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%101 = llvm.load %104 : !llvm.ptr -> i32
%105 = arith.extsi %101 : i32 to i64
%106 = arith.muli %105, %105 : i64
%107 = arith.muli %106, %105 : i64
%108 = arith.muli %107, %105 : i64
%109 = arith.cmpi sge, %108, %1 : i64
cf.cond_br %109, ^bb24, ^bb25
^bb24:
cf.br ^bb23
^bb25:
cf.br ^bb26
^bb26:
%110 = arith.constant 0 : i32
%111 = llvm.mlir.constant(1 : i64) : i64
%112 = llvm.alloca %111 x i32 : (i64) -> !llvm.ptr
llvm.store %110, %112 : i32, !llvm.ptr
cf.br ^bb27
^bb27:
%113 = llvm.load %112 : !llvm.ptr -> i32
%114 = llvm.load %62 : !llvm.ptr -> i32
%115 = arith.cmpi slt, %113, %114 : i32
cf.cond_br %115, ^bb28, ^bb29
^bb28:
%117 = llvm.load %112 : !llvm.ptr -> i32
%118 = arith.extsi %117 : i32 to i64
%119 = llvm.getelementptr %7[%118] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%116 = llvm.load %119 : !llvm.ptr -> i32
%120 = arith.extsi %116 : i32 to i64
%121 = arith.muli %120, %120 : i64
%122 = arith.muli %121, %120 : i64
%123 = arith.addi %108, %122 : i64
%124 = arith.cmpi sge, %123, %1 : i64
cf.cond_br %124, ^bb30, ^bb31
^bb30:
cf.br ^bb29
^bb31:
cf.br ^bb32
^bb32:
%125 = arith.constant 0 : i32
%126 = llvm.mlir.constant(1 : i64) : i64
%127 = llvm.alloca %126 x i32 : (i64) -> !llvm.ptr
llvm.store %125, %127 : i32, !llvm.ptr
cf.br ^bb33
^bb33:
%128 = llvm.load %127 : !llvm.ptr -> i32
%129 = llvm.load %62 : !llvm.ptr -> i32
%130 = arith.cmpi slt, %128, %129 : i32
cf.cond_br %130, ^bb34, ^bb35
^bb34:
%132 = llvm.load %127 : !llvm.ptr -> i32
%133 = arith.extsi %132 : i32 to i64
%134 = llvm.getelementptr %7[%133] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%131 = llvm.load %134 : !llvm.ptr -> i32
%135 = arith.extsi %131 : i32 to i64
%136 = arith.muli %135, %135 : i64
%137 = arith.addi %108, %122 : i64
%138 = arith.addi %137, %136 : i64
%139 = arith.cmpi sge, %138, %1 : i64
cf.cond_br %139, ^bb36, ^bb37
^bb36:
cf.br ^bb35
^bb37:
cf.br ^bb38
^bb38:
%141 = llvm.getelementptr %85[%138] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%140 = llvm.load %141 : !llvm.ptr -> i8
%142 = arith.constant 0 : i32
%144 = arith.extsi %140 : i8 to i32
%143 = arith.cmpi eq, %144, %142 : i32
cf.cond_br %143, ^bb39, ^bb40
^bb39:
%145 = arith.constant 1 : i32
%146 = arith.trunci %145 : i32 to i8
%147 = llvm.getelementptr %85[%138] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %146, %147 : i8, !llvm.ptr
%148 = llvm.load %94 : !llvm.ptr -> i64
%149 = arith.constant 1 : i32
%151 = arith.extsi %149 : i32 to i64
%150 = arith.addi %148, %151 : i64
llvm.store %150, %94 : i64, !llvm.ptr
cf.br ^bb41
^bb40:
cf.br ^bb41
^bb41:
%152 = llvm.load %127 : !llvm.ptr -> i32
%153 = arith.constant 1 : i32
%154 = arith.addi %152, %153 : i32
llvm.store %154, %127 : i32, !llvm.ptr
cf.br ^bb33
^bb35:
%155 = llvm.load %112 : !llvm.ptr -> i32
%156 = arith.constant 1 : i32
%157 = arith.addi %155, %156 : i32
llvm.store %157, %112 : i32, !llvm.ptr
cf.br ^bb27
^bb29:
%158 = llvm.load %97 : !llvm.ptr -> i32
%159 = arith.constant 1 : i32
%160 = arith.addi %158, %159 : i32
llvm.store %160, %97 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%161 = llvm.mlir.addressof @str_0 : !llvm.ptr
%162 = llvm.load %94 : !llvm.ptr -> i64
%163 = llvm.call @printf(%161, %162) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%3) : (!llvm.ptr) -> ()
func.call @free(%7) : (!llvm.ptr) -> ()
func.call @free(%85) : (!llvm.ptr) -> ()
%167 = arith.constant 0 : i32
func.return %167 : i32
}
}