Problem 580

Squarefree Hilbert Numbers Inclusion over odd squareful sieves: f[d] = #Hilbert multiples of d^2 <= N.

Answer2327213148095366
Output2327213148095366
StatusPASS
Native helperno
Runtime4690 ms
Peak memory879984 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 solutionMobius sieve
VerdictSuboptimal

Flow source

# Project Euler 580
# Squarefree Hilbert Numbers
# Inclusion over odd squareful sieves: f[d] = #Hilbert multiples of d^2 <= N.

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

const N: i64 = 10000000000000000
const M: i64 = 100000000

function solve() -> i64 {
    let f: ptr<i64> = calloc(M, 8)
    if f == null { return -1 }

    let mut i: i64 = ((M | 1) - 2)
    while i >= 0 {
        f[i] = (N / i / i + 3) / 4
        let T: i64 = M / i
        let mut j: i64 = 3
        while j <= T {
            f[i] = f[i] - f[i * j]
            j = j + 2
        }
        i = i - 2
    }

    let sieve: ptr<i8> = calloc(M, 1)
    if sieve == null { return -1 }
    let mut p: i64 = 0
    while p < M {
        sieve[p] = 1
        p = p + 1
    }
    sieve[0] = 0
    sieve[1] = 0
    p = 2
    while p * p < M {
        if sieve[p] != 0 {
            let mut q: i64 = p * p
            while q < M {
                sieve[q] = 0
                q = q + p
            }
        }
        p = p + 1
    }

    let mut ans: i64 = f[1]
    p = 3
    while p < M {
        if sieve[p] != 0 && (p % 4) == 3 {
            ans = ans + f[p]
        }
        p = p + 2
    }

    free(f)
    free(sieve)
    return ans
}

function main() -> i32 {
    printf("%lld\n", solve())
    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 solve(void);
int32_t main(void);

static const int64_t N = 10000000000000000;
static const int64_t M = 100000000;



int64_t solve(void) {
    int64_t* f = (int64_t*)(calloc(M, 8));
    if (f == NULL) {
        return (-1);
    }
    int64_t i = ((M | 1) - 2);
    while (i >= 0) {
        f[i] = FLOW_CHECKED_DIV(((FLOW_CHECKED_DIV((FLOW_CHECKED_DIV((N), (i))), (i)) + 3)), (4));
        int64_t T = FLOW_CHECKED_DIV((M), (i));
        int64_t j = 3;
        while (j <= T) {
            f[i] = (f[i] - f[(i * j)]);
            j = (j + 2);
        }
        i = (i - 2);
    }
    int8_t* sieve = (int8_t*)(calloc(M, 1));
    if (sieve == NULL) {
        return (-1);
    }
    int64_t p = 0;
    while (p < M) {
        sieve[p] = 1;
        p = (p + 1);
    }
    sieve[0] = 0;
    sieve[1] = 0;
    p = 2;
    while ((p * p) < M) {
        if (sieve[p] != 0) {
            int64_t q = (p * p);
            while (q < M) {
                sieve[q] = 0;
                q = (q + p);
            }
        }
        p = (p + 1);
    }
    int64_t ans = f[1];
    p = 3;
    while (p < M) {
        if ((sieve[p] != 0 && FLOW_CHECKED_MOD((p), (4)) == 3)) {
            ans = (ans + f[p]);
        }
        p = (p + 2);
    }
    free(f);
    free(sieve);
    return ans;
}

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