Problem 764

Asymmetric Diophantine Equation: 16x^2 + y^4 = z^2 with x, y, z positive integers and gcd(x, y, z) = 1. S(N) sums x + y + z over all solutions with 1 <= x, y, z <= N. Find S(10^16) mod 10^9. Rewrite as (4x)^2 + (y^2)^2 = z^2, a Pythagorean triple. y odd: the triple is primitive, so y^2 = m^2 - n^2, 4x = 2mn, z = m^2 + n^2. Then m - n = s^2, m + n = t^2 for odd coprime s < t, giving x = (t^4 - s^4)/8, y = st, z = (t^4 + s^4)/2. y even: x is odd, z = 4w, and (x, (y/2)^2, w) is primitive: x = m^2 - n^2, (y/2)^2 = 2mn, w = m^2 + n^2. Since 2mn is a square with gcd(m, n) = 1, {m, n} = {2s^2, t^2} with t odd and gcd(s, t) = 1, so y = 4st and, by which of 2s^2, t^2 is larger, x = 4s^4 - t^4, z = 16s^4 + 4t^4 (2s^2 > t^2), or x = t^4 - 4s^4, z = 4t^4 + 16s^4 (t^2 > 2s^2). Each primitive triple arises once, so the three families enumerate every solution exactly once. z <= 10^16 bounds s, t near 12000; everything fits in i64 and the running sum is reduced mod 10^9.

Answer255228881
Output255228881
StatusPASS
Native helperno
Runtime1290 ms
Peak memory1104 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n)
Space complexityO(1)O(1)
ApproachFlow solutionEuclid parametrisation
VerdictSuboptimal

Flow source

# Project Euler 764
# Asymmetric Diophantine Equation: 16x^2 + y^4 = z^2 with x, y, z positive
# integers and gcd(x, y, z) = 1.  S(N) sums x + y + z over all solutions
# with 1 <= x, y, z <= N.  Find S(10^16) mod 10^9.
#
# Rewrite as (4x)^2 + (y^2)^2 = z^2, a Pythagorean triple.
#   y odd: the triple is primitive, so y^2 = m^2 - n^2, 4x = 2mn,
#          z = m^2 + n^2.  Then m - n = s^2, m + n = t^2 for odd coprime
#          s < t, giving x = (t^4 - s^4)/8, y = st, z = (t^4 + s^4)/2.
#   y even: x is odd, z = 4w, and (x, (y/2)^2, w) is primitive:
#          x = m^2 - n^2, (y/2)^2 = 2mn, w = m^2 + n^2.  Since 2mn is a
#          square with gcd(m, n) = 1, {m, n} = {2s^2, t^2} with t odd and
#          gcd(s, t) = 1, so y = 4st and, by which of 2s^2, t^2 is larger,
#          x = 4s^4 - t^4, z = 16s^4 + 4t^4   (2s^2 > t^2), or
#          x = t^4 - 4s^4, z = 4t^4 + 16s^4   (t^2 > 2s^2).
# Each primitive triple arises once, so the three families enumerate every
# solution exactly once.  z <= 10^16 bounds s, t near 12000; everything
# fits in i64 and the running sum is reduced mod 10^9.

const N: i64 = 10000000000000000
const MOD: i64 = 1000000000

function gcd(a: i64, b: i64) -> i64 {
    let mut x: i64 = a
    let mut y: i64 = b
    while y != 0 {
        let r: i64 = x % y
        x = y
        y = r
    }
    return x
}

function main() -> i32 {
    let mut total: i64 = 0

    # Family 1: y odd.  Odd coprime s < t.
    # x = (t^4 - s^4)/8, y = s t, z = (t^4 + s^4)/2.
    let mut t: i64 = 3
    while (t * t * t * t + 1) / 2 <= N {
        let t4: i64 = t * t * t * t
        let mut s: i64 = 1
        while s < t {
            let s4: i64 = s * s * s * s
            let z: i64 = (t4 + s4) / 2
            if z > N {
                s = t
            } else {
                if gcd(s, t) == 1 {
                    let x: i64 = (t4 - s4) / 8
                    let y: i64 = s * t
                    if x >= 1 && x <= N && y <= N {
                        total = (total + (x + y + z) % MOD) % MOD
                    }
                }
                s = s + 2
            }
        }
        t = t + 2
    }

    # Family 2: y even, m = 2s^2 > n = t^2, t odd, gcd(s, t) = 1.
    # x = 4s^4 - t^4, y = 4 s t, z = 16s^4 + 4t^4.
    let mut s2: i64 = 1
    while 16 * s2 * s2 * s2 * s2 + 4 <= N {
        let s4: i64 = s2 * s2 * s2 * s2
        let mut tt: i64 = 1
        while tt * tt < 2 * s2 * s2 {
            let t4: i64 = tt * tt * tt * tt
            let z: i64 = 16 * s4 + 4 * t4
            if z > N {
                tt = 2 * s2 * s2
            } else {
                if gcd(s2, tt) == 1 {
                    let x: i64 = 4 * s4 - t4
                    let y: i64 = 4 * s2 * tt
                    if x >= 1 && x <= N && y <= N {
                        total = (total + (x + y + z) % MOD) % MOD
                    }
                }
                tt = tt + 2
            }
        }
        s2 = s2 + 1
    }

    # Family 3: y even, m = t^2 > n = 2s^2, t odd, gcd(s, t) = 1.
    # x = t^4 - 4s^4, y = 4 s t, z = 4t^4 + 16s^4.
    let mut t3: i64 = 1
    while 4 * t3 * t3 * t3 * t3 + 16 <= N {
        let t4: i64 = t3 * t3 * t3 * t3
        let mut ss: i64 = 1
        while 2 * ss * ss < t3 * t3 {
            let s4b: i64 = ss * ss * ss * ss
            let z: i64 = 4 * t4 + 16 * s4b
            if z > N {
                ss = t3 * t3
            } else {
                if gcd(ss, t3) == 1 {
                    let x: i64 = t4 - 4 * s4b
                    let y: i64 = 4 * ss * t3
                    if x >= 1 && x <= N && y <= N {
                        total = (total + (x + y + z) % MOD) % MOD
                    }
                }
                ss = ss + 1
            }
        }
        t3 = t3 + 2
    }

    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 gcd_i64_i64(int64_t a, int64_t b);
int32_t main(void);

static const int64_t N = 10000000000000000;
static const int64_t MOD = 1000000000;

int64_t gcd_i64_i64(int64_t a, int64_t b) {
    int64_t x = a;
    int64_t y = b;
    while (y != 0) {
        int64_t r = FLOW_CHECKED_MOD((x), (y));
        x = y;
        y = r;
    }
    return x;
}

int32_t main(void) {
    int64_t total = 0;
    int64_t t = 3;
    while (FLOW_CHECKED_DIV((((((t * t) * t) * t) + 1)), (2)) <= N) {
        int64_t t4 = (((t * t) * t) * t);
        int64_t s = 1;
        while (s < t) {
            int64_t s4 = (((s * s) * s) * s);
            int64_t z = FLOW_CHECKED_DIV(((t4 + s4)), (2));
            if (z > N) {
                s = t;
            } else {
                if (gcd_i64_i64(s, t) == 1) {
                    int64_t x = FLOW_CHECKED_DIV(((t4 - s4)), (8));
                    int64_t y = (s * t);
                    if (((x >= 1 && x <= N) && y <= N)) {
                        total = FLOW_CHECKED_MOD(((total + FLOW_CHECKED_MOD((((x + y) + z)), (MOD)))), (MOD));
                    }
                }
                s = (s + 2);
            }
        }
        t = (t + 2);
    }
    int64_t s2 = 1;
    while ((((((16 * s2) * s2) * s2) * s2) + 4) <= N) {
        int64_t s4 = (((s2 * s2) * s2) * s2);
        int64_t tt = 1;
        while ((tt * tt) < ((2 * s2) * s2)) {
            int64_t t4 = (((tt * tt) * tt) * tt);
            int64_t z = ((16 * s4) + (4 * t4));
            if (z > N) {
                tt = ((2 * s2) * s2);
            } else {
                if (gcd_i64_i64(s2, tt) == 1) {
                    int64_t x = ((4 * s4) - t4);
                    int64_t y = ((4 * s2) * tt);
                    if (((x >= 1 && x <= N) && y <= N)) {
                        total = FLOW_CHECKED_MOD(((total + FLOW_CHECKED_MOD((((x + y) + z)), (MOD)))), (MOD));
                    }
                }
                tt = (tt + 2);
            }
        }
        s2 = (s2 + 1);
    }
    int64_t t3 = 1;
    while ((((((4 * t3) * t3) * t3) * t3) + 16) <= N) {
        int64_t t4 = (((t3 * t3) * t3) * t3);
        int64_t ss = 1;
        while (((2 * ss) * ss) < (t3 * t3)) {
            int64_t s4b = (((ss * ss) * ss) * ss);
            int64_t z = ((4 * t4) + (16 * s4b));
            if (z > N) {
                ss = (t3 * t3);
            } else {
                if (gcd_i64_i64(ss, t3) == 1) {
                    int64_t x = (t4 - (4 * s4b));
                    int64_t y = ((4 * ss) * t3);
                    if (((x >= 1 && x <= N) && y <= N)) {
                        total = FLOW_CHECKED_MOD(((total + FLOW_CHECKED_MOD((((x + y) + z)), (MOD)))), (MOD));
                    }
                }
                ss = (ss + 1);
            }
        }
        t3 = (t3 + 2);
    }
    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>
  // Constant: N
  llvm.mlir.global internal constant @N(10000000000000000 : i64) : i64
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000000 : i64) : i64
  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 @main() -> i32 {
    %13 = arith.constant 0 : i32
    %14 = arith.extsi %13 : i32 to i64
    %15 = llvm.mlir.constant(1 : i64) : i64
    %16 = llvm.alloca %15 x i64 : (i64) -> !llvm.ptr
    llvm.store %14, %16 : i64, !llvm.ptr
    %17 = arith.constant 3 : i32
    %18 = arith.extsi %17 : i32 to i64
    %19 = llvm.mlir.constant(1 : i64) : i64
    %20 = llvm.alloca %19 x i64 : (i64) -> !llvm.ptr
    llvm.store %18, %20 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %21 = llvm.load %20 : !llvm.ptr -> i64
    %22 = llvm.load %20 : !llvm.ptr -> i64
    %23 = arith.muli %21, %22 : i64
    %24 = llvm.load %20 : !llvm.ptr -> i64
    %25 = arith.muli %23, %24 : i64
    %26 = llvm.load %20 : !llvm.ptr -> i64
    %27 = arith.muli %25, %26 : i64
    %28 = arith.constant 1 : i32
    %30 = arith.extsi %28 : i32 to i64
    %29 = arith.addi %27, %30 : i64
    %31 = arith.constant 2 : i32
    %33 = arith.extsi %31 : i32 to i64
    %32 = arith.divsi %29, %33 : i64
    %34 = llvm.mlir.addressof @N : !llvm.ptr
    %35 = llvm.load %34 : !llvm.ptr -> i64
    %36 = arith.cmpi sle, %32, %35 : i64
    cf.cond_br %36, ^bb4, ^bb5
    ^bb4:
      %37 = llvm.load %20 : !llvm.ptr -> i64
      %38 = llvm.load %20 : !llvm.ptr -> i64
      %39 = arith.muli %37, %38 : i64
      %40 = llvm.load %20 : !llvm.ptr -> i64
      %41 = arith.muli %39, %40 : i64
      %42 = llvm.load %20 : !llvm.ptr -> i64
      %43 = arith.muli %41, %42 : i64
      %44 = arith.constant 1 : i32
      %45 = arith.extsi %44 : i32 to i64
      %46 = llvm.mlir.constant(1 : i64) : i64
      %47 = llvm.alloca %46 x i64 : (i64) -> !llvm.ptr
      llvm.store %45, %47 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %48 = llvm.load %47 : !llvm.ptr -> i64
      %49 = llvm.load %20 : !llvm.ptr -> i64
      %50 = arith.cmpi slt, %48, %49 : i64
      cf.cond_br %50, ^bb7, ^bb8
      ^bb7:
        %51 = llvm.load %47 : !llvm.ptr -> i64
        %52 = llvm.load %47 : !llvm.ptr -> i64
        %53 = arith.muli %51, %52 : i64
        %54 = llvm.load %47 : !llvm.ptr -> i64
        %55 = arith.muli %53, %54 : i64
        %56 = llvm.load %47 : !llvm.ptr -> i64
        %57 = arith.muli %55, %56 : i64
        %58 = arith.addi %43, %57 : i64
        %59 = arith.constant 2 : i32
        %61 = arith.extsi %59 : i32 to i64
        %60 = arith.divsi %58, %61 : i64
        %62 = llvm.mlir.addressof @N : !llvm.ptr
        %63 = llvm.load %62 : !llvm.ptr -> i64
        %64 = arith.cmpi sgt, %60, %63 : i64
        cf.cond_br %64, ^bb9, ^bb10
        ^bb9:
          %65 = llvm.load %20 : !llvm.ptr -> i64
          llvm.store %65, %47 : i64, !llvm.ptr
          cf.br ^bb11
        ^bb10:
          %67 = llvm.load %47 : !llvm.ptr -> i64
          %68 = llvm.load %20 : !llvm.ptr -> i64
          %66 = func.call @gcd(%67, %68) : (i64, i64) -> i64
          %69 = arith.constant 1 : i32
          %71 = arith.extsi %69 : i32 to i64
          %70 = arith.cmpi eq, %66, %71 : i64
          cf.cond_br %70, ^bb12, ^bb13
          ^bb12:
            %72 = arith.subi %43, %57 : i64
            %73 = arith.constant 8 : i32
            %75 = arith.extsi %73 : i32 to i64
            %74 = arith.divsi %72, %75 : i64
            %76 = llvm.load %47 : !llvm.ptr -> i64
            %77 = llvm.load %20 : !llvm.ptr -> i64
            %78 = arith.muli %76, %77 : i64
            %79 = arith.constant 1 : i32
            %81 = arith.extsi %79 : i32 to i64
            %80 = arith.cmpi sge, %74, %81 : i64
            %82 = scf.if %80 -> (i1) {
              %83 = llvm.mlir.addressof @N : !llvm.ptr
              %84 = llvm.load %83 : !llvm.ptr -> i64
              %85 = arith.cmpi sle, %74, %84 : i64
              scf.yield %85 : i1
            } else {
              %86 = arith.constant false
              scf.yield %86 : i1
            }
            %87 = scf.if %82 -> (i1) {
              %88 = llvm.mlir.addressof @N : !llvm.ptr
              %89 = llvm.load %88 : !llvm.ptr -> i64
              %90 = arith.cmpi sle, %78, %89 : i64
              scf.yield %90 : i1
            } else {
              %91 = arith.constant false
              scf.yield %91 : i1
            }
            cf.cond_br %87, ^bb15, ^bb16
            ^bb15:
              %92 = llvm.load %16 : !llvm.ptr -> i64
              %93 = arith.addi %74, %78 : i64
              %94 = arith.addi %93, %60 : i64
              %95 = llvm.mlir.addressof @MOD : !llvm.ptr
              %96 = llvm.load %95 : !llvm.ptr -> i64
              %97 = arith.remsi %94, %96 : i64
              %98 = arith.addi %92, %97 : i64
              %99 = llvm.mlir.addressof @MOD : !llvm.ptr
              %100 = llvm.load %99 : !llvm.ptr -> i64
              %101 = arith.remsi %98, %100 : i64
              llvm.store %101, %16 : i64, !llvm.ptr
              cf.br ^bb17
            ^bb16:
              cf.br ^bb17
            ^bb17:
            cf.br ^bb14
          ^bb13:
            cf.br ^bb14
          ^bb14:
          %102 = llvm.load %47 : !llvm.ptr -> i64
          %103 = arith.constant 2 : i32
          %105 = arith.extsi %103 : i32 to i64
          %104 = arith.addi %102, %105 : i64
          llvm.store %104, %47 : i64, !llvm.ptr
          cf.br ^bb11
        ^bb11:
        cf.br ^bb6
      ^bb8:
      %106 = llvm.load %20 : !llvm.ptr -> i64
      %107 = arith.constant 2 : i32
      %109 = arith.extsi %107 : i32 to i64
      %108 = arith.addi %106, %109 : i64
      llvm.store %108, %20 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %110 = arith.constant 1 : i32
    %111 = arith.extsi %110 : i32 to i64
    %112 = llvm.mlir.constant(1 : i64) : i64
    %113 = llvm.alloca %112 x i64 : (i64) -> !llvm.ptr
    llvm.store %111, %113 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %114 = arith.constant 16 : i32
    %115 = llvm.load %113 : !llvm.ptr -> i64
    %117 = arith.extsi %114 : i32 to i64
    %116 = arith.muli %117, %115 : i64
    %118 = llvm.load %113 : !llvm.ptr -> i64
    %119 = arith.muli %116, %118 : i64
    %120 = llvm.load %113 : !llvm.ptr -> i64
    %121 = arith.muli %119, %120 : i64
    %122 = llvm.load %113 : !llvm.ptr -> i64
    %123 = arith.muli %121, %122 : i64
    %124 = arith.constant 4 : i32
    %126 = arith.extsi %124 : i32 to i64
    %125 = arith.addi %123, %126 : i64
    %127 = llvm.mlir.addressof @N : !llvm.ptr
    %128 = llvm.load %127 : !llvm.ptr -> i64
    %129 = arith.cmpi sle, %125, %128 : i64
    cf.cond_br %129, ^bb19, ^bb20
    ^bb19:
      %130 = llvm.load %113 : !llvm.ptr -> i64
      %131 = llvm.load %113 : !llvm.ptr -> i64
      %132 = arith.muli %130, %131 : i64
      %133 = llvm.load %113 : !llvm.ptr -> i64
      %134 = arith.muli %132, %133 : i64
      %135 = llvm.load %113 : !llvm.ptr -> i64
      %136 = arith.muli %134, %135 : i64
      %137 = arith.constant 1 : i32
      %138 = arith.extsi %137 : i32 to i64
      %139 = llvm.mlir.constant(1 : i64) : i64
      %140 = llvm.alloca %139 x i64 : (i64) -> !llvm.ptr
      llvm.store %138, %140 : i64, !llvm.ptr
      cf.br ^bb21
      ^bb21:
      %141 = llvm.load %140 : !llvm.ptr -> i64
      %142 = llvm.load %140 : !llvm.ptr -> i64
      %143 = arith.muli %141, %142 : i64
      %144 = arith.constant 2 : i32
      %145 = llvm.load %113 : !llvm.ptr -> i64
      %147 = arith.extsi %144 : i32 to i64
      %146 = arith.muli %147, %145 : i64
      %148 = llvm.load %113 : !llvm.ptr -> i64
      %149 = arith.muli %146, %148 : i64
      %150 = arith.cmpi slt, %143, %149 : i64
      cf.cond_br %150, ^bb22, ^bb23
      ^bb22:
        %151 = llvm.load %140 : !llvm.ptr -> i64
        %152 = llvm.load %140 : !llvm.ptr -> i64
        %153 = arith.muli %151, %152 : i64
        %154 = llvm.load %140 : !llvm.ptr -> i64
        %155 = arith.muli %153, %154 : i64
        %156 = llvm.load %140 : !llvm.ptr -> i64
        %157 = arith.muli %155, %156 : i64
        %158 = arith.constant 16 : i32
        %160 = arith.extsi %158 : i32 to i64
        %159 = arith.muli %160, %136 : i64
        %161 = arith.constant 4 : i32
        %163 = arith.extsi %161 : i32 to i64
        %162 = arith.muli %163, %157 : i64
        %164 = arith.addi %159, %162 : i64
        %165 = llvm.mlir.addressof @N : !llvm.ptr
        %166 = llvm.load %165 : !llvm.ptr -> i64
        %167 = arith.cmpi sgt, %164, %166 : i64
        cf.cond_br %167, ^bb24, ^bb25
        ^bb24:
          %168 = arith.constant 2 : i32
          %169 = llvm.load %113 : !llvm.ptr -> i64
          %171 = arith.extsi %168 : i32 to i64
          %170 = arith.muli %171, %169 : i64
          %172 = llvm.load %113 : !llvm.ptr -> i64
          %173 = arith.muli %170, %172 : i64
          llvm.store %173, %140 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb25:
          %175 = llvm.load %113 : !llvm.ptr -> i64
          %176 = llvm.load %140 : !llvm.ptr -> i64
          %174 = func.call @gcd(%175, %176) : (i64, i64) -> i64
          %177 = arith.constant 1 : i32
          %179 = arith.extsi %177 : i32 to i64
          %178 = arith.cmpi eq, %174, %179 : i64
          cf.cond_br %178, ^bb27, ^bb28
          ^bb27:
            %180 = arith.constant 4 : i32
            %182 = arith.extsi %180 : i32 to i64
            %181 = arith.muli %182, %136 : i64
            %183 = arith.subi %181, %157 : i64
            %184 = arith.constant 4 : i32
            %185 = llvm.load %113 : !llvm.ptr -> i64
            %187 = arith.extsi %184 : i32 to i64
            %186 = arith.muli %187, %185 : i64
            %188 = llvm.load %140 : !llvm.ptr -> i64
            %189 = arith.muli %186, %188 : i64
            %190 = arith.constant 1 : i32
            %192 = arith.extsi %190 : i32 to i64
            %191 = arith.cmpi sge, %183, %192 : i64
            %193 = scf.if %191 -> (i1) {
              %194 = llvm.mlir.addressof @N : !llvm.ptr
              %195 = llvm.load %194 : !llvm.ptr -> i64
              %196 = arith.cmpi sle, %183, %195 : i64
              scf.yield %196 : i1
            } else {
              %197 = arith.constant false
              scf.yield %197 : i1
            }
            %198 = scf.if %193 -> (i1) {
              %199 = llvm.mlir.addressof @N : !llvm.ptr
              %200 = llvm.load %199 : !llvm.ptr -> i64
              %201 = arith.cmpi sle, %189, %200 : i64
              scf.yield %201 : i1
            } else {
              %202 = arith.constant false
              scf.yield %202 : i1
            }
            cf.cond_br %198, ^bb30, ^bb31
            ^bb30:
              %203 = llvm.load %16 : !llvm.ptr -> i64
              %204 = arith.addi %183, %189 : i64
              %205 = arith.addi %204, %164 : i64
              %206 = llvm.mlir.addressof @MOD : !llvm.ptr
              %207 = llvm.load %206 : !llvm.ptr -> i64
              %208 = arith.remsi %205, %207 : i64
              %209 = arith.addi %203, %208 : i64
              %210 = llvm.mlir.addressof @MOD : !llvm.ptr
              %211 = llvm.load %210 : !llvm.ptr -> i64
              %212 = arith.remsi %209, %211 : i64
              llvm.store %212, %16 : i64, !llvm.ptr
              cf.br ^bb32
            ^bb31:
              cf.br ^bb32
            ^bb32:
            cf.br ^bb29
          ^bb28:
            cf.br ^bb29
          ^bb29:
          %213 = llvm.load %140 : !llvm.ptr -> i64
          %214 = arith.constant 2 : i32
          %216 = arith.extsi %214 : i32 to i64
          %215 = arith.addi %213, %216 : i64
          llvm.store %215, %140 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb26:
        cf.br ^bb21
      ^bb23:
      %217 = llvm.load %113 : !llvm.ptr -> i64
      %218 = arith.constant 1 : i32
      %220 = arith.extsi %218 : i32 to i64
      %219 = arith.addi %217, %220 : i64
      llvm.store %219, %113 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %221 = arith.constant 1 : i32
    %222 = arith.extsi %221 : i32 to i64
    %223 = llvm.mlir.constant(1 : i64) : i64
    %224 = llvm.alloca %223 x i64 : (i64) -> !llvm.ptr
    llvm.store %222, %224 : i64, !llvm.ptr
    cf.br ^bb33
    ^bb33:
    %225 = arith.constant 4 : i32
    %226 = llvm.load %224 : !llvm.ptr -> i64
    %228 = arith.extsi %225 : i32 to i64
    %227 = arith.muli %228, %226 : i64
    %229 = llvm.load %224 : !llvm.ptr -> i64
    %230 = arith.muli %227, %229 : i64
    %231 = llvm.load %224 : !llvm.ptr -> i64
    %232 = arith.muli %230, %231 : i64
    %233 = llvm.load %224 : !llvm.ptr -> i64
    %234 = arith.muli %232, %233 : i64
    %235 = arith.constant 16 : i32
    %237 = arith.extsi %235 : i32 to i64
    %236 = arith.addi %234, %237 : i64
    %238 = llvm.mlir.addressof @N : !llvm.ptr
    %239 = llvm.load %238 : !llvm.ptr -> i64
    %240 = arith.cmpi sle, %236, %239 : i64
    cf.cond_br %240, ^bb34, ^bb35
    ^bb34:
      %241 = llvm.load %224 : !llvm.ptr -> i64
      %242 = llvm.load %224 : !llvm.ptr -> i64
      %243 = arith.muli %241, %242 : i64
      %244 = llvm.load %224 : !llvm.ptr -> i64
      %245 = arith.muli %243, %244 : i64
      %246 = llvm.load %224 : !llvm.ptr -> i64
      %247 = arith.muli %245, %246 : i64
      %248 = arith.constant 1 : i32
      %249 = arith.extsi %248 : i32 to i64
      %250 = llvm.mlir.constant(1 : i64) : i64
      %251 = llvm.alloca %250 x i64 : (i64) -> !llvm.ptr
      llvm.store %249, %251 : i64, !llvm.ptr
      cf.br ^bb36
      ^bb36:
      %252 = arith.constant 2 : i32
      %253 = llvm.load %251 : !llvm.ptr -> i64
      %255 = arith.extsi %252 : i32 to i64
      %254 = arith.muli %255, %253 : i64
      %256 = llvm.load %251 : !llvm.ptr -> i64
      %257 = arith.muli %254, %256 : i64
      %258 = llvm.load %224 : !llvm.ptr -> i64
      %259 = llvm.load %224 : !llvm.ptr -> i64
      %260 = arith.muli %258, %259 : i64
      %261 = arith.cmpi slt, %257, %260 : i64
      cf.cond_br %261, ^bb37, ^bb38
      ^bb37:
        %262 = llvm.load %251 : !llvm.ptr -> i64
        %263 = llvm.load %251 : !llvm.ptr -> i64
        %264 = arith.muli %262, %263 : i64
        %265 = llvm.load %251 : !llvm.ptr -> i64
        %266 = arith.muli %264, %265 : i64
        %267 = llvm.load %251 : !llvm.ptr -> i64
        %268 = arith.muli %266, %267 : i64
        %269 = arith.constant 4 : i32
        %271 = arith.extsi %269 : i32 to i64
        %270 = arith.muli %271, %247 : i64
        %272 = arith.constant 16 : i32
        %274 = arith.extsi %272 : i32 to i64
        %273 = arith.muli %274, %268 : i64
        %275 = arith.addi %270, %273 : i64
        %276 = llvm.mlir.addressof @N : !llvm.ptr
        %277 = llvm.load %276 : !llvm.ptr -> i64
        %278 = arith.cmpi sgt, %275, %277 : i64
        cf.cond_br %278, ^bb39, ^bb40
        ^bb39:
          %279 = llvm.load %224 : !llvm.ptr -> i64
          %280 = llvm.load %224 : !llvm.ptr -> i64
          %281 = arith.muli %279, %280 : i64
          llvm.store %281, %251 : i64, !llvm.ptr
          cf.br ^bb41
        ^bb40:
          %283 = llvm.load %251 : !llvm.ptr -> i64
          %284 = llvm.load %224 : !llvm.ptr -> i64
          %282 = func.call @gcd(%283, %284) : (i64, i64) -> i64
          %285 = arith.constant 1 : i32
          %287 = arith.extsi %285 : i32 to i64
          %286 = arith.cmpi eq, %282, %287 : i64
          cf.cond_br %286, ^bb42, ^bb43
          ^bb42:
            %288 = arith.constant 4 : i32
            %290 = arith.extsi %288 : i32 to i64
            %289 = arith.muli %290, %268 : i64
            %291 = arith.subi %247, %289 : i64
            %292 = arith.constant 4 : i32
            %293 = llvm.load %251 : !llvm.ptr -> i64
            %295 = arith.extsi %292 : i32 to i64
            %294 = arith.muli %295, %293 : i64
            %296 = llvm.load %224 : !llvm.ptr -> i64
            %297 = arith.muli %294, %296 : i64
            %298 = arith.constant 1 : i32
            %300 = arith.extsi %298 : i32 to i64
            %299 = arith.cmpi sge, %291, %300 : i64
            %301 = scf.if %299 -> (i1) {
              %302 = llvm.mlir.addressof @N : !llvm.ptr
              %303 = llvm.load %302 : !llvm.ptr -> i64
              %304 = arith.cmpi sle, %291, %303 : i64
              scf.yield %304 : i1
            } else {
              %305 = arith.constant false
              scf.yield %305 : i1
            }
            %306 = scf.if %301 -> (i1) {
              %307 = llvm.mlir.addressof @N : !llvm.ptr
              %308 = llvm.load %307 : !llvm.ptr -> i64
              %309 = arith.cmpi sle, %297, %308 : i64
              scf.yield %309 : i1
            } else {
              %310 = arith.constant false
              scf.yield %310 : i1
            }
            cf.cond_br %306, ^bb45, ^bb46
            ^bb45:
              %311 = llvm.load %16 : !llvm.ptr -> i64
              %312 = arith.addi %291, %297 : i64
              %313 = arith.addi %312, %275 : i64
              %314 = llvm.mlir.addressof @MOD : !llvm.ptr
              %315 = llvm.load %314 : !llvm.ptr -> i64
              %316 = arith.remsi %313, %315 : i64
              %317 = arith.addi %311, %316 : i64
              %318 = llvm.mlir.addressof @MOD : !llvm.ptr
              %319 = llvm.load %318 : !llvm.ptr -> i64
              %320 = arith.remsi %317, %319 : i64
              llvm.store %320, %16 : i64, !llvm.ptr
              cf.br ^bb47
            ^bb46:
              cf.br ^bb47
            ^bb47:
            cf.br ^bb44
          ^bb43:
            cf.br ^bb44
          ^bb44:
          %321 = llvm.load %251 : !llvm.ptr -> i64
          %322 = arith.constant 1 : i32
          %324 = arith.extsi %322 : i32 to i64
          %323 = arith.addi %321, %324 : i64
          llvm.store %323, %251 : i64, !llvm.ptr
          cf.br ^bb41
        ^bb41:
        cf.br ^bb36
      ^bb38:
      %325 = llvm.load %224 : !llvm.ptr -> i64
      %326 = arith.constant 2 : i32
      %328 = arith.extsi %326 : i32 to i64
      %327 = arith.addi %325, %328 : i64
      llvm.store %327, %224 : i64, !llvm.ptr
      cf.br ^bb33
    ^bb35:
    %329 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %330 = llvm.load %16 : !llvm.ptr -> i64
    %331 = llvm.call @printf(%329, %330) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %332 = arith.constant 0 : i32
    func.return %332 : i32
  }
}