Problem 274

Sum of divisibility multipliers m for primes p < 10^7, p != 2,5.

Answer1601912348822
Output1601912348822
StatusPASS
Native helperno
Runtime30 ms
Peak memory10896 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(n)O(n)
ApproachFlow solutionSieve-based computation
VerdictSuboptimal

Flow source

# Project Euler 274
# Sum of divisibility multipliers m for primes p < 10^7, p != 2,5.

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

function mod_pow(base0: i64, exp0: i64, mod: i64) -> i64 {
    let mut result: i64 = 1
    let mut base: i64 = base0 % mod
    let mut exp: i64 = exp0
    while exp > 0 {
        if exp % 2 == 1 {
            result = (result * base) % mod
        }
        base = (base * base) % mod
        exp = exp / 2
    }
    return result
}

function main() -> i32 {
    let limit: i64 = 10000000
    let sieve: ptr<i8> = calloc(limit, 1)
    if sieve == null { return 1 }
    let mut i: i64 = 0
    while i < limit {
        sieve[i] = 1
        i = i + 1
    }
    sieve[0] = 0
    sieve[1] = 0
    let mut p: i64 = 2
    while p * p < limit {
        if sieve[p] == 1 {
            let mut m: i64 = p * p
            while m < limit {
                sieve[m] = 0
                m = m + p
            }
        }
        p = p + 1
    }
    let mut total: i64 = 0
    let mut n: i64 = 3
    while n < limit {
        if sieve[n] == 1 && n != 5 {
            let last_digit: i64 = n % 10
            let first_digits: i64 = n / 10
            if last_digit != 7 {
                total = total + (n - first_digits) / last_digit
            } else {
                total = total + mod_pow(10, n - 2, n)
            }
        }
        n = n + 2
    }
    printf("%lld\n", total)
    free(sieve)
    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 mod_pow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int32_t main(void);



int64_t mod_pow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
    int64_t result = 1;
    int64_t base = FLOW_CHECKED_MOD((base0), (mod));
    int64_t exp = exp0;
    while (exp > 0) {
        if (FLOW_CHECKED_MOD((exp), (2)) == 1) {
            result = FLOW_CHECKED_MOD(((result * base)), (mod));
        }
        base = FLOW_CHECKED_MOD(((base * base)), (mod));
        exp = FLOW_CHECKED_DIV((exp), (2));
    }
    return result;
}

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