Problem 221

150000th Alexandrian integer.

Answer1884161251122450
Output1884161251122450
StatusPASS
Native helperno
Runtime6160 ms
Peak memory2320 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n log n)
Space complexityO(n)O(n)
ApproachFlow solutionDivisor-based enumeration
VerdictSuboptimal

Flow source

# Project Euler 221
# 150000th Alexandrian integer.

import euler.nt { isqrt }

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

function mul_ok(a: i64, b: i64) -> bool {
    if a == 0 || b == 0 { return true }
    let MAX: i64 = 9223372036854775807
    return a <= MAX / b
}

function main() -> i32 {
    let K: i64 = 150000
    let heap: ptr<i64> = calloc(K, 8)
    if heap == null { return 1 }
    let mut hn: i64 = 0
    let mut p: i64 = 1
    while true {
        let n: i64 = p * p + 1
        let root: i64 = isqrt(n)
        for d in 1..(root + 1) {
            if n % d == 0 {
                let e: i64 = n / d
                if mul_ok(p, p + d) && mul_ok(p * (p + d), p + e) {
                    let value: i64 = p * (p + d) * (p + e)
                    if hn < K {
                        let mut i: i64 = hn
                        heap[i] = value
                        while i > 0 {
                            let par: i64 = (i - 1) / 2
                            if heap[par] >= heap[i] { break }
                            let t: i64 = heap[par]; heap[par] = heap[i]; heap[i] = t
                            i = par
                        }
                        hn = hn + 1
                    } elif value < heap[0] {
                        heap[0] = value
                        let mut i: i64 = 0
                        while true {
                            let l: i64 = 2 * i + 1
                            let r: i64 = 2 * i + 2
                            let mut best: i64 = i
                            if l < hn && heap[l] > heap[best] { best = l }
                            if r < hn && heap[r] > heap[best] { best = r }
                            if best == i { break }
                            let t: i64 = heap[i]; heap[i] = heap[best]; heap[best] = t
                            i = best
                        }
                    }
                }
            }
        }
        p = p + 1
        if hn == K {
            if !mul_ok(p, p + 1) || !mul_ok(p * (p + 1), p + 1) || p * (p + 1) * (p + 1) > heap[0] {
                printf("%lld\n", heap[0])
                free(heap)
                return 0
            }
        }
    }
    return 1
}

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);
bool mul_ok_i64_i64(int64_t a, int64_t b);
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;
}



bool mul_ok_i64_i64(int64_t a, int64_t b) {
    if ((a == 0 || b == 0)) {
        return 1;
    }
    int64_t MAX = 9223372036854775807;
    return a <= FLOW_CHECKED_DIV((MAX), (b));
}

int32_t main(void) {
    int64_t K = 150000;
    int64_t* heap = (int64_t*)(calloc(K, 8));
    if (heap == NULL) {
        return 1;
    }
    int64_t hn = 0;
    int64_t p = 1;
    while (1) {
        int64_t n = ((p * p) + 1);
        int64_t root = isqrt_i64(n);
        int32_t __flow_step_1 = 1;
        for (int32_t d = 1; (1 <= (root + 1)) ? d < (root + 1) : d > (root + 1); d += (1 <= (root + 1)) ? 1 : -1) {
            if (FLOW_CHECKED_MOD((n), (d)) == 0) {
                int64_t e = FLOW_CHECKED_DIV((n), (d));
                if ((mul_ok_i64_i64(p, (p + d)) && mul_ok_i64_i64((p * (p + d)), (p + e)))) {
                    int64_t value = ((p * (p + d)) * (p + e));
                    if (hn < K) {
                        int64_t i = hn;
                        heap[i] = value;
                        while (i > 0) {
                            int64_t par = FLOW_CHECKED_DIV(((i - 1)), (2));
                            if (heap[par] >= heap[i]) {
                                break;
                            }
                            int64_t t = heap[par];
                            heap[par] = heap[i];
                            heap[i] = t;
                            i = par;
                        }
                        hn = (hn + 1);
                    } else if (value < heap[0]) {
                        heap[0] = value;
                        int64_t i = 0;
                        while (1) {
                            int64_t l = ((2 * i) + 1);
                            int64_t r = ((2 * i) + 2);
                            int64_t best = i;
                            if ((l < hn && heap[l] > heap[best])) {
                                best = l;
                            }
                            if ((r < hn && heap[r] > heap[best])) {
                                best = r;
                            }
                            if (best == i) {
                                break;
                            }
                            int64_t t = heap[i];
                            heap[i] = heap[best];
                            heap[best] = t;
                            i = best;
                        }
                    }
                }
            }
        }
        p = (p + 1);
        if (hn == K) {
            if ((((!(mul_ok_i64_i64(p, (p + 1)))) || (!(mul_ok_i64_i64((p * (p + 1)), (p + 1))))) || ((p * (p + 1)) * (p + 1)) > heap[0])) {
                printf("%lld\n", heap[0]);
                free(heap);
                return 0;
            }
        }
    }
    return 1;
}

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 @mul_ok(%arg0: i64, %arg1: i64) -> i1 {
    %175 = arith.constant 0 : i32
    %177 = arith.extsi %175 : i32 to i64
    %176 = arith.cmpi eq, %arg0, %177 : i64
    %178 = scf.if %176 -> (i1) {
      %179 = arith.constant true
      scf.yield %179 : i1
    } else {
      %180 = arith.constant 0 : i32
      %182 = arith.extsi %180 : i32 to i64
      %181 = arith.cmpi eq, %arg1, %182 : i64
      scf.yield %181 : i1
    }
    cf.cond_br %178, ^bb42, ^bb43
    ^bb42:
      %183 = arith.constant 1 : i1
      func.return %183 : i1
    ^bb43:
      cf.br ^bb44
    ^bb44:
    %184 = arith.constant 9223372032559808511 : i32
    %185 = arith.extsi %184 : i32 to i64
    %186 = arith.divsi %185, %arg1 : i64
    %187 = arith.cmpi sle, %arg0, %186 : i64
    func.return %187 : i1
  }
  func.func @main() -> i32 {
    %188 = arith.constant 150000 : i32
    %189 = arith.extsi %188 : i32 to i64
    %191 = arith.constant 8 : i32
    %192 = arith.extsi %191 : i32 to i64
    %190 = func.call @calloc(%189, %192) : (i64, i64) -> !llvm.ptr
    %193 = llvm.mlir.zero : !llvm.ptr
    %194 = llvm.icmp "eq" %190, %193 : !llvm.ptr
    cf.cond_br %194, ^bb45, ^bb46
    ^bb45:
      %195 = arith.constant 1 : i32
      func.return %195 : i32
    ^bb46:
      cf.br ^bb47
    ^bb47:
    %196 = arith.constant 0 : i32
    %197 = arith.extsi %196 : i32 to i64
    %198 = llvm.mlir.constant(1 : i64) : i64
    %199 = llvm.alloca %198 x i64 : (i64) -> !llvm.ptr
    llvm.store %197, %199 : i64, !llvm.ptr
    %200 = arith.constant 1 : i32
    %201 = arith.extsi %200 : i32 to i64
    %202 = llvm.mlir.constant(1 : i64) : i64
    %203 = llvm.alloca %202 x i64 : (i64) -> !llvm.ptr
    llvm.store %201, %203 : i64, !llvm.ptr
    cf.br ^bb48
    ^bb48:
    %204 = arith.constant 1 : i1
    cf.cond_br %204, ^bb49, ^bb50
    ^bb49:
      %205 = llvm.load %203 : !llvm.ptr -> i64
      %206 = llvm.load %203 : !llvm.ptr -> i64
      %207 = arith.muli %205, %206 : i64
      %208 = arith.constant 1 : i32
      %210 = arith.extsi %208 : i32 to i64
      %209 = arith.addi %207, %210 : i64
      %211 = func.call @isqrt(%209) : (i64) -> i64
      %212 = arith.constant 1 : i32
      %213 = arith.constant 1 : i32
      %215 = arith.extsi %213 : i32 to i64
      %214 = arith.addi %211, %215 : i64
      %216 = arith.index_cast %212 : i32 to index
      %217 = arith.index_cast %214 : i32 to index
      %219 = arith.constant 1 : index
      %220 = arith.constant -1 : index
      %221 = arith.cmpi sle, %216, %217 : index
      %218 = arith.select %221, %219, %220 : index
      cf.br ^bb51(%216 : index)
      ^bb51(%222: index):
      %223 = arith.cmpi slt, %222, %217 : index
      %224 = arith.cmpi sgt, %222, %217 : index
      %225 = arith.select %221, %223, %224 : i1
      cf.cond_br %225, ^bb52(%222 : index), ^bb53(%222 : index)
      ^bb52(%226: index):
        %228 = arith.trunci %209 : i64 to i32
        %229 = arith.index_cast %226 : index to i32
        %227 = arith.remsi %228, %229 : i32
        %230 = arith.constant 0 : i32
        %231 = arith.cmpi eq, %227, %230 : i32
        cf.cond_br %231, ^bb54, ^bb55
        ^bb54:
          %233 = arith.trunci %209 : i64 to i32
          %234 = arith.index_cast %226 : index to i32
          %232 = arith.divsi %233, %234 : i32
          %235 = arith.extsi %232 : i32 to i64
          %237 = llvm.load %203 : !llvm.ptr -> i64
          %238 = llvm.load %203 : !llvm.ptr -> i64
          %240 = arith.trunci %238 : i64 to i32
          %241 = arith.index_cast %226 : index to i32
          %239 = arith.addi %240, %241 : i32
          %242 = arith.extsi %239 : i32 to i64
          %236 = func.call @mul_ok(%237, %242) : (i64, i64) -> i1
          %243 = scf.if %236 -> (i1) {
            %245 = llvm.load %203 : !llvm.ptr -> i64
            %246 = llvm.load %203 : !llvm.ptr -> i64
            %248 = arith.trunci %246 : i64 to i32
            %249 = arith.index_cast %226 : index to i32
            %247 = arith.addi %248, %249 : i32
            %251 = arith.extsi %247 : i32 to i64
            %250 = arith.muli %245, %251 : i64
            %252 = llvm.load %203 : !llvm.ptr -> i64
            %253 = arith.addi %252, %235 : i64
            %244 = func.call @mul_ok(%250, %253) : (i64, i64) -> i1
            scf.yield %244 : i1
          } else {
            %254 = arith.constant false
            scf.yield %254 : i1
          }
          cf.cond_br %243, ^bb57, ^bb58
          ^bb57:
            %255 = llvm.load %203 : !llvm.ptr -> i64
            %256 = llvm.load %203 : !llvm.ptr -> i64
            %258 = arith.trunci %256 : i64 to i32
            %259 = arith.index_cast %226 : index to i32
            %257 = arith.addi %258, %259 : i32
            %261 = arith.extsi %257 : i32 to i64
            %260 = arith.muli %255, %261 : i64
            %262 = llvm.load %203 : !llvm.ptr -> i64
            %263 = arith.addi %262, %235 : i64
            %264 = arith.muli %260, %263 : i64
            %265 = llvm.load %199 : !llvm.ptr -> i64
            %266 = arith.cmpi slt, %265, %189 : i64
            cf.cond_br %266, ^bb60, ^bb61
            ^bb60:
              %267 = llvm.load %199 : !llvm.ptr -> i64
              %268 = llvm.mlir.constant(1 : i64) : i64
              %269 = llvm.alloca %268 x i64 : (i64) -> !llvm.ptr
              llvm.store %267, %269 : i64, !llvm.ptr
              %270 = llvm.load %269 : !llvm.ptr -> i64
              %271 = llvm.getelementptr %190[%270] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              llvm.store %264, %271 : i64, !llvm.ptr
              cf.br ^bb63
              ^bb63:
              %272 = llvm.load %269 : !llvm.ptr -> i64
              %273 = arith.constant 0 : i32
              %275 = arith.extsi %273 : i32 to i64
              %274 = arith.cmpi sgt, %272, %275 : i64
              cf.cond_br %274, ^bb64, ^bb65
              ^bb64:
                %276 = llvm.load %269 : !llvm.ptr -> i64
                %277 = arith.constant 1 : i32
                %279 = arith.extsi %277 : i32 to i64
                %278 = arith.subi %276, %279 : i64
                %280 = arith.constant 2 : i32
                %282 = arith.extsi %280 : i32 to i64
                %281 = arith.divsi %278, %282 : i64
                %284 = llvm.getelementptr %190[%281] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                %283 = llvm.load %284 : !llvm.ptr -> i64
                %286 = llvm.load %269 : !llvm.ptr -> i64
                %287 = llvm.getelementptr %190[%286] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                %285 = llvm.load %287 : !llvm.ptr -> i64
                %288 = arith.cmpi sge, %283, %285 : i64
                cf.cond_br %288, ^bb66, ^bb67
                ^bb66:
                  cf.br ^bb65
                ^bb67:
                  cf.br ^bb68
                ^bb68:
                %290 = llvm.getelementptr %190[%281] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                %289 = llvm.load %290 : !llvm.ptr -> i64
                %292 = llvm.load %269 : !llvm.ptr -> i64
                %293 = llvm.getelementptr %190[%292] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                %291 = llvm.load %293 : !llvm.ptr -> i64
                %294 = llvm.getelementptr %190[%281] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                llvm.store %291, %294 : i64, !llvm.ptr
                %295 = llvm.load %269 : !llvm.ptr -> i64
                %296 = llvm.getelementptr %190[%295] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                llvm.store %289, %296 : i64, !llvm.ptr
                llvm.store %281, %269 : i64, !llvm.ptr
                cf.br ^bb63
              ^bb65:
              %297 = llvm.load %199 : !llvm.ptr -> i64
              %298 = arith.constant 1 : i32
              %300 = arith.extsi %298 : i32 to i64
              %299 = arith.addi %297, %300 : i64
              llvm.store %299, %199 : i64, !llvm.ptr
              cf.br ^bb62
            ^bb61:
              %302 = arith.constant 0 : i32
              %303 = arith.extsi %302 : i32 to i64
              %304 = llvm.getelementptr %190[%303] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              %301 = llvm.load %304 : !llvm.ptr -> i64
              %305 = arith.cmpi slt, %264, %301 : i64
              cf.cond_br %305, ^bb69, ^bb62
            ^bb69:
              %306 = arith.constant 0 : i32
              %307 = arith.extsi %306 : i32 to i64
              %308 = llvm.getelementptr %190[%307] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              llvm.store %264, %308 : i64, !llvm.ptr
              %309 = arith.constant 0 : i32
              %310 = arith.extsi %309 : i32 to i64
              %311 = llvm.mlir.constant(1 : i64) : i64
              %312 = llvm.alloca %311 x i64 : (i64) -> !llvm.ptr
              llvm.store %310, %312 : i64, !llvm.ptr
              cf.br ^bb70
              ^bb70:
              %313 = arith.constant 1 : i1
              cf.cond_br %313, ^bb71, ^bb72
              ^bb71:
                %314 = arith.constant 2 : i32
                %315 = llvm.load %312 : !llvm.ptr -> i64
                %317 = arith.extsi %314 : i32 to i64
                %316 = arith.muli %317, %315 : i64
                %318 = arith.constant 1 : i32
                %320 = arith.extsi %318 : i32 to i64
                %319 = arith.addi %316, %320 : i64
                %321 = arith.constant 2 : i32
                %322 = llvm.load %312 : !llvm.ptr -> i64
                %324 = arith.extsi %321 : i32 to i64
                %323 = arith.muli %324, %322 : i64
                %325 = arith.constant 2 : i32
                %327 = arith.extsi %325 : i32 to i64
                %326 = arith.addi %323, %327 : i64
                %328 = llvm.load %312 : !llvm.ptr -> i64
                %329 = llvm.mlir.constant(1 : i64) : i64
                %330 = llvm.alloca %329 x i64 : (i64) -> !llvm.ptr
                llvm.store %328, %330 : i64, !llvm.ptr
                %331 = llvm.load %199 : !llvm.ptr -> i64
                %332 = arith.cmpi slt, %319, %331 : i64
                %333 = scf.if %332 -> (i1) {
                  %335 = llvm.getelementptr %190[%319] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                  %334 = llvm.load %335 : !llvm.ptr -> i64
                  %337 = llvm.load %330 : !llvm.ptr -> i64
                  %338 = llvm.getelementptr %190[%337] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                  %336 = llvm.load %338 : !llvm.ptr -> i64
                  %339 = arith.cmpi sgt, %334, %336 : i64
                  scf.yield %339 : i1
                } else {
                  %340 = arith.constant false
                  scf.yield %340 : i1
                }
                cf.cond_br %333, ^bb73, ^bb74
                ^bb73:
                  llvm.store %319, %330 : i64, !llvm.ptr
                  cf.br ^bb75
                ^bb74:
                  cf.br ^bb75
                ^bb75:
                %341 = llvm.load %199 : !llvm.ptr -> i64
                %342 = arith.cmpi slt, %326, %341 : i64
                %343 = scf.if %342 -> (i1) {
                  %345 = llvm.getelementptr %190[%326] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                  %344 = llvm.load %345 : !llvm.ptr -> i64
                  %347 = llvm.load %330 : !llvm.ptr -> i64
                  %348 = llvm.getelementptr %190[%347] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                  %346 = llvm.load %348 : !llvm.ptr -> i64
                  %349 = arith.cmpi sgt, %344, %346 : i64
                  scf.yield %349 : i1
                } else {
                  %350 = arith.constant false
                  scf.yield %350 : i1
                }
                cf.cond_br %343, ^bb76, ^bb77
                ^bb76:
                  llvm.store %326, %330 : i64, !llvm.ptr
                  cf.br ^bb78
                ^bb77:
                  cf.br ^bb78
                ^bb78:
                %351 = llvm.load %330 : !llvm.ptr -> i64
                %352 = llvm.load %312 : !llvm.ptr -> i64
                %353 = arith.cmpi eq, %351, %352 : i64
                cf.cond_br %353, ^bb79, ^bb80
                ^bb79:
                  cf.br ^bb72
                ^bb80:
                  cf.br ^bb81
                ^bb81:
                %355 = llvm.load %312 : !llvm.ptr -> i64
                %356 = llvm.getelementptr %190[%355] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                %354 = llvm.load %356 : !llvm.ptr -> i64
                %358 = llvm.load %330 : !llvm.ptr -> i64
                %359 = llvm.getelementptr %190[%358] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                %357 = llvm.load %359 : !llvm.ptr -> i64
                %360 = llvm.load %312 : !llvm.ptr -> i64
                %361 = llvm.getelementptr %190[%360] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                llvm.store %357, %361 : i64, !llvm.ptr
                %362 = llvm.load %330 : !llvm.ptr -> i64
                %363 = llvm.getelementptr %190[%362] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                llvm.store %354, %363 : i64, !llvm.ptr
                %364 = llvm.load %330 : !llvm.ptr -> i64
                llvm.store %364, %312 : i64, !llvm.ptr
                cf.br ^bb70
              ^bb72:
              cf.br ^bb62
            ^bb62:
            cf.br ^bb59
          ^bb58:
            cf.br ^bb59
          ^bb59:
          cf.br ^bb56
        ^bb55:
          cf.br ^bb56
        ^bb56:
        %365 = arith.addi %226, %218 : index
        cf.br ^bb51(%365 : index)
      ^bb53(%366: index):
      %367 = llvm.load %203 : !llvm.ptr -> i64
      %368 = arith.constant 1 : i32
      %370 = arith.extsi %368 : i32 to i64
      %369 = arith.addi %367, %370 : i64
      llvm.store %369, %203 : i64, !llvm.ptr
      %371 = llvm.load %199 : !llvm.ptr -> i64
      %372 = arith.cmpi eq, %371, %189 : i64
      cf.cond_br %372, ^bb82, ^bb83
      ^bb82:
        %374 = llvm.load %203 : !llvm.ptr -> i64
        %375 = llvm.load %203 : !llvm.ptr -> i64
        %376 = arith.constant 1 : i32
        %378 = arith.extsi %376 : i32 to i64
        %377 = arith.addi %375, %378 : i64
        %373 = func.call @mul_ok(%374, %377) : (i64, i64) -> i1
        %380 = arith.constant 1 : i1
        %379 = arith.xori %373, %380 : i1
        %382 = scf.if %379 -> (i1) {
          %383 = arith.constant true
          scf.yield %383 : i1
        } else {
          %385 = llvm.load %203 : !llvm.ptr -> i64
          %386 = llvm.load %203 : !llvm.ptr -> i64
          %387 = arith.constant 1 : i32
          %389 = arith.extsi %387 : i32 to i64
          %388 = arith.addi %386, %389 : i64
          %390 = arith.muli %385, %388 : i64
          %391 = llvm.load %203 : !llvm.ptr -> i64
          %392 = arith.constant 1 : i32
          %394 = arith.extsi %392 : i32 to i64
          %393 = arith.addi %391, %394 : i64
          %384 = func.call @mul_ok(%390, %393) : (i64, i64) -> i1
          %396 = arith.constant 1 : i1
          %395 = arith.xori %384, %396 : i1
          scf.yield %395 : i1
        }
        %398 = scf.if %382 -> (i1) {
          %399 = arith.constant true
          scf.yield %399 : i1
        } else {
          %400 = llvm.load %203 : !llvm.ptr -> i64
          %401 = llvm.load %203 : !llvm.ptr -> i64
          %402 = arith.constant 1 : i32
          %404 = arith.extsi %402 : i32 to i64
          %403 = arith.addi %401, %404 : i64
          %405 = arith.muli %400, %403 : i64
          %406 = llvm.load %203 : !llvm.ptr -> i64
          %407 = arith.constant 1 : i32
          %409 = arith.extsi %407 : i32 to i64
          %408 = arith.addi %406, %409 : i64
          %410 = arith.muli %405, %408 : i64
          %412 = arith.constant 0 : i32
          %413 = arith.extsi %412 : i32 to i64
          %414 = llvm.getelementptr %190[%413] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %411 = llvm.load %414 : !llvm.ptr -> i64
          %415 = arith.cmpi sgt, %410, %411 : i64
          scf.yield %415 : i1
        }
        cf.cond_br %398, ^bb85, ^bb86
        ^bb85:
          %416 = llvm.mlir.addressof @str_0 : !llvm.ptr
          %418 = arith.constant 0 : i32
          %419 = arith.extsi %418 : i32 to i64
          %420 = llvm.getelementptr %190[%419] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %417 = llvm.load %420 : !llvm.ptr -> i64
          %421 = llvm.call @printf(%416, %417) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
          func.call @free(%190) : (!llvm.ptr) -> ()
          %423 = arith.constant 0 : i32
          func.return %423 : i32
        ^bb86:
          cf.br ^bb87
        ^bb87:
        cf.br ^bb84
      ^bb83:
        cf.br ^bb84
      ^bb84:
      cf.br ^bb48
    ^bb50:
    %424 = arith.constant 1 : i32
    func.return %424 : i32
  }
}