Problem 118

How many sets of primes using digits 1-9 exactly once?

Answer44680
Output44680
StatusPASS
Native helperno
Runtime4290 ms
Peak memory1104 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log log n)
Space complexityO(1)O(n)
ApproachFlow solutionSieve or enumeration
VerdictOptimal

Flow source

# Project Euler 118
# How many sets of primes using digits 1-9 exactly once?

function mulmod(a0: i64, b0: i64, mod: i64) -> i64 {
    let mut a: i64 = a0 % mod
    let mut b: i64 = b0 % mod
    let mut result: i64 = 0
    while b > 0 {
        if b % 2 == 1 { result = (result + a) % mod }
        a = (a * 2) % mod
        b = b / 2
    }
    return result
}

function modpow(base: i64, exp: i64, mod: i64) -> i64 {
    let mut result: i64 = 1
    let mut b: i64 = base % mod
    let mut e: i64 = exp
    while e > 0 {
        if e % 2 == 1 { result = mulmod(result, b, mod) }
        b = mulmod(b, b, mod)
        e = e / 2
    }
    return result
}

function is_prime(n: i64) -> bool {
    if n < 2 { return false }
    if n % 2 == 0 { return n == 2 }
    if n % 3 == 0 { return n == 3 }
    if n % 5 == 0 { return n == 5 }
    let bases: array<i64, 5>
    bases[0] = 2
    bases[1] = 3
    bases[2] = 5
    bases[3] = 7
    bases[4] = 11
    let mut d: i64 = n - 1
    let mut s: i32 = 0
    while d % 2 == 0 {
        d = d / 2
        s = s + 1
    }
    for bi in 0..5 {
        let a: i64 = bases[bi] % n
        if a != 0 {
            let mut x: i64 = modpow(a, d, n)
            if x != 1 && x != n - 1 {
                let mut r: i32 = 1
                let mut bad: bool = true
                while r < s {
                    x = mulmod(x, x, n)
                    if x == n - 1 { bad = false; break }
                    r = r + 1
                }
                if bad { return false }
            }
        }
    }
    return true
}

# Count splits of digit array into nondecreasing prime sequence.
function count_splits(digs: ptr<i32>, start: i32, last: i64) -> i64 {
    if start == 9 { return 1 }
    let mut total: i64 = 0
    let mut num: i64 = 0
    for j in start..9 {
        num = num * 10 + (digs[j] as i64)
        if num >= last && is_prime(num) {
            total = total + count_splits(digs, j + 1, num)
        }
    }
    return total
}

function next_perm(a: ptr<i32>, n: i32) -> bool {
    # standard next permutation
    let mut i: i32 = n - 2
    while i >= 0 && a[i] >= a[i + 1] {
        i = i - 1
    }
    if i < 0 { return false }
    let mut j: i32 = n - 1
    while a[j] <= a[i] {
        j = j - 1
    }
    let t: i32 = a[i]
    a[i] = a[j]
    a[j] = t
    # reverse i+1..n-1
    let mut L: i32 = i + 1
    let mut R: i32 = n - 1
    while L < R {
        let u: i32 = a[L]
        a[L] = a[R]
        a[R] = u
        L = L + 1
        R = R - 1
    }
    return true
}

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

function main() -> i32 {
    let digs: ptr<i32> = calloc(9, 4)
    if digs == null { return 1 }
    for i in 0..9 {
        digs[i] = i + 1
    }
    let mut total: i64 = 0
    while true {
        total = total + count_splits(digs, 0, 0)
        if !next_perm(digs, 9) { break }
    }
    # Each set is counted |set|! / automorphisms? NO —
    # We form ordered concatenations via permutations, and splits require
    # nondecreasing primes, so each set is counted once per ordering of digits
    # consistent with concatenating the primes in sorted order.
    # That is exactly once per set (digits follow sorted primes concatenated).
    # Wait: for a set {p1<p2<...<pk}, only ONE permutation of digits matches
    # the concatenation p1|p2|...|pk. So each set counted once. Good.
    printf("%lld\n", total)
    free(digs)
    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; }

int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t count_splits_ptr_i32_i32_i64(int32_t* digs, int32_t start, int64_t last);
bool next_perm_ptr_i32_i32(int32_t* a, int32_t n);
int32_t main(void);

int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
    int64_t a = FLOW_CHECKED_MOD((a0), (mod));
    int64_t b = FLOW_CHECKED_MOD((b0), (mod));
    int64_t result = 0;
    while (b > 0) {
        if (FLOW_CHECKED_MOD((b), (2)) == 1) {
            result = FLOW_CHECKED_MOD(((result + a)), (mod));
        }
        a = FLOW_CHECKED_MOD(((a * 2)), (mod));
        b = FLOW_CHECKED_DIV((b), (2));
    }
    return result;
}

int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    int64_t result = 1;
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            result = mulmod_i64_i64_i64(result, b, mod);
        }
        b = mulmod_i64_i64_i64(b, b, mod);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return result;
}

bool is_prime_i64(int64_t n) {
    if (n < 2) {
        return 0;
    }
    if (FLOW_CHECKED_MOD((n), (2)) == 0) {
        return n == 2;
    }
    if (FLOW_CHECKED_MOD((n), (3)) == 0) {
        return n == 3;
    }
    if (FLOW_CHECKED_MOD((n), (5)) == 0) {
        return n == 5;
    }
    int64_t bases[5];
    bases[0] = 2;
    bases[1] = 3;
    bases[2] = 5;
    bases[3] = 7;
    bases[4] = 11;
    int64_t d = (n - 1);
    int32_t s = 0;
    while (FLOW_CHECKED_MOD((d), (2)) == 0) {
        d = FLOW_CHECKED_DIV((d), (2));
        s = (s + 1);
    }
    int32_t __flow_step_1 = 1;
    for (int32_t bi = 0; (0 <= 5) ? bi < 5 : bi > 5; bi += (0 <= 5) ? 1 : -1) {
        int64_t a = FLOW_CHECKED_MOD(((((unsigned)(bi) < 5) ? bases[bi] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(bi), 5), flow_fault_handler("array index out of bounds"), bases[0]))), (n));
        if (a != 0) {
            int64_t x = modpow_i64_i64_i64(a, d, n);
            if ((x != 1 && x != (n - 1))) {
                int32_t r = 1;
                bool bad = 1;
                while (r < s) {
                    x = mulmod_i64_i64_i64(x, x, n);
                    if (x == (n - 1)) {
                        bad = 0;
                        break;
                    }
                    r = (r + 1);
                }
                if (bad) {
                    return 0;
                }
            }
        }
    }
    return 1;
}

int64_t count_splits_ptr_i32_i32_i64(int32_t* digs, int32_t start, int64_t last) {
    if (start == 9) {
        return 1;
    }
    int64_t total = 0;
    int64_t num = 0;
    int32_t __flow_step_2 = 1;
    for (int32_t j = start; (start <= 9) ? j < 9 : j > 9; j += (start <= 9) ? 1 : -1) {
        num = ((num * 10) + ((int64_t)(digs[j])));
        if ((num >= last && is_prime_i64(num))) {
            total = (total + count_splits_ptr_i32_i32_i64(digs, (j + 1), num));
        }
    }
    return total;
}

bool next_perm_ptr_i32_i32(int32_t* a, int32_t n) {
    int32_t i = (n - 2);
    while ((i >= 0 && a[i] >= a[(i + 1)])) {
        i = (i - 1);
    }
    if (i < 0) {
        return 0;
    }
    int32_t j = (n - 1);
    while (a[j] <= a[i]) {
        j = (j - 1);
    }
    int32_t t = a[i];
    a[i] = a[j];
    a[j] = t;
    int32_t L = (i + 1);
    int32_t R = (n - 1);
    while (L < R) {
        int32_t u = a[L];
        a[L] = a[R];
        a[R] = u;
        L = (L + 1);
        R = (R - 1);
    }
    return 1;
}



int32_t main(void) {
    int32_t* digs = (int32_t*)(calloc(9, 4));
    if (digs == NULL) {
        return 1;
    }
    int32_t __flow_step_3 = 1;
    for (int32_t i = 0; (0 <= 9) ? i < 9 : i > 9; i += (0 <= 9) ? 1 : -1) {
        digs[i] = (i + 1);
    }
    int64_t total = 0;
    while (1) {
        total = (total + count_splits_ptr_i32_i32_i64(digs, 0, 0));
        if ((!(next_perm_ptr_i32_i32(digs, 9)))) {
            break;
        }
    }
    printf("%lld\n", total);
    free(digs);
    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 @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.remsi %arg0, %arg2 : i64
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i64 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i64, !llvm.ptr
    %3 = arith.remsi %arg1, %arg2 : i64
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %3, %5 : i64, !llvm.ptr
    %6 = arith.constant 0 : i32
    %7 = arith.extsi %6 : i32 to i64
    %8 = llvm.mlir.constant(1 : i64) : i64
    %9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
    llvm.store %7, %9 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %10 = llvm.load %5 : !llvm.ptr -> i64
    %11 = arith.constant 0 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.cmpi sgt, %10, %13 : i64
    cf.cond_br %12, ^bb1, ^bb2
    ^bb1:
      %14 = llvm.load %5 : !llvm.ptr -> i64
      %15 = arith.constant 2 : i32
      %17 = arith.extsi %15 : i32 to i64
      %16 = arith.remsi %14, %17 : i64
      %18 = arith.constant 1 : i32
      %20 = arith.extsi %18 : i32 to i64
      %19 = arith.cmpi eq, %16, %20 : i64
      cf.cond_br %19, ^bb3, ^bb4
      ^bb3:
        %21 = llvm.load %9 : !llvm.ptr -> i64
        %22 = llvm.load %2 : !llvm.ptr -> i64
        %23 = arith.addi %21, %22 : i64
        %24 = arith.remsi %23, %arg2 : i64
        llvm.store %24, %9 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %25 = llvm.load %2 : !llvm.ptr -> i64
      %26 = arith.constant 2 : i32
      %28 = arith.extsi %26 : i32 to i64
      %27 = arith.muli %25, %28 : i64
      %29 = arith.remsi %27, %arg2 : i64
      llvm.store %29, %2 : i64, !llvm.ptr
      %30 = llvm.load %5 : !llvm.ptr -> i64
      %31 = arith.constant 2 : i32
      %33 = arith.extsi %31 : i32 to i64
      %32 = arith.divsi %30, %33 : i64
      llvm.store %32, %5 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %34 = llvm.load %9 : !llvm.ptr -> i64
    func.return %34 : i64
  }
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %35 = arith.constant 1 : i32
    %36 = arith.extsi %35 : i32 to i64
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : i64, !llvm.ptr
    %39 = arith.remsi %arg0, %arg2 : i64
    %40 = llvm.mlir.constant(1 : i64) : i64
    %41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
    llvm.store %39, %41 : i64, !llvm.ptr
    %42 = llvm.mlir.constant(1 : i64) : i64
    %43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %43 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %44 = llvm.load %43 : !llvm.ptr -> i64
    %45 = arith.constant 0 : i32
    %47 = arith.extsi %45 : i32 to i64
    %46 = arith.cmpi sgt, %44, %47 : i64
    cf.cond_br %46, ^bb7, ^bb8
    ^bb7:
      %48 = llvm.load %43 : !llvm.ptr -> i64
      %49 = arith.constant 2 : i32
      %51 = arith.extsi %49 : i32 to i64
      %50 = arith.remsi %48, %51 : i64
      %52 = arith.constant 1 : i32
      %54 = arith.extsi %52 : i32 to i64
      %53 = arith.cmpi eq, %50, %54 : i64
      cf.cond_br %53, ^bb9, ^bb10
      ^bb9:
        %56 = llvm.load %38 : !llvm.ptr -> i64
        %57 = llvm.load %41 : !llvm.ptr -> i64
        %55 = func.call @mulmod(%56, %57, %arg2) : (i64, i64, i64) -> i64
        llvm.store %55, %38 : i64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %59 = llvm.load %41 : !llvm.ptr -> i64
      %60 = llvm.load %41 : !llvm.ptr -> i64
      %58 = func.call @mulmod(%59, %60, %arg2) : (i64, i64, i64) -> i64
      llvm.store %58, %41 : i64, !llvm.ptr
      %61 = llvm.load %43 : !llvm.ptr -> i64
      %62 = arith.constant 2 : i32
      %64 = arith.extsi %62 : i32 to i64
      %63 = arith.divsi %61, %64 : i64
      llvm.store %63, %43 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %65 = llvm.load %38 : !llvm.ptr -> i64
    func.return %65 : i64
  }
  func.func @is_prime(%arg0: i64) -> i1 {
    %66 = arith.constant 2 : i32
    %68 = arith.extsi %66 : i32 to i64
    %67 = arith.cmpi slt, %arg0, %68 : i64
    cf.cond_br %67, ^bb12, ^bb13
    ^bb12:
      %69 = arith.constant 0 : i1
      func.return %69 : i1
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %70 = arith.constant 2 : i32
    %72 = arith.extsi %70 : i32 to i64
    %71 = arith.remsi %arg0, %72 : i64
    %73 = arith.constant 0 : i32
    %75 = arith.extsi %73 : i32 to i64
    %74 = arith.cmpi eq, %71, %75 : i64
    cf.cond_br %74, ^bb15, ^bb16
    ^bb15:
      %76 = arith.constant 2 : i32
      %78 = arith.extsi %76 : i32 to i64
      %77 = arith.cmpi eq, %arg0, %78 : i64
      func.return %77 : i1
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %79 = arith.constant 3 : i32
    %81 = arith.extsi %79 : i32 to i64
    %80 = arith.remsi %arg0, %81 : i64
    %82 = arith.constant 0 : i32
    %84 = arith.extsi %82 : i32 to i64
    %83 = arith.cmpi eq, %80, %84 : i64
    cf.cond_br %83, ^bb18, ^bb19
    ^bb18:
      %85 = arith.constant 3 : i32
      %87 = arith.extsi %85 : i32 to i64
      %86 = arith.cmpi eq, %arg0, %87 : i64
      func.return %86 : i1
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %88 = arith.constant 5 : i32
    %90 = arith.extsi %88 : i32 to i64
    %89 = arith.remsi %arg0, %90 : i64
    %91 = arith.constant 0 : i32
    %93 = arith.extsi %91 : i32 to i64
    %92 = arith.cmpi eq, %89, %93 : i64
    cf.cond_br %92, ^bb21, ^bb22
    ^bb21:
      %94 = arith.constant 5 : i32
      %96 = arith.extsi %94 : i32 to i64
      %95 = arith.cmpi eq, %arg0, %96 : i64
      func.return %95 : i1
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %97 = memref.alloca() {type = memref<5xi64>} : memref<5xi64>
    %98 = arith.constant 2 : i32
    %99 = arith.constant 0 : i32
    %100 = arith.index_cast %99 : i32 to index
    %101 = arith.extsi %98 : i32 to i64
    memref.store %101, %97[%100] : memref<5xi64>
    %102 = arith.constant 3 : i32
    %103 = arith.constant 1 : i32
    %104 = arith.index_cast %103 : i32 to index
    %105 = arith.extsi %102 : i32 to i64
    memref.store %105, %97[%104] : memref<5xi64>
    %106 = arith.constant 5 : i32
    %107 = arith.constant 2 : i32
    %108 = arith.index_cast %107 : i32 to index
    %109 = arith.extsi %106 : i32 to i64
    memref.store %109, %97[%108] : memref<5xi64>
    %110 = arith.constant 7 : i32
    %111 = arith.constant 3 : i32
    %112 = arith.index_cast %111 : i32 to index
    %113 = arith.extsi %110 : i32 to i64
    memref.store %113, %97[%112] : memref<5xi64>
    %114 = arith.constant 11 : i32
    %115 = arith.constant 4 : i32
    %116 = arith.index_cast %115 : i32 to index
    %117 = arith.extsi %114 : i32 to i64
    memref.store %117, %97[%116] : memref<5xi64>
    %118 = arith.constant 1 : i32
    %120 = arith.extsi %118 : i32 to i64
    %119 = arith.subi %arg0, %120 : i64
    %121 = llvm.mlir.constant(1 : i64) : i64
    %122 = llvm.alloca %121 x i64 : (i64) -> !llvm.ptr
    llvm.store %119, %122 : i64, !llvm.ptr
    %123 = arith.constant 0 : i32
    %124 = llvm.mlir.constant(1 : i64) : i64
    %125 = llvm.alloca %124 x i32 : (i64) -> !llvm.ptr
    llvm.store %123, %125 : i32, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %126 = llvm.load %122 : !llvm.ptr -> i64
    %127 = arith.constant 2 : i32
    %129 = arith.extsi %127 : i32 to i64
    %128 = arith.remsi %126, %129 : i64
    %130 = arith.constant 0 : i32
    %132 = arith.extsi %130 : i32 to i64
    %131 = arith.cmpi eq, %128, %132 : i64
    cf.cond_br %131, ^bb25, ^bb26
    ^bb25:
      %133 = llvm.load %122 : !llvm.ptr -> i64
      %134 = arith.constant 2 : i32
      %136 = arith.extsi %134 : i32 to i64
      %135 = arith.divsi %133, %136 : i64
      llvm.store %135, %122 : i64, !llvm.ptr
      %137 = llvm.load %125 : !llvm.ptr -> i32
      %138 = arith.constant 1 : i32
      %139 = arith.addi %137, %138 : i32
      llvm.store %139, %125 : i32, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %140 = arith.constant 0 : i32
    %141 = arith.constant 5 : i32
    %142 = arith.index_cast %140 : i32 to index
    %143 = arith.index_cast %141 : i32 to index
    %145 = arith.constant 1 : index
    %146 = arith.constant -1 : index
    %147 = arith.cmpi sle, %142, %143 : index
    %144 = arith.select %147, %145, %146 : index
    cf.br ^bb27(%142 : index)
    ^bb27(%148: index):
    %149 = arith.cmpi slt, %148, %143 : index
    %150 = arith.cmpi sgt, %148, %143 : index
    %151 = arith.select %147, %149, %150 : i1
    cf.cond_br %151, ^bb28(%148 : index), ^bb29(%148 : index)
    ^bb28(%152: index):
      %153 = memref.load %97[%152] : memref<5xi64>
      %154 = arith.remsi %153, %arg0 : i64
      %155 = arith.constant 0 : i32
      %157 = arith.extsi %155 : i32 to i64
      %156 = arith.cmpi ne, %154, %157 : i64
      cf.cond_br %156, ^bb30, ^bb31
      ^bb30:
        %159 = llvm.load %122 : !llvm.ptr -> i64
        %158 = func.call @modpow(%154, %159, %arg0) : (i64, i64, i64) -> i64
        %160 = llvm.mlir.constant(1 : i64) : i64
        %161 = llvm.alloca %160 x i64 : (i64) -> !llvm.ptr
        llvm.store %158, %161 : i64, !llvm.ptr
        %162 = llvm.load %161 : !llvm.ptr -> i64
        %163 = arith.constant 1 : i32
        %165 = arith.extsi %163 : i32 to i64
        %164 = arith.cmpi ne, %162, %165 : i64
        %166 = scf.if %164 -> (i1) {
          %167 = llvm.load %161 : !llvm.ptr -> i64
          %168 = arith.constant 1 : i32
          %170 = arith.extsi %168 : i32 to i64
          %169 = arith.subi %arg0, %170 : i64
          %171 = arith.cmpi ne, %167, %169 : i64
          scf.yield %171 : i1
        } else {
          %172 = arith.constant false
          scf.yield %172 : i1
        }
        cf.cond_br %166, ^bb33, ^bb34
        ^bb33:
          %173 = arith.constant 1 : i32
          %174 = llvm.mlir.constant(1 : i64) : i64
          %175 = llvm.alloca %174 x i32 : (i64) -> !llvm.ptr
          llvm.store %173, %175 : i32, !llvm.ptr
          %176 = arith.constant 1 : i1
          %177 = llvm.mlir.constant(1 : i64) : i64
          %178 = llvm.alloca %177 x i1 : (i64) -> !llvm.ptr
          llvm.store %176, %178 : i1, !llvm.ptr
          cf.br ^bb36
          ^bb36:
          %179 = llvm.load %175 : !llvm.ptr -> i32
          %180 = llvm.load %125 : !llvm.ptr -> i32
          %181 = arith.cmpi slt, %179, %180 : i32
          cf.cond_br %181, ^bb37, ^bb38
          ^bb37:
            %183 = llvm.load %161 : !llvm.ptr -> i64
            %184 = llvm.load %161 : !llvm.ptr -> i64
            %182 = func.call @mulmod(%183, %184, %arg0) : (i64, i64, i64) -> i64
            llvm.store %182, %161 : i64, !llvm.ptr
            %185 = llvm.load %161 : !llvm.ptr -> i64
            %186 = arith.constant 1 : i32
            %188 = arith.extsi %186 : i32 to i64
            %187 = arith.subi %arg0, %188 : i64
            %189 = arith.cmpi eq, %185, %187 : i64
            cf.cond_br %189, ^bb39, ^bb40
            ^bb39:
              %190 = arith.constant 0 : i1
              llvm.store %190, %178 : i1, !llvm.ptr
              cf.br ^bb38
            ^bb40:
              cf.br ^bb41
            ^bb41:
            %191 = llvm.load %175 : !llvm.ptr -> i32
            %192 = arith.constant 1 : i32
            %193 = arith.addi %191, %192 : i32
            llvm.store %193, %175 : i32, !llvm.ptr
            cf.br ^bb36
          ^bb38:
          %194 = llvm.load %178 : !llvm.ptr -> i1
          cf.cond_br %194, ^bb42, ^bb43
          ^bb42:
            %195 = arith.constant 0 : i1
            func.return %195 : i1
          ^bb43:
            cf.br ^bb44
          ^bb44:
          cf.br ^bb35
        ^bb34:
          cf.br ^bb35
        ^bb35:
        cf.br ^bb32
      ^bb31:
        cf.br ^bb32
      ^bb32:
      %196 = arith.addi %152, %144 : index
      cf.br ^bb27(%196 : index)
    ^bb29(%197: index):
    %198 = arith.constant 1 : i1
    func.return %198 : i1
  }
  func.func @count_splits(%arg0: !llvm.ptr, %arg1: i32, %arg2: i64) -> i64 {
    %199 = arith.constant 9 : i32
    %200 = arith.cmpi eq, %arg1, %199 : i32
    cf.cond_br %200, ^bb45, ^bb46
    ^bb45:
      %201 = arith.constant 1 : i32
      %202 = arith.extsi %201 : i32 to i64
      func.return %202 : i64
    ^bb46:
      cf.br ^bb47
    ^bb47:
    %203 = arith.constant 0 : i32
    %204 = arith.extsi %203 : i32 to i64
    %205 = llvm.mlir.constant(1 : i64) : i64
    %206 = llvm.alloca %205 x i64 : (i64) -> !llvm.ptr
    llvm.store %204, %206 : i64, !llvm.ptr
    %207 = arith.constant 0 : 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
    %211 = arith.constant 9 : i32
    %212 = arith.index_cast %arg1 : i32 to index
    %213 = arith.index_cast %211 : i32 to index
    %215 = arith.constant 1 : index
    %216 = arith.constant -1 : index
    %217 = arith.cmpi sle, %212, %213 : index
    %214 = arith.select %217, %215, %216 : index
    cf.br ^bb48(%212 : index)
    ^bb48(%218: index):
    %219 = arith.cmpi slt, %218, %213 : index
    %220 = arith.cmpi sgt, %218, %213 : index
    %221 = arith.select %217, %219, %220 : i1
    cf.cond_br %221, ^bb49(%218 : index), ^bb50(%218 : index)
    ^bb49(%222: index):
      %223 = llvm.load %210 : !llvm.ptr -> i64
      %224 = arith.constant 10 : i32
      %226 = arith.extsi %224 : i32 to i64
      %225 = arith.muli %223, %226 : i64
      %228 = arith.index_cast %222 : index to i64
      %229 = llvm.getelementptr %arg0[%228] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %227 = llvm.load %229 : !llvm.ptr -> i32
      %230 = arith.extsi %227 : i32 to i64
      %231 = arith.addi %225, %230 : i64
      llvm.store %231, %210 : i64, !llvm.ptr
      %232 = llvm.load %210 : !llvm.ptr -> i64
      %233 = arith.cmpi sge, %232, %arg2 : i64
      %234 = scf.if %233 -> (i1) {
        %236 = llvm.load %210 : !llvm.ptr -> i64
        %235 = func.call @is_prime(%236) : (i64) -> i1
        scf.yield %235 : i1
      } else {
        %237 = arith.constant false
        scf.yield %237 : i1
      }
      cf.cond_br %234, ^bb51, ^bb52
      ^bb51:
        %238 = llvm.load %206 : !llvm.ptr -> i64
        %240 = arith.constant 1 : i32
        %242 = arith.index_cast %222 : index to i32
        %241 = arith.addi %242, %240 : i32
        %243 = llvm.load %210 : !llvm.ptr -> i64
        %239 = func.call @count_splits(%arg0, %241, %243) : (!llvm.ptr, i32, i64) -> i64
        %244 = arith.addi %238, %239 : i64
        llvm.store %244, %206 : i64, !llvm.ptr
        cf.br ^bb53
      ^bb52:
        cf.br ^bb53
      ^bb53:
      %245 = arith.addi %222, %214 : index
      cf.br ^bb48(%245 : index)
    ^bb50(%246: index):
    %247 = llvm.load %206 : !llvm.ptr -> i64
    func.return %247 : i64
  }
  func.func @next_perm(%arg0: !llvm.ptr, %arg1: i32) -> i1 {
    %248 = arith.constant 2 : i32
    %249 = arith.subi %arg1, %248 : i32
    %250 = llvm.mlir.constant(1 : i64) : i64
    %251 = llvm.alloca %250 x i32 : (i64) -> !llvm.ptr
    llvm.store %249, %251 : i32, !llvm.ptr
    cf.br ^bb54
    ^bb54:
    %252 = llvm.load %251 : !llvm.ptr -> i32
    %253 = arith.constant 0 : i32
    %254 = arith.cmpi sge, %252, %253 : i32
    %255 = scf.if %254 -> (i1) {
      %257 = llvm.load %251 : !llvm.ptr -> i32
      %258 = arith.extsi %257 : i32 to i64
      %259 = llvm.getelementptr %arg0[%258] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %256 = llvm.load %259 : !llvm.ptr -> i32
      %261 = llvm.load %251 : !llvm.ptr -> i32
      %262 = arith.constant 1 : i32
      %263 = arith.addi %261, %262 : i32
      %264 = arith.extsi %263 : i32 to i64
      %265 = llvm.getelementptr %arg0[%264] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %260 = llvm.load %265 : !llvm.ptr -> i32
      %266 = arith.cmpi sge, %256, %260 : i32
      scf.yield %266 : i1
    } else {
      %267 = arith.constant false
      scf.yield %267 : i1
    }
    cf.cond_br %255, ^bb55, ^bb56
    ^bb55:
      %268 = llvm.load %251 : !llvm.ptr -> i32
      %269 = arith.constant 1 : i32
      %270 = arith.subi %268, %269 : i32
      llvm.store %270, %251 : i32, !llvm.ptr
      cf.br ^bb54
    ^bb56:
    %271 = llvm.load %251 : !llvm.ptr -> i32
    %272 = arith.constant 0 : i32
    %273 = arith.cmpi slt, %271, %272 : i32
    cf.cond_br %273, ^bb57, ^bb58
    ^bb57:
      %274 = arith.constant 0 : i1
      func.return %274 : i1
    ^bb58:
      cf.br ^bb59
    ^bb59:
    %275 = arith.constant 1 : i32
    %276 = arith.subi %arg1, %275 : i32
    %277 = llvm.mlir.constant(1 : i64) : i64
    %278 = llvm.alloca %277 x i32 : (i64) -> !llvm.ptr
    llvm.store %276, %278 : i32, !llvm.ptr
    cf.br ^bb60
    ^bb60:
    %280 = llvm.load %278 : !llvm.ptr -> i32
    %281 = arith.extsi %280 : i32 to i64
    %282 = llvm.getelementptr %arg0[%281] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %279 = llvm.load %282 : !llvm.ptr -> i32
    %284 = llvm.load %251 : !llvm.ptr -> i32
    %285 = arith.extsi %284 : i32 to i64
    %286 = llvm.getelementptr %arg0[%285] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %283 = llvm.load %286 : !llvm.ptr -> i32
    %287 = arith.cmpi sle, %279, %283 : i32
    cf.cond_br %287, ^bb61, ^bb62
    ^bb61:
      %288 = llvm.load %278 : !llvm.ptr -> i32
      %289 = arith.constant 1 : i32
      %290 = arith.subi %288, %289 : i32
      llvm.store %290, %278 : i32, !llvm.ptr
      cf.br ^bb60
    ^bb62:
    %292 = llvm.load %251 : !llvm.ptr -> i32
    %293 = arith.extsi %292 : i32 to i64
    %294 = llvm.getelementptr %arg0[%293] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %291 = llvm.load %294 : !llvm.ptr -> i32
    %296 = llvm.load %278 : !llvm.ptr -> i32
    %297 = arith.extsi %296 : i32 to i64
    %298 = llvm.getelementptr %arg0[%297] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %295 = llvm.load %298 : !llvm.ptr -> i32
    %299 = llvm.load %251 : !llvm.ptr -> i32
    %300 = arith.extsi %299 : i32 to i64
    %301 = llvm.getelementptr %arg0[%300] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %295, %301 : i32, !llvm.ptr
    %302 = llvm.load %278 : !llvm.ptr -> i32
    %303 = arith.extsi %302 : i32 to i64
    %304 = llvm.getelementptr %arg0[%303] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %291, %304 : i32, !llvm.ptr
    %305 = llvm.load %251 : !llvm.ptr -> i32
    %306 = arith.constant 1 : i32
    %307 = arith.addi %305, %306 : i32
    %308 = llvm.mlir.constant(1 : i64) : i64
    %309 = llvm.alloca %308 x i32 : (i64) -> !llvm.ptr
    llvm.store %307, %309 : i32, !llvm.ptr
    %310 = arith.constant 1 : i32
    %311 = arith.subi %arg1, %310 : i32
    %312 = llvm.mlir.constant(1 : i64) : i64
    %313 = llvm.alloca %312 x i32 : (i64) -> !llvm.ptr
    llvm.store %311, %313 : i32, !llvm.ptr
    cf.br ^bb63
    ^bb63:
    %314 = llvm.load %309 : !llvm.ptr -> i32
    %315 = llvm.load %313 : !llvm.ptr -> i32
    %316 = arith.cmpi slt, %314, %315 : i32
    cf.cond_br %316, ^bb64, ^bb65
    ^bb64:
      %318 = llvm.load %309 : !llvm.ptr -> i32
      %319 = arith.extsi %318 : i32 to i64
      %320 = llvm.getelementptr %arg0[%319] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %317 = llvm.load %320 : !llvm.ptr -> i32
      %322 = llvm.load %313 : !llvm.ptr -> i32
      %323 = arith.extsi %322 : i32 to i64
      %324 = llvm.getelementptr %arg0[%323] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %321 = llvm.load %324 : !llvm.ptr -> i32
      %325 = llvm.load %309 : !llvm.ptr -> i32
      %326 = arith.extsi %325 : i32 to i64
      %327 = llvm.getelementptr %arg0[%326] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %321, %327 : i32, !llvm.ptr
      %328 = llvm.load %313 : !llvm.ptr -> i32
      %329 = arith.extsi %328 : i32 to i64
      %330 = llvm.getelementptr %arg0[%329] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %317, %330 : i32, !llvm.ptr
      %331 = llvm.load %309 : !llvm.ptr -> i32
      %332 = arith.constant 1 : i32
      %333 = arith.addi %331, %332 : i32
      llvm.store %333, %309 : i32, !llvm.ptr
      %334 = llvm.load %313 : !llvm.ptr -> i32
      %335 = arith.constant 1 : i32
      %336 = arith.subi %334, %335 : i32
      llvm.store %336, %313 : i32, !llvm.ptr
      cf.br ^bb63
    ^bb65:
    %337 = arith.constant 1 : i1
    func.return %337 : i1
  }
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @main() -> i32 {
    %339 = arith.constant 9 : i32
    %340 = arith.constant 4 : i32
    %341 = arith.extsi %339 : i32 to i64
    %342 = arith.extsi %340 : i32 to i64
    %338 = func.call @calloc(%341, %342) : (i64, i64) -> !llvm.ptr
    %343 = llvm.mlir.zero : !llvm.ptr
    %344 = llvm.icmp "eq" %338, %343 : !llvm.ptr
    cf.cond_br %344, ^bb66, ^bb67
    ^bb66:
      %345 = arith.constant 1 : i32
      func.return %345 : i32
    ^bb67:
      cf.br ^bb68
    ^bb68:
    %346 = arith.constant 0 : i32
    %347 = arith.constant 9 : i32
    %348 = arith.index_cast %346 : i32 to index
    %349 = arith.index_cast %347 : i32 to index
    %351 = arith.constant 1 : index
    %352 = arith.constant -1 : index
    %353 = arith.cmpi sle, %348, %349 : index
    %350 = arith.select %353, %351, %352 : index
    cf.br ^bb69(%348 : index)
    ^bb69(%354: index):
    %355 = arith.cmpi slt, %354, %349 : index
    %356 = arith.cmpi sgt, %354, %349 : index
    %357 = arith.select %353, %355, %356 : i1
    cf.cond_br %357, ^bb70(%354 : index), ^bb71(%354 : index)
    ^bb70(%358: index):
      %359 = arith.constant 1 : i32
      %361 = arith.index_cast %358 : index to i32
      %360 = arith.addi %361, %359 : i32
      %362 = arith.index_cast %358 : index to i64
      %363 = llvm.getelementptr %338[%362] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %360, %363 : i32, !llvm.ptr
      %364 = arith.addi %358, %350 : index
      cf.br ^bb69(%364 : index)
    ^bb71(%365: index):
    %366 = arith.constant 0 : i32
    %367 = arith.extsi %366 : i32 to i64
    %368 = llvm.mlir.constant(1 : i64) : i64
    %369 = llvm.alloca %368 x i64 : (i64) -> !llvm.ptr
    llvm.store %367, %369 : i64, !llvm.ptr
    cf.br ^bb72
    ^bb72:
    %370 = arith.constant 1 : i1
    cf.cond_br %370, ^bb73, ^bb74
    ^bb73:
      %371 = llvm.load %369 : !llvm.ptr -> i64
      %373 = arith.constant 0 : i32
      %374 = arith.constant 0 : i32
      %375 = arith.extsi %374 : i32 to i64
      %372 = func.call @count_splits(%338, %373, %375) : (!llvm.ptr, i32, i64) -> i64
      %376 = arith.addi %371, %372 : i64
      llvm.store %376, %369 : i64, !llvm.ptr
      %378 = arith.constant 9 : i32
      %377 = func.call @next_perm(%338, %378) : (!llvm.ptr, i32) -> i1
      %380 = arith.constant 1 : i1
      %379 = arith.xori %377, %380 : i1
      cf.cond_br %379, ^bb75, ^bb76
      ^bb75:
        cf.br ^bb74
      ^bb76:
        cf.br ^bb77
      ^bb77:
      cf.br ^bb72
    ^bb74:
    %382 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %383 = llvm.load %369 : !llvm.ptr -> i64
    %384 = llvm.call @printf(%382, %383) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%338) : (!llvm.ptr) -> ()
    %386 = arith.constant 0 : i32
    func.return %386 : i32
  }
}