Problem 656

Palindromic Sequences: sum of H_100(sqrt(beta)) for non-square beta in 2..1000. Uses continued fraction periods and palindromic prefix length formula.

Answer888873503555187
Output888873503555187
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n^2)
Space complexityO(1)O(1)
ApproachFlow solutionBrute-force or constructive search
VerdictOptimal

Flow source

# Project Euler 656
# Palindromic Sequences: sum of H_100(sqrt(beta)) for non-square beta in 2..1000.
# Uses continued fraction periods and palindromic prefix length formula.

import euler.nt { isqrt }

# Compute the continued fraction period of sqrt(n) for non-square n.
# Returns period length in *plen, period coefficients in *period.
function cf_period(n: i64, period: ptr<i64>) -> i64 {
    let a0: i64 = isqrt(n)
    let mut m: i64 = 0
    let mut d: i64 = 1
    let mut a: i64 = a0
    let mut len: i64 = 0
    while true {
        m = d * a - m
        d = (n - m * m) / d
        a = (a0 + m) / d
        period[len] = a
        len = len + 1
        if a == 2 * a0 { break }
    }
    return len
}

# Compute H_g(sqrt(beta)) mod 10^15 using the continued fraction period.
# Palindromic prefix lengths: n = q_{k-2} + t * q_{k-1} for odd k, 1 <= t <= a_k.
function H_mod(period: ptr<i64>, plen: i64, g: i64, mod: i64) -> i64 {
    let mut q_m1: i64 = 0
    let mut q0: i64 = 1
    let mut k: i64 = 1
    let mut i: i64 = 0
    let mut count: i64 = 0
    let mut s: i64 = 0
    while count < g {
        let a: i64 = period[i]
        i = i + 1
        if i == plen { i = 0 }

        if k % 2 == 1 {
            let remaining: i64 = g - count
            let tmax: i64 = a
            if tmax > remaining { tmax = remaining }
            # sum_{t=1..tmax} (q_m1 + t*q0) = tmax*q_m1 + q0*tmax*(tmax+1)/2
            let t1: i128 = ((tmax % mod) as i128) * ((q_m1 % mod) as i128) % (mod as i128)
            s = (s + (t1 as i64)) % mod
            let tri_val: i64 = (tmax * (tmax + 1) / 2) % mod
            let t2: i128 = ((q0 % mod) as i128) * (tri_val as i128) % (mod as i128)
            s = (s + (t2 as i64)) % mod
            count = count + tmax
        }

        let q1: i64 = (((a as i128) * (q0 as i128) + (q_m1 as i128)) % (mod as i128)) as i64
        q_m1 = q0
        q0 = q1
        k = k + 1
    }
    return s % mod
}

function main() -> i32 {
    let MOD: i64 = 1000000000000000
    let LIMIT_BETA: i64 = 1000
    let G: i64 = 100

    let period: ptr<i64> = calloc(256, 8)
    if period == null { return 1 }

    let mut total: i64 = 0
    for beta in 2..(LIMIT_BETA + 1) {
        let r: i64 = isqrt(beta)
        if r * r == beta { continue }
        let plen: i64 = cf_period(beta, period)
        let h: i64 = H_mod(period, plen, G, MOD)
        total = (total + h) % MOD
        if total < 0 { total = total + MOD }
    }

    printf("%015lld\n", total)
    free(period)
    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 cf_period_i64_ptr_i64(int64_t n, int64_t* period);
int64_t H_mod_ptr_i64_i64_i64_i64(int64_t* period, int64_t plen, int64_t g, int64_t mod);
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 cf_period_i64_ptr_i64(int64_t n, int64_t* period) {
    int64_t a0 = isqrt_i64(n);
    int64_t m = 0;
    int64_t d = 1;
    int64_t a = a0;
    int64_t len = 0;
    while (1) {
        m = ((d * a) - m);
        d = FLOW_CHECKED_DIV(((n - (m * m))), (d));
        a = FLOW_CHECKED_DIV(((a0 + m)), (d));
        period[len] = a;
        len = (len + 1);
        if (a == (2 * a0)) {
            break;
        }
    }
    return len;
}

int64_t H_mod_ptr_i64_i64_i64_i64(int64_t* period, int64_t plen, int64_t g, int64_t mod) {
    int64_t q_m1 = 0;
    int64_t q0 = 1;
    int64_t k = 1;
    int64_t i = 0;
    int64_t count = 0;
    int64_t s = 0;
    while (count < g) {
        int64_t a = period[i];
        i = (i + 1);
        if (i == plen) {
            i = 0;
        }
        if (FLOW_CHECKED_MOD((k), (2)) == 1) {
            int64_t remaining = (g - count);
            int64_t tmax = a;
            if (tmax > remaining) {
                tmax = remaining;
            }
            __int128 t1 = FLOW_CHECKED_MOD(((((__int128)(FLOW_CHECKED_MOD((tmax), (mod)))) * ((__int128)(FLOW_CHECKED_MOD((q_m1), (mod)))))), (((__int128)(mod))));
            s = FLOW_CHECKED_MOD(((s + ((int64_t)(t1)))), (mod));
            int64_t tri_val = FLOW_CHECKED_MOD((FLOW_CHECKED_DIV(((tmax * (tmax + 1))), (2))), (mod));
            __int128 t2 = FLOW_CHECKED_MOD(((((__int128)(FLOW_CHECKED_MOD((q0), (mod)))) * ((__int128)(tri_val)))), (((__int128)(mod))));
            s = FLOW_CHECKED_MOD(((s + ((int64_t)(t2)))), (mod));
            count = (count + tmax);
        }
        int64_t q1 = ((int64_t)(FLOW_CHECKED_MOD((((((__int128)(a)) * ((__int128)(q0))) + ((__int128)(q_m1)))), (((__int128)(mod))))));
        q_m1 = q0;
        q0 = q1;
        k = (k + 1);
    }
    return FLOW_CHECKED_MOD((s), (mod));
}

int32_t main(void) {
    int64_t MOD = 1000000000000000;
    int64_t LIMIT_BETA = 1000;
    int64_t G = 100;
    int64_t* period = (int64_t*)(calloc(256, 8));
    if (period == NULL) {
        return 1;
    }
    int64_t total = 0;
    int32_t __flow_step_1 = 1;
    for (int32_t beta = 2; (2 <= (LIMIT_BETA + 1)) ? beta < (LIMIT_BETA + 1) : beta > (LIMIT_BETA + 1); beta += (2 <= (LIMIT_BETA + 1)) ? 1 : -1) {
        int64_t r = isqrt_i64(beta);
        if ((r * r) == beta) {
            continue;
        }
        int64_t plen = cf_period_i64_ptr_i64(beta, period);
        int64_t h = H_mod_ptr_i64_i64_i64_i64(period, plen, G, MOD);
        total = FLOW_CHECKED_MOD(((total + h)), (MOD));
        if (total < 0) {
            total = (total + MOD);
        }
    }
    printf("%015lld\n", total);
    free(period);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%015lld\n\00") {addr_space = 0 : i32} : !llvm.array<9 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 @cf_period(%arg0: i64, %arg1: !llvm.ptr) -> i64 {
    %175 = func.call @isqrt(%arg0) : (i64) -> i64
    %176 = arith.constant 0 : i32
    %177 = arith.extsi %176 : i32 to i64
    %178 = llvm.mlir.constant(1 : i64) : i64
    %179 = llvm.alloca %178 x i64 : (i64) -> !llvm.ptr
    llvm.store %177, %179 : i64, !llvm.ptr
    %180 = arith.constant 1 : 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 = llvm.mlir.constant(1 : i64) : i64
    %185 = llvm.alloca %184 x i64 : (i64) -> !llvm.ptr
    llvm.store %175, %185 : i64, !llvm.ptr
    %186 = arith.constant 0 : i32
    %187 = arith.extsi %186 : i32 to i64
    %188 = llvm.mlir.constant(1 : i64) : i64
    %189 = llvm.alloca %188 x i64 : (i64) -> !llvm.ptr
    llvm.store %187, %189 : i64, !llvm.ptr
    cf.br ^bb42
    ^bb42:
    %190 = arith.constant 1 : i1
    cf.cond_br %190, ^bb43, ^bb44
    ^bb43:
      %191 = llvm.load %183 : !llvm.ptr -> i64
      %192 = llvm.load %185 : !llvm.ptr -> i64
      %193 = arith.muli %191, %192 : i64
      %194 = llvm.load %179 : !llvm.ptr -> i64
      %195 = arith.subi %193, %194 : i64
      llvm.store %195, %179 : i64, !llvm.ptr
      %196 = llvm.load %179 : !llvm.ptr -> i64
      %197 = llvm.load %179 : !llvm.ptr -> i64
      %198 = arith.muli %196, %197 : i64
      %199 = arith.subi %arg0, %198 : i64
      %200 = llvm.load %183 : !llvm.ptr -> i64
      %201 = arith.divsi %199, %200 : i64
      llvm.store %201, %183 : i64, !llvm.ptr
      %202 = llvm.load %179 : !llvm.ptr -> i64
      %203 = arith.addi %175, %202 : i64
      %204 = llvm.load %183 : !llvm.ptr -> i64
      %205 = arith.divsi %203, %204 : i64
      llvm.store %205, %185 : i64, !llvm.ptr
      %206 = llvm.load %185 : !llvm.ptr -> i64
      %207 = llvm.load %189 : !llvm.ptr -> i64
      %208 = llvm.getelementptr %arg1[%207] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %206, %208 : i64, !llvm.ptr
      %209 = llvm.load %189 : !llvm.ptr -> i64
      %210 = arith.constant 1 : i32
      %212 = arith.extsi %210 : i32 to i64
      %211 = arith.addi %209, %212 : i64
      llvm.store %211, %189 : i64, !llvm.ptr
      %213 = llvm.load %185 : !llvm.ptr -> i64
      %214 = arith.constant 2 : i32
      %216 = arith.extsi %214 : i32 to i64
      %215 = arith.muli %216, %175 : i64
      %217 = arith.cmpi eq, %213, %215 : i64
      cf.cond_br %217, ^bb45, ^bb46
      ^bb45:
        cf.br ^bb44
      ^bb46:
        cf.br ^bb47
      ^bb47:
      cf.br ^bb42
    ^bb44:
    %218 = llvm.load %189 : !llvm.ptr -> i64
    func.return %218 : i64
  }
  func.func @H_mod(%arg0: !llvm.ptr, %arg1: i64, %arg2: i64, %arg3: i64) -> i64 {
    %219 = arith.constant 0 : i32
    %220 = arith.extsi %219 : i32 to i64
    %221 = llvm.mlir.constant(1 : i64) : i64
    %222 = llvm.alloca %221 x i64 : (i64) -> !llvm.ptr
    llvm.store %220, %222 : i64, !llvm.ptr
    %223 = arith.constant 1 : i32
    %224 = arith.extsi %223 : i32 to i64
    %225 = llvm.mlir.constant(1 : i64) : i64
    %226 = llvm.alloca %225 x i64 : (i64) -> !llvm.ptr
    llvm.store %224, %226 : i64, !llvm.ptr
    %227 = arith.constant 1 : i32
    %228 = arith.extsi %227 : i32 to i64
    %229 = llvm.mlir.constant(1 : i64) : i64
    %230 = llvm.alloca %229 x i64 : (i64) -> !llvm.ptr
    llvm.store %228, %230 : i64, !llvm.ptr
    %231 = arith.constant 0 : i32
    %232 = arith.extsi %231 : i32 to i64
    %233 = llvm.mlir.constant(1 : i64) : i64
    %234 = llvm.alloca %233 x i64 : (i64) -> !llvm.ptr
    llvm.store %232, %234 : i64, !llvm.ptr
    %235 = arith.constant 0 : i32
    %236 = arith.extsi %235 : i32 to i64
    %237 = llvm.mlir.constant(1 : i64) : i64
    %238 = llvm.alloca %237 x i64 : (i64) -> !llvm.ptr
    llvm.store %236, %238 : i64, !llvm.ptr
    %239 = arith.constant 0 : i32
    %240 = arith.extsi %239 : i32 to i64
    %241 = llvm.mlir.constant(1 : i64) : i64
    %242 = llvm.alloca %241 x i64 : (i64) -> !llvm.ptr
    llvm.store %240, %242 : i64, !llvm.ptr
    cf.br ^bb48
    ^bb48:
    %243 = llvm.load %238 : !llvm.ptr -> i64
    %244 = arith.cmpi slt, %243, %arg2 : i64
    cf.cond_br %244, ^bb49, ^bb50
    ^bb49:
      %246 = llvm.load %234 : !llvm.ptr -> i64
      %247 = llvm.getelementptr %arg0[%246] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %245 = llvm.load %247 : !llvm.ptr -> i64
      %248 = llvm.load %234 : !llvm.ptr -> i64
      %249 = arith.constant 1 : i32
      %251 = arith.extsi %249 : i32 to i64
      %250 = arith.addi %248, %251 : i64
      llvm.store %250, %234 : i64, !llvm.ptr
      %252 = llvm.load %234 : !llvm.ptr -> i64
      %253 = arith.cmpi eq, %252, %arg1 : i64
      cf.cond_br %253, ^bb51, ^bb52
      ^bb51:
        %254 = arith.constant 0 : i32
        %255 = arith.extsi %254 : i32 to i64
        llvm.store %255, %234 : i64, !llvm.ptr
        cf.br ^bb53
      ^bb52:
        cf.br ^bb53
      ^bb53:
      %256 = llvm.load %230 : !llvm.ptr -> i64
      %257 = arith.constant 2 : i32
      %259 = arith.extsi %257 : i32 to i64
      %258 = arith.remsi %256, %259 : i64
      %260 = arith.constant 1 : i32
      %262 = arith.extsi %260 : i32 to i64
      %261 = arith.cmpi eq, %258, %262 : i64
      cf.cond_br %261, ^bb54, ^bb55
      ^bb54:
        %263 = llvm.load %238 : !llvm.ptr -> i64
        %264 = arith.subi %arg2, %263 : i64
        %265 = arith.cmpi sgt, %245, %264 : i64
        %266 = scf.if %265 -> (i64) {
          scf.yield %264 : i64
        } else {
          scf.yield %245 : i64
        }
        %267 = arith.remsi %266, %arg3 : i64
        %268 = arith.extsi %267 : i64 to i128
        %269 = llvm.load %222 : !llvm.ptr -> i64
        %270 = arith.remsi %269, %arg3 : i64
        %271 = arith.extsi %270 : i64 to i128
        %273 = arith.trunci %268 : i128 to i64
        %274 = arith.trunci %271 : i128 to i64
        %272 = arith.muli %273, %274 : i64
        %275 = arith.extsi %arg3 : i64 to i128
        %277 = arith.trunci %275 : i128 to i64
        %276 = arith.remsi %272, %277 : i64
        %278 = arith.extsi %276 : i64 to i128
        %279 = llvm.load %242 : !llvm.ptr -> i64
        %280 = arith.trunci %278 : i128 to i64
        %281 = arith.addi %279, %280 : i64
        %282 = arith.remsi %281, %arg3 : i64
        llvm.store %282, %242 : i64, !llvm.ptr
        %283 = arith.constant 1 : i32
        %285 = arith.extsi %283 : i32 to i64
        %284 = arith.addi %266, %285 : i64
        %286 = arith.muli %266, %284 : i64
        %287 = arith.constant 2 : i32
        %289 = arith.extsi %287 : i32 to i64
        %288 = arith.divsi %286, %289 : i64
        %290 = arith.remsi %288, %arg3 : i64
        %291 = llvm.load %226 : !llvm.ptr -> i64
        %292 = arith.remsi %291, %arg3 : i64
        %293 = arith.extsi %292 : i64 to i128
        %294 = arith.extsi %290 : i64 to i128
        %296 = arith.trunci %293 : i128 to i64
        %297 = arith.trunci %294 : i128 to i64
        %295 = arith.muli %296, %297 : i64
        %298 = arith.extsi %arg3 : i64 to i128
        %300 = arith.trunci %298 : i128 to i64
        %299 = arith.remsi %295, %300 : i64
        %301 = arith.extsi %299 : i64 to i128
        %302 = llvm.load %242 : !llvm.ptr -> i64
        %303 = arith.trunci %301 : i128 to i64
        %304 = arith.addi %302, %303 : i64
        %305 = arith.remsi %304, %arg3 : i64
        llvm.store %305, %242 : i64, !llvm.ptr
        %306 = llvm.load %238 : !llvm.ptr -> i64
        %307 = arith.addi %306, %266 : i64
        llvm.store %307, %238 : i64, !llvm.ptr
        cf.br ^bb56
      ^bb55:
        cf.br ^bb56
      ^bb56:
      %308 = arith.extsi %245 : i64 to i128
      %309 = llvm.load %226 : !llvm.ptr -> i64
      %310 = arith.extsi %309 : i64 to i128
      %312 = arith.trunci %308 : i128 to i64
      %313 = arith.trunci %310 : i128 to i64
      %311 = arith.muli %312, %313 : i64
      %314 = llvm.load %222 : !llvm.ptr -> i64
      %315 = arith.extsi %314 : i64 to i128
      %317 = arith.trunci %315 : i128 to i64
      %316 = arith.addi %311, %317 : i64
      %318 = arith.extsi %arg3 : i64 to i128
      %320 = arith.trunci %318 : i128 to i64
      %319 = arith.remsi %316, %320 : i64
      %321 = llvm.load %226 : !llvm.ptr -> i64
      llvm.store %321, %222 : i64, !llvm.ptr
      llvm.store %319, %226 : i64, !llvm.ptr
      %322 = llvm.load %230 : !llvm.ptr -> i64
      %323 = arith.constant 1 : i32
      %325 = arith.extsi %323 : i32 to i64
      %324 = arith.addi %322, %325 : i64
      llvm.store %324, %230 : i64, !llvm.ptr
      cf.br ^bb48
    ^bb50:
    %326 = llvm.load %242 : !llvm.ptr -> i64
    %327 = arith.remsi %326, %arg3 : i64
    func.return %327 : i64
  }
  func.func @main() -> i32 {
    %328 = arith.constant 999995705032704 : i32
    %329 = arith.extsi %328 : i32 to i64
    %330 = arith.constant 1000 : i32
    %331 = arith.extsi %330 : i32 to i64
    %332 = arith.constant 100 : i32
    %333 = arith.extsi %332 : i32 to i64
    %335 = arith.constant 256 : i32
    %336 = arith.constant 8 : i32
    %334 = func.call @calloc(%335, %336) : (i32, i32) -> i32
    %337 = llvm.inttoptr %334 : i32 to !llvm.ptr
    %338 = llvm.mlir.zero : !llvm.ptr
    %339 = llvm.icmp "eq" %337, %338 : !llvm.ptr
    cf.cond_br %339, ^bb57, ^bb58
    ^bb57:
      %340 = arith.constant 1 : i32
      func.return %340 : i32
    ^bb58:
      cf.br ^bb59
    ^bb59:
    %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 2 : i32
    %346 = arith.constant 1 : i32
    %348 = arith.extsi %346 : i32 to i64
    %347 = arith.addi %331, %348 : i64
    %349 = arith.index_cast %345 : i32 to index
    %350 = arith.index_cast %347 : i32 to index
    %352 = arith.constant 1 : index
    %353 = arith.constant -1 : index
    %354 = arith.cmpi sle, %349, %350 : index
    %351 = arith.select %354, %352, %353 : index
    cf.br ^bb60(%349 : index)
    ^bb60(%355: index):
    %356 = arith.cmpi slt, %355, %350 : index
    %357 = arith.cmpi sgt, %355, %350 : index
    %358 = arith.select %354, %356, %357 : i1
    cf.cond_br %358, ^bb61(%355 : index), ^bb62(%355 : index)
    ^bb61(%359: index):
      %361 = arith.index_cast %359 : index to i64
      %360 = func.call @isqrt(%361) : (i64) -> i64
      %362 = arith.muli %360, %360 : i64
      %364 = arith.trunci %362 : i64 to i32
      %365 = arith.index_cast %359 : index to i32
      %363 = arith.cmpi eq, %364, %365 : i32
      cf.cond_br %363, ^bb63, ^bb64
      ^bb63:
        %366 = arith.addi %359, %351 : index
        cf.br ^bb60(%366 : index)
      ^bb64:
        cf.br ^bb65
      ^bb65:
      %368 = arith.index_cast %359 : index to i64
      %367 = func.call @cf_period(%368, %337) : (i64, !llvm.ptr) -> i64
      %369 = func.call @H_mod(%337, %367, %333, %329) : (!llvm.ptr, i64, i64, i64) -> i64
      %370 = llvm.load %344 : !llvm.ptr -> i64
      %371 = arith.addi %370, %369 : i64
      %372 = arith.remsi %371, %329 : i64
      llvm.store %372, %344 : i64, !llvm.ptr
      %373 = llvm.load %344 : !llvm.ptr -> i64
      %374 = arith.constant 0 : i32
      %376 = arith.extsi %374 : i32 to i64
      %375 = arith.cmpi slt, %373, %376 : i64
      cf.cond_br %375, ^bb66, ^bb67
      ^bb66:
        %377 = llvm.load %344 : !llvm.ptr -> i64
        %378 = arith.addi %377, %329 : i64
        llvm.store %378, %344 : i64, !llvm.ptr
        cf.br ^bb68
      ^bb67:
        cf.br ^bb68
      ^bb68:
      %379 = arith.addi %359, %351 : index
      cf.br ^bb60(%379 : index)
    ^bb62(%380: index):
    %381 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %382 = llvm.load %344 : !llvm.ptr -> i64
    %383 = llvm.call @printf(%381, %382) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %384 = func.call @free(%337) : (!llvm.ptr) -> i32
    %385 = arith.constant 0 : i32
    func.return %385 : i32
  }
}