Problem 050

Prime below one million that can be written as the sum of the most consecutive primes.

Answer997651
Output997651
StatusPASS
Native helperno
Runtime0 ms
Peak memory3376 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n^2 / log n)
Space complexityO(n)O(n)
ApproachFlow solutionConsecutive prime sum search
VerdictUnknown

Flow source

# Project Euler 050
# Prime below one million that can be written as the sum of the most
# consecutive primes.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function main() -> i32 {
    let limit: i64 = 1000000
    let sieve: ptr<i8> = calloc(limit, 1)
    if sieve == null { return 1 }
    sieve[0] = 1
    sieve[1] = 1
    let mut p: i64 = 2
    while p * p < limit {
        if sieve[p] == 0 {
            let mut m: i64 = p * p
            while m < limit {
                sieve[m] = 1
                m = m + p
            }
        }
        p = p + 1
    }

    # collect primes
    let primes: ptr<i64> = calloc(80000, 8)
    if primes == null { free(sieve); return 1 }
    let mut pc: i32 = 0
    let mut i: i64 = 2
    while i < limit {
        if sieve[i] == 0 {
            primes[pc] = i
            pc = pc + 1
        }
        i = i + 1
    }

    # prefix sums
    let pref: ptr<i64> = calloc((pc + 1) as i64, 8)
    if pref == null { free(primes); free(sieve); return 1 }
    let mut j: i32 = 0
    while j < pc {
        pref[j + 1] = pref[j] + primes[j]
        j = j + 1
    }

    let mut best_len: i32 = 0
    let mut best_prime: i64 = 0
    let mut start: i32 = 0
    while start < pc {
        let mut end: i32 = start + best_len + 1
        while end <= pc {
            let s: i64 = pref[end] - pref[start]
            if s >= limit { break }
            let len: i32 = end - start
            if len > best_len && sieve[s] == 0 {
                best_len = len
                best_prime = s
            }
            end = end + 1
        }
        start = start + 1
    }

    printf("%lld\n", best_prime)
    free(pref)
    free(primes)
    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;
    int8_t* sieve = (int8_t*)(calloc(limit, 1));
    if (sieve == NULL) {
        return 1;
    }
    sieve[0] = 1;
    sieve[1] = 1;
    int64_t p = 2;
    while ((p * p) < limit) {
        if (sieve[p] == 0) {
            int64_t m = (p * p);
            while (m < limit) {
                sieve[m] = 1;
                m = (m + p);
            }
        }
        p = (p + 1);
    }
    int64_t* primes = (int64_t*)(calloc(80000, 8));
    if (primes == NULL) {
        free(sieve);
        return 1;
    }
    int32_t pc = 0;
    int64_t i = 2;
    while (i < limit) {
        if (sieve[i] == 0) {
            primes[pc] = i;
            pc = (pc + 1);
        }
        i = (i + 1);
    }
    int64_t* pref = (int64_t*)(calloc(((int64_t)((pc + 1))), 8));
    if (pref == NULL) {
        free(primes);
        free(sieve);
        return 1;
    }
    int32_t j = 0;
    while (j < pc) {
        pref[(j + 1)] = (pref[j] + primes[j]);
        j = (j + 1);
    }
    int32_t best_len = 0;
    int64_t best_prime = 0;
    int32_t start = 0;
    while (start < pc) {
        int32_t end = ((start + best_len) + 1);
        while (end <= pc) {
            int64_t s = (pref[end] - pref[start]);
            if (s >= limit) {
                break;
            }
            int32_t len = (end - start);
            if ((len > best_len && sieve[s] == 0)) {
                best_len = len;
                best_prime = s;
            }
            end = (end + 1);
        }
        start = (start + 1);
    }
    printf("%lld\n", best_prime);
    free(pref);
    free(primes);
    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
    %3 = arith.constant 1 : 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 1 : i32
    %9 = arith.constant 0 : i32
    %10 = arith.trunci %8 : i32 to i8
    %11 = arith.extsi %9 : i32 to i64
    %12 = llvm.getelementptr %2[%11] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %10, %12 : i8, !llvm.ptr
    %13 = arith.constant 1 : i32
    %14 = arith.constant 1 : i32
    %15 = arith.trunci %13 : i32 to i8
    %16 = arith.extsi %14 : i32 to i64
    %17 = llvm.getelementptr %2[%16] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %15, %17 : i8, !llvm.ptr
    %18 = arith.constant 2 : i32
    %19 = arith.extsi %18 : i32 to 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 ^bb3
    ^bb3:
    %22 = llvm.load %21 : !llvm.ptr -> i64
    %23 = llvm.load %21 : !llvm.ptr -> i64
    %24 = arith.muli %22, %23 : i64
    %25 = arith.cmpi slt, %24, %1 : i64
    cf.cond_br %25, ^bb4, ^bb5
    ^bb4:
      %27 = llvm.load %21 : !llvm.ptr -> i64
      %28 = llvm.getelementptr %2[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %26 = llvm.load %28 : !llvm.ptr -> i8
      %29 = arith.constant 0 : i32
      %31 = arith.extsi %26 : i8 to i32
      %30 = arith.cmpi eq, %31, %29 : i32
      cf.cond_br %30, ^bb6, ^bb7
      ^bb6:
        %32 = llvm.load %21 : !llvm.ptr -> i64
        %33 = llvm.load %21 : !llvm.ptr -> i64
        %34 = arith.muli %32, %33 : i64
        %35 = llvm.mlir.constant(1 : i64) : i64
        %36 = llvm.alloca %35 x i64 : (i64) -> !llvm.ptr
        llvm.store %34, %36 : i64, !llvm.ptr
        cf.br ^bb9
        ^bb9:
        %37 = llvm.load %36 : !llvm.ptr -> i64
        %38 = arith.cmpi slt, %37, %1 : i64
        cf.cond_br %38, ^bb10, ^bb11
        ^bb10:
          %39 = arith.constant 1 : i32
          %40 = llvm.load %36 : !llvm.ptr -> i64
          %41 = arith.trunci %39 : i32 to i8
          %42 = llvm.getelementptr %2[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %41, %42 : i8, !llvm.ptr
          %43 = llvm.load %36 : !llvm.ptr -> i64
          %44 = llvm.load %21 : !llvm.ptr -> i64
          %45 = arith.addi %43, %44 : i64
          llvm.store %45, %36 : i64, !llvm.ptr
          cf.br ^bb9
        ^bb11:
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %46 = llvm.load %21 : !llvm.ptr -> i64
      %47 = arith.constant 1 : i32
      %49 = arith.extsi %47 : i32 to i64
      %48 = arith.addi %46, %49 : i64
      llvm.store %48, %21 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %51 = arith.constant 80000 : i32
    %52 = arith.constant 8 : i32
    %53 = arith.extsi %51 : i32 to i64
    %54 = arith.extsi %52 : i32 to i64
    %50 = func.call @calloc(%53, %54) : (i64, i64) -> !llvm.ptr
    %55 = llvm.mlir.zero : !llvm.ptr
    %56 = llvm.icmp "eq" %50, %55 : !llvm.ptr
    cf.cond_br %56, ^bb12, ^bb13
    ^bb12:
      func.call @free(%2) : (!llvm.ptr) -> ()
      %58 = arith.constant 1 : i32
      func.return %58 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %59 = arith.constant 0 : i32
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i32 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i32, !llvm.ptr
    %62 = arith.constant 2 : i32
    %63 = arith.extsi %62 : i32 to i64
    %64 = llvm.mlir.constant(1 : i64) : i64
    %65 = llvm.alloca %64 x i64 : (i64) -> !llvm.ptr
    llvm.store %63, %65 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %66 = llvm.load %65 : !llvm.ptr -> i64
    %67 = arith.cmpi slt, %66, %1 : i64
    cf.cond_br %67, ^bb16, ^bb17
    ^bb16:
      %69 = llvm.load %65 : !llvm.ptr -> i64
      %70 = llvm.getelementptr %2[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %68 = llvm.load %70 : !llvm.ptr -> i8
      %71 = arith.constant 0 : i32
      %73 = arith.extsi %68 : i8 to i32
      %72 = arith.cmpi eq, %73, %71 : i32
      cf.cond_br %72, ^bb18, ^bb19
      ^bb18:
        %74 = llvm.load %65 : !llvm.ptr -> i64
        %75 = llvm.load %61 : !llvm.ptr -> i32
        %76 = arith.extsi %75 : i32 to i64
        %77 = llvm.getelementptr %50[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %74, %77 : i64, !llvm.ptr
        %78 = llvm.load %61 : !llvm.ptr -> i32
        %79 = arith.constant 1 : i32
        %80 = arith.addi %78, %79 : i32
        llvm.store %80, %61 : i32, !llvm.ptr
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %81 = llvm.load %65 : !llvm.ptr -> i64
      %82 = arith.constant 1 : i32
      %84 = arith.extsi %82 : i32 to i64
      %83 = arith.addi %81, %84 : i64
      llvm.store %83, %65 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %86 = llvm.load %61 : !llvm.ptr -> i32
    %87 = arith.constant 1 : i32
    %88 = arith.addi %86, %87 : i32
    %89 = arith.extsi %88 : i32 to i64
    %90 = arith.constant 8 : i32
    %91 = arith.extsi %90 : i32 to i64
    %85 = func.call @calloc(%89, %91) : (i64, i64) -> !llvm.ptr
    %92 = llvm.mlir.zero : !llvm.ptr
    %93 = llvm.icmp "eq" %85, %92 : !llvm.ptr
    cf.cond_br %93, ^bb21, ^bb22
    ^bb21:
      func.call @free(%50) : (!llvm.ptr) -> ()
      func.call @free(%2) : (!llvm.ptr) -> ()
      %96 = arith.constant 1 : i32
      func.return %96 : i32
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %97 = arith.constant 0 : i32
    %98 = llvm.mlir.constant(1 : i64) : i64
    %99 = llvm.alloca %98 x i32 : (i64) -> !llvm.ptr
    llvm.store %97, %99 : i32, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %100 = llvm.load %99 : !llvm.ptr -> i32
    %101 = llvm.load %61 : !llvm.ptr -> i32
    %102 = arith.cmpi slt, %100, %101 : i32
    cf.cond_br %102, ^bb25, ^bb26
    ^bb25:
      %104 = llvm.load %99 : !llvm.ptr -> i32
      %105 = arith.extsi %104 : i32 to i64
      %106 = llvm.getelementptr %85[%105] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %103 = llvm.load %106 : !llvm.ptr -> i64
      %108 = llvm.load %99 : !llvm.ptr -> i32
      %109 = arith.extsi %108 : i32 to i64
      %110 = llvm.getelementptr %50[%109] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %107 = llvm.load %110 : !llvm.ptr -> i64
      %111 = arith.addi %103, %107 : i64
      %112 = llvm.load %99 : !llvm.ptr -> i32
      %113 = arith.constant 1 : i32
      %114 = arith.addi %112, %113 : i32
      %115 = arith.extsi %114 : i32 to i64
      %116 = llvm.getelementptr %85[%115] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %111, %116 : i64, !llvm.ptr
      %117 = llvm.load %99 : !llvm.ptr -> i32
      %118 = arith.constant 1 : i32
      %119 = arith.addi %117, %118 : i32
      llvm.store %119, %99 : i32, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %120 = arith.constant 0 : i32
    %121 = llvm.mlir.constant(1 : i64) : i64
    %122 = llvm.alloca %121 x i32 : (i64) -> !llvm.ptr
    llvm.store %120, %122 : i32, !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
    %127 = arith.constant 0 : i32
    %128 = llvm.mlir.constant(1 : i64) : i64
    %129 = llvm.alloca %128 x i32 : (i64) -> !llvm.ptr
    llvm.store %127, %129 : i32, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %130 = llvm.load %129 : !llvm.ptr -> i32
    %131 = llvm.load %61 : !llvm.ptr -> i32
    %132 = arith.cmpi slt, %130, %131 : i32
    cf.cond_br %132, ^bb28, ^bb29
    ^bb28:
      %133 = llvm.load %129 : !llvm.ptr -> i32
      %134 = llvm.load %122 : !llvm.ptr -> i32
      %135 = arith.addi %133, %134 : i32
      %136 = arith.constant 1 : i32
      %137 = arith.addi %135, %136 : i32
      %138 = llvm.mlir.constant(1 : i64) : i64
      %139 = llvm.alloca %138 x i32 : (i64) -> !llvm.ptr
      llvm.store %137, %139 : i32, !llvm.ptr
      cf.br ^bb30
      ^bb30:
      %140 = llvm.load %139 : !llvm.ptr -> i32
      %141 = llvm.load %61 : !llvm.ptr -> i32
      %142 = arith.cmpi sle, %140, %141 : i32
      cf.cond_br %142, ^bb31, ^bb32
      ^bb31:
        %144 = llvm.load %139 : !llvm.ptr -> i32
        %145 = arith.extsi %144 : i32 to i64
        %146 = llvm.getelementptr %85[%145] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %143 = llvm.load %146 : !llvm.ptr -> i64
        %148 = llvm.load %129 : !llvm.ptr -> i32
        %149 = arith.extsi %148 : i32 to i64
        %150 = llvm.getelementptr %85[%149] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %147 = llvm.load %150 : !llvm.ptr -> i64
        %151 = arith.subi %143, %147 : i64
        %152 = arith.cmpi sge, %151, %1 : i64
        cf.cond_br %152, ^bb33, ^bb34
        ^bb33:
          cf.br ^bb32
        ^bb34:
          cf.br ^bb35
        ^bb35:
        %153 = llvm.load %139 : !llvm.ptr -> i32
        %154 = llvm.load %129 : !llvm.ptr -> i32
        %155 = arith.subi %153, %154 : i32
        %156 = llvm.load %122 : !llvm.ptr -> i32
        %157 = arith.cmpi sgt, %155, %156 : i32
        %158 = scf.if %157 -> (i1) {
          %160 = llvm.getelementptr %2[%151] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          %159 = llvm.load %160 : !llvm.ptr -> i8
          %161 = arith.constant 0 : i32
          %163 = arith.extsi %159 : i8 to i32
          %162 = arith.cmpi eq, %163, %161 : i32
          scf.yield %162 : i1
        } else {
          %164 = arith.constant false
          scf.yield %164 : i1
        }
        cf.cond_br %158, ^bb36, ^bb37
        ^bb36:
          llvm.store %155, %122 : i32, !llvm.ptr
          llvm.store %151, %126 : i64, !llvm.ptr
          cf.br ^bb38
        ^bb37:
          cf.br ^bb38
        ^bb38:
        %165 = llvm.load %139 : !llvm.ptr -> i32
        %166 = arith.constant 1 : i32
        %167 = arith.addi %165, %166 : i32
        llvm.store %167, %139 : i32, !llvm.ptr
        cf.br ^bb30
      ^bb32:
      %168 = llvm.load %129 : !llvm.ptr -> i32
      %169 = arith.constant 1 : i32
      %170 = arith.addi %168, %169 : i32
      llvm.store %170, %129 : i32, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %171 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %172 = llvm.load %126 : !llvm.ptr -> i64
    %173 = llvm.call @printf(%171, %172) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%85) : (!llvm.ptr) -> ()
    func.call @free(%50) : (!llvm.ptr) -> ()
    func.call @free(%2) : (!llvm.ptr) -> ()
    %177 = arith.constant 0 : i32
    func.return %177 : i32
  }
}