Problem 035

How many circular primes below one million?

Answer55
Output55
StatusPASS
Native helperno
Runtime0 ms
Peak memory2096 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 + circular prime check
VerdictSuboptimal

Flow source

# Project Euler 035
# How many circular primes below one million?

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

function pow10(n: i32) -> i64 {
    let mut r: i64 = 1
    let mut i: i32 = 0
    while i < n {
        r = r * 10
        i = i + 1
    }
    return r
}

function digit_count(n0: i64) -> i32 {
    let mut n: i64 = n0
    let mut c: i32 = 0
    while n > 0 {
        c = c + 1
        n = n / 10
    }
    return c
}

function rotate(n: i64, digits: i32) -> i64 {
    let p: i64 = pow10(digits - 1)
    let first: i64 = n / p
    return (n % p) * 10 + first
}

function main() -> i32 {
    let limit: i64 = 1000000
    let sieve: ptr<i8> = calloc(limit, 1)
    if sieve == null { return 1 }
    sieve[0] = 1
    sieve[1] = 1
    let mut p: i64 = 2
    while p * p < limit {
        if sieve[p] == 0 {
            let mut m: i64 = p * p
            while m < limit {
                sieve[m] = 1
                m = m + p
            }
        }
        p = p + 1
    }

    let mut count: i64 = 0
    let mut n: i64 = 2
    while n < limit {
        if sieve[n] == 0 {
            let d: i32 = digit_count(n)
            let mut x: i64 = n
            let mut ok: bool = true
            let mut i: i32 = 0
            while i < d {
                if sieve[x] != 0 {
                    ok = false
                    break
                }
                x = rotate(x, d)
                i = i + 1
            }
            if ok {
                count = count + 1
            }
        }
        n = n + 1
    }
    printf("%lld\n", count)
    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 pow10_i32(int32_t n);
int32_t digit_count_i64(int64_t n0);
int64_t rotate_i64_i32(int64_t n, int32_t digits);
int32_t main(void);



int64_t pow10_i32(int32_t n) {
    int64_t r = 1;
    int32_t i = 0;
    while (i < n) {
        r = (r * 10);
        i = (i + 1);
    }
    return r;
}

int32_t digit_count_i64(int64_t n0) {
    int64_t n = n0;
    int32_t c = 0;
    while (n > 0) {
        c = (c + 1);
        n = FLOW_CHECKED_DIV((n), (10));
    }
    return c;
}

int64_t rotate_i64_i32(int64_t n, int32_t digits) {
    int64_t p = pow10_i32((digits - 1));
    int64_t first = FLOW_CHECKED_DIV((n), (p));
    return ((FLOW_CHECKED_MOD((n), (p)) * 10) + first);
}

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