Problem 128

PD(n)=3 hexagonal tile differences; find the 2000th such n.

Answer14516824220
Output14516824220
StatusPASS
Native helperno
Runtime0 ms
Peak memory3072 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n)
Space complexityO(n)O(1)
ApproachFlow solutionTile difference enumeration
VerdictSuboptimal

Flow source

# Project Euler 128
# PD(n)=3 hexagonal tile differences; find the 2000th such n.

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

function is_prime_arr(sieve: ptr<i8>, x: i64) -> bool {
    if x < 2 { return false }
    if x % 2 == 0 { return x == 2 }
    return sieve[x] == 0
}

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

    let mut found: i64 = 2  # 1 and 2
    let mut first: i64 = 8
    let mut ring: i64 = 2
    let mut answer: i64 = 0
    while found < 2000 {
        let inc_from: i64 = (ring - 1) * 6
        let inc_to: i64 = ring * 6
        let inc_to2: i64 = (ring + 1) * 6 + inc_to

        if is_prime_arr(sieve, inc_to - 1) {
            if is_prime_arr(sieve, inc_to + 1) && is_prime_arr(sieve, inc_to2 - 1) {
                found = found + 1
                if found == 2000 { answer = first }
            }
            if is_prime_arr(sieve, inc_from + inc_to - 1) && is_prime_arr(sieve, inc_to2 - inc_to - 1) {
                found = found + 1
                let last: i64 = first + inc_to - 1
                if found == 2000 { answer = last }
            }
        }
        first = first + inc_to
        ring = ring + 1
    }

    printf("%lld\n", answer)
    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; }

bool is_prime_arr_ptr_i8_i64(int8_t* sieve, int64_t x);
int32_t main(void);



bool is_prime_arr_ptr_i8_i64(int8_t* sieve, int64_t x) {
    if (x < 2) {
        return 0;
    }
    if (FLOW_CHECKED_MOD((x), (2)) == 0) {
        return x == 2;
    }
    return sieve[x] == 0;
}

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