Problem 187

Count semiprimes n < 10^8.

Answer17427258
Output17427258
StatusPASS
Native helperno
Runtime240 ms
Peak memory73408 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 of Eratosthenes
VerdictSuboptimal

Flow source

# Project Euler 187
# Count semiprimes n < 10^8.

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

function main() -> i32 {
    let LIMIT: i64 = 100000000
    let N: i64 = LIMIT / 2
    let sieve: ptr<i8> = calloc(N + 1, 1)
    if sieve == null { return 1 }
    let mut i: i64 = 0
    while i <= N {
        sieve[i] = 1
        i = i + 1
    }
    sieve[0] = 0
    sieve[1] = 0
    let mut p: i64 = 2
    while p * p <= N {
        if sieve[p] == 1 {
            let mut m: i64 = p * p
            while m <= N {
                sieve[m] = 0
                m = m + p
            }
        }
        p = p + 1
    }
    # count primes and collect
    let mut pc: i64 = 0
    i = 2
    while i <= N {
        if sieve[i] == 1 { pc = pc + 1 }
        i = i + 1
    }
    let primes: ptr<i64> = calloc(pc, 8)
    if primes == null { free(sieve); return 1 }
    let mut idx: i64 = 0
    i = 2
    while i <= N {
        if sieve[i] == 1 {
            primes[idx] = i
            idx = idx + 1
        }
        i = i + 1
    }
    let mut count: i64 = 0
    let mut j: i64 = pc - 1
    let mut ii: i64 = 0
    while ii < pc {
        let pp: i64 = primes[ii]
        if pp * pp >= LIMIT { break }
        while j >= ii && primes[j] * pp >= LIMIT {
            j = j - 1
        }
        if j < ii { break }
        count = count + (j - ii + 1)
        ii = ii + 1
    }
    printf("%lld\n", count)
    free(primes)
    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; }

int32_t main(void);



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