← All problems
Problem 077
First value which can be written as a sum of primes in over 5000 ways.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n * m)
Space complexity O(n)O(n)
Approach Flow solution Dynamic programming or generating function
Verdict Unknown
Flow source
# Project Euler 077
# First value which can be written as a sum of primes in over 5000 ways.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let limit: i32 = 200
let sieve: ptr<i8> = calloc(limit as i64, 1)
let primes: ptr<i32> = calloc(limit as i64, 4)
let ways: ptr<i64> = calloc(limit as i64, 8)
if sieve == null || primes == null || ways == null { return 1 }
sieve[0] = 1
sieve[1] = 1
let mut p: i32 = 2
while p * p < limit {
if sieve[p] == 0 {
let mut m: i32 = p * p
while m < limit {
sieve[m] = 1
m = m + p
}
}
p = p + 1
}
let mut pc: i32 = 0
let mut i: i32 = 2
while i < limit {
if sieve[i] == 0 {
primes[pc] = i
pc = pc + 1
}
i = i + 1
}
ways[0] = 1
let mut pi: i32 = 0
while pi < pc {
let c: i32 = primes[pi]
let mut x: i32 = c
while x < limit {
ways[x] = ways[x] + ways[x - c]
x = x + 1
}
pi = pi + 1
}
let mut ans: i64 = 0
i = 2
while i < limit {
if ways[i] > 5000 {
ans = i as i64
break
}
i = i + 1
}
printf("%lld\n", ans)
free(sieve)
free(primes)
free(ways)
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) {
int32_t limit = 200;
int8_t* sieve = (int8_t*)(calloc(((int64_t)(limit)), 1));
int32_t* primes = (int32_t*)(calloc(((int64_t)(limit)), 4));
int64_t* ways = (int64_t*)(calloc(((int64_t)(limit)), 8));
if (((sieve == NULL || primes == NULL) || ways == NULL)) {
return 1;
}
sieve[0] = 1;
sieve[1] = 1;
int32_t p = 2;
while ((p * p) < limit) {
if (sieve[p] == 0) {
int32_t m = (p * p);
while (m < limit) {
sieve[m] = 1;
m = (m + p);
}
}
p = (p + 1);
}
int32_t pc = 0;
int32_t i = 2;
while (i < limit) {
if (sieve[i] == 0) {
primes[pc] = i;
pc = (pc + 1);
}
i = (i + 1);
}
ways[0] = 1;
int32_t pi = 0;
while (pi < pc) {
int32_t c = primes[pi];
int32_t x = c;
while (x < limit) {
ways[x] = (ways[x] + ways[(x - c)]);
x = (x + 1);
}
pi = (pi + 1);
}
int64_t ans = 0;
i = 2;
while (i < limit) {
if (ways[i] > 5000) {
ans = ((int64_t)(i));
break;
}
i = (i + 1);
}
printf("%lld\n", ans);
free(sieve);
free(primes);
free(ways);
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 200 : i32
%2 = arith.extsi %0 : i32 to i64
%3 = arith.constant 1 : i32
%4 = arith.extsi %3 : i32 to i64
%1 = func.call @calloc(%2, %4) : (i64, i64) -> !llvm.ptr
%6 = arith.extsi %0 : i32 to i64
%7 = arith.constant 4 : i32
%8 = arith.extsi %7 : i32 to i64
%5 = func.call @calloc(%6, %8) : (i64, i64) -> !llvm.ptr
%10 = arith.extsi %0 : i32 to i64
%11 = arith.constant 8 : i32
%12 = arith.extsi %11 : i32 to i64
%9 = func.call @calloc(%10, %12) : (i64, i64) -> !llvm.ptr
%13 = llvm.mlir.zero : !llvm.ptr
%14 = llvm.icmp "eq" %1, %13 : !llvm.ptr
%15 = scf.if %14 -> (i1) {
%16 = arith.constant true
scf.yield %16 : i1
} else {
%17 = llvm.mlir.zero : !llvm.ptr
%18 = llvm.icmp "eq" %5, %17 : !llvm.ptr
scf.yield %18 : i1
}
%19 = scf.if %15 -> (i1) {
%20 = arith.constant true
scf.yield %20 : i1
} else {
%21 = llvm.mlir.zero : !llvm.ptr
%22 = llvm.icmp "eq" %9, %21 : !llvm.ptr
scf.yield %22 : i1
}
cf.cond_br %19, ^bb0, ^bb1
^bb0:
%23 = arith.constant 1 : i32
func.return %23 : i32
^bb1:
cf.br ^bb2
^bb2:
%24 = arith.constant 1 : i32
%25 = arith.constant 0 : i32
%26 = arith.trunci %24 : i32 to i8
%27 = arith.extsi %25 : i32 to i64
%28 = llvm.getelementptr %1[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %26, %28 : i8, !llvm.ptr
%29 = arith.constant 1 : i32
%30 = arith.constant 1 : i32
%31 = arith.trunci %29 : i32 to i8
%32 = arith.extsi %30 : i32 to i64
%33 = llvm.getelementptr %1[%32] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %31, %33 : i8, !llvm.ptr
%34 = arith.constant 2 : i32
%35 = llvm.mlir.constant(1 : i64) : i64
%36 = llvm.alloca %35 x i32 : (i64) -> !llvm.ptr
llvm.store %34, %36 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%37 = llvm.load %36 : !llvm.ptr -> i32
%38 = llvm.load %36 : !llvm.ptr -> i32
%39 = arith.muli %37, %38 : i32
%40 = arith.cmpi slt, %39, %0 : i32
cf.cond_br %40, ^bb4, ^bb5
^bb4:
%42 = llvm.load %36 : !llvm.ptr -> i32
%43 = arith.extsi %42 : i32 to i64
%44 = llvm.getelementptr %1[%43] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%41 = llvm.load %44 : !llvm.ptr -> i8
%45 = arith.constant 0 : i32
%47 = arith.extsi %41 : i8 to i32
%46 = arith.cmpi eq, %47, %45 : i32
cf.cond_br %46, ^bb6, ^bb7
^bb6:
%48 = llvm.load %36 : !llvm.ptr -> i32
%49 = llvm.load %36 : !llvm.ptr -> i32
%50 = arith.muli %48, %49 : i32
%51 = llvm.mlir.constant(1 : i64) : i64
%52 = llvm.alloca %51 x i32 : (i64) -> !llvm.ptr
llvm.store %50, %52 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%53 = llvm.load %52 : !llvm.ptr -> i32
%54 = arith.cmpi slt, %53, %0 : i32
cf.cond_br %54, ^bb10, ^bb11
^bb10:
%55 = arith.constant 1 : i32
%56 = llvm.load %52 : !llvm.ptr -> i32
%57 = arith.trunci %55 : i32 to i8
%58 = arith.extsi %56 : i32 to i64
%59 = llvm.getelementptr %1[%58] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %57, %59 : i8, !llvm.ptr
%60 = llvm.load %52 : !llvm.ptr -> i32
%61 = llvm.load %36 : !llvm.ptr -> i32
%62 = arith.addi %60, %61 : i32
llvm.store %62, %52 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%63 = llvm.load %36 : !llvm.ptr -> i32
%64 = arith.constant 1 : i32
%65 = arith.addi %63, %64 : i32
llvm.store %65, %36 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%66 = arith.constant 0 : i32
%67 = llvm.mlir.constant(1 : i64) : i64
%68 = llvm.alloca %67 x i32 : (i64) -> !llvm.ptr
llvm.store %66, %68 : i32, !llvm.ptr
%69 = arith.constant 2 : i32
%70 = llvm.mlir.constant(1 : i64) : i64
%71 = llvm.alloca %70 x i32 : (i64) -> !llvm.ptr
llvm.store %69, %71 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%72 = llvm.load %71 : !llvm.ptr -> i32
%73 = arith.cmpi slt, %72, %0 : i32
cf.cond_br %73, ^bb13, ^bb14
^bb13:
%75 = llvm.load %71 : !llvm.ptr -> i32
%76 = arith.extsi %75 : i32 to i64
%77 = llvm.getelementptr %1[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%74 = llvm.load %77 : !llvm.ptr -> i8
%78 = arith.constant 0 : i32
%80 = arith.extsi %74 : i8 to i32
%79 = arith.cmpi eq, %80, %78 : i32
cf.cond_br %79, ^bb15, ^bb16
^bb15:
%81 = llvm.load %71 : !llvm.ptr -> i32
%82 = llvm.load %68 : !llvm.ptr -> i32
%83 = arith.extsi %82 : i32 to i64
%84 = llvm.getelementptr %5[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %81, %84 : i32, !llvm.ptr
%85 = llvm.load %68 : !llvm.ptr -> i32
%86 = arith.constant 1 : i32
%87 = arith.addi %85, %86 : i32
llvm.store %87, %68 : i32, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%88 = llvm.load %71 : !llvm.ptr -> i32
%89 = arith.constant 1 : i32
%90 = arith.addi %88, %89 : i32
llvm.store %90, %71 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%91 = arith.constant 1 : i32
%92 = arith.constant 0 : i32
%93 = arith.extsi %91 : i32 to i64
%94 = arith.extsi %92 : i32 to i64
%95 = llvm.getelementptr %9[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %93, %95 : i64, !llvm.ptr
%96 = arith.constant 0 : i32
%97 = llvm.mlir.constant(1 : i64) : i64
%98 = llvm.alloca %97 x i32 : (i64) -> !llvm.ptr
llvm.store %96, %98 : i32, !llvm.ptr
cf.br ^bb18
^bb18:
%99 = llvm.load %98 : !llvm.ptr -> i32
%100 = llvm.load %68 : !llvm.ptr -> i32
%101 = arith.cmpi slt, %99, %100 : i32
cf.cond_br %101, ^bb19, ^bb20
^bb19:
%103 = llvm.load %98 : !llvm.ptr -> i32
%104 = arith.extsi %103 : i32 to i64
%105 = llvm.getelementptr %5[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%102 = llvm.load %105 : !llvm.ptr -> i32
%106 = llvm.mlir.constant(1 : i64) : i64
%107 = llvm.alloca %106 x i32 : (i64) -> !llvm.ptr
llvm.store %102, %107 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%108 = llvm.load %107 : !llvm.ptr -> i32
%109 = arith.cmpi slt, %108, %0 : i32
cf.cond_br %109, ^bb22, ^bb23
^bb22:
%111 = llvm.load %107 : !llvm.ptr -> i32
%112 = arith.extsi %111 : i32 to i64
%113 = llvm.getelementptr %9[%112] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%110 = llvm.load %113 : !llvm.ptr -> i64
%115 = llvm.load %107 : !llvm.ptr -> i32
%116 = arith.subi %115, %102 : i32
%117 = arith.extsi %116 : i32 to i64
%118 = llvm.getelementptr %9[%117] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%114 = llvm.load %118 : !llvm.ptr -> i64
%119 = arith.addi %110, %114 : i64
%120 = llvm.load %107 : !llvm.ptr -> i32
%121 = arith.extsi %120 : i32 to i64
%122 = llvm.getelementptr %9[%121] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %119, %122 : i64, !llvm.ptr
%123 = llvm.load %107 : !llvm.ptr -> i32
%124 = arith.constant 1 : i32
%125 = arith.addi %123, %124 : i32
llvm.store %125, %107 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%126 = llvm.load %98 : !llvm.ptr -> i32
%127 = arith.constant 1 : i32
%128 = arith.addi %126, %127 : i32
llvm.store %128, %98 : i32, !llvm.ptr
cf.br ^bb18
^bb20:
%129 = arith.constant 0 : i32
%130 = arith.extsi %129 : i32 to i64
%131 = llvm.mlir.constant(1 : i64) : i64
%132 = llvm.alloca %131 x i64 : (i64) -> !llvm.ptr
llvm.store %130, %132 : i64, !llvm.ptr
%133 = arith.constant 2 : i32
llvm.store %133, %71 : i32, !llvm.ptr
cf.br ^bb24
^bb24:
%134 = llvm.load %71 : !llvm.ptr -> i32
%135 = arith.cmpi slt, %134, %0 : i32
cf.cond_br %135, ^bb25, ^bb26
^bb25:
%137 = llvm.load %71 : !llvm.ptr -> i32
%138 = arith.extsi %137 : i32 to i64
%139 = llvm.getelementptr %9[%138] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%136 = llvm.load %139 : !llvm.ptr -> i64
%140 = arith.constant 5000 : i32
%142 = arith.extsi %140 : i32 to i64
%141 = arith.cmpi sgt, %136, %142 : i64
cf.cond_br %141, ^bb27, ^bb28
^bb27:
%143 = llvm.load %71 : !llvm.ptr -> i32
%144 = arith.extsi %143 : i32 to i64
llvm.store %144, %132 : i64, !llvm.ptr
cf.br ^bb26
^bb28:
cf.br ^bb29
^bb29:
%145 = llvm.load %71 : !llvm.ptr -> i32
%146 = arith.constant 1 : i32
%147 = arith.addi %145, %146 : i32
llvm.store %147, %71 : i32, !llvm.ptr
cf.br ^bb24
^bb26:
%148 = llvm.mlir.addressof @str_0 : !llvm.ptr
%149 = llvm.load %132 : !llvm.ptr -> i64
%150 = llvm.call @printf(%148, %149) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%1) : (!llvm.ptr) -> ()
func.call @free(%5) : (!llvm.ptr) -> ()
func.call @free(%9) : (!llvm.ptr) -> ()
%154 = arith.constant 0 : i32
func.return %154 : i32
}
}