← All problems
Problem 779
Prime factor and exponent. sum_{K>=1} bar(f_K) = sum_{p prime} prod_{q<p}(1 - 1/q) * 1 / (p * (p - 1)^2) Sieve up to 1e6, accumulate the running product of (p-1)/p.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(sqrt(n))
Space complexity O(n)O(1)
Approach Flow solution Trial division or Pollard rho
Verdict Suboptimal
Flow source
# Project Euler 779
# Prime factor and exponent.
#
# sum_{K>=1} bar(f_K) =
# sum_{p prime} prod_{q<p}(1 - 1/q) * 1 / (p * (p - 1)^2)
#
# Sieve up to 1e6, accumulate the running product of (p-1)/p.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memset(p: ptr<void>, c: i32, n: i64) -> ptr<void>
function sqrt(x: f64) -> f64
function floor(x: f64) -> f64
function round(x: f64) -> f64
}
const SIEVE_LIMIT: i64 = 1000000
function main() -> i32 {
let is_prime: ptr<i8> = calloc(SIEVE_LIMIT + 1, 1)
if is_prime == null {
return 1
}
memset(is_prime, 1, SIEVE_LIMIT + 1)
is_prime[0] = 0
is_prime[1] = 0
let limit: i64 = (floor(sqrt((SIEVE_LIMIT as f64))) as i64)
let mut i: i64 = 2
while i <= limit {
if is_prime[i] != 0 {
let mut j: i64 = i * i
while j <= SIEVE_LIMIT {
is_prime[j] = 0
j = j + i
}
}
i = i + 1
}
let mut total: f64 = 0.0
let mut curr: f64 = 1.0
let mut p: i64 = 2
while p <= SIEVE_LIMIT {
if is_prime[p] != 0 {
let dp: f64 = (p as f64)
let term: f64 = curr * (1.0 / (dp * (dp - 1.0) * (dp - 1.0)))
total = total + term
curr = curr * (dp - 1.0) / dp
}
p = p + 1
}
free(is_prime)
let result: f64 = round(total * 1e12) / 1e12
printf("%.12f\n", result)
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 SIEVE_LIMIT = 1000000;
int32_t main(void) {
int8_t* is_prime = (int8_t*)(calloc((SIEVE_LIMIT + 1), 1));
if (is_prime == NULL) {
return 1;
}
memset(is_prime, 1, (SIEVE_LIMIT + 1));
is_prime[0] = 0;
is_prime[1] = 0;
int64_t limit = ((int64_t)(floor(sqrt(((double)(SIEVE_LIMIT))))));
int64_t i = 2;
while (i <= limit) {
if (is_prime[i] != 0) {
int64_t j = (i * i);
while (j <= SIEVE_LIMIT) {
is_prime[j] = 0;
j = (j + i);
}
}
i = (i + 1);
}
double total = 0.0;
double curr = 1.0;
int64_t p = 2;
while (p <= SIEVE_LIMIT) {
if (is_prime[p] != 0) {
double dp = ((double)(p));
double term = (curr * (1.0 / ((dp * (dp - 1.0)) * (dp - 1.0))));
total = (total + term);
curr = ((curr * (dp - 1.0)) / dp);
}
p = (p + 1);
}
free(is_prime);
double result = (round((total * 1e12)) / 1e12);
printf("%.12f\n", result);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.12f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func private @memset(!llvm.ptr, i32, i64) -> !llvm.ptr
func.func private @sqrt(f64) -> f64
func.func private @floor(f64) -> f64
func.func private @round(f64) -> f64
// Constant: SIEVE_LIMIT
llvm.mlir.global internal constant @SIEVE_LIMIT(1000000 : i64) : i64
func.func @main() -> i32 {
%1 = llvm.mlir.addressof @SIEVE_LIMIT : !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 = llvm.mlir.zero : !llvm.ptr
%9 = llvm.icmp "eq" %0, %8 : !llvm.ptr
cf.cond_br %9, ^bb0, ^bb1
^bb0:
%10 = arith.constant 1 : i32
func.return %10 : i32
^bb1:
cf.br ^bb2
^bb2:
%12 = arith.constant 1 : i32
%13 = llvm.mlir.addressof @SIEVE_LIMIT : !llvm.ptr
%14 = llvm.load %13 : !llvm.ptr -> i64
%15 = arith.constant 1 : i32
%17 = arith.extsi %15 : i32 to i64
%16 = arith.addi %14, %17 : i64
%11 = func.call @memset(%0, %12, %16) : (!llvm.ptr, i32, i64) -> !llvm.ptr
%18 = arith.constant 0 : i32
%19 = arith.constant 0 : i32
%20 = arith.trunci %18 : i32 to i8
%21 = arith.extsi %19 : i32 to i64
%22 = llvm.getelementptr %0[%21] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %20, %22 : i8, !llvm.ptr
%23 = arith.constant 0 : i32
%24 = arith.constant 1 : i32
%25 = arith.trunci %23 : i32 to i8
%26 = arith.extsi %24 : i32 to i64
%27 = llvm.getelementptr %0[%26] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %25, %27 : i8, !llvm.ptr
%29 = llvm.mlir.addressof @SIEVE_LIMIT : !llvm.ptr
%30 = llvm.load %29 : !llvm.ptr -> i64
%31 = arith.sitofp %30 : i64 to f64
%32 = math.sqrt %31 : f64
%28 = func.call @floor(%32) : (f64) -> f64
%33 = arith.fptosi %28 : f64 to i64
%34 = arith.constant 2 : i32
%35 = arith.extsi %34 : i32 to i64
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x i64 : (i64) -> !llvm.ptr
llvm.store %35, %37 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%38 = llvm.load %37 : !llvm.ptr -> i64
%39 = arith.cmpi sle, %38, %33 : i64
cf.cond_br %39, ^bb4, ^bb5
^bb4:
%41 = llvm.load %37 : !llvm.ptr -> i64
%42 = llvm.getelementptr %0[%41] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%40 = llvm.load %42 : !llvm.ptr -> i8
%43 = arith.constant 0 : i32
%45 = arith.extsi %40 : i8 to i32
%44 = arith.cmpi ne, %45, %43 : i32
cf.cond_br %44, ^bb6, ^bb7
^bb6:
%46 = llvm.load %37 : !llvm.ptr -> i64
%47 = llvm.load %37 : !llvm.ptr -> i64
%48 = arith.muli %46, %47 : i64
%49 = llvm.mlir.constant(1 : i64) : i64
%50 = llvm.alloca %49 x i64 : (i64) -> !llvm.ptr
llvm.store %48, %50 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%51 = llvm.load %50 : !llvm.ptr -> i64
%52 = llvm.mlir.addressof @SIEVE_LIMIT : !llvm.ptr
%53 = llvm.load %52 : !llvm.ptr -> i64
%54 = arith.cmpi sle, %51, %53 : i64
cf.cond_br %54, ^bb10, ^bb11
^bb10:
%55 = arith.constant 0 : i32
%56 = llvm.load %50 : !llvm.ptr -> i64
%57 = arith.trunci %55 : i32 to i8
%58 = llvm.getelementptr %0[%56] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %57, %58 : i8, !llvm.ptr
%59 = llvm.load %50 : !llvm.ptr -> i64
%60 = llvm.load %37 : !llvm.ptr -> i64
%61 = arith.addi %59, %60 : i64
llvm.store %61, %50 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%62 = llvm.load %37 : !llvm.ptr -> i64
%63 = arith.constant 1 : i32
%65 = arith.extsi %63 : i32 to i64
%64 = arith.addi %62, %65 : i64
llvm.store %64, %37 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%66 = arith.constant 0.0 : f32
%67 = arith.extf %66 : f32 to f64
%68 = llvm.mlir.constant(1 : i64) : i64
%69 = llvm.alloca %68 x f64 : (i64) -> !llvm.ptr
llvm.store %67, %69 : f64, !llvm.ptr
%70 = arith.constant 1.0 : f32
%71 = arith.extf %70 : f32 to f64
%72 = llvm.mlir.constant(1 : i64) : i64
%73 = llvm.alloca %72 x f64 : (i64) -> !llvm.ptr
llvm.store %71, %73 : f64, !llvm.ptr
%74 = arith.constant 2 : i32
%75 = arith.extsi %74 : i32 to i64
%76 = llvm.mlir.constant(1 : i64) : i64
%77 = llvm.alloca %76 x i64 : (i64) -> !llvm.ptr
llvm.store %75, %77 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%78 = llvm.load %77 : !llvm.ptr -> i64
%79 = llvm.mlir.addressof @SIEVE_LIMIT : !llvm.ptr
%80 = llvm.load %79 : !llvm.ptr -> i64
%81 = arith.cmpi sle, %78, %80 : i64
cf.cond_br %81, ^bb13, ^bb14
^bb13:
%83 = llvm.load %77 : !llvm.ptr -> i64
%84 = llvm.getelementptr %0[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%82 = llvm.load %84 : !llvm.ptr -> i8
%85 = arith.constant 0 : i32
%87 = arith.extsi %82 : i8 to i32
%86 = arith.cmpi ne, %87, %85 : i32
cf.cond_br %86, ^bb15, ^bb16
^bb15:
%88 = llvm.load %77 : !llvm.ptr -> i64
%89 = arith.sitofp %88 : i64 to f64
%90 = llvm.load %73 : !llvm.ptr -> f64
%91 = arith.constant 1.0 : f32
%92 = arith.constant 1.0 : f32
%94 = arith.extf %92 : f32 to f64
%93 = arith.subf %89, %94 : f64
%95 = arith.mulf %89, %93 : f64
%96 = arith.constant 1.0 : f32
%98 = arith.extf %96 : f32 to f64
%97 = arith.subf %89, %98 : f64
%99 = arith.mulf %95, %97 : f64
%101 = arith.extf %91 : f32 to f64
%100 = arith.divf %101, %99 : f64
%102 = arith.mulf %90, %100 : f64
%103 = llvm.load %69 : !llvm.ptr -> f64
%104 = arith.addf %103, %102 : f64
llvm.store %104, %69 : f64, !llvm.ptr
%105 = llvm.load %73 : !llvm.ptr -> f64
%106 = arith.constant 1.0 : f32
%108 = arith.extf %106 : f32 to f64
%107 = arith.subf %89, %108 : f64
%109 = arith.mulf %105, %107 : f64
%110 = arith.divf %109, %89 : f64
llvm.store %110, %73 : f64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%111 = llvm.load %77 : !llvm.ptr -> i64
%112 = arith.constant 1 : i32
%114 = arith.extsi %112 : i32 to i64
%113 = arith.addi %111, %114 : i64
llvm.store %113, %77 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
func.call @free(%0) : (!llvm.ptr) -> ()
%117 = llvm.load %69 : !llvm.ptr -> f64
%118 = arith.constant 1000000000000 : f32
%120 = arith.extf %118 : f32 to f64
%119 = arith.mulf %117, %120 : f64
%116 = func.call @round(%119) : (f64) -> f64
%121 = arith.constant 1000000000000 : f32
%123 = arith.extf %121 : f32 to f64
%122 = arith.divf %116, %123 : f64
%124 = llvm.mlir.addressof @str_0 : !llvm.ptr
%125 = llvm.call @printf(%124, %122) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%126 = arith.constant 0 : i32
func.return %126 : i32
}
}