Problem 694

Cube-full divisors: S(10^18) via DFS over p^3 | n cube-fulls. Ported from native C to pure Flow.

Answer1339784153569958487
Output1339784153569958487
StatusPASS
Native helperno
Runtime40 ms
Peak memory10048 KB
Time complexityO(n log n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n log n)O(n log log n)
Space complexityO(n)O(n)
ApproachFlow solutionSieve-based divisor sums
VerdictSuboptimal

Flow source

# Project Euler 694
# Cube-full divisors: S(10^18) via DFS over p^3 | n cube-fulls.
# Ported from native C to pure Flow.

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

let mut N: i64 = 0
let mut primes: ptr<i64> = null
let mut pc: i64 = 0

function generate(curr0: i64, idx: i64) -> i64 {
    let mut total: i64 = 0
    let p: i64 = primes[idx]
    if curr0 > N / (p * p * p) {
        return 0
    }
    let mut curr: i64 = curr0 * (p * p * p)
    while curr <= N {
        total = total + N / curr
        for j in (idx + 1)..pc {
            let t: i64 = generate(curr, j)
            if t == 0 {
                break
            }
            total = total + t
        }
        if curr > N / p {
            break
        }
        curr = curr * p
    }
    return total
}

function main() -> i32 {
    N = 1000000000000000000
    let lim: i64 = (cbrt(N as f64) + 2.0) as i64
    let sieve: ptr<i8> = calloc(lim + 1, 1) as ptr<i8>
    primes = calloc(lim, 8) as ptr<i64>
    pc = 0
    let mut i: i64 = 2
    while i <= lim {
        if sieve[i] == 0 {
            primes[pc] = i
            pc = pc + 1
            let mut j: i64 = i * i
            while j <= lim {
                sieve[j] = 1
                j = j + i
            }
        }
        i = i + 1
    }
    let mut ans: i64 = N
    for i in 0..pc {
        ans = ans + generate(1, i)
    }
    free(sieve as ptr<void>)
    free(primes as ptr<void>)
    printf("%lld\n", ans)
    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; }

double cbrt(double x);
int64_t generate_i64_i64(int64_t curr0, int64_t idx);
int32_t main(void);

/* Module statics */
static int64_t N = 0;
static int64_t* primes = NULL;
static int64_t pc = 0;




int64_t generate_i64_i64(int64_t curr0, int64_t idx) {
    int64_t total = 0;
    int64_t p = primes[idx];
    if (curr0 > FLOW_CHECKED_DIV((N), (((p * p) * p)))) {
        return 0;
    }
    int64_t curr = (curr0 * ((p * p) * p));
    while (curr <= N) {
        total = (total + FLOW_CHECKED_DIV((N), (curr)));
        int32_t __flow_step_1 = 1;
        for (int32_t j = (idx + 1); ((idx + 1) <= pc) ? j < pc : j > pc; j += ((idx + 1) <= pc) ? 1 : -1) {
            int64_t t = generate_i64_i64(curr, j);
            if (t == 0) {
                break;
            }
            total = (total + t);
        }
        if (curr > FLOW_CHECKED_DIV((N), (p))) {
            break;
        }
        curr = (curr * p);
    }
    return total;
}

int32_t main(void) {
    N = 1000000000000000000;
    int64_t lim = ((int64_t)((cbrt(((double)(N))) + 2.0)));
    int8_t* sieve = (int8_t*)(((int8_t*)(calloc((lim + 1), 1))));
    primes = ((int64_t*)(calloc(lim, 8)));
    pc = 0;
    int64_t i = 2;
    while (i <= lim) {
        if (sieve[i] == 0) {
            primes[pc] = i;
            pc = (pc + 1);
            int64_t j = (i * i);
            while (j <= lim) {
                sieve[j] = 1;
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    int64_t ans = N;
    int32_t __flow_step_2 = 1;
    for (int32_t i = 0; (0 <= pc) ? i < pc : i > pc; i += (0 <= pc) ? 1 : -1) {
        ans = (ans + generate_i64_i64(1, i));
    }
    free(((void*)(sieve)));
    free(((void*)(primes)));
    printf("%lld\n", ans);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func private @cbrt(f64) -> f64
  // Module static: N
  llvm.mlir.global internal @N(0 : i64) : i64
  // Module static: primes
  llvm.mlir.global internal @primes() {addr_space = 0 : i32} : !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    llvm.return %0 : !llvm.ptr
  }
  // Module static: pc
  llvm.mlir.global internal @pc(0 : i64) : i64
  func.func @generate(%arg0: i64, %arg1: i64) -> i64 {
    %1 = arith.constant 0 : i32
    %2 = arith.extsi %1 : i32 to i64
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
    llvm.store %2, %4 : i64, !llvm.ptr
    %6 = llvm.mlir.addressof @primes : !llvm.ptr
    %7 = llvm.load %6 : !llvm.ptr -> !llvm.ptr
    %8 = llvm.getelementptr %7[%arg1] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %5 = llvm.load %8 : !llvm.ptr -> i64
    %9 = llvm.mlir.addressof @N : !llvm.ptr
    %10 = llvm.load %9 : !llvm.ptr -> i64
    %11 = arith.muli %5, %5 : i64
    %12 = arith.muli %11, %5 : i64
    %13 = arith.divsi %10, %12 : i64
    %14 = arith.cmpi sgt, %arg0, %13 : i64
    cf.cond_br %14, ^bb0, ^bb1
    ^bb0:
      %15 = arith.constant 0 : i32
      %16 = arith.extsi %15 : i32 to i64
      func.return %16 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %17 = arith.muli %5, %5 : i64
    %18 = arith.muli %17, %5 : i64
    %19 = arith.muli %arg0, %18 : i64
    %20 = llvm.mlir.constant(1 : i64) : i64
    %21 = llvm.alloca %20 x i64 : (i64) -> !llvm.ptr
    llvm.store %19, %21 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %22 = llvm.load %21 : !llvm.ptr -> i64
    %23 = llvm.mlir.addressof @N : !llvm.ptr
    %24 = llvm.load %23 : !llvm.ptr -> i64
    %25 = arith.cmpi sle, %22, %24 : i64
    cf.cond_br %25, ^bb4, ^bb5
    ^bb4:
      %26 = llvm.load %4 : !llvm.ptr -> i64
      %27 = llvm.mlir.addressof @N : !llvm.ptr
      %28 = llvm.load %27 : !llvm.ptr -> i64
      %29 = llvm.load %21 : !llvm.ptr -> i64
      %30 = arith.divsi %28, %29 : i64
      %31 = arith.addi %26, %30 : i64
      llvm.store %31, %4 : i64, !llvm.ptr
      %32 = arith.constant 1 : i32
      %34 = arith.extsi %32 : i32 to i64
      %33 = arith.addi %arg1, %34 : i64
      %35 = llvm.mlir.addressof @pc : !llvm.ptr
      %36 = llvm.load %35 : !llvm.ptr -> i64
      %37 = arith.index_cast %33 : i32 to index
      %38 = arith.index_cast %36 : i32 to index
      %40 = arith.constant 1 : index
      %41 = arith.constant -1 : index
      %42 = arith.cmpi sle, %37, %38 : index
      %39 = arith.select %42, %40, %41 : index
      cf.br ^bb6(%37 : index)
      ^bb6(%43: index):
      %44 = arith.cmpi slt, %43, %38 : index
      %45 = arith.cmpi sgt, %43, %38 : index
      %46 = arith.select %42, %44, %45 : i1
      cf.cond_br %46, ^bb7(%43 : index), ^bb8(%43 : index)
      ^bb7(%47: index):
        %49 = llvm.load %21 : !llvm.ptr -> i64
        %50 = arith.index_cast %47 : index to i64
        %48 = func.call @generate(%49, %50) : (i64, i64) -> i64
        %51 = arith.constant 0 : i32
        %53 = arith.extsi %51 : i32 to i64
        %52 = arith.cmpi eq, %48, %53 : i64
        cf.cond_br %52, ^bb9, ^bb10
        ^bb9:
          cf.br ^bb8(%47 : index)
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %54 = llvm.load %4 : !llvm.ptr -> i64
        %55 = arith.addi %54, %48 : i64
        llvm.store %55, %4 : i64, !llvm.ptr
        %56 = arith.addi %47, %39 : index
        cf.br ^bb6(%56 : index)
      ^bb8(%57: index):
      %58 = llvm.load %21 : !llvm.ptr -> i64
      %59 = llvm.mlir.addressof @N : !llvm.ptr
      %60 = llvm.load %59 : !llvm.ptr -> i64
      %61 = arith.divsi %60, %5 : i64
      %62 = arith.cmpi sgt, %58, %61 : i64
      cf.cond_br %62, ^bb12, ^bb13
      ^bb12:
        cf.br ^bb5
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %63 = llvm.load %21 : !llvm.ptr -> i64
      %64 = arith.muli %63, %5 : i64
      llvm.store %64, %21 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %65 = llvm.load %4 : !llvm.ptr -> i64
    func.return %65 : i64
  }
  func.func @main() -> i32 {
    %66 = arith.constant 999999995705032704 : i32
    %67 = arith.extsi %66 : i32 to i64
    %68 = llvm.mlir.addressof @N : !llvm.ptr
    llvm.store %67, %68 : i64, !llvm.ptr
    %70 = llvm.mlir.addressof @N : !llvm.ptr
    %71 = llvm.load %70 : !llvm.ptr -> i64
    %72 = arith.sitofp %71 : i64 to f64
    %69 = func.call @cbrt(%72) : (f64) -> f64
    %73 = arith.constant 2.0 : f32
    %75 = arith.extf %73 : f32 to f64
    %74 = arith.addf %69, %75 : f64
    %76 = arith.fptosi %74 : f64 to i64
    %78 = arith.constant 1 : i32
    %80 = arith.extsi %78 : i32 to i64
    %79 = arith.addi %76, %80 : i64
    %81 = arith.constant 1 : i32
    %82 = arith.extsi %81 : i32 to i64
    %77 = func.call @calloc(%79, %82) : (i64, i64) -> !llvm.ptr
    %84 = arith.constant 8 : i32
    %85 = arith.extsi %84 : i32 to i64
    %83 = func.call @calloc(%76, %85) : (i64, i64) -> !llvm.ptr
    %86 = llvm.mlir.addressof @primes : !llvm.ptr
    llvm.store %83, %86 : !llvm.ptr, !llvm.ptr
    %87 = arith.constant 0 : i32
    %88 = arith.extsi %87 : i32 to i64
    %89 = llvm.mlir.addressof @pc : !llvm.ptr
    llvm.store %88, %89 : i64, !llvm.ptr
    %90 = arith.constant 2 : i32
    %91 = arith.extsi %90 : i32 to i64
    %92 = llvm.mlir.constant(1 : i64) : i64
    %93 = llvm.alloca %92 x i64 : (i64) -> !llvm.ptr
    llvm.store %91, %93 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %94 = llvm.load %93 : !llvm.ptr -> i64
    %95 = arith.cmpi sle, %94, %76 : i64
    cf.cond_br %95, ^bb16, ^bb17
    ^bb16:
      %97 = llvm.load %93 : !llvm.ptr -> i64
      %98 = llvm.getelementptr %77[%97] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %96 = llvm.load %98 : !llvm.ptr -> i8
      %99 = arith.constant 0 : i32
      %101 = arith.extsi %96 : i8 to i32
      %100 = arith.cmpi eq, %101, %99 : i32
      cf.cond_br %100, ^bb18, ^bb19
      ^bb18:
        %102 = llvm.load %93 : !llvm.ptr -> i64
        %103 = llvm.mlir.addressof @primes : !llvm.ptr
        %104 = llvm.load %103 : !llvm.ptr -> !llvm.ptr
        %105 = llvm.mlir.addressof @pc : !llvm.ptr
        %106 = llvm.load %105 : !llvm.ptr -> i64
        %107 = llvm.getelementptr %104[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %102, %107 : i64, !llvm.ptr
        %108 = llvm.mlir.addressof @pc : !llvm.ptr
        %109 = llvm.load %108 : !llvm.ptr -> i64
        %110 = arith.constant 1 : i32
        %112 = arith.extsi %110 : i32 to i64
        %111 = arith.addi %109, %112 : i64
        %113 = llvm.mlir.addressof @pc : !llvm.ptr
        llvm.store %111, %113 : i64, !llvm.ptr
        %114 = llvm.load %93 : !llvm.ptr -> i64
        %115 = llvm.load %93 : !llvm.ptr -> i64
        %116 = arith.muli %114, %115 : i64
        %117 = llvm.mlir.constant(1 : i64) : i64
        %118 = llvm.alloca %117 x i64 : (i64) -> !llvm.ptr
        llvm.store %116, %118 : i64, !llvm.ptr
        cf.br ^bb21
        ^bb21:
        %119 = llvm.load %118 : !llvm.ptr -> i64
        %120 = arith.cmpi sle, %119, %76 : i64
        cf.cond_br %120, ^bb22, ^bb23
        ^bb22:
          %121 = arith.constant 1 : i32
          %122 = llvm.load %118 : !llvm.ptr -> i64
          %123 = arith.trunci %121 : i32 to i8
          %124 = llvm.getelementptr %77[%122] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %123, %124 : i8, !llvm.ptr
          %125 = llvm.load %118 : !llvm.ptr -> i64
          %126 = llvm.load %93 : !llvm.ptr -> i64
          %127 = arith.addi %125, %126 : i64
          llvm.store %127, %118 : i64, !llvm.ptr
          cf.br ^bb21
        ^bb23:
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %128 = llvm.load %93 : !llvm.ptr -> i64
      %129 = arith.constant 1 : i32
      %131 = arith.extsi %129 : i32 to i64
      %130 = arith.addi %128, %131 : i64
      llvm.store %130, %93 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %132 = llvm.mlir.addressof @N : !llvm.ptr
    %133 = llvm.load %132 : !llvm.ptr -> i64
    %134 = llvm.mlir.constant(1 : i64) : i64
    %135 = llvm.alloca %134 x i64 : (i64) -> !llvm.ptr
    llvm.store %133, %135 : i64, !llvm.ptr
    %136 = arith.constant 0 : i32
    %137 = llvm.mlir.addressof @pc : !llvm.ptr
    %138 = llvm.load %137 : !llvm.ptr -> i64
    %139 = arith.index_cast %136 : i32 to index
    %140 = arith.index_cast %138 : i32 to index
    %142 = arith.constant 1 : index
    %143 = arith.constant -1 : index
    %144 = arith.cmpi sle, %139, %140 : index
    %141 = arith.select %144, %142, %143 : index
    cf.br ^bb24(%139 : index)
    ^bb24(%145: index):
    %146 = arith.cmpi slt, %145, %140 : index
    %147 = arith.cmpi sgt, %145, %140 : index
    %148 = arith.select %144, %146, %147 : i1
    cf.cond_br %148, ^bb25(%145 : index), ^bb26(%145 : index)
    ^bb25(%149: index):
      %150 = llvm.load %135 : !llvm.ptr -> i64
      %152 = arith.constant 1 : i32
      %153 = arith.extsi %152 : i32 to i64
      %154 = arith.index_cast %149 : index to i64
      %151 = func.call @generate(%153, %154) : (i64, i64) -> i64
      %155 = arith.addi %150, %151 : i64
      llvm.store %155, %135 : i64, !llvm.ptr
      %156 = arith.addi %149, %141 : index
      cf.br ^bb24(%156 : index)
    ^bb26(%157: index):
    func.call @free(%77) : (!llvm.ptr) -> ()
    %160 = llvm.mlir.addressof @primes : !llvm.ptr
    %161 = llvm.load %160 : !llvm.ptr -> !llvm.ptr
    func.call @free(%161) : (!llvm.ptr) -> ()
    %162 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %163 = llvm.load %135 : !llvm.ptr -> i64
    %164 = llvm.call @printf(%162, %163) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %165 = arith.constant 0 : i32
    func.return %165 : i32
  }
}