Problem 231

Prime factor sum of C(20000000, 15000000).

Answer7526965179680
Output7526965179680
StatusPASS
Native helperno
Runtime50 ms
Peak memory30560 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(sqrt(n))
Space complexityO(n)O(1)
ApproachFlow solutionTrial division or Pollard rho
VerdictSuboptimal

Flow source

# Project Euler 231
# Prime factor sum of C(20000000, 15000000).

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

function add_fact(n: i64, primes: ptr<i64>, pc: i64) -> i64 {
    let mut total: i64 = 0
    let mut i: i64 = 0
    while i < pc {
        let p: i64 = primes[i]
        if p > n { break }
        let mut multiple: i64 = p
        let mut count: i64 = n / multiple
        while count > 0 {
            total = total + p * count
            if multiple > n / p { break }
            multiple = multiple * p
            count = n / multiple
        }
        i = i + 1
    }
    return total
}

function main() -> i32 {
    let N: i64 = 20000000
    let K: i64 = 15000000
    let sieve: ptr<i8> = calloc(N + 1, 1)
    let primes: ptr<i64> = calloc(N / 10, 8)
    if sieve == null || primes == null { return 1 }
    let mut i: i64 = 0
    while i <= N {
        sieve[i] = 1
        i = i + 1
    }
    sieve[0] = 0
    sieve[1] = 0
    i = 2
    while i * i <= N {
        if sieve[i] == 1 {
            let mut j: i64 = i * i
            while j <= N {
                sieve[j] = 0
                j = j + i
            }
        }
        i = i + 1
    }
    let mut pc: i64 = 0
    i = 2
    while i <= N {
        if sieve[i] == 1 {
            primes[pc] = i
            pc = pc + 1
        }
        i = i + 1
    }
    let ans: i64 = add_fact(N, primes, pc) - add_fact(N - K, primes, pc) - add_fact(K, primes, pc)
    printf("%lld\n", ans)
    free(sieve); free(primes)
    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 add_fact_i64_ptr_i64_i64(int64_t n, int64_t* primes, int64_t pc);
int32_t main(void);



int64_t add_fact_i64_ptr_i64_i64(int64_t n, int64_t* primes, int64_t pc) {
    int64_t total = 0;
    int64_t i = 0;
    while (i < pc) {
        int64_t p = primes[i];
        if (p > n) {
            break;
        }
        int64_t multiple = p;
        int64_t count = FLOW_CHECKED_DIV((n), (multiple));
        while (count > 0) {
            total = (total + (p * count));
            if (multiple > FLOW_CHECKED_DIV((n), (p))) {
                break;
            }
            multiple = (multiple * p);
            count = FLOW_CHECKED_DIV((n), (multiple));
        }
        i = (i + 1);
    }
    return total;
}

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