Problem 633

Square Prime Factors II: the limiting density of integers with exactly seven square prime factors. Divisibility by p^2 for distinct primes is independent in density, so c_k = [z^k] prod_p (1 - 1/p^2 + z/p^2) = (6/pi^2) * e_k({1/(p^2-1)}), the elementary symmetric function accumulated prime by prime (all terms positive, no cancellation). Primes above 5*10^7 shift e_7 by under 1e-16 relative, far below the 5 significant digits asked for.

Answer1.0012e-10
Output1.0012e-10
StatusPASS
Native helperno
Runtime430 ms
Peak memory50000 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(sqrt(n))
Space complexityO(n)O(1)
ApproachFlow solutionTrial division or Pollard rho
VerdictSuboptimal

Flow source

# Project Euler 633
# Square Prime Factors II: the limiting density of integers with exactly
# seven square prime factors.
#
# Divisibility by p^2 for distinct primes is independent in density, so
#   c_k = [z^k] prod_p (1 - 1/p^2 + z/p^2) = (6/pi^2) * e_k({1/(p^2-1)}),
# the elementary symmetric function accumulated prime by prime (all terms
# positive, no cancellation).  Primes above 5*10^7 shift e_7 by under
# 1e-16 relative, far below the 5 significant digits asked for.

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

const X: i64 = 50000000

function main() -> i32 {
    let sieve: ptr<i8> = calloc(X + 1, 1)
    let mut i: i64 = 2
    while i * i <= X {
        if sieve[i] == 0 {
            let mut j: i64 = i * i
            while j <= X {
                sieve[j] = 1
                j = j + i
            }
        }
        i = i + 1
    }

    let e: ptr<f64> = calloc(8, 8)
    e[0] = 1.0
    let mut p: i64 = 2
    while p <= X {
        if sieve[p] == 0 {
            let x: f64 = 1.0 / ((p as f64) * (p as f64) - 1.0)
            let mut k: i64 = 7
            while k >= 1 {
                e[k] = e[k] + x * e[k - 1]
                k = k - 1
            }
        }
        p = p + 1
    }

    let pi: f64 = 3.14159265358979323846
    let c7: f64 = 6.0 / (pi * pi) * e[7]

    # format as m.mmmm e-XX with 5 significant digits
    let mut mant: f64 = c7
    let mut ex: i64 = 0
    while mant < 1.0 {
        mant = mant * 10.0
        ex = ex - 1
    }
    while mant >= 10.0 {
        mant = mant / 10.0
        ex = ex + 1
    }
    printf("%.4fe%lld\n", mant, ex)

    free(sieve)
    free(e)
    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);

static const int64_t X = 50000000;



int32_t main(void) {
    int8_t* sieve = (int8_t*)(calloc((X + 1), 1));
    int64_t i = 2;
    while ((i * i) <= X) {
        if (sieve[i] == 0) {
            int64_t j = (i * i);
            while (j <= X) {
                sieve[j] = 1;
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    double* e = (double*)(calloc(8, 8));
    e[0] = 1.0;
    int64_t p = 2;
    while (p <= X) {
        if (sieve[p] == 0) {
            double x = (1.0 / ((((double)(p)) * ((double)(p))) - 1.0));
            int64_t k = 7;
            while (k >= 1) {
                e[k] = (e[k] + (x * e[(k - 1)]));
                k = (k - 1);
            }
        }
        p = (p + 1);
    }
    double pi = 3.14159265358979323846;
    double c7 = ((6.0 / (pi * pi)) * e[7]);
    double mant = c7;
    int64_t ex = 0;
    while (mant < 1.0) {
        mant = (mant * 10.0);
        ex = (ex - 1);
    }
    while (mant >= 10.0) {
        mant = (mant / 10.0);
        ex = (ex + 1);
    }
    printf("%.4fe%lld\n", mant, ex);
    free(sieve);
    free(e);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.4fe%lld\n\00") {addr_space = 0 : i32} : !llvm.array<11 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  // Constant: X
  llvm.mlir.global internal constant @X(50000000 : i64) : i64
  func.func @main() -> i32 {
    %1 = llvm.mlir.addressof @X : !llvm.ptr
    %2 = llvm.load %1 : !llvm.ptr -> i64
    %3 = arith.constant 1 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.addi %2, %5 : i64
    %6 = arith.constant 1 : i32
    %7 = arith.extsi %6 : i32 to i64
    %0 = func.call @calloc(%4, %7) : (i64, i64) -> !llvm.ptr
    %8 = arith.constant 2 : i32
    %9 = arith.extsi %8 : i32 to i64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %12 = llvm.load %11 : !llvm.ptr -> i64
    %13 = llvm.load %11 : !llvm.ptr -> i64
    %14 = arith.muli %12, %13 : i64
    %15 = llvm.mlir.addressof @X : !llvm.ptr
    %16 = llvm.load %15 : !llvm.ptr -> i64
    %17 = arith.cmpi sle, %14, %16 : i64
    cf.cond_br %17, ^bb1, ^bb2
    ^bb1:
      %19 = llvm.load %11 : !llvm.ptr -> i64
      %20 = llvm.getelementptr %0[%19] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %18 = llvm.load %20 : !llvm.ptr -> i8
      %21 = arith.constant 0 : i32
      %23 = arith.extsi %18 : i8 to i32
      %22 = arith.cmpi eq, %23, %21 : i32
      cf.cond_br %22, ^bb3, ^bb4
      ^bb3:
        %24 = llvm.load %11 : !llvm.ptr -> i64
        %25 = llvm.load %11 : !llvm.ptr -> i64
        %26 = arith.muli %24, %25 : i64
        %27 = llvm.mlir.constant(1 : i64) : i64
        %28 = llvm.alloca %27 x i64 : (i64) -> !llvm.ptr
        llvm.store %26, %28 : i64, !llvm.ptr
        cf.br ^bb6
        ^bb6:
        %29 = llvm.load %28 : !llvm.ptr -> i64
        %30 = llvm.mlir.addressof @X : !llvm.ptr
        %31 = llvm.load %30 : !llvm.ptr -> i64
        %32 = arith.cmpi sle, %29, %31 : i64
        cf.cond_br %32, ^bb7, ^bb8
        ^bb7:
          %33 = arith.constant 1 : i32
          %34 = llvm.load %28 : !llvm.ptr -> i64
          %35 = arith.trunci %33 : i32 to i8
          %36 = llvm.getelementptr %0[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %35, %36 : i8, !llvm.ptr
          %37 = llvm.load %28 : !llvm.ptr -> i64
          %38 = llvm.load %11 : !llvm.ptr -> i64
          %39 = arith.addi %37, %38 : i64
          llvm.store %39, %28 : i64, !llvm.ptr
          cf.br ^bb6
        ^bb8:
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %40 = llvm.load %11 : !llvm.ptr -> i64
      %41 = arith.constant 1 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.addi %40, %43 : i64
      llvm.store %42, %11 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %45 = arith.constant 8 : i32
    %46 = arith.constant 8 : i32
    %47 = arith.extsi %45 : i32 to i64
    %48 = arith.extsi %46 : i32 to i64
    %44 = func.call @calloc(%47, %48) : (i64, i64) -> !llvm.ptr
    %49 = arith.constant 1.0 : f32
    %50 = arith.constant 0 : i32
    %51 = arith.extf %49 : f32 to f64
    %52 = arith.extsi %50 : i32 to i64
    %53 = llvm.getelementptr %44[%52] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    llvm.store %51, %53 : f64, !llvm.ptr
    %54 = arith.constant 2 : i32
    %55 = arith.extsi %54 : i32 to i64
    %56 = llvm.mlir.constant(1 : i64) : i64
    %57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
    llvm.store %55, %57 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %58 = llvm.load %57 : !llvm.ptr -> i64
    %59 = llvm.mlir.addressof @X : !llvm.ptr
    %60 = llvm.load %59 : !llvm.ptr -> i64
    %61 = arith.cmpi sle, %58, %60 : i64
    cf.cond_br %61, ^bb10, ^bb11
    ^bb10:
      %63 = llvm.load %57 : !llvm.ptr -> i64
      %64 = llvm.getelementptr %0[%63] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %62 = llvm.load %64 : !llvm.ptr -> i8
      %65 = arith.constant 0 : i32
      %67 = arith.extsi %62 : i8 to i32
      %66 = arith.cmpi eq, %67, %65 : i32
      cf.cond_br %66, ^bb12, ^bb13
      ^bb12:
        %68 = arith.constant 1.0 : f32
        %69 = llvm.load %57 : !llvm.ptr -> i64
        %70 = arith.sitofp %69 : i64 to f64
        %71 = llvm.load %57 : !llvm.ptr -> i64
        %72 = arith.sitofp %71 : i64 to f64
        %73 = arith.mulf %70, %72 : f64
        %74 = arith.constant 1.0 : f32
        %76 = arith.extf %74 : f32 to f64
        %75 = arith.subf %73, %76 : f64
        %78 = arith.extf %68 : f32 to f64
        %77 = arith.divf %78, %75 : f64
        %79 = arith.constant 7 : 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 ^bb15
        ^bb15:
        %83 = llvm.load %82 : !llvm.ptr -> i64
        %84 = arith.constant 1 : i32
        %86 = arith.extsi %84 : i32 to i64
        %85 = arith.cmpi sge, %83, %86 : i64
        cf.cond_br %85, ^bb16, ^bb17
        ^bb16:
          %88 = llvm.load %82 : !llvm.ptr -> i64
          %89 = llvm.getelementptr %44[%88] : (!llvm.ptr, i64) -> !llvm.ptr, f64
          %87 = llvm.load %89 : !llvm.ptr -> f64
          %91 = llvm.load %82 : !llvm.ptr -> i64
          %92 = arith.constant 1 : i32
          %94 = arith.extsi %92 : i32 to i64
          %93 = arith.subi %91, %94 : i64
          %95 = llvm.getelementptr %44[%93] : (!llvm.ptr, i64) -> !llvm.ptr, f64
          %90 = llvm.load %95 : !llvm.ptr -> f64
          %96 = arith.mulf %77, %90 : f64
          %97 = arith.addf %87, %96 : f64
          %98 = llvm.load %82 : !llvm.ptr -> i64
          %99 = llvm.getelementptr %44[%98] : (!llvm.ptr, i64) -> !llvm.ptr, f64
          llvm.store %97, %99 : f64, !llvm.ptr
          %100 = llvm.load %82 : !llvm.ptr -> i64
          %101 = arith.constant 1 : i32
          %103 = arith.extsi %101 : i32 to i64
          %102 = arith.subi %100, %103 : i64
          llvm.store %102, %82 : i64, !llvm.ptr
          cf.br ^bb15
        ^bb17:
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %104 = llvm.load %57 : !llvm.ptr -> i64
      %105 = arith.constant 1 : i32
      %107 = arith.extsi %105 : i32 to i64
      %106 = arith.addi %104, %107 : i64
      llvm.store %106, %57 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %108 = arith.constant 3.14159265358979323846 : f32
    %109 = arith.extf %108 : f32 to f64
    %110 = arith.constant 6.0 : f32
    %111 = arith.mulf %109, %109 : f64
    %113 = arith.extf %110 : f32 to f64
    %112 = arith.divf %113, %111 : f64
    %115 = arith.constant 7 : i32
    %116 = arith.extsi %115 : i32 to i64
    %117 = llvm.getelementptr %44[%116] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %114 = llvm.load %117 : !llvm.ptr -> f64
    %118 = arith.mulf %112, %114 : f64
    %119 = llvm.mlir.constant(1 : i64) : i64
    %120 = llvm.alloca %119 x f64 : (i64) -> !llvm.ptr
    llvm.store %118, %120 : f64, !llvm.ptr
    %121 = arith.constant 0 : i32
    %122 = arith.extsi %121 : i32 to i64
    %123 = llvm.mlir.constant(1 : i64) : i64
    %124 = llvm.alloca %123 x i64 : (i64) -> !llvm.ptr
    llvm.store %122, %124 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %125 = llvm.load %120 : !llvm.ptr -> f64
    %126 = arith.constant 1.0 : f32
    %128 = arith.extf %126 : f32 to f64
    %127 = arith.cmpf olt, %125, %128 : f64
    cf.cond_br %127, ^bb19, ^bb20
    ^bb19:
      %129 = llvm.load %120 : !llvm.ptr -> f64
      %130 = arith.constant 10.0 : f32
      %132 = arith.extf %130 : f32 to f64
      %131 = arith.mulf %129, %132 : f64
      llvm.store %131, %120 : f64, !llvm.ptr
      %133 = llvm.load %124 : !llvm.ptr -> i64
      %134 = arith.constant 1 : i32
      %136 = arith.extsi %134 : i32 to i64
      %135 = arith.subi %133, %136 : i64
      llvm.store %135, %124 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    cf.br ^bb21
    ^bb21:
    %137 = llvm.load %120 : !llvm.ptr -> f64
    %138 = arith.constant 10.0 : f32
    %140 = arith.extf %138 : f32 to f64
    %139 = arith.cmpf oge, %137, %140 : f64
    cf.cond_br %139, ^bb22, ^bb23
    ^bb22:
      %141 = llvm.load %120 : !llvm.ptr -> f64
      %142 = arith.constant 10.0 : f32
      %144 = arith.extf %142 : f32 to f64
      %143 = arith.divf %141, %144 : f64
      llvm.store %143, %120 : f64, !llvm.ptr
      %145 = llvm.load %124 : !llvm.ptr -> i64
      %146 = arith.constant 1 : i32
      %148 = arith.extsi %146 : i32 to i64
      %147 = arith.addi %145, %148 : i64
      llvm.store %147, %124 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %149 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %150 = llvm.load %120 : !llvm.ptr -> f64
    %151 = llvm.load %124 : !llvm.ptr -> i64
    %152 = llvm.call @printf(%149, %150, %151) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64, i64) -> i32
    func.call @free(%0) : (!llvm.ptr) -> ()
    func.call @free(%44) : (!llvm.ptr) -> ()
    %155 = arith.constant 0 : i32
    func.return %155 : i32
  }
}