Problem 874

Maximal Prime Score M(k, n): choose a_i in [0, k) with sum(a_i) divisible by k, maximize sum(p(a_i)). Strategy: start from all a_i = k-1, find minimal reduction r = (-n) % k, then minimize prime-score loss via unbounded knapsack.

Answer4992775389
Output4992775389
StatusPASS
Native helperno
Runtime0 ms
Peak memory1360 KB
Time complexityO(1) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(1)O(n log n)
Space complexityO(1)O(n)
ApproachFlow solutionSearch with pruning or sieve
VerdictOptimal

Flow source

# Project Euler 874
# Maximal Prime Score
# M(k, n): choose a_i in [0, k) with sum(a_i) divisible by k, maximize sum(p(a_i)).
# Strategy: start from all a_i = k-1, find minimal reduction r = (-n) % k,
# then minimize prime-score loss via unbounded knapsack.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
    function malloc(n: i64) -> ptr<void>
    function memset(p: ptr<void>, c: i32, n: i64) -> ptr<void>
    function log(x: f64) -> f64
}

function first_n_primes(n: i64) -> ptr<i32> {
    if n <= 0 { return null }
    let mut limit: i64 = 0
    if n < 6 {
        limit = 15
    } else {
        let nn: f64 = n as f64
        limit = (nn * (log(nn) + log(log(nn))) + 10.0) as i64
    }

    let mut found: bool = false
    let mut primes: ptr<i32> = null
    while !found {
        let sieve: ptr<i8> = malloc(limit + 1)
        memset(sieve, 1, limit + 1)
        sieve[0] = 0
        sieve[1] = 0
        let mut i: i64 = 2
        while i * i <= limit {
            if sieve[i] != 0 {
                let mut j: i64 = i * i
                while j <= limit {
                    sieve[j] = 0
                    j = j + i
                }
            }
            i = i + 1
        }
        let mut count: i64 = 0
        i = 2
        while i <= limit {
            if sieve[i] != 0 { count = count + 1 }
            i = i + 1
        }
        if count >= n {
            primes = malloc(n * 4)
            let mut idx: i64 = 0
            i = 2
            while i <= limit && idx < n {
                if sieve[i] != 0 {
                    primes[idx] = i as i32
                    idx = idx + 1
                }
                i = i + 1
            }
            free(sieve)
            found = true
        } else {
            free(sieve)
            limit = limit * 2
        }
    }
    return primes
}

function min_prime_loss(primes: ptr<i32>, k: i64, reduction: i64) -> i64 {
    if reduction == 0 { return 0 }
    let m: i64 = k - 1
    let top: i64 = primes[m] as i64

    # loss[d] = top - primes[m - d] for d = 1..reduction
    let loss: ptr<i64> = malloc((reduction + 1) * 8)
    let mut d: i64 = 1
    while d <= reduction {
        loss[d] = top - (primes[m - d] as i64)
        d = d + 1
    }

    # dp[s] = minimal loss to achieve sum s
    let INF: i64 = 1000000000000000000
    let dp: ptr<i64> = malloc((reduction + 1) * 8)
    dp[0] = 0
    let mut s: i64 = 1
    while s <= reduction {
        dp[s] = INF
        s = s + 1
    }

    d = 1
    while d <= reduction {
        let c: i64 = loss[d]
        s = d
        while s <= reduction {
            let cand: i64 = dp[s - d] + c
            if cand < dp[s] {
                dp[s] = cand
            }
            s = s + 1
        }
        d = d + 1
    }

    let result: i64 = dp[reduction]
    free(loss)
    free(dp)
    return result
}

function maximal_prime_score(k: i64, n: i64, primes: ptr<i32>) -> i64 {
    if k == 1 { return n * 2 }
    let top: i64 = primes[k - 1] as i64
    # r = ((-n) % k + k) % k
    let mut r: i64 = (0 - n) % k
    if r < 0 { r = r + k }
    let loss: i64 = min_prime_loss(primes, k, r)
    return n * top - loss
}

function main() -> i32 {
    let k: i64 = 7000
    let primes_7001: ptr<i32> = first_n_primes(k + 1)
    let n: i64 = primes_7001[k] as i64
    let ans: i64 = maximal_prime_score(k, n, primes_7001)
    printf("%lld\n", ans)
    free(primes_7001)
    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* first_n_primes_i64(int64_t n);
int64_t min_prime_loss_ptr_i32_i64_i64(int32_t* primes, int64_t k, int64_t reduction);
int64_t maximal_prime_score_i64_i64_ptr_i32(int64_t k, int64_t n, int32_t* primes);
int32_t main(void);






int32_t* first_n_primes_i64(int64_t n) {
    if (n <= 0) {
        return NULL;
    }
    int64_t limit = 0;
    if (n < 6) {
        limit = 15;
    } else {
        double nn = ((double)(n));
        limit = ((int64_t)(((nn * (log(nn) + log(log(nn)))) + 10.0)));
    }
    bool found = 0;
    int32_t* primes = (int32_t*)(NULL);
    while ((!(found))) {
        int8_t* sieve = (int8_t*)(malloc((limit + 1)));
        memset(sieve, 1, (limit + 1));
        sieve[0] = 0;
        sieve[1] = 0;
        int64_t i = 2;
        while ((i * i) <= limit) {
            if (sieve[i] != 0) {
                int64_t j = (i * i);
                while (j <= limit) {
                    sieve[j] = 0;
                    j = (j + i);
                }
            }
            i = (i + 1);
        }
        int64_t count = 0;
        i = 2;
        while (i <= limit) {
            if (sieve[i] != 0) {
                count = (count + 1);
            }
            i = (i + 1);
        }
        if (count >= n) {
            primes = malloc((n * 4));
            int64_t idx = 0;
            i = 2;
            while ((i <= limit && idx < n)) {
                if (sieve[i] != 0) {
                    primes[idx] = ((int32_t)(i));
                    idx = (idx + 1);
                }
                i = (i + 1);
            }
            free(sieve);
            found = 1;
        } else {
            free(sieve);
            limit = (limit * 2);
        }
    }
    return primes;
}

int64_t min_prime_loss_ptr_i32_i64_i64(int32_t* primes, int64_t k, int64_t reduction) {
    if (reduction == 0) {
        return 0;
    }
    int64_t m = (k - 1);
    int64_t top = ((int64_t)(primes[m]));
    int64_t* loss = (int64_t*)(malloc(((reduction + 1) * 8)));
    int64_t d = 1;
    while (d <= reduction) {
        loss[d] = (top - ((int64_t)(primes[(m - d)])));
        d = (d + 1);
    }
    int64_t INF = 1000000000000000000;
    int64_t* dp = (int64_t*)(malloc(((reduction + 1) * 8)));
    dp[0] = 0;
    int64_t s = 1;
    while (s <= reduction) {
        dp[s] = INF;
        s = (s + 1);
    }
    d = 1;
    while (d <= reduction) {
        int64_t c = loss[d];
        s = d;
        while (s <= reduction) {
            int64_t cand = (dp[(s - d)] + c);
            if (cand < dp[s]) {
                dp[s] = cand;
            }
            s = (s + 1);
        }
        d = (d + 1);
    }
    int64_t result = dp[reduction];
    free(loss);
    free(dp);
    return result;
}

int64_t maximal_prime_score_i64_i64_ptr_i32(int64_t k, int64_t n, int32_t* primes) {
    if (k == 1) {
        return (n * 2);
    }
    int64_t top = ((int64_t)(primes[(k - 1)]));
    int64_t r = FLOW_CHECKED_MOD(((0 - n)), (k));
    if (r < 0) {
        r = (r + k);
    }
    int64_t loss = min_prime_loss_ptr_i32_i64_i64(primes, k, r);
    return ((n * top) - loss);
}

int32_t main(void) {
    int64_t k = 7000;
    int32_t* primes_7001 = (int32_t*)(first_n_primes_i64((k + 1)));
    int64_t n = ((int64_t)(primes_7001[k]));
    int64_t ans = maximal_prime_score_i64_i64_ptr_i32(k, n, primes_7001);
    printf("%lld\n", ans);
    free(primes_7001);
    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 private @malloc(i64) -> !llvm.ptr
  func.func private @memset(!llvm.ptr, i32, i64) -> !llvm.ptr
  func.func private @log(f64) -> f64
  func.func @first_n_primes(%arg0: i64) -> !llvm.ptr {
    %0 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi sle, %arg0, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %3 = llvm.mlir.zero : !llvm.ptr
      func.return %3 : !llvm.ptr
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %4 = arith.constant 0 : i32
    %5 = arith.extsi %4 : i32 to i64
    %6 = llvm.mlir.constant(1 : i64) : i64
    %7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
    llvm.store %5, %7 : i64, !llvm.ptr
    %8 = arith.constant 6 : i32
    %10 = arith.extsi %8 : i32 to i64
    %9 = arith.cmpi slt, %arg0, %10 : i64
    cf.cond_br %9, ^bb3, ^bb4
    ^bb3:
      %11 = arith.constant 15 : i32
      %12 = arith.extsi %11 : i32 to i64
      llvm.store %12, %7 : i64, !llvm.ptr
      cf.br ^bb5
    ^bb4:
      %13 = arith.sitofp %arg0 : i64 to f64
      %14 = math.log %13 : f64
      %15 = math.log %13 : f64
      %16 = math.log %15 : f64
      %17 = arith.addf %14, %16 : f64
      %18 = arith.mulf %13, %17 : f64
      %19 = arith.constant 10.0 : f32
      %21 = arith.extf %19 : f32 to f64
      %20 = arith.addf %18, %21 : f64
      %22 = arith.fptosi %20 : f64 to i64
      llvm.store %22, %7 : i64, !llvm.ptr
      cf.br ^bb5
    ^bb5:
    %23 = arith.constant 0 : i1
    %24 = llvm.mlir.constant(1 : i64) : i64
    %25 = llvm.alloca %24 x i1 : (i64) -> !llvm.ptr
    llvm.store %23, %25 : i1, !llvm.ptr
    %26 = llvm.mlir.zero : !llvm.ptr
    %27 = llvm.mlir.constant(1 : i64) : i64
    %28 = llvm.alloca %27 x !llvm.ptr : (i64) -> !llvm.ptr
    llvm.store %26, %28 : !llvm.ptr, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %29 = llvm.load %25 : !llvm.ptr -> i1
    %31 = arith.constant 1 : i1
    %30 = arith.xori %29, %31 : i1
    cf.cond_br %30, ^bb7, ^bb8
    ^bb7:
      %34 = llvm.load %7 : !llvm.ptr -> i64
      %35 = arith.constant 1 : i32
      %37 = arith.extsi %35 : i32 to i64
      %36 = arith.addi %34, %37 : i64
      %33 = func.call @malloc(%36) : (i64) -> !llvm.ptr
      %39 = arith.constant 1 : i32
      %40 = llvm.load %7 : !llvm.ptr -> i64
      %41 = arith.constant 1 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.addi %40, %43 : i64
      %38 = func.call @memset(%33, %39, %42) : (!llvm.ptr, i32, i64) -> !llvm.ptr
      %44 = arith.constant 0 : i32
      %45 = arith.constant 0 : i32
      %46 = arith.trunci %44 : i32 to i8
      %47 = arith.extsi %45 : i32 to i64
      %48 = llvm.getelementptr %33[%47] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %46, %48 : i8, !llvm.ptr
      %49 = arith.constant 0 : i32
      %50 = arith.constant 1 : i32
      %51 = arith.trunci %49 : i32 to i8
      %52 = arith.extsi %50 : i32 to i64
      %53 = llvm.getelementptr %33[%52] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %51, %53 : i8, !llvm.ptr
      %54 = arith.constant 2 : i32
      %55 = arith.extsi %54 : i32 to i64
      %56 = llvm.mlir.constant(1 : i64) : i64
      %57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
      llvm.store %55, %57 : i64, !llvm.ptr
      cf.br ^bb9
      ^bb9:
      %58 = llvm.load %57 : !llvm.ptr -> i64
      %59 = llvm.load %57 : !llvm.ptr -> i64
      %60 = arith.muli %58, %59 : i64
      %61 = llvm.load %7 : !llvm.ptr -> i64
      %62 = arith.cmpi sle, %60, %61 : i64
      cf.cond_br %62, ^bb10, ^bb11
      ^bb10:
        %64 = llvm.load %57 : !llvm.ptr -> i64
        %65 = llvm.getelementptr %33[%64] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %63 = llvm.load %65 : !llvm.ptr -> i8
        %66 = arith.constant 0 : i32
        %68 = arith.extsi %63 : i8 to i32
        %67 = arith.cmpi ne, %68, %66 : i32
        cf.cond_br %67, ^bb12, ^bb13
        ^bb12:
          %69 = llvm.load %57 : !llvm.ptr -> i64
          %70 = llvm.load %57 : !llvm.ptr -> i64
          %71 = arith.muli %69, %70 : i64
          %72 = llvm.mlir.constant(1 : i64) : i64
          %73 = llvm.alloca %72 x i64 : (i64) -> !llvm.ptr
          llvm.store %71, %73 : i64, !llvm.ptr
          cf.br ^bb15
          ^bb15:
          %74 = llvm.load %73 : !llvm.ptr -> i64
          %75 = llvm.load %7 : !llvm.ptr -> i64
          %76 = arith.cmpi sle, %74, %75 : i64
          cf.cond_br %76, ^bb16, ^bb17
          ^bb16:
            %77 = arith.constant 0 : i32
            %78 = llvm.load %73 : !llvm.ptr -> i64
            %79 = arith.trunci %77 : i32 to i8
            %80 = llvm.getelementptr %33[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i8
            llvm.store %79, %80 : i8, !llvm.ptr
            %81 = llvm.load %73 : !llvm.ptr -> i64
            %82 = llvm.load %57 : !llvm.ptr -> i64
            %83 = arith.addi %81, %82 : i64
            llvm.store %83, %73 : i64, !llvm.ptr
            cf.br ^bb15
          ^bb17:
          cf.br ^bb14
        ^bb13:
          cf.br ^bb14
        ^bb14:
        %84 = llvm.load %57 : !llvm.ptr -> i64
        %85 = arith.constant 1 : i32
        %87 = arith.extsi %85 : i32 to i64
        %86 = arith.addi %84, %87 : i64
        llvm.store %86, %57 : i64, !llvm.ptr
        cf.br ^bb9
      ^bb11:
      %88 = arith.constant 0 : i32
      %89 = arith.extsi %88 : i32 to i64
      %90 = llvm.mlir.constant(1 : i64) : i64
      %91 = llvm.alloca %90 x i64 : (i64) -> !llvm.ptr
      llvm.store %89, %91 : i64, !llvm.ptr
      %92 = arith.constant 2 : i32
      %93 = arith.extsi %92 : i32 to i64
      llvm.store %93, %57 : i64, !llvm.ptr
      cf.br ^bb18
      ^bb18:
      %94 = llvm.load %57 : !llvm.ptr -> i64
      %95 = llvm.load %7 : !llvm.ptr -> i64
      %96 = arith.cmpi sle, %94, %95 : i64
      cf.cond_br %96, ^bb19, ^bb20
      ^bb19:
        %98 = llvm.load %57 : !llvm.ptr -> i64
        %99 = llvm.getelementptr %33[%98] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %97 = llvm.load %99 : !llvm.ptr -> i8
        %100 = arith.constant 0 : i32
        %102 = arith.extsi %97 : i8 to i32
        %101 = arith.cmpi ne, %102, %100 : i32
        cf.cond_br %101, ^bb21, ^bb22
        ^bb21:
          %103 = llvm.load %91 : !llvm.ptr -> i64
          %104 = arith.constant 1 : i32
          %106 = arith.extsi %104 : i32 to i64
          %105 = arith.addi %103, %106 : i64
          llvm.store %105, %91 : i64, !llvm.ptr
          cf.br ^bb23
        ^bb22:
          cf.br ^bb23
        ^bb23:
        %107 = llvm.load %57 : !llvm.ptr -> i64
        %108 = arith.constant 1 : i32
        %110 = arith.extsi %108 : i32 to i64
        %109 = arith.addi %107, %110 : i64
        llvm.store %109, %57 : i64, !llvm.ptr
        cf.br ^bb18
      ^bb20:
      %111 = llvm.load %91 : !llvm.ptr -> i64
      %112 = arith.cmpi sge, %111, %arg0 : i64
      cf.cond_br %112, ^bb24, ^bb25
      ^bb24:
        %114 = arith.constant 4 : i32
        %116 = arith.extsi %114 : i32 to i64
        %115 = arith.muli %arg0, %116 : i64
        %113 = func.call @malloc(%115) : (i64) -> !llvm.ptr
        llvm.store %113, %28 : !llvm.ptr, !llvm.ptr
        %117 = arith.constant 0 : i32
        %118 = arith.extsi %117 : i32 to i64
        %119 = llvm.mlir.constant(1 : i64) : i64
        %120 = llvm.alloca %119 x i64 : (i64) -> !llvm.ptr
        llvm.store %118, %120 : i64, !llvm.ptr
        %121 = arith.constant 2 : i32
        %122 = arith.extsi %121 : i32 to i64
        llvm.store %122, %57 : i64, !llvm.ptr
        cf.br ^bb27
        ^bb27:
        %123 = llvm.load %57 : !llvm.ptr -> i64
        %124 = llvm.load %7 : !llvm.ptr -> i64
        %125 = arith.cmpi sle, %123, %124 : i64
        %126 = scf.if %125 -> (i1) {
          %127 = llvm.load %120 : !llvm.ptr -> i64
          %128 = arith.cmpi slt, %127, %arg0 : i64
          scf.yield %128 : i1
        } else {
          %129 = arith.constant false
          scf.yield %129 : i1
        }
        cf.cond_br %126, ^bb28, ^bb29
        ^bb28:
          %131 = llvm.load %57 : !llvm.ptr -> i64
          %132 = llvm.getelementptr %33[%131] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          %130 = llvm.load %132 : !llvm.ptr -> i8
          %133 = arith.constant 0 : i32
          %135 = arith.extsi %130 : i8 to i32
          %134 = arith.cmpi ne, %135, %133 : i32
          cf.cond_br %134, ^bb30, ^bb31
          ^bb30:
            %136 = llvm.load %57 : !llvm.ptr -> i64
            %137 = arith.trunci %136 : i64 to i32
            %138 = llvm.load %28 : !llvm.ptr -> !llvm.ptr
            %139 = llvm.load %120 : !llvm.ptr -> i64
            %140 = llvm.getelementptr %138[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            llvm.store %137, %140 : i32, !llvm.ptr
            %141 = llvm.load %120 : !llvm.ptr -> i64
            %142 = arith.constant 1 : i32
            %144 = arith.extsi %142 : i32 to i64
            %143 = arith.addi %141, %144 : i64
            llvm.store %143, %120 : i64, !llvm.ptr
            cf.br ^bb32
          ^bb31:
            cf.br ^bb32
          ^bb32:
          %145 = llvm.load %57 : !llvm.ptr -> i64
          %146 = arith.constant 1 : i32
          %148 = arith.extsi %146 : i32 to i64
          %147 = arith.addi %145, %148 : i64
          llvm.store %147, %57 : i64, !llvm.ptr
          cf.br ^bb27
        ^bb29:
        func.call @free(%33) : (!llvm.ptr) -> ()
        %150 = arith.constant 1 : i1
        llvm.store %150, %25 : i1, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        func.call @free(%33) : (!llvm.ptr) -> ()
        %152 = llvm.load %7 : !llvm.ptr -> i64
        %153 = arith.constant 2 : i32
        %155 = arith.extsi %153 : i32 to i64
        %154 = arith.muli %152, %155 : i64
        llvm.store %154, %7 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb26:
      cf.br ^bb6
    ^bb8:
    %156 = llvm.load %28 : !llvm.ptr -> !llvm.ptr
    func.return %156 : !llvm.ptr
  }
  func.func @min_prime_loss(%arg0: !llvm.ptr, %arg1: i64, %arg2: i64) -> i64 {
    %157 = arith.constant 0 : i32
    %159 = arith.extsi %157 : i32 to i64
    %158 = arith.cmpi eq, %arg2, %159 : i64
    cf.cond_br %158, ^bb33, ^bb34
    ^bb33:
      %160 = arith.constant 0 : i32
      %161 = arith.extsi %160 : i32 to i64
      func.return %161 : i64
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %162 = arith.constant 1 : i32
    %164 = arith.extsi %162 : i32 to i64
    %163 = arith.subi %arg1, %164 : i64
    %166 = llvm.getelementptr %arg0[%163] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %165 = llvm.load %166 : !llvm.ptr -> i32
    %167 = arith.extsi %165 : i32 to i64
    %169 = arith.constant 1 : i32
    %171 = arith.extsi %169 : i32 to i64
    %170 = arith.addi %arg2, %171 : i64
    %172 = arith.constant 8 : i32
    %174 = arith.extsi %172 : i32 to i64
    %173 = arith.muli %170, %174 : i64
    %168 = func.call @malloc(%173) : (i64) -> !llvm.ptr
    %175 = arith.constant 1 : i32
    %176 = arith.extsi %175 : i32 to i64
    %177 = llvm.mlir.constant(1 : i64) : i64
    %178 = llvm.alloca %177 x i64 : (i64) -> !llvm.ptr
    llvm.store %176, %178 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %179 = llvm.load %178 : !llvm.ptr -> i64
    %180 = arith.cmpi sle, %179, %arg2 : i64
    cf.cond_br %180, ^bb37, ^bb38
    ^bb37:
      %182 = llvm.load %178 : !llvm.ptr -> i64
      %183 = arith.subi %163, %182 : i64
      %184 = llvm.getelementptr %arg0[%183] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %181 = llvm.load %184 : !llvm.ptr -> i32
      %185 = arith.extsi %181 : i32 to i64
      %186 = arith.subi %167, %185 : i64
      %187 = llvm.load %178 : !llvm.ptr -> i64
      %188 = llvm.getelementptr %168[%187] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %186, %188 : i64, !llvm.ptr
      %189 = llvm.load %178 : !llvm.ptr -> i64
      %190 = arith.constant 1 : i32
      %192 = arith.extsi %190 : i32 to i64
      %191 = arith.addi %189, %192 : i64
      llvm.store %191, %178 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %193 = arith.constant 999999995705032704 : i32
    %194 = arith.extsi %193 : i32 to i64
    %196 = arith.constant 1 : i32
    %198 = arith.extsi %196 : i32 to i64
    %197 = arith.addi %arg2, %198 : i64
    %199 = arith.constant 8 : i32
    %201 = arith.extsi %199 : i32 to i64
    %200 = arith.muli %197, %201 : i64
    %195 = func.call @malloc(%200) : (i64) -> !llvm.ptr
    %202 = arith.constant 0 : i32
    %203 = arith.constant 0 : i32
    %204 = arith.extsi %202 : i32 to i64
    %205 = arith.extsi %203 : i32 to i64
    %206 = llvm.getelementptr %195[%205] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %204, %206 : i64, !llvm.ptr
    %207 = arith.constant 1 : i32
    %208 = arith.extsi %207 : i32 to i64
    %209 = llvm.mlir.constant(1 : i64) : i64
    %210 = llvm.alloca %209 x i64 : (i64) -> !llvm.ptr
    llvm.store %208, %210 : i64, !llvm.ptr
    cf.br ^bb39
    ^bb39:
    %211 = llvm.load %210 : !llvm.ptr -> i64
    %212 = arith.cmpi sle, %211, %arg2 : i64
    cf.cond_br %212, ^bb40, ^bb41
    ^bb40:
      %213 = llvm.load %210 : !llvm.ptr -> i64
      %214 = llvm.getelementptr %195[%213] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %194, %214 : i64, !llvm.ptr
      %215 = llvm.load %210 : !llvm.ptr -> i64
      %216 = arith.constant 1 : i32
      %218 = arith.extsi %216 : i32 to i64
      %217 = arith.addi %215, %218 : i64
      llvm.store %217, %210 : i64, !llvm.ptr
      cf.br ^bb39
    ^bb41:
    %219 = arith.constant 1 : i32
    %220 = arith.extsi %219 : i32 to i64
    llvm.store %220, %178 : i64, !llvm.ptr
    cf.br ^bb42
    ^bb42:
    %221 = llvm.load %178 : !llvm.ptr -> i64
    %222 = arith.cmpi sle, %221, %arg2 : i64
    cf.cond_br %222, ^bb43, ^bb44
    ^bb43:
      %224 = llvm.load %178 : !llvm.ptr -> i64
      %225 = llvm.getelementptr %168[%224] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %223 = llvm.load %225 : !llvm.ptr -> i64
      %226 = llvm.load %178 : !llvm.ptr -> i64
      llvm.store %226, %210 : i64, !llvm.ptr
      cf.br ^bb45
      ^bb45:
      %227 = llvm.load %210 : !llvm.ptr -> i64
      %228 = arith.cmpi sle, %227, %arg2 : i64
      cf.cond_br %228, ^bb46, ^bb47
      ^bb46:
        %230 = llvm.load %210 : !llvm.ptr -> i64
        %231 = llvm.load %178 : !llvm.ptr -> i64
        %232 = arith.subi %230, %231 : i64
        %233 = llvm.getelementptr %195[%232] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %229 = llvm.load %233 : !llvm.ptr -> i64
        %234 = arith.addi %229, %223 : i64
        %236 = llvm.load %210 : !llvm.ptr -> i64
        %237 = llvm.getelementptr %195[%236] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %235 = llvm.load %237 : !llvm.ptr -> i64
        %238 = arith.cmpi slt, %234, %235 : i64
        cf.cond_br %238, ^bb48, ^bb49
        ^bb48:
          %239 = llvm.load %210 : !llvm.ptr -> i64
          %240 = llvm.getelementptr %195[%239] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %234, %240 : i64, !llvm.ptr
          cf.br ^bb50
        ^bb49:
          cf.br ^bb50
        ^bb50:
        %241 = llvm.load %210 : !llvm.ptr -> i64
        %242 = arith.constant 1 : i32
        %244 = arith.extsi %242 : i32 to i64
        %243 = arith.addi %241, %244 : i64
        llvm.store %243, %210 : i64, !llvm.ptr
        cf.br ^bb45
      ^bb47:
      %245 = llvm.load %178 : !llvm.ptr -> i64
      %246 = arith.constant 1 : i32
      %248 = arith.extsi %246 : i32 to i64
      %247 = arith.addi %245, %248 : i64
      llvm.store %247, %178 : i64, !llvm.ptr
      cf.br ^bb42
    ^bb44:
    %250 = llvm.getelementptr %195[%arg2] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %249 = llvm.load %250 : !llvm.ptr -> i64
    func.call @free(%168) : (!llvm.ptr) -> ()
    func.call @free(%195) : (!llvm.ptr) -> ()
    func.return %249 : i64
  }
  func.func @maximal_prime_score(%arg0: i64, %arg1: i64, %arg2: !llvm.ptr) -> i64 {
    %253 = arith.constant 1 : i32
    %255 = arith.extsi %253 : i32 to i64
    %254 = arith.cmpi eq, %arg0, %255 : i64
    cf.cond_br %254, ^bb51, ^bb52
    ^bb51:
      %256 = arith.constant 2 : i32
      %258 = arith.extsi %256 : i32 to i64
      %257 = arith.muli %arg1, %258 : i64
      func.return %257 : i64
    ^bb52:
      cf.br ^bb53
    ^bb53:
    %260 = arith.constant 1 : i32
    %262 = arith.extsi %260 : i32 to i64
    %261 = arith.subi %arg0, %262 : i64
    %263 = llvm.getelementptr %arg2[%261] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %259 = llvm.load %263 : !llvm.ptr -> i32
    %264 = arith.extsi %259 : i32 to i64
    %265 = arith.constant 0 : i32
    %267 = arith.extsi %265 : i32 to i64
    %266 = arith.subi %267, %arg1 : i64
    %268 = arith.remsi %266, %arg0 : i64
    %269 = llvm.mlir.constant(1 : i64) : i64
    %270 = llvm.alloca %269 x i64 : (i64) -> !llvm.ptr
    llvm.store %268, %270 : i64, !llvm.ptr
    %271 = llvm.load %270 : !llvm.ptr -> i64
    %272 = arith.constant 0 : i32
    %274 = arith.extsi %272 : i32 to i64
    %273 = arith.cmpi slt, %271, %274 : i64
    cf.cond_br %273, ^bb54, ^bb55
    ^bb54:
      %275 = llvm.load %270 : !llvm.ptr -> i64
      %276 = arith.addi %275, %arg0 : i64
      llvm.store %276, %270 : i64, !llvm.ptr
      cf.br ^bb56
    ^bb55:
      cf.br ^bb56
    ^bb56:
    %278 = llvm.load %270 : !llvm.ptr -> i64
    %277 = func.call @min_prime_loss(%arg2, %arg0, %278) : (!llvm.ptr, i64, i64) -> i64
    %279 = arith.muli %arg1, %264 : i64
    %280 = arith.subi %279, %277 : i64
    func.return %280 : i64
  }
  func.func @main() -> i32 {
    %281 = arith.constant 7000 : i32
    %282 = arith.extsi %281 : i32 to i64
    %284 = arith.constant 1 : i32
    %286 = arith.extsi %284 : i32 to i64
    %285 = arith.addi %282, %286 : i64
    %283 = func.call @first_n_primes(%285) : (i64) -> !llvm.ptr
    %288 = llvm.getelementptr %283[%282] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %287 = llvm.load %288 : !llvm.ptr -> i32
    %289 = arith.extsi %287 : i32 to i64
    %290 = func.call @maximal_prime_score(%282, %289, %283) : (i64, i64, !llvm.ptr) -> i64
    %291 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %292 = llvm.call @printf(%291, %290) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%283) : (!llvm.ptr) -> ()
    %294 = arith.constant 0 : i32
    func.return %294 : i32
  }
}