Problem 549

Divisibility of Factorials — S(10^8) via prime-power sieve.

Answer476001479068717
Output476001479068717
StatusPASS
Native helperno
Runtime2960 ms
Peak memory489408 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n)
Space complexityO(n)O(n)
ApproachFlow solutionBig-integer factorial
VerdictSuboptimal

Flow source

# Project Euler 549
# Divisibility of Factorials — S(10^8) via prime-power sieve.

import euler.nt { isqrt }

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

function fact_val(n0: i64, p: i64) -> i64 {
    let mut n: i64 = n0
    let mut total: i64 = 0
    while n > 0 {
        n = n / p
        total = total + n
    }
    return total
}

function threshold(p: i64, exponent: i64) -> i64 {
    let mut lo: i64 = 1
    let mut hi: i64 = p * exponent
    while lo < hi {
        let mid: i64 = (lo + hi) / 2
        if fact_val(mid, p) >= exponent {
            hi = mid
        } else {
            lo = mid + 1
        }
    }
    return lo
}

function main() -> i32 {
    let limit: i64 = 100000000
    let values: ptr<i32> = calloc(limit + 1, 4)
    let sieve: ptr<i8> = calloc(limit + 1, 1)
    if values == null || sieve == null { return 1 }

    let mut i: i64 = 2
    while i <= limit {
        sieve[i] = 1
        i = i + 1
    }
    i = 2
    while i * i <= limit {
        if sieve[i] == 1 {
            let mut j: i64 = i * i
            while j <= limit {
                sieve[j] = 0
                j = j + i
            }
        }
        i = i + 1
    }

    # Initial: s(p*k) at least p for prime p
    let mut p: i64 = 2
    while p <= limit {
        if sieve[p] == 1 {
            let mut m: i64 = p
            while m <= limit {
                values[m] = p as i32
                m = m + p
            }
        }
        p = p + 1
    }

    let root: i64 = isqrt(limit)
    p = 2
    while p <= root {
        if sieve[p] == 1 {
            let mut power: i64 = p * p
            let mut exponent: i64 = 2
            while power <= limit {
                let thr: i64 = threshold(p, exponent)
                let mut m: i64 = power
                while m <= limit {
                    if (values[m] as i64) < thr {
                        values[m] = thr as i32
                    }
                    m = m + power
                }
                # careful overflow for large p^e
                if power > limit / p { break }
                power = power * p
                exponent = exponent + 1
            }
        }
        p = p + 1
    }

    let mut total: i64 = 0
    i = 2
    while i <= limit {
        total = total + (values[i] as i64)
        i = i + 1
    }
    printf("%lld\n", total)
    free(values)
    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; }

int64_t gcd_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t fact_val_i64_i64(int64_t n0, int64_t p);
int64_t threshold_i64_i64(int64_t p, int64_t exponent);
int32_t main(void);

int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
    int64_t a = a0;
    int64_t b = b0;
    while (b != 0) {
        int64_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
    }
    return a;
}

int64_t lcm_i64_i64(int64_t a, int64_t b) {
    if ((a == 0 || b == 0)) {
        return 0;
    }
    return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}

int64_t isqrt_i64(int64_t n) {
    if (n < 2) {
        return n;
    }
    int64_t x = n;
    int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

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 mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    if (mod == 1) {
        return 0;
    }
    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 (n < 4) {
        return 1;
    }
    if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
        return 0;
    }
    int64_t i = 5;
    while ((i * i) <= n) {
        if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
            return 0;
        }
        i = (i + 6);
    }
    return 1;
}



int64_t fact_val_i64_i64(int64_t n0, int64_t p) {
    int64_t n = n0;
    int64_t total = 0;
    while (n > 0) {
        n = FLOW_CHECKED_DIV((n), (p));
        total = (total + n);
    }
    return total;
}

int64_t threshold_i64_i64(int64_t p, int64_t exponent) {
    int64_t lo = 1;
    int64_t hi = (p * exponent);
    while (lo < hi) {
        int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
        if (fact_val_i64_i64(mid, p) >= exponent) {
            hi = mid;
        } else {
            lo = (mid + 1);
        }
    }
    return lo;
}

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