Problem 785

Symmetric Diophantine Equation. S(N) = sum (x+y+z) over primitive solutions of 15(x^2+y^2+z^2) = 34(xy+yz+zx) with 1 <= x <= y <= z <= N and gcd(x,y,z)=1. Coprime-pair parameterisation with SPF sieve.

Answer29526986315080920
Output29526986315080920
StatusPASS
Native helperno
Runtime210 ms
Peak memory1200 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(1)
ApproachFlow solutionDivisor-based Diophantine solver
VerdictSuboptimal

Flow source

# Project Euler 785
# Symmetric Diophantine Equation.
# S(N) = sum (x+y+z) over primitive solutions of
#   15(x^2+y^2+z^2) = 34(xy+yz+zx)
# with 1 <= x <= y <= z <= N and gcd(x,y,z)=1.
# Coprime-pair parameterisation with SPF sieve.

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

function isqrt_i64(x: i64) -> i64 {
    if x <= 0 { return 0 }
    let mut r: i64 = sqrt(x as f64) as i64
    while r * r > x {
        r = r - 1
    }
    while (r + 1) * (r + 1) <= x {
        r = r + 1
    }
    return r
}

function main() -> i32 {
    let N: i64 = 1000000000
    let max_a: i64 = isqrt_i64(N / 3)
    if max_a < 1 {
        printf("%lld\n", 0 as i64)
        return 0
    }

    # SPF sieve
    let spf: ptr<i32> = calloc(max_a + 1, 4) as ptr<i32>
    for i in 0..(max_a + 1) {
        spf[i] = i as i32
    }
    let mut i: i64 = 2
    while i * i <= max_a {
        if spf[i] == i as i32 {
            let mut j: i64 = i * i
            while j <= max_a {
                if spf[j] == j as i32 {
                    spf[j] = i as i32
                }
                j = j + i
            }
        }
        i = i + 1
    }

    let N3: i64 = 12 * N
    let N5: i64 = 5 * N

    let mut total: i64 = 0
    let pf: ptr<i32> = calloc(64, 4) as ptr<i32>

    for a in 1..(max_a + 1) {
        let aa: i64 = a * a

        # distinct prime factors of a
        let mut pf_n: i64 = 0
        let mut x: i64 = a
        while x > 1 {
            let p: i64 = spf[x] as i64
            pf[pf_n] = p as i32
            pf_n = pf_n + 1
            while x % p == 0 {
                x = x / p
            }
        }

        # bmin from B = 5a^2 - 2ab <= N  ==>  b >= (5a^2 - N)/(2a)
        let mut bmin: i64
        if 5 * aa <= N {
            bmin = 1
        } else {
            let num: i64 = 5 * aa - N
            bmin = (num + (2 * a - 1)) / (2 * a)
            if bmin < 1 { bmin = 1 }
        }

        # C > 0 requires b < 3a/5
        let bmax_pos: i64 = (3 * a - 1) / 5
        if bmax_pos < bmin { continue }

        # A = 2ab + 3b^2 <= N  ->  b <= floor((-2a + sqrt(4a^2+12N))/6)
        let disc1: i64 = 4 * aa + N3
        let bmax1: i64 = (-2 * a + isqrt_i64(disc1)) / 6

        # C = 3a^2 - 8ab + 5b^2 <= N
        let disc3: i64 = aa + N5
        let sdisc3: i64 = isqrt_i64(disc3)
        let bmax3: i64 = (4 * a + sdisc3) / 5
        let bmin3: i64 = (4 * a - sdisc3 + 4) / 5

        if bmin3 > bmin { bmin = bmin3 }

        let mut bmax: i64 = bmax_pos
        if bmax1 < bmax { bmax = bmax1 }
        if bmax3 < bmax { bmax = bmax3 }
        if bmax >= a { bmax = a - 1 }

        if bmax < bmin { continue }

        let mut b: i64 = bmin
        while b <= bmax {
            # coprimality: b not divisible by any prime factor of a
            let mut ok: i64 = 1
            let mut k: i64 = 0
            while k < pf_n {
                if b % (pf[k] as i64) == 0 {
                    ok = 0
                    break
                }
                k = k + 1
            }
            if ok == 0 {
                b = b + 1
                continue
            }

            let ab: i64 = a * b
            let bb: i64 = b * b

            let A: i64 = 2 * ab + 3 * bb
            let B: i64 = 5 * aa - 2 * ab
            let C: i64 = 3 * aa - 8 * ab + 5 * bb

            if C <= 0 {
                b = b + 1
                continue
            }
            if A > N || B > N || C > N {
                b = b + 1
                continue
            }

            # non-primitive iff all divisible by 19
            if A % 19 == 0 && B % 19 == 0 {
                b = b + 1
                continue
            }

            total = total + 8 * (aa - ab + bb)
            b = b + 1
        }
    }

    free(spf)
    free(pf)

    printf("%lld\n", total)
    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 isqrt_i64_i64(int64_t x);
int32_t main(void);




int64_t isqrt_i64_i64(int64_t x) {
    if (x <= 0) {
        return 0;
    }
    int64_t r = ((int64_t)(sqrt(((double)(x)))));
    while ((r * r) > x) {
        r = (r - 1);
    }
    while (((r + 1) * (r + 1)) <= x) {
        r = (r + 1);
    }
    return r;
}

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