Problem 192

Sum of denominators of best approximations to sqrt(n), bound 10^12, n<=100000.

Answer57060635927998347
Output57060635927998347
StatusPASS
Native helperno
Runtime20 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(log n)
Space complexityO(1)O(1)
ApproachFlow solutionContinued fraction convergents
VerdictSuboptimal

Flow source

# Project Euler 192
# Sum of denominators of best approximations to sqrt(n), bound 10^12, n<=100000.

import euler.nt { isqrt }

function best_denom(n: i64, bound: i64) -> i64 {
    let a0: i64 = isqrt(n)
    if a0 * a0 == n { return 0 }
    let mut m: i64 = 0
    let mut d: i64 = 1
    let mut a: i64 = a0
    let mut p_m2: i64 = 0
    let mut p_m1: i64 = 1
    let mut q_m2: i64 = 1
    let mut q_m1: i64 = 0
    let mut p: i64 = a * p_m1 + p_m2
    let mut q: i64 = a * q_m1 + q_m2
    p_m2 = p_m1; p_m1 = p
    q_m2 = q_m1; q_m1 = q

    while true {
        m = d * a - m
        d = (n - m * m) / d
        a = (a0 + m) / d
        p = a * p_m1 + p_m2
        q = a * q_m1 + q_m2
        if q > bound {
            let pC: i64 = p_m1
            let qC: i64 = q_m1
            let pP: i64 = p_m2
            let qP: i64 = q_m2
            let t: i64 = (bound - qP) / qC
            if t <= 0 { return qC }
            let pS: i64 = t * pC + pP
            let qS: i64 = t * qC + qP
            let sC: i128 = (pC as i128) * (pC as i128) - (n as i128) * (qC as i128) * (qC as i128)
            let sS: i128 = (pS as i128) * (pS as i128) - (n as i128) * (qS as i128) * (qS as i128)
            if sC < (0 as i128) && sS < (0 as i128) {
                if (pS as i128) * (qC as i128) > (pC as i128) * (qS as i128) { return qS }
                return qC
            }
            if sC > (0 as i128) && sS > (0 as i128) {
                if (pS as i128) * (qC as i128) < (pC as i128) * (qS as i128) { return qS }
                return qC
            }
            let mut pL: i64 = 0
            let mut qL: i64 = 0
            let mut pH: i64 = 0
            let mut qH: i64 = 0
            if (pC as i128) * (qS as i128) < (pS as i128) * (qC as i128) {
                pL = pC; qL = qC; pH = pS; qH = qS
            } else {
                pL = pS; qL = qS; pH = pC; qH = qC
            }
            let num: i128 = (pL as i128) * (qH as i128) + (pH as i128) * (qL as i128)
            let den: i128 = (2 as i128) * (qL as i128) * (qH as i128)
            if (n as i128) * den * den <= num * num {
                return qL
            }
            return qH
        }
        p_m2 = p_m1; p_m1 = p
        q_m2 = q_m1; q_m1 = q
    }
    return 0
}

function main() -> i32 {
    let bound: i64 = 1000000000000
    let mut s: i64 = 0
    let mut n: i64 = 2
    while n <= 100000 {
        let r: i64 = isqrt(n)
        if r * r != n {
            s = s + best_denom(n, bound)
        }
        n = n + 1
    }
    printf("%lld\n", s)
    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 best_denom_i64_i64(int64_t n, int64_t bound);
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 best_denom_i64_i64(int64_t n, int64_t bound) {
    int64_t a0 = isqrt_i64(n);
    if ((a0 * a0) == n) {
        return 0;
    }
    int64_t m = 0;
    int64_t d = 1;
    int64_t a = a0;
    int64_t p_m2 = 0;
    int64_t p_m1 = 1;
    int64_t q_m2 = 1;
    int64_t q_m1 = 0;
    int64_t p = ((a * p_m1) + p_m2);
    int64_t q = ((a * q_m1) + q_m2);
    p_m2 = p_m1;
    p_m1 = p;
    q_m2 = q_m1;
    q_m1 = q;
    while (1) {
        m = ((d * a) - m);
        d = FLOW_CHECKED_DIV(((n - (m * m))), (d));
        a = FLOW_CHECKED_DIV(((a0 + m)), (d));
        p = ((a * p_m1) + p_m2);
        q = ((a * q_m1) + q_m2);
        if (q > bound) {
            int64_t pC = p_m1;
            int64_t qC = q_m1;
            int64_t pP = p_m2;
            int64_t qP = q_m2;
            int64_t t = FLOW_CHECKED_DIV(((bound - qP)), (qC));
            if (t <= 0) {
                return qC;
            }
            int64_t pS = ((t * pC) + pP);
            int64_t qS = ((t * qC) + qP);
            __int128 sC = ((((__int128)(pC)) * ((__int128)(pC))) - ((((__int128)(n)) * ((__int128)(qC))) * ((__int128)(qC))));
            __int128 sS = ((((__int128)(pS)) * ((__int128)(pS))) - ((((__int128)(n)) * ((__int128)(qS))) * ((__int128)(qS))));
            if ((sC < ((__int128)(0)) && sS < ((__int128)(0)))) {
                if ((((__int128)(pS)) * ((__int128)(qC))) > (((__int128)(pC)) * ((__int128)(qS)))) {
                    return qS;
                }
                return qC;
            }
            if ((sC > ((__int128)(0)) && sS > ((__int128)(0)))) {
                if ((((__int128)(pS)) * ((__int128)(qC))) < (((__int128)(pC)) * ((__int128)(qS)))) {
                    return qS;
                }
                return qC;
            }
            int64_t pL = 0;
            int64_t qL = 0;
            int64_t pH = 0;
            int64_t qH = 0;
            if ((((__int128)(pC)) * ((__int128)(qS))) < (((__int128)(pS)) * ((__int128)(qC)))) {
                pL = pC;
                qL = qC;
                pH = pS;
                qH = qS;
            } else {
                pL = pS;
                qL = qS;
                pH = pC;
                qH = qC;
            }
            __int128 num = ((((__int128)(pL)) * ((__int128)(qH))) + (((__int128)(pH)) * ((__int128)(qL))));
            __int128 den = ((((__int128)(2)) * ((__int128)(qL))) * ((__int128)(qH)));
            if (((((__int128)(n)) * den) * den) <= (num * num)) {
                return qL;
            }
            return qH;
        }
        p_m2 = p_m1;
        p_m1 = p;
        q_m2 = q_m1;
        q_m1 = q;
    }
    return 0;
}

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