Problem 316

Sum g(floor(10^16/n)) for n=2..999999 using KMP borders.

Answer542934735751917735
Output542934735751917735
StatusPASS
Native helperno
Runtime90 ms
Peak memory1136 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 316
# Sum g(floor(10^16/n)) for n=2..999999 using KMP borders.

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

function digits_of(n: i64, buf: ptr<i8>) -> i32 {
    # write decimal digits of n into buf, return length
    if n == 0 {
        buf[0] = 48
        return 1
    }
    let tmp: ptr<i8> = calloc(24, 1)
    let mut len: i32 = 0
    let mut v: i64 = n
    while v > 0 {
        tmp[len] = ((v % 10) as i8) + 48
        v = v / 10
        len = len + 1
    }
    # reverse into buf
    let mut i: i32 = 0
    while i < len {
        buf[i] = tmp[len - 1 - i]
        i = i + 1
    }
    free(tmp)
    return len
}

function g(n: i64) -> i64 {
    let word: ptr<i8> = calloc(24, 1)
    let length: i32 = digits_of(n, word)
    let pi: ptr<i32> = calloc(length as i64, 4)
    let mut matched: i32 = 0
    let mut i: i32 = 1
    while i < length {
        while matched > 0 && word[i] != word[matched] {
            matched = pi[matched - 1]
        }
        if word[i] == word[matched] {
            matched = matched + 1
        }
        pi[i] = matched
        i = i + 1
    }
    let mut expected_end: i64 = 0
    let mut border: i32 = length
    while border > 0 {
        # POW10[border]
        let mut p: i64 = 1
        let mut e: i32 = 0
        while e < border {
            p = p * 10
            e = e + 1
        }
        expected_end = expected_end + p
        border = pi[border - 1]
    }
    let ans: i64 = expected_end - (length as i64) + 1
    free(word); free(pi)
    return ans
}

function main() -> i32 {
    let mut total: i64 = 0
    let divider: i64 = 10000000000000000
    let mut n: i64 = 2
    while n <= 999999 {
        total = total + g(divider / n)
        n = n + 1
    }
    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; }

int32_t digits_of_i64_ptr_i8(int64_t n, int8_t* buf);
int64_t g_i64(int64_t n);
int32_t main(void);



int32_t digits_of_i64_ptr_i8(int64_t n, int8_t* buf) {
    if (n == 0) {
        buf[0] = 48;
        return 1;
    }
    int8_t* tmp = (int8_t*)(calloc(24, 1));
    int32_t len = 0;
    int64_t v = n;
    while (v > 0) {
        tmp[len] = (((int8_t)(FLOW_CHECKED_MOD((v), (10)))) + 48);
        v = FLOW_CHECKED_DIV((v), (10));
        len = (len + 1);
    }
    int32_t i = 0;
    while (i < len) {
        buf[i] = tmp[((len - 1) - i)];
        i = (i + 1);
    }
    free(tmp);
    return len;
}

int64_t g_i64(int64_t n) {
    int8_t* word = (int8_t*)(calloc(24, 1));
    int32_t length = digits_of_i64_ptr_i8(n, word);
    int32_t* pi = (int32_t*)(calloc(((int64_t)(length)), 4));
    int32_t matched = 0;
    int32_t i = 1;
    while (i < length) {
        while ((matched > 0 && word[i] != word[matched])) {
            matched = pi[(matched - 1)];
        }
        if (word[i] == word[matched]) {
            matched = (matched + 1);
        }
        pi[i] = matched;
        i = (i + 1);
    }
    int64_t expected_end = 0;
    int32_t border = length;
    while (border > 0) {
        int64_t p = 1;
        int32_t e = 0;
        while (e < border) {
            p = (p * 10);
            e = (e + 1);
        }
        expected_end = (expected_end + p);
        border = pi[(border - 1)];
    }
    int64_t ans = ((expected_end - ((int64_t)(length))) + 1);
    free(word);
    free(pi);
    return ans;
}

int32_t main(void) {
    int64_t total = 0;
    int64_t divider = 10000000000000000;
    int64_t n = 2;
    while (n <= 999999) {
        total = (total + g_i64(FLOW_CHECKED_DIV((divider), (n))));
        n = (n + 1);
    }
    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 @digits_of(%arg0: i64, %arg1: !llvm.ptr) -> i32 {
    %0 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi eq, %arg0, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 48 : i32
      %4 = arith.constant 0 : i32
      %5 = arith.trunci %3 : i32 to i8
      %6 = arith.extsi %4 : i32 to i64
      %7 = llvm.getelementptr %arg1[%6] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %5, %7 : i8, !llvm.ptr
      %8 = arith.constant 1 : i32
      func.return %8 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %10 = arith.constant 24 : i32
    %11 = arith.constant 1 : i32
    %12 = arith.extsi %10 : i32 to i64
    %13 = arith.extsi %11 : i32 to i64
    %9 = func.call @calloc(%12, %13) : (i64, i64) -> !llvm.ptr
    %14 = arith.constant 0 : i32
    %15 = llvm.mlir.constant(1 : i64) : i64
    %16 = llvm.alloca %15 x i32 : (i64) -> !llvm.ptr
    llvm.store %14, %16 : i32, !llvm.ptr
    %17 = llvm.mlir.constant(1 : i64) : i64
    %18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %18 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %19 = llvm.load %18 : !llvm.ptr -> i64
    %20 = arith.constant 0 : i32
    %22 = arith.extsi %20 : i32 to i64
    %21 = arith.cmpi sgt, %19, %22 : i64
    cf.cond_br %21, ^bb4, ^bb5
    ^bb4:
      %23 = llvm.load %18 : !llvm.ptr -> i64
      %24 = arith.constant 10 : i32
      %26 = arith.extsi %24 : i32 to i64
      %25 = arith.remsi %23, %26 : i64
      %27 = arith.trunci %25 : i64 to i8
      %28 = arith.constant 48 : i32
      %30 = arith.extsi %27 : i8 to i32
      %29 = arith.addi %30, %28 : i32
      %31 = llvm.load %16 : !llvm.ptr -> i32
      %32 = arith.trunci %29 : i32 to i8
      %33 = arith.extsi %31 : i32 to i64
      %34 = llvm.getelementptr %9[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %32, %34 : i8, !llvm.ptr
      %35 = llvm.load %18 : !llvm.ptr -> i64
      %36 = arith.constant 10 : i32
      %38 = arith.extsi %36 : i32 to i64
      %37 = arith.divsi %35, %38 : i64
      llvm.store %37, %18 : i64, !llvm.ptr
      %39 = llvm.load %16 : !llvm.ptr -> i32
      %40 = arith.constant 1 : i32
      %41 = arith.addi %39, %40 : i32
      llvm.store %41, %16 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %42 = arith.constant 0 : i32
    %43 = llvm.mlir.constant(1 : i64) : i64
    %44 = llvm.alloca %43 x i32 : (i64) -> !llvm.ptr
    llvm.store %42, %44 : i32, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %45 = llvm.load %44 : !llvm.ptr -> i32
    %46 = llvm.load %16 : !llvm.ptr -> i32
    %47 = arith.cmpi slt, %45, %46 : i32
    cf.cond_br %47, ^bb7, ^bb8
    ^bb7:
      %49 = llvm.load %16 : !llvm.ptr -> i32
      %50 = arith.constant 1 : i32
      %51 = arith.subi %49, %50 : i32
      %52 = llvm.load %44 : !llvm.ptr -> i32
      %53 = arith.subi %51, %52 : i32
      %54 = arith.extsi %53 : i32 to i64
      %55 = llvm.getelementptr %9[%54] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %48 = llvm.load %55 : !llvm.ptr -> i8
      %56 = llvm.load %44 : !llvm.ptr -> i32
      %57 = arith.extsi %56 : i32 to i64
      %58 = llvm.getelementptr %arg1[%57] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %48, %58 : i8, !llvm.ptr
      %59 = llvm.load %44 : !llvm.ptr -> i32
      %60 = arith.constant 1 : i32
      %61 = arith.addi %59, %60 : i32
      llvm.store %61, %44 : i32, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    func.call @free(%9) : (!llvm.ptr) -> ()
    %63 = llvm.load %16 : !llvm.ptr -> i32
    func.return %63 : i32
  }
  func.func @g(%arg0: i64) -> i64 {
    %65 = arith.constant 24 : i32
    %66 = arith.constant 1 : i32
    %67 = arith.extsi %65 : i32 to i64
    %68 = arith.extsi %66 : i32 to i64
    %64 = func.call @calloc(%67, %68) : (i64, i64) -> !llvm.ptr
    %69 = func.call @digits_of(%arg0, %64) : (i64, !llvm.ptr) -> i32
    %71 = arith.extsi %69 : i32 to i64
    %72 = arith.constant 4 : i32
    %73 = arith.extsi %72 : i32 to i64
    %70 = func.call @calloc(%71, %73) : (i64, i64) -> !llvm.ptr
    %74 = arith.constant 0 : i32
    %75 = llvm.mlir.constant(1 : i64) : i64
    %76 = llvm.alloca %75 x i32 : (i64) -> !llvm.ptr
    llvm.store %74, %76 : i32, !llvm.ptr
    %77 = arith.constant 1 : i32
    %78 = llvm.mlir.constant(1 : i64) : i64
    %79 = llvm.alloca %78 x i32 : (i64) -> !llvm.ptr
    llvm.store %77, %79 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %80 = llvm.load %79 : !llvm.ptr -> i32
    %81 = arith.cmpi slt, %80, %69 : i32
    cf.cond_br %81, ^bb10, ^bb11
    ^bb10:
      cf.br ^bb12
      ^bb12:
      %82 = llvm.load %76 : !llvm.ptr -> i32
      %83 = arith.constant 0 : i32
      %84 = arith.cmpi sgt, %82, %83 : i32
      %85 = scf.if %84 -> (i1) {
        %87 = llvm.load %79 : !llvm.ptr -> i32
        %88 = arith.extsi %87 : i32 to i64
        %89 = llvm.getelementptr %64[%88] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %86 = llvm.load %89 : !llvm.ptr -> i8
        %91 = llvm.load %76 : !llvm.ptr -> i32
        %92 = arith.extsi %91 : i32 to i64
        %93 = llvm.getelementptr %64[%92] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %90 = llvm.load %93 : !llvm.ptr -> i8
        %95 = arith.extsi %86 : i8 to i32
        %96 = arith.extsi %90 : i8 to i32
        %94 = arith.cmpi ne, %95, %96 : i32
        scf.yield %94 : i1
      } else {
        %97 = arith.constant false
        scf.yield %97 : i1
      }
      cf.cond_br %85, ^bb13, ^bb14
      ^bb13:
        %99 = llvm.load %76 : !llvm.ptr -> i32
        %100 = arith.constant 1 : i32
        %101 = arith.subi %99, %100 : i32
        %102 = arith.extsi %101 : i32 to i64
        %103 = llvm.getelementptr %70[%102] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %98 = llvm.load %103 : !llvm.ptr -> i32
        llvm.store %98, %76 : i32, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      %105 = llvm.load %79 : !llvm.ptr -> i32
      %106 = arith.extsi %105 : i32 to i64
      %107 = llvm.getelementptr %64[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %104 = llvm.load %107 : !llvm.ptr -> i8
      %109 = llvm.load %76 : !llvm.ptr -> i32
      %110 = arith.extsi %109 : i32 to i64
      %111 = llvm.getelementptr %64[%110] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %108 = llvm.load %111 : !llvm.ptr -> i8
      %113 = arith.extsi %104 : i8 to i32
      %114 = arith.extsi %108 : i8 to i32
      %112 = arith.cmpi eq, %113, %114 : i32
      cf.cond_br %112, ^bb15, ^bb16
      ^bb15:
        %115 = llvm.load %76 : !llvm.ptr -> i32
        %116 = arith.constant 1 : i32
        %117 = arith.addi %115, %116 : i32
        llvm.store %117, %76 : i32, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %118 = llvm.load %76 : !llvm.ptr -> i32
      %119 = llvm.load %79 : !llvm.ptr -> i32
      %120 = arith.extsi %119 : i32 to i64
      %121 = llvm.getelementptr %70[%120] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %118, %121 : i32, !llvm.ptr
      %122 = llvm.load %79 : !llvm.ptr -> i32
      %123 = arith.constant 1 : i32
      %124 = arith.addi %122, %123 : i32
      llvm.store %124, %79 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %125 = arith.constant 0 : i32
    %126 = arith.extsi %125 : i32 to i64
    %127 = llvm.mlir.constant(1 : i64) : i64
    %128 = llvm.alloca %127 x i64 : (i64) -> !llvm.ptr
    llvm.store %126, %128 : i64, !llvm.ptr
    %129 = llvm.mlir.constant(1 : i64) : i64
    %130 = llvm.alloca %129 x i32 : (i64) -> !llvm.ptr
    llvm.store %69, %130 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %131 = llvm.load %130 : !llvm.ptr -> i32
    %132 = arith.constant 0 : i32
    %133 = arith.cmpi sgt, %131, %132 : i32
    cf.cond_br %133, ^bb19, ^bb20
    ^bb19:
      %134 = arith.constant 1 : i32
      %135 = arith.extsi %134 : i32 to i64
      %136 = llvm.mlir.constant(1 : i64) : i64
      %137 = llvm.alloca %136 x i64 : (i64) -> !llvm.ptr
      llvm.store %135, %137 : i64, !llvm.ptr
      %138 = arith.constant 0 : i32
      %139 = llvm.mlir.constant(1 : i64) : i64
      %140 = llvm.alloca %139 x i32 : (i64) -> !llvm.ptr
      llvm.store %138, %140 : i32, !llvm.ptr
      cf.br ^bb21
      ^bb21:
      %141 = llvm.load %140 : !llvm.ptr -> i32
      %142 = llvm.load %130 : !llvm.ptr -> i32
      %143 = arith.cmpi slt, %141, %142 : i32
      cf.cond_br %143, ^bb22, ^bb23
      ^bb22:
        %144 = llvm.load %137 : !llvm.ptr -> i64
        %145 = arith.constant 10 : i32
        %147 = arith.extsi %145 : i32 to i64
        %146 = arith.muli %144, %147 : i64
        llvm.store %146, %137 : i64, !llvm.ptr
        %148 = llvm.load %140 : !llvm.ptr -> i32
        %149 = arith.constant 1 : i32
        %150 = arith.addi %148, %149 : i32
        llvm.store %150, %140 : i32, !llvm.ptr
        cf.br ^bb21
      ^bb23:
      %151 = llvm.load %128 : !llvm.ptr -> i64
      %152 = llvm.load %137 : !llvm.ptr -> i64
      %153 = arith.addi %151, %152 : i64
      llvm.store %153, %128 : i64, !llvm.ptr
      %155 = llvm.load %130 : !llvm.ptr -> i32
      %156 = arith.constant 1 : i32
      %157 = arith.subi %155, %156 : i32
      %158 = arith.extsi %157 : i32 to i64
      %159 = llvm.getelementptr %70[%158] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %154 = llvm.load %159 : !llvm.ptr -> i32
      llvm.store %154, %130 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %160 = llvm.load %128 : !llvm.ptr -> i64
    %161 = arith.extsi %69 : i32 to i64
    %162 = arith.subi %160, %161 : i64
    %163 = arith.constant 1 : i32
    %165 = arith.extsi %163 : i32 to i64
    %164 = arith.addi %162, %165 : i64
    func.call @free(%64) : (!llvm.ptr) -> ()
    func.call @free(%70) : (!llvm.ptr) -> ()
    func.return %164 : i64
  }
  func.func @main() -> i32 {
    %168 = arith.constant 0 : i32
    %169 = arith.extsi %168 : i32 to i64
    %170 = llvm.mlir.constant(1 : i64) : i64
    %171 = llvm.alloca %170 x i64 : (i64) -> !llvm.ptr
    llvm.store %169, %171 : i64, !llvm.ptr
    %172 = arith.constant 9999995705032704 : i32
    %173 = arith.extsi %172 : i32 to i64
    %174 = arith.constant 2 : i32
    %175 = arith.extsi %174 : i32 to i64
    %176 = llvm.mlir.constant(1 : i64) : i64
    %177 = llvm.alloca %176 x i64 : (i64) -> !llvm.ptr
    llvm.store %175, %177 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %178 = llvm.load %177 : !llvm.ptr -> i64
    %179 = arith.constant 999999 : i32
    %181 = arith.extsi %179 : i32 to i64
    %180 = arith.cmpi sle, %178, %181 : i64
    cf.cond_br %180, ^bb25, ^bb26
    ^bb25:
      %182 = llvm.load %171 : !llvm.ptr -> i64
      %184 = llvm.load %177 : !llvm.ptr -> i64
      %185 = arith.divsi %173, %184 : i64
      %183 = func.call @g(%185) : (i64) -> i64
      %186 = arith.addi %182, %183 : i64
      llvm.store %186, %171 : i64, !llvm.ptr
      %187 = llvm.load %177 : !llvm.ptr -> i64
      %188 = arith.constant 1 : i32
      %190 = arith.extsi %188 : i32 to i64
      %189 = arith.addi %187, %190 : i64
      llvm.store %189, %177 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %191 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %192 = llvm.load %171 : !llvm.ptr -> i64
    %193 = llvm.call @printf(%191, %192) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %194 = arith.constant 0 : i32
    func.return %194 : i32
  }
}