Problem 467

Superintegers — lex-smallest SCS of prime/composite digital roots mod 10^9+7.

Answer775181359
Output775181359
StatusPASS
Native helperno
Runtime180 ms
Peak memory196832 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionModular DP or matrix exponentiation
VerdictSuboptimal

Flow source

# Project Euler 467
# Superintegers — lex-smallest SCS of prime/composite digital roots mod 10^9+7.

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

const N: i64 = 10000
const MOD: i64 = 1000000007
const SIEVE: i64 = 200000

function digital_root(x: i64) -> i32 {
    return (1 + (x - 1) % 9) as i32
}

function main() -> i32 {
    let is_p: ptr<i8> = calloc(SIEVE + 1, 1)
    let PD: ptr<i32> = calloc(N, 4)
    let CD: ptr<i32> = calloc(N, 4)
    let lcs: ptr<i16> = calloc((N + 1) * (N + 1), 2)
    if is_p == null || PD == null || CD == null || lcs == null { return 1 }

    let mut i: i64 = 0
    while i <= SIEVE {
        is_p[i] = 1
        i = i + 1
    }
    is_p[0] = 0
    is_p[1] = 0
    i = 2
    while i * i <= SIEVE {
        if is_p[i] != 0 {
            let mut j: i64 = i * i
            while j <= SIEVE {
                is_p[j] = 0
                j = j + i
            }
        }
        i = i + 1
    }

    let mut np: i64 = 0
    let mut nc: i64 = 0
    i = 2
    while i <= SIEVE && (np < N || nc < N) {
        if is_p[i] != 0 {
            if np < N {
                PD[np] = digital_root(i)
                np = np + 1
            }
        } else {
            if i >= 4 && nc < N {
                CD[nc] = digital_root(i)
                nc = nc + 1
            }
        }
        i = i + 1
    }
    if np < N || nc < N { return 1 }

    # lcs[i*(N+1)+j] = LCS length of PD[i..] and CD[j..]
    let mut ii: i64 = N - 1
    while ii >= 0 {
        let mut jj: i64 = N - 1
        while jj >= 0 {
            let idx: i64 = ii * (N + 1) + jj
            if PD[ii] == CD[jj] {
                lcs[idx] = (1 + (lcs[(ii + 1) * (N + 1) + (jj + 1)] as i64)) as i16
            } else {
                let a: i64 = lcs[(ii + 1) * (N + 1) + jj] as i64
                let b: i64 = lcs[ii * (N + 1) + (jj + 1)] as i64
                if a >= b {
                    lcs[idx] = a as i16
                } else {
                    lcs[idx] = b as i16
                }
            }
            jj = jj - 1
        }
        ii = ii - 1
    }

    let mut ans: i64 = 0
    let mut a: i64 = 0
    let mut b: i64 = 0
    while a < N && b < N {
        let mut d: i32 = 0
        if PD[a] == CD[b] {
            d = PD[a]
            a = a + 1
            b = b + 1
        } else {
            let l1: i64 = lcs[(a + 1) * (N + 1) + b] as i64
            let l2: i64 = lcs[a * (N + 1) + (b + 1)] as i64
            if l1 > l2 {
                d = PD[a]
                a = a + 1
            } else {
                if l1 < l2 {
                    d = CD[b]
                    b = b + 1
                } else {
                    if PD[a] < CD[b] {
                        d = PD[a]
                        a = a + 1
                    } else {
                        d = CD[b]
                        b = b + 1
                    }
                }
            }
        }
        ans = (ans * 10 + (d as i64)) % MOD
    }
    while a < N {
        ans = (ans * 10 + (PD[a] as i64)) % MOD
        a = a + 1
    }
    while b < N {
        ans = (ans * 10 + (CD[b] as i64)) % MOD
        b = b + 1
    }

    printf("%lld\n", ans)
    free(lcs)
    free(CD)
    free(PD)
    free(is_p)
    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; }

int32_t digital_root_i64(int64_t x);
int32_t main(void);

static const int64_t N = 10000;
static const int64_t MOD = 1000000007;
static const int64_t SIEVE = 200000;



int32_t digital_root_i64(int64_t x) {
    return ((int32_t)((1 + FLOW_CHECKED_MOD(((x - 1)), (9)))));
}

int32_t main(void) {
    int8_t* is_p = (int8_t*)(calloc((SIEVE + 1), 1));
    int32_t* PD = (int32_t*)(calloc(N, 4));
    int32_t* CD = (int32_t*)(calloc(N, 4));
    int16_t* lcs = (int16_t*)(calloc(((N + 1) * (N + 1)), 2));
    if ((((is_p == NULL || PD == NULL) || CD == NULL) || lcs == NULL)) {
        return 1;
    }
    int64_t i = 0;
    while (i <= SIEVE) {
        is_p[i] = 1;
        i = (i + 1);
    }
    is_p[0] = 0;
    is_p[1] = 0;
    i = 2;
    while ((i * i) <= SIEVE) {
        if (is_p[i] != 0) {
            int64_t j = (i * i);
            while (j <= SIEVE) {
                is_p[j] = 0;
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    int64_t np = 0;
    int64_t nc = 0;
    i = 2;
    while ((i <= SIEVE && (np < N || nc < N))) {
        if (is_p[i] != 0) {
            if (np < N) {
                PD[np] = digital_root_i64(i);
                np = (np + 1);
            }
        } else {
            if ((i >= 4 && nc < N)) {
                CD[nc] = digital_root_i64(i);
                nc = (nc + 1);
            }
        }
        i = (i + 1);
    }
    if ((np < N || nc < N)) {
        return 1;
    }
    int64_t ii = (N - 1);
    while (ii >= 0) {
        int64_t jj = (N - 1);
        while (jj >= 0) {
            int64_t idx = ((ii * (N + 1)) + jj);
            if (PD[ii] == CD[jj]) {
                lcs[idx] = ((int16_t)((1 + ((int64_t)(lcs[(((ii + 1) * (N + 1)) + (jj + 1))])))));
            } else {
                int64_t a = ((int64_t)(lcs[(((ii + 1) * (N + 1)) + jj)]));
                int64_t b = ((int64_t)(lcs[((ii * (N + 1)) + (jj + 1))]));
                if (a >= b) {
                    lcs[idx] = ((int16_t)(a));
                } else {
                    lcs[idx] = ((int16_t)(b));
                }
            }
            jj = (jj - 1);
        }
        ii = (ii - 1);
    }
    int64_t ans = 0;
    int64_t a = 0;
    int64_t b = 0;
    while ((a < N && b < N)) {
        int32_t d = 0;
        if (PD[a] == CD[b]) {
            d = PD[a];
            a = (a + 1);
            b = (b + 1);
        } else {
            int64_t l1 = ((int64_t)(lcs[(((a + 1) * (N + 1)) + b)]));
            int64_t l2 = ((int64_t)(lcs[((a * (N + 1)) + (b + 1))]));
            if (l1 > l2) {
                d = PD[a];
                a = (a + 1);
            } else {
                if (l1 < l2) {
                    d = CD[b];
                    b = (b + 1);
                } else {
                    if (PD[a] < CD[b]) {
                        d = PD[a];
                        a = (a + 1);
                    } else {
                        d = CD[b];
                        b = (b + 1);
                    }
                }
            }
        }
        ans = FLOW_CHECKED_MOD((((ans * 10) + ((int64_t)(d)))), (MOD));
    }
    while (a < N) {
        ans = FLOW_CHECKED_MOD((((ans * 10) + ((int64_t)(PD[a])))), (MOD));
        a = (a + 1);
    }
    while (b < N) {
        ans = FLOW_CHECKED_MOD((((ans * 10) + ((int64_t)(CD[b])))), (MOD));
        b = (b + 1);
    }
    printf("%lld\n", ans);
    free(lcs);
    free(CD);
    free(PD);
    free(is_p);
    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) -> ()
  // Constant: N
  llvm.mlir.global internal constant @N(10000 : i64) : i64
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000007 : i64) : i64
  // Constant: SIEVE
  llvm.mlir.global internal constant @SIEVE(200000 : i64) : i64
  func.func @digital_root(%arg0: i64) -> i32 {
    %0 = arith.constant 1 : i32
    %1 = arith.constant 1 : i32
    %3 = arith.extsi %1 : i32 to i64
    %2 = arith.subi %arg0, %3 : i64
    %4 = arith.constant 9 : i32
    %6 = arith.extsi %4 : i32 to i64
    %5 = arith.remsi %2, %6 : i64
    %8 = arith.extsi %0 : i32 to i64
    %7 = arith.addi %8, %5 : i64
    %9 = arith.trunci %7 : i64 to i32
    func.return %9 : i32
  }
  func.func @main() -> i32 {
    %11 = llvm.mlir.addressof @SIEVE : !llvm.ptr
    %12 = llvm.load %11 : !llvm.ptr -> i64
    %13 = arith.constant 1 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.addi %12, %15 : i64
    %16 = arith.constant 1 : i32
    %17 = arith.extsi %16 : i32 to i64
    %10 = func.call @calloc(%14, %17) : (i64, i64) -> !llvm.ptr
    %19 = llvm.mlir.addressof @N : !llvm.ptr
    %20 = llvm.load %19 : !llvm.ptr -> i64
    %21 = arith.constant 4 : i32
    %22 = arith.extsi %21 : i32 to i64
    %18 = func.call @calloc(%20, %22) : (i64, i64) -> !llvm.ptr
    %24 = llvm.mlir.addressof @N : !llvm.ptr
    %25 = llvm.load %24 : !llvm.ptr -> i64
    %26 = arith.constant 4 : i32
    %27 = arith.extsi %26 : i32 to i64
    %23 = func.call @calloc(%25, %27) : (i64, i64) -> !llvm.ptr
    %29 = llvm.mlir.addressof @N : !llvm.ptr
    %30 = llvm.load %29 : !llvm.ptr -> i64
    %31 = arith.constant 1 : i32
    %33 = arith.extsi %31 : i32 to i64
    %32 = arith.addi %30, %33 : i64
    %34 = llvm.mlir.addressof @N : !llvm.ptr
    %35 = llvm.load %34 : !llvm.ptr -> i64
    %36 = arith.constant 1 : i32
    %38 = arith.extsi %36 : i32 to i64
    %37 = arith.addi %35, %38 : i64
    %39 = arith.muli %32, %37 : i64
    %40 = arith.constant 2 : i32
    %41 = arith.extsi %40 : i32 to i64
    %28 = func.call @calloc(%39, %41) : (i64, i64) -> !llvm.ptr
    %42 = llvm.mlir.zero : !llvm.ptr
    %43 = llvm.icmp "eq" %10, %42 : !llvm.ptr
    %44 = scf.if %43 -> (i1) {
      %45 = arith.constant true
      scf.yield %45 : i1
    } else {
      %46 = llvm.mlir.zero : !llvm.ptr
      %47 = llvm.icmp "eq" %18, %46 : !llvm.ptr
      scf.yield %47 : i1
    }
    %48 = scf.if %44 -> (i1) {
      %49 = arith.constant true
      scf.yield %49 : i1
    } else {
      %50 = llvm.mlir.zero : !llvm.ptr
      %51 = llvm.icmp "eq" %23, %50 : !llvm.ptr
      scf.yield %51 : i1
    }
    %52 = scf.if %48 -> (i1) {
      %53 = arith.constant true
      scf.yield %53 : i1
    } else {
      %54 = llvm.mlir.zero : !llvm.ptr
      %55 = llvm.icmp "eq" %28, %54 : !llvm.ptr
      scf.yield %55 : i1
    }
    cf.cond_br %52, ^bb0, ^bb1
    ^bb0:
      %56 = arith.constant 1 : i32
      func.return %56 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %57 = arith.constant 0 : i32
    %58 = arith.extsi %57 : i32 to i64
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x i64 : (i64) -> !llvm.ptr
    llvm.store %58, %60 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %61 = llvm.load %60 : !llvm.ptr -> i64
    %62 = llvm.mlir.addressof @SIEVE : !llvm.ptr
    %63 = llvm.load %62 : !llvm.ptr -> i64
    %64 = arith.cmpi sle, %61, %63 : i64
    cf.cond_br %64, ^bb4, ^bb5
    ^bb4:
      %65 = arith.constant 1 : i32
      %66 = llvm.load %60 : !llvm.ptr -> i64
      %67 = arith.trunci %65 : i32 to i8
      %68 = llvm.getelementptr %10[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %67, %68 : i8, !llvm.ptr
      %69 = llvm.load %60 : !llvm.ptr -> i64
      %70 = arith.constant 1 : i32
      %72 = arith.extsi %70 : i32 to i64
      %71 = arith.addi %69, %72 : i64
      llvm.store %71, %60 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %73 = arith.constant 0 : i32
    %74 = arith.constant 0 : i32
    %75 = arith.trunci %73 : i32 to i8
    %76 = arith.extsi %74 : i32 to i64
    %77 = llvm.getelementptr %10[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %75, %77 : i8, !llvm.ptr
    %78 = arith.constant 0 : i32
    %79 = arith.constant 1 : i32
    %80 = arith.trunci %78 : i32 to i8
    %81 = arith.extsi %79 : i32 to i64
    %82 = llvm.getelementptr %10[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %80, %82 : i8, !llvm.ptr
    %83 = arith.constant 2 : i32
    %84 = arith.extsi %83 : i32 to i64
    llvm.store %84, %60 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %85 = llvm.load %60 : !llvm.ptr -> i64
    %86 = llvm.load %60 : !llvm.ptr -> i64
    %87 = arith.muli %85, %86 : i64
    %88 = llvm.mlir.addressof @SIEVE : !llvm.ptr
    %89 = llvm.load %88 : !llvm.ptr -> i64
    %90 = arith.cmpi sle, %87, %89 : i64
    cf.cond_br %90, ^bb7, ^bb8
    ^bb7:
      %92 = llvm.load %60 : !llvm.ptr -> i64
      %93 = llvm.getelementptr %10[%92] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %91 = llvm.load %93 : !llvm.ptr -> i8
      %94 = arith.constant 0 : i32
      %96 = arith.extsi %91 : i8 to i32
      %95 = arith.cmpi ne, %96, %94 : i32
      cf.cond_br %95, ^bb9, ^bb10
      ^bb9:
        %97 = llvm.load %60 : !llvm.ptr -> i64
        %98 = llvm.load %60 : !llvm.ptr -> i64
        %99 = arith.muli %97, %98 : i64
        %100 = llvm.mlir.constant(1 : i64) : i64
        %101 = llvm.alloca %100 x i64 : (i64) -> !llvm.ptr
        llvm.store %99, %101 : i64, !llvm.ptr
        cf.br ^bb12
        ^bb12:
        %102 = llvm.load %101 : !llvm.ptr -> i64
        %103 = llvm.mlir.addressof @SIEVE : !llvm.ptr
        %104 = llvm.load %103 : !llvm.ptr -> i64
        %105 = arith.cmpi sle, %102, %104 : i64
        cf.cond_br %105, ^bb13, ^bb14
        ^bb13:
          %106 = arith.constant 0 : i32
          %107 = llvm.load %101 : !llvm.ptr -> i64
          %108 = arith.trunci %106 : i32 to i8
          %109 = llvm.getelementptr %10[%107] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %108, %109 : i8, !llvm.ptr
          %110 = llvm.load %101 : !llvm.ptr -> i64
          %111 = llvm.load %60 : !llvm.ptr -> i64
          %112 = arith.addi %110, %111 : i64
          llvm.store %112, %101 : i64, !llvm.ptr
          cf.br ^bb12
        ^bb14:
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %113 = llvm.load %60 : !llvm.ptr -> i64
      %114 = arith.constant 1 : i32
      %116 = arith.extsi %114 : i32 to i64
      %115 = arith.addi %113, %116 : i64
      llvm.store %115, %60 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %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
    %121 = arith.constant 0 : i32
    %122 = arith.extsi %121 : i32 to i64
    %123 = llvm.mlir.constant(1 : i64) : i64
    %124 = llvm.alloca %123 x i64 : (i64) -> !llvm.ptr
    llvm.store %122, %124 : i64, !llvm.ptr
    %125 = arith.constant 2 : i32
    %126 = arith.extsi %125 : i32 to i64
    llvm.store %126, %60 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %127 = llvm.load %60 : !llvm.ptr -> i64
    %128 = llvm.mlir.addressof @SIEVE : !llvm.ptr
    %129 = llvm.load %128 : !llvm.ptr -> i64
    %130 = arith.cmpi sle, %127, %129 : i64
    %131 = scf.if %130 -> (i1) {
      %132 = llvm.load %120 : !llvm.ptr -> i64
      %133 = llvm.mlir.addressof @N : !llvm.ptr
      %134 = llvm.load %133 : !llvm.ptr -> i64
      %135 = arith.cmpi slt, %132, %134 : i64
      %136 = scf.if %135 -> (i1) {
        %137 = arith.constant true
        scf.yield %137 : i1
      } else {
        %138 = llvm.load %124 : !llvm.ptr -> i64
        %139 = llvm.mlir.addressof @N : !llvm.ptr
        %140 = llvm.load %139 : !llvm.ptr -> i64
        %141 = arith.cmpi slt, %138, %140 : i64
        scf.yield %141 : i1
      }
      scf.yield %136 : i1
    } else {
      %142 = arith.constant false
      scf.yield %142 : i1
    }
    cf.cond_br %131, ^bb16, ^bb17
    ^bb16:
      %144 = llvm.load %60 : !llvm.ptr -> i64
      %145 = llvm.getelementptr %10[%144] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %143 = llvm.load %145 : !llvm.ptr -> i8
      %146 = arith.constant 0 : i32
      %148 = arith.extsi %143 : i8 to i32
      %147 = arith.cmpi ne, %148, %146 : i32
      cf.cond_br %147, ^bb18, ^bb19
      ^bb18:
        %149 = llvm.load %120 : !llvm.ptr -> i64
        %150 = llvm.mlir.addressof @N : !llvm.ptr
        %151 = llvm.load %150 : !llvm.ptr -> i64
        %152 = arith.cmpi slt, %149, %151 : i64
        cf.cond_br %152, ^bb21, ^bb22
        ^bb21:
          %154 = llvm.load %60 : !llvm.ptr -> i64
          %153 = func.call @digital_root(%154) : (i64) -> i32
          %155 = llvm.load %120 : !llvm.ptr -> i64
          %156 = llvm.getelementptr %18[%155] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %153, %156 : i32, !llvm.ptr
          %157 = llvm.load %120 : !llvm.ptr -> i64
          %158 = arith.constant 1 : i32
          %160 = arith.extsi %158 : i32 to i64
          %159 = arith.addi %157, %160 : i64
          llvm.store %159, %120 : i64, !llvm.ptr
          cf.br ^bb23
        ^bb22:
          cf.br ^bb23
        ^bb23:
        cf.br ^bb20
      ^bb19:
        %161 = llvm.load %60 : !llvm.ptr -> i64
        %162 = arith.constant 4 : i32
        %164 = arith.extsi %162 : i32 to i64
        %163 = arith.cmpi sge, %161, %164 : i64
        %165 = scf.if %163 -> (i1) {
          %166 = llvm.load %124 : !llvm.ptr -> i64
          %167 = llvm.mlir.addressof @N : !llvm.ptr
          %168 = llvm.load %167 : !llvm.ptr -> i64
          %169 = arith.cmpi slt, %166, %168 : i64
          scf.yield %169 : i1
        } else {
          %170 = arith.constant false
          scf.yield %170 : i1
        }
        cf.cond_br %165, ^bb24, ^bb25
        ^bb24:
          %172 = llvm.load %60 : !llvm.ptr -> i64
          %171 = func.call @digital_root(%172) : (i64) -> i32
          %173 = llvm.load %124 : !llvm.ptr -> i64
          %174 = llvm.getelementptr %23[%173] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %171, %174 : i32, !llvm.ptr
          %175 = llvm.load %124 : !llvm.ptr -> i64
          %176 = arith.constant 1 : i32
          %178 = arith.extsi %176 : i32 to i64
          %177 = arith.addi %175, %178 : i64
          llvm.store %177, %124 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb25:
          cf.br ^bb26
        ^bb26:
        cf.br ^bb20
      ^bb20:
      %179 = llvm.load %60 : !llvm.ptr -> i64
      %180 = arith.constant 1 : i32
      %182 = arith.extsi %180 : i32 to i64
      %181 = arith.addi %179, %182 : i64
      llvm.store %181, %60 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %183 = llvm.load %120 : !llvm.ptr -> i64
    %184 = llvm.mlir.addressof @N : !llvm.ptr
    %185 = llvm.load %184 : !llvm.ptr -> i64
    %186 = arith.cmpi slt, %183, %185 : i64
    %187 = scf.if %186 -> (i1) {
      %188 = arith.constant true
      scf.yield %188 : i1
    } else {
      %189 = llvm.load %124 : !llvm.ptr -> i64
      %190 = llvm.mlir.addressof @N : !llvm.ptr
      %191 = llvm.load %190 : !llvm.ptr -> i64
      %192 = arith.cmpi slt, %189, %191 : i64
      scf.yield %192 : i1
    }
    cf.cond_br %187, ^bb27, ^bb28
    ^bb27:
      %193 = arith.constant 1 : i32
      func.return %193 : i32
    ^bb28:
      cf.br ^bb29
    ^bb29:
    %194 = llvm.mlir.addressof @N : !llvm.ptr
    %195 = llvm.load %194 : !llvm.ptr -> i64
    %196 = arith.constant 1 : i32
    %198 = arith.extsi %196 : i32 to i64
    %197 = arith.subi %195, %198 : i64
    %199 = llvm.mlir.constant(1 : i64) : i64
    %200 = llvm.alloca %199 x i64 : (i64) -> !llvm.ptr
    llvm.store %197, %200 : i64, !llvm.ptr
    cf.br ^bb30
    ^bb30:
    %201 = llvm.load %200 : !llvm.ptr -> i64
    %202 = arith.constant 0 : i32
    %204 = arith.extsi %202 : i32 to i64
    %203 = arith.cmpi sge, %201, %204 : i64
    cf.cond_br %203, ^bb31, ^bb32
    ^bb31:
      %205 = llvm.mlir.addressof @N : !llvm.ptr
      %206 = llvm.load %205 : !llvm.ptr -> i64
      %207 = arith.constant 1 : i32
      %209 = arith.extsi %207 : i32 to i64
      %208 = arith.subi %206, %209 : i64
      %210 = llvm.mlir.constant(1 : i64) : i64
      %211 = llvm.alloca %210 x i64 : (i64) -> !llvm.ptr
      llvm.store %208, %211 : i64, !llvm.ptr
      cf.br ^bb33
      ^bb33:
      %212 = llvm.load %211 : !llvm.ptr -> i64
      %213 = arith.constant 0 : i32
      %215 = arith.extsi %213 : i32 to i64
      %214 = arith.cmpi sge, %212, %215 : i64
      cf.cond_br %214, ^bb34, ^bb35
      ^bb34:
        %216 = llvm.load %200 : !llvm.ptr -> i64
        %217 = llvm.mlir.addressof @N : !llvm.ptr
        %218 = llvm.load %217 : !llvm.ptr -> i64
        %219 = arith.constant 1 : i32
        %221 = arith.extsi %219 : i32 to i64
        %220 = arith.addi %218, %221 : i64
        %222 = arith.muli %216, %220 : i64
        %223 = llvm.load %211 : !llvm.ptr -> i64
        %224 = arith.addi %222, %223 : i64
        %226 = llvm.load %200 : !llvm.ptr -> i64
        %227 = llvm.getelementptr %18[%226] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %225 = llvm.load %227 : !llvm.ptr -> i32
        %229 = llvm.load %211 : !llvm.ptr -> i64
        %230 = llvm.getelementptr %23[%229] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %228 = llvm.load %230 : !llvm.ptr -> i32
        %231 = arith.cmpi eq, %225, %228 : i32
        cf.cond_br %231, ^bb36, ^bb37
        ^bb36:
          %232 = arith.constant 1 : i32
          %234 = llvm.load %200 : !llvm.ptr -> i64
          %235 = arith.constant 1 : 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.constant 1 : i32
          %242 = arith.extsi %240 : i32 to i64
          %241 = arith.addi %239, %242 : i64
          %243 = arith.muli %236, %241 : i64
          %244 = llvm.load %211 : !llvm.ptr -> i64
          %245 = arith.constant 1 : i32
          %247 = arith.extsi %245 : i32 to i64
          %246 = arith.addi %244, %247 : i64
          %248 = arith.addi %243, %246 : i64
          %249 = llvm.getelementptr %28[%248] : (!llvm.ptr, i64) -> !llvm.ptr, i16
          %233 = llvm.load %249 : !llvm.ptr -> i16
          %250 = arith.extsi %233 : i16 to i64
          %252 = arith.extsi %232 : i32 to i64
          %251 = arith.addi %252, %250 : i64
          %253 = arith.trunci %251 : i64 to i16
          %254 = llvm.getelementptr %28[%224] : (!llvm.ptr, i64) -> !llvm.ptr, i16
          llvm.store %253, %254 : i16, !llvm.ptr
          cf.br ^bb38
        ^bb37:
          %256 = llvm.load %200 : !llvm.ptr -> i64
          %257 = arith.constant 1 : i32
          %259 = arith.extsi %257 : i32 to i64
          %258 = arith.addi %256, %259 : i64
          %260 = llvm.mlir.addressof @N : !llvm.ptr
          %261 = llvm.load %260 : !llvm.ptr -> i64
          %262 = arith.constant 1 : i32
          %264 = arith.extsi %262 : i32 to i64
          %263 = arith.addi %261, %264 : i64
          %265 = arith.muli %258, %263 : i64
          %266 = llvm.load %211 : !llvm.ptr -> i64
          %267 = arith.addi %265, %266 : i64
          %268 = llvm.getelementptr %28[%267] : (!llvm.ptr, i64) -> !llvm.ptr, i16
          %255 = llvm.load %268 : !llvm.ptr -> i16
          %269 = arith.extsi %255 : i16 to i64
          %271 = llvm.load %200 : !llvm.ptr -> i64
          %272 = llvm.mlir.addressof @N : !llvm.ptr
          %273 = llvm.load %272 : !llvm.ptr -> i64
          %274 = arith.constant 1 : i32
          %276 = arith.extsi %274 : i32 to i64
          %275 = arith.addi %273, %276 : i64
          %277 = arith.muli %271, %275 : i64
          %278 = llvm.load %211 : !llvm.ptr -> i64
          %279 = arith.constant 1 : i32
          %281 = arith.extsi %279 : i32 to i64
          %280 = arith.addi %278, %281 : i64
          %282 = arith.addi %277, %280 : i64
          %283 = llvm.getelementptr %28[%282] : (!llvm.ptr, i64) -> !llvm.ptr, i16
          %270 = llvm.load %283 : !llvm.ptr -> i16
          %284 = arith.extsi %270 : i16 to i64
          %285 = arith.cmpi sge, %269, %284 : i64
          cf.cond_br %285, ^bb39, ^bb40
          ^bb39:
            %286 = arith.trunci %269 : i64 to i16
            %287 = llvm.getelementptr %28[%224] : (!llvm.ptr, i64) -> !llvm.ptr, i16
            llvm.store %286, %287 : i16, !llvm.ptr
            cf.br ^bb41
          ^bb40:
            %288 = arith.trunci %284 : i64 to i16
            %289 = llvm.getelementptr %28[%224] : (!llvm.ptr, i64) -> !llvm.ptr, i16
            llvm.store %288, %289 : i16, !llvm.ptr
            cf.br ^bb41
          ^bb41:
          cf.br ^bb38
        ^bb38:
        %290 = llvm.load %211 : !llvm.ptr -> i64
        %291 = arith.constant 1 : i32
        %293 = arith.extsi %291 : i32 to i64
        %292 = arith.subi %290, %293 : i64
        llvm.store %292, %211 : i64, !llvm.ptr
        cf.br ^bb33
      ^bb35:
      %294 = llvm.load %200 : !llvm.ptr -> i64
      %295 = arith.constant 1 : i32
      %297 = arith.extsi %295 : i32 to i64
      %296 = arith.subi %294, %297 : i64
      llvm.store %296, %200 : i64, !llvm.ptr
      cf.br ^bb30
    ^bb32:
    %298 = arith.constant 0 : i32
    %299 = arith.extsi %298 : i32 to i64
    %300 = llvm.mlir.constant(1 : i64) : i64
    %301 = llvm.alloca %300 x i64 : (i64) -> !llvm.ptr
    llvm.store %299, %301 : i64, !llvm.ptr
    %302 = arith.constant 0 : i32
    %303 = arith.extsi %302 : i32 to i64
    %304 = llvm.mlir.constant(1 : i64) : i64
    %305 = llvm.alloca %304 x i64 : (i64) -> !llvm.ptr
    llvm.store %303, %305 : i64, !llvm.ptr
    %306 = arith.constant 0 : i32
    %307 = arith.extsi %306 : i32 to i64
    %308 = llvm.mlir.constant(1 : i64) : i64
    %309 = llvm.alloca %308 x i64 : (i64) -> !llvm.ptr
    llvm.store %307, %309 : i64, !llvm.ptr
    cf.br ^bb42
    ^bb42:
    %310 = llvm.load %305 : !llvm.ptr -> i64
    %311 = llvm.mlir.addressof @N : !llvm.ptr
    %312 = llvm.load %311 : !llvm.ptr -> i64
    %313 = arith.cmpi slt, %310, %312 : i64
    %314 = scf.if %313 -> (i1) {
      %315 = llvm.load %309 : !llvm.ptr -> i64
      %316 = llvm.mlir.addressof @N : !llvm.ptr
      %317 = llvm.load %316 : !llvm.ptr -> i64
      %318 = arith.cmpi slt, %315, %317 : i64
      scf.yield %318 : i1
    } else {
      %319 = arith.constant false
      scf.yield %319 : i1
    }
    cf.cond_br %314, ^bb43, ^bb44
    ^bb43:
      %320 = arith.constant 0 : i32
      %321 = llvm.mlir.constant(1 : i64) : i64
      %322 = llvm.alloca %321 x i32 : (i64) -> !llvm.ptr
      llvm.store %320, %322 : i32, !llvm.ptr
      %324 = llvm.load %305 : !llvm.ptr -> i64
      %325 = llvm.getelementptr %18[%324] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %323 = llvm.load %325 : !llvm.ptr -> i32
      %327 = llvm.load %309 : !llvm.ptr -> i64
      %328 = llvm.getelementptr %23[%327] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %326 = llvm.load %328 : !llvm.ptr -> i32
      %329 = arith.cmpi eq, %323, %326 : i32
      cf.cond_br %329, ^bb45, ^bb46
      ^bb45:
        %331 = llvm.load %305 : !llvm.ptr -> i64
        %332 = llvm.getelementptr %18[%331] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %330 = llvm.load %332 : !llvm.ptr -> i32
        llvm.store %330, %322 : i32, !llvm.ptr
        %333 = llvm.load %305 : !llvm.ptr -> i64
        %334 = arith.constant 1 : i32
        %336 = arith.extsi %334 : i32 to i64
        %335 = arith.addi %333, %336 : i64
        llvm.store %335, %305 : i64, !llvm.ptr
        %337 = llvm.load %309 : !llvm.ptr -> i64
        %338 = arith.constant 1 : i32
        %340 = arith.extsi %338 : i32 to i64
        %339 = arith.addi %337, %340 : i64
        llvm.store %339, %309 : i64, !llvm.ptr
        cf.br ^bb47
      ^bb46:
        %342 = llvm.load %305 : !llvm.ptr -> i64
        %343 = arith.constant 1 : i32
        %345 = arith.extsi %343 : i32 to i64
        %344 = arith.addi %342, %345 : i64
        %346 = llvm.mlir.addressof @N : !llvm.ptr
        %347 = llvm.load %346 : !llvm.ptr -> i64
        %348 = arith.constant 1 : i32
        %350 = arith.extsi %348 : i32 to i64
        %349 = arith.addi %347, %350 : i64
        %351 = arith.muli %344, %349 : i64
        %352 = llvm.load %309 : !llvm.ptr -> i64
        %353 = arith.addi %351, %352 : i64
        %354 = llvm.getelementptr %28[%353] : (!llvm.ptr, i64) -> !llvm.ptr, i16
        %341 = llvm.load %354 : !llvm.ptr -> i16
        %355 = arith.extsi %341 : i16 to i64
        %357 = llvm.load %305 : !llvm.ptr -> i64
        %358 = llvm.mlir.addressof @N : !llvm.ptr
        %359 = llvm.load %358 : !llvm.ptr -> i64
        %360 = arith.constant 1 : i32
        %362 = arith.extsi %360 : i32 to i64
        %361 = arith.addi %359, %362 : i64
        %363 = arith.muli %357, %361 : i64
        %364 = llvm.load %309 : !llvm.ptr -> i64
        %365 = arith.constant 1 : i32
        %367 = arith.extsi %365 : i32 to i64
        %366 = arith.addi %364, %367 : i64
        %368 = arith.addi %363, %366 : i64
        %369 = llvm.getelementptr %28[%368] : (!llvm.ptr, i64) -> !llvm.ptr, i16
        %356 = llvm.load %369 : !llvm.ptr -> i16
        %370 = arith.extsi %356 : i16 to i64
        %371 = arith.cmpi sgt, %355, %370 : i64
        cf.cond_br %371, ^bb48, ^bb49
        ^bb48:
          %373 = llvm.load %305 : !llvm.ptr -> i64
          %374 = llvm.getelementptr %18[%373] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %372 = llvm.load %374 : !llvm.ptr -> i32
          llvm.store %372, %322 : i32, !llvm.ptr
          %375 = llvm.load %305 : !llvm.ptr -> i64
          %376 = arith.constant 1 : i32
          %378 = arith.extsi %376 : i32 to i64
          %377 = arith.addi %375, %378 : i64
          llvm.store %377, %305 : i64, !llvm.ptr
          cf.br ^bb50
        ^bb49:
          %379 = arith.cmpi slt, %355, %370 : i64
          cf.cond_br %379, ^bb51, ^bb52
          ^bb51:
            %381 = llvm.load %309 : !llvm.ptr -> i64
            %382 = llvm.getelementptr %23[%381] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %380 = llvm.load %382 : !llvm.ptr -> i32
            llvm.store %380, %322 : i32, !llvm.ptr
            %383 = llvm.load %309 : !llvm.ptr -> i64
            %384 = arith.constant 1 : i32
            %386 = arith.extsi %384 : i32 to i64
            %385 = arith.addi %383, %386 : i64
            llvm.store %385, %309 : i64, !llvm.ptr
            cf.br ^bb53
          ^bb52:
            %388 = llvm.load %305 : !llvm.ptr -> i64
            %389 = llvm.getelementptr %18[%388] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %387 = llvm.load %389 : !llvm.ptr -> i32
            %391 = llvm.load %309 : !llvm.ptr -> i64
            %392 = llvm.getelementptr %23[%391] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %390 = llvm.load %392 : !llvm.ptr -> i32
            %393 = arith.cmpi slt, %387, %390 : i32
            cf.cond_br %393, ^bb54, ^bb55
            ^bb54:
              %395 = llvm.load %305 : !llvm.ptr -> i64
              %396 = llvm.getelementptr %18[%395] : (!llvm.ptr, i64) -> !llvm.ptr, i32
              %394 = llvm.load %396 : !llvm.ptr -> i32
              llvm.store %394, %322 : i32, !llvm.ptr
              %397 = llvm.load %305 : !llvm.ptr -> i64
              %398 = arith.constant 1 : i32
              %400 = arith.extsi %398 : i32 to i64
              %399 = arith.addi %397, %400 : i64
              llvm.store %399, %305 : i64, !llvm.ptr
              cf.br ^bb56
            ^bb55:
              %402 = llvm.load %309 : !llvm.ptr -> i64
              %403 = llvm.getelementptr %23[%402] : (!llvm.ptr, i64) -> !llvm.ptr, i32
              %401 = llvm.load %403 : !llvm.ptr -> i32
              llvm.store %401, %322 : i32, !llvm.ptr
              %404 = llvm.load %309 : !llvm.ptr -> i64
              %405 = arith.constant 1 : i32
              %407 = arith.extsi %405 : i32 to i64
              %406 = arith.addi %404, %407 : i64
              llvm.store %406, %309 : i64, !llvm.ptr
              cf.br ^bb56
            ^bb56:
            cf.br ^bb53
          ^bb53:
          cf.br ^bb50
        ^bb50:
        cf.br ^bb47
      ^bb47:
      %408 = llvm.load %301 : !llvm.ptr -> i64
      %409 = arith.constant 10 : i32
      %411 = arith.extsi %409 : i32 to i64
      %410 = arith.muli %408, %411 : i64
      %412 = llvm.load %322 : !llvm.ptr -> i32
      %413 = arith.extsi %412 : i32 to i64
      %414 = arith.addi %410, %413 : i64
      %415 = llvm.mlir.addressof @MOD : !llvm.ptr
      %416 = llvm.load %415 : !llvm.ptr -> i64
      %417 = arith.remsi %414, %416 : i64
      llvm.store %417, %301 : i64, !llvm.ptr
      cf.br ^bb42
    ^bb44:
    cf.br ^bb57
    ^bb57:
    %418 = llvm.load %305 : !llvm.ptr -> i64
    %419 = llvm.mlir.addressof @N : !llvm.ptr
    %420 = llvm.load %419 : !llvm.ptr -> i64
    %421 = arith.cmpi slt, %418, %420 : i64
    cf.cond_br %421, ^bb58, ^bb59
    ^bb58:
      %422 = llvm.load %301 : !llvm.ptr -> i64
      %423 = arith.constant 10 : i32
      %425 = arith.extsi %423 : i32 to i64
      %424 = arith.muli %422, %425 : i64
      %427 = llvm.load %305 : !llvm.ptr -> i64
      %428 = llvm.getelementptr %18[%427] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %426 = llvm.load %428 : !llvm.ptr -> i32
      %429 = arith.extsi %426 : i32 to i64
      %430 = arith.addi %424, %429 : i64
      %431 = llvm.mlir.addressof @MOD : !llvm.ptr
      %432 = llvm.load %431 : !llvm.ptr -> i64
      %433 = arith.remsi %430, %432 : i64
      llvm.store %433, %301 : i64, !llvm.ptr
      %434 = llvm.load %305 : !llvm.ptr -> i64
      %435 = arith.constant 1 : i32
      %437 = arith.extsi %435 : i32 to i64
      %436 = arith.addi %434, %437 : i64
      llvm.store %436, %305 : i64, !llvm.ptr
      cf.br ^bb57
    ^bb59:
    cf.br ^bb60
    ^bb60:
    %438 = llvm.load %309 : !llvm.ptr -> i64
    %439 = llvm.mlir.addressof @N : !llvm.ptr
    %440 = llvm.load %439 : !llvm.ptr -> i64
    %441 = arith.cmpi slt, %438, %440 : i64
    cf.cond_br %441, ^bb61, ^bb62
    ^bb61:
      %442 = llvm.load %301 : !llvm.ptr -> i64
      %443 = arith.constant 10 : i32
      %445 = arith.extsi %443 : i32 to i64
      %444 = arith.muli %442, %445 : i64
      %447 = llvm.load %309 : !llvm.ptr -> i64
      %448 = llvm.getelementptr %23[%447] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %446 = llvm.load %448 : !llvm.ptr -> i32
      %449 = arith.extsi %446 : i32 to i64
      %450 = arith.addi %444, %449 : i64
      %451 = llvm.mlir.addressof @MOD : !llvm.ptr
      %452 = llvm.load %451 : !llvm.ptr -> i64
      %453 = arith.remsi %450, %452 : i64
      llvm.store %453, %301 : i64, !llvm.ptr
      %454 = llvm.load %309 : !llvm.ptr -> i64
      %455 = arith.constant 1 : i32
      %457 = arith.extsi %455 : i32 to i64
      %456 = arith.addi %454, %457 : i64
      llvm.store %456, %309 : i64, !llvm.ptr
      cf.br ^bb60
    ^bb62:
    %458 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %459 = llvm.load %301 : !llvm.ptr -> i64
    %460 = llvm.call @printf(%458, %459) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%28) : (!llvm.ptr) -> ()
    func.call @free(%23) : (!llvm.ptr) -> ()
    func.call @free(%18) : (!llvm.ptr) -> ()
    func.call @free(%10) : (!llvm.ptr) -> ()
    %465 = arith.constant 0 : i32
    func.return %465 : i32
  }
}