Problem 346

Sum of strong repunits below 10^12.

Answer336108797689259276
Output336108797689259276
StatusPASS
Native helperno
Runtime60 ms
Peak memory8992 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(sqrt(n))
Space complexityO(n)O(1)
ApproachFlow solutionRepunit divisibility
VerdictSuboptimal

Flow source

# Project Euler 346
# Sum of strong repunits below 10^12.

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

function main() -> i32 {
    let limit: i64 = 1000000000000
    let cap: i64 = 2000000
    let found: ptr<i64> = calloc(cap, 8)
    if found == null { return 1 }
    let mut fc: i64 = 0
    found[fc] = 1
    fc = fc + 1
    let mut base: i64 = 2
    while base * base < limit {
        let mut current: i64 = 1 + base + base * base
        let mut powers: i64 = base * base
        while current < limit {
            found[fc] = current
            fc = fc + 1
            if powers > limit / base { break }
            powers = powers * base
            current = current + powers
        }
        base = base + 1
    }
    # sort
    let mut gap: i64 = fc / 2
    while gap > 0 {
        let mut i: i64 = gap
        while i < fc {
            let tmp: i64 = found[i]
            let mut j: i64 = i
            while j >= gap && found[j - gap] > tmp {
                found[j] = found[j - gap]
                j = j - gap
            }
            found[j] = tmp
            i = i + 1
        }
        gap = gap / 2
    }
    let mut total: i64 = 0
    let mut prev: i64 = -1
    let mut i: i64 = 0
    while i < fc {
        if found[i] != prev {
            total = total + found[i]
            prev = found[i]
        }
        i = i + 1
    }
    printf("%lld\n", total)
    free(found)
    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 main(void);



int32_t main(void) {
    int64_t limit = 1000000000000;
    int64_t cap = 2000000;
    int64_t* found = (int64_t*)(calloc(cap, 8));
    if (found == NULL) {
        return 1;
    }
    int64_t fc = 0;
    found[fc] = 1;
    fc = (fc + 1);
    int64_t base = 2;
    while ((base * base) < limit) {
        int64_t current = ((1 + base) + (base * base));
        int64_t powers = (base * base);
        while (current < limit) {
            found[fc] = current;
            fc = (fc + 1);
            if (powers > FLOW_CHECKED_DIV((limit), (base))) {
                break;
            }
            powers = (powers * base);
            current = (current + powers);
        }
        base = (base + 1);
    }
    int64_t gap = FLOW_CHECKED_DIV((fc), (2));
    while (gap > 0) {
        int64_t i = gap;
        while (i < fc) {
            int64_t tmp = found[i];
            int64_t j = i;
            while ((j >= gap && found[(j - gap)] > tmp)) {
                found[j] = found[(j - gap)];
                j = (j - gap);
            }
            found[j] = tmp;
            i = (i + 1);
        }
        gap = FLOW_CHECKED_DIV((gap), (2));
    }
    int64_t total = 0;
    int64_t prev = (-1);
    int64_t i = 0;
    while (i < fc) {
        if (found[i] != prev) {
            total = (total + found[i]);
            prev = found[i];
        }
        i = (i + 1);
    }
    printf("%lld\n", total);
    free(found);
    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 @main() -> i32 {
    %0 = arith.constant 995705032704 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = arith.constant 2000000 : i32
    %3 = arith.extsi %2 : i32 to i64
    %5 = arith.constant 8 : i32
    %6 = arith.extsi %5 : i32 to i64
    %4 = func.call @calloc(%3, %6) : (i64, i64) -> !llvm.ptr
    %7 = llvm.mlir.zero : !llvm.ptr
    %8 = llvm.icmp "eq" %4, %7 : !llvm.ptr
    cf.cond_br %8, ^bb0, ^bb1
    ^bb0:
      %9 = arith.constant 1 : i32
      func.return %9 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %10 = arith.constant 0 : i32
    %11 = arith.extsi %10 : i32 to i64
    %12 = llvm.mlir.constant(1 : i64) : i64
    %13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
    llvm.store %11, %13 : i64, !llvm.ptr
    %14 = arith.constant 1 : i32
    %15 = llvm.load %13 : !llvm.ptr -> i64
    %16 = arith.extsi %14 : i32 to i64
    %17 = llvm.getelementptr %4[%15] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %16, %17 : i64, !llvm.ptr
    %18 = llvm.load %13 : !llvm.ptr -> i64
    %19 = arith.constant 1 : i32
    %21 = arith.extsi %19 : i32 to i64
    %20 = arith.addi %18, %21 : i64
    llvm.store %20, %13 : i64, !llvm.ptr
    %22 = arith.constant 2 : i32
    %23 = arith.extsi %22 : i32 to i64
    %24 = llvm.mlir.constant(1 : i64) : i64
    %25 = llvm.alloca %24 x i64 : (i64) -> !llvm.ptr
    llvm.store %23, %25 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %26 = llvm.load %25 : !llvm.ptr -> i64
    %27 = llvm.load %25 : !llvm.ptr -> i64
    %28 = arith.muli %26, %27 : i64
    %29 = arith.cmpi slt, %28, %1 : i64
    cf.cond_br %29, ^bb4, ^bb5
    ^bb4:
      %30 = arith.constant 1 : i32
      %31 = llvm.load %25 : !llvm.ptr -> i64
      %33 = arith.extsi %30 : i32 to i64
      %32 = arith.addi %33, %31 : i64
      %34 = llvm.load %25 : !llvm.ptr -> i64
      %35 = llvm.load %25 : !llvm.ptr -> i64
      %36 = arith.muli %34, %35 : i64
      %37 = arith.addi %32, %36 : i64
      %38 = llvm.mlir.constant(1 : i64) : i64
      %39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
      llvm.store %37, %39 : i64, !llvm.ptr
      %40 = llvm.load %25 : !llvm.ptr -> i64
      %41 = llvm.load %25 : !llvm.ptr -> i64
      %42 = arith.muli %40, %41 : i64
      %43 = llvm.mlir.constant(1 : i64) : i64
      %44 = llvm.alloca %43 x i64 : (i64) -> !llvm.ptr
      llvm.store %42, %44 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %45 = llvm.load %39 : !llvm.ptr -> i64
      %46 = arith.cmpi slt, %45, %1 : i64
      cf.cond_br %46, ^bb7, ^bb8
      ^bb7:
        %47 = llvm.load %39 : !llvm.ptr -> i64
        %48 = llvm.load %13 : !llvm.ptr -> i64
        %49 = llvm.getelementptr %4[%48] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %47, %49 : i64, !llvm.ptr
        %50 = llvm.load %13 : !llvm.ptr -> i64
        %51 = arith.constant 1 : i32
        %53 = arith.extsi %51 : i32 to i64
        %52 = arith.addi %50, %53 : i64
        llvm.store %52, %13 : i64, !llvm.ptr
        %54 = llvm.load %44 : !llvm.ptr -> i64
        %55 = llvm.load %25 : !llvm.ptr -> i64
        %56 = arith.divsi %1, %55 : i64
        %57 = arith.cmpi sgt, %54, %56 : i64
        cf.cond_br %57, ^bb9, ^bb10
        ^bb9:
          cf.br ^bb8
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %58 = llvm.load %44 : !llvm.ptr -> i64
        %59 = llvm.load %25 : !llvm.ptr -> i64
        %60 = arith.muli %58, %59 : i64
        llvm.store %60, %44 : i64, !llvm.ptr
        %61 = llvm.load %39 : !llvm.ptr -> i64
        %62 = llvm.load %44 : !llvm.ptr -> i64
        %63 = arith.addi %61, %62 : i64
        llvm.store %63, %39 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %64 = llvm.load %25 : !llvm.ptr -> i64
      %65 = arith.constant 1 : i32
      %67 = arith.extsi %65 : i32 to i64
      %66 = arith.addi %64, %67 : i64
      llvm.store %66, %25 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %68 = llvm.load %13 : !llvm.ptr -> i64
    %69 = arith.constant 2 : i32
    %71 = arith.extsi %69 : i32 to i64
    %70 = arith.divsi %68, %71 : i64
    %72 = llvm.mlir.constant(1 : i64) : i64
    %73 = llvm.alloca %72 x i64 : (i64) -> !llvm.ptr
    llvm.store %70, %73 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %74 = llvm.load %73 : !llvm.ptr -> i64
    %75 = arith.constant 0 : i32
    %77 = arith.extsi %75 : i32 to i64
    %76 = arith.cmpi sgt, %74, %77 : i64
    cf.cond_br %76, ^bb13, ^bb14
    ^bb13:
      %78 = llvm.load %73 : !llvm.ptr -> i64
      %79 = llvm.mlir.constant(1 : i64) : i64
      %80 = llvm.alloca %79 x i64 : (i64) -> !llvm.ptr
      llvm.store %78, %80 : i64, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %81 = llvm.load %80 : !llvm.ptr -> i64
      %82 = llvm.load %13 : !llvm.ptr -> i64
      %83 = arith.cmpi slt, %81, %82 : i64
      cf.cond_br %83, ^bb16, ^bb17
      ^bb16:
        %85 = llvm.load %80 : !llvm.ptr -> i64
        %86 = llvm.getelementptr %4[%85] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %84 = llvm.load %86 : !llvm.ptr -> i64
        %87 = llvm.load %80 : !llvm.ptr -> i64
        %88 = llvm.mlir.constant(1 : i64) : i64
        %89 = llvm.alloca %88 x i64 : (i64) -> !llvm.ptr
        llvm.store %87, %89 : i64, !llvm.ptr
        cf.br ^bb18
        ^bb18:
        %90 = llvm.load %89 : !llvm.ptr -> i64
        %91 = llvm.load %73 : !llvm.ptr -> i64
        %92 = arith.cmpi sge, %90, %91 : i64
        %93 = scf.if %92 -> (i1) {
          %95 = llvm.load %89 : !llvm.ptr -> i64
          %96 = llvm.load %73 : !llvm.ptr -> i64
          %97 = arith.subi %95, %96 : i64
          %98 = llvm.getelementptr %4[%97] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %94 = llvm.load %98 : !llvm.ptr -> i64
          %99 = arith.cmpi sgt, %94, %84 : i64
          scf.yield %99 : i1
        } else {
          %100 = arith.constant false
          scf.yield %100 : i1
        }
        cf.cond_br %93, ^bb19, ^bb20
        ^bb19:
          %102 = llvm.load %89 : !llvm.ptr -> i64
          %103 = llvm.load %73 : !llvm.ptr -> i64
          %104 = arith.subi %102, %103 : i64
          %105 = llvm.getelementptr %4[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %101 = llvm.load %105 : !llvm.ptr -> i64
          %106 = llvm.load %89 : !llvm.ptr -> i64
          %107 = llvm.getelementptr %4[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %101, %107 : i64, !llvm.ptr
          %108 = llvm.load %89 : !llvm.ptr -> i64
          %109 = llvm.load %73 : !llvm.ptr -> i64
          %110 = arith.subi %108, %109 : i64
          llvm.store %110, %89 : i64, !llvm.ptr
          cf.br ^bb18
        ^bb20:
        %111 = llvm.load %89 : !llvm.ptr -> i64
        %112 = llvm.getelementptr %4[%111] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %84, %112 : i64, !llvm.ptr
        %113 = llvm.load %80 : !llvm.ptr -> i64
        %114 = arith.constant 1 : i32
        %116 = arith.extsi %114 : i32 to i64
        %115 = arith.addi %113, %116 : i64
        llvm.store %115, %80 : i64, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %117 = llvm.load %73 : !llvm.ptr -> i64
      %118 = arith.constant 2 : i32
      %120 = arith.extsi %118 : i32 to i64
      %119 = arith.divsi %117, %120 : i64
      llvm.store %119, %73 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %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 1 : i32
    %127 = arith.constant 0 : i32
    %126 = arith.subi %127, %125 : i32
    %128 = arith.extsi %126 : i32 to i64
    %129 = llvm.mlir.constant(1 : i64) : i64
    %130 = llvm.alloca %129 x i64 : (i64) -> !llvm.ptr
    llvm.store %128, %130 : i64, !llvm.ptr
    %131 = arith.constant 0 : i32
    %132 = arith.extsi %131 : i32 to i64
    %133 = llvm.mlir.constant(1 : i64) : i64
    %134 = llvm.alloca %133 x i64 : (i64) -> !llvm.ptr
    llvm.store %132, %134 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %135 = llvm.load %134 : !llvm.ptr -> i64
    %136 = llvm.load %13 : !llvm.ptr -> i64
    %137 = arith.cmpi slt, %135, %136 : i64
    cf.cond_br %137, ^bb22, ^bb23
    ^bb22:
      %139 = llvm.load %134 : !llvm.ptr -> i64
      %140 = llvm.getelementptr %4[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %138 = llvm.load %140 : !llvm.ptr -> i64
      %141 = llvm.load %130 : !llvm.ptr -> i64
      %142 = arith.cmpi ne, %138, %141 : i64
      cf.cond_br %142, ^bb24, ^bb25
      ^bb24:
        %143 = llvm.load %124 : !llvm.ptr -> i64
        %145 = llvm.load %134 : !llvm.ptr -> i64
        %146 = llvm.getelementptr %4[%145] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %144 = llvm.load %146 : !llvm.ptr -> i64
        %147 = arith.addi %143, %144 : i64
        llvm.store %147, %124 : i64, !llvm.ptr
        %149 = llvm.load %134 : !llvm.ptr -> i64
        %150 = llvm.getelementptr %4[%149] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %148 = llvm.load %150 : !llvm.ptr -> i64
        llvm.store %148, %130 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %151 = llvm.load %134 : !llvm.ptr -> i64
      %152 = arith.constant 1 : i32
      %154 = arith.extsi %152 : i32 to i64
      %153 = arith.addi %151, %154 : i64
      llvm.store %153, %134 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %155 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %156 = llvm.load %124 : !llvm.ptr -> i64
    %157 = llvm.call @printf(%155, %156) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%4) : (!llvm.ptr) -> ()
    %159 = arith.constant 0 : i32
    func.return %159 : i32
  }
}