← All problems
Problem 307
P(some chip has ≥3 defects) for k=20000, n=10^6; 10 decimals.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)?
Space complexity O(1)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 307
# P(some chip has ≥3 defects) for k=20000, n=10^6; 10 decimals.
extern {
function log(x: f64) -> f64
function exp(x: f64) -> f64
}
function main() -> i32 {
let k: i64 = 20000
let n: i64 = 1000000
let inv_n: f64 = 1.0 / (n as f64)
let mut log_t0: f64 = 0.0
let mut i: i64 = 0
while i < k {
log_t0 = log_t0 + log(1.0 - (i as f64) * inv_n)
i = i + 1
}
let mut t: f64 = exp(log_t0)
let mut q: f64 = t
let mut a: i64 = 0
while true {
let b: i64 = k - 2 * a
if b < 2 { break }
let ratio: f64 = ((b * (b - 1)) as f64) / (2.0 * ((a + 1) as f64) * ((n - k + a + 1) as f64))
t = t * ratio
q = q + t
a = a + 1
}
printf("%.10f\n", 1.0 - q)
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);
int32_t main(void) {
int64_t k = 20000;
int64_t n = 1000000;
double inv_n = (1.0 / ((double)(n)));
double log_t0 = 0.0;
int64_t i = 0;
while (i < k) {
log_t0 = (log_t0 + log((1.0 - (((double)(i)) * inv_n))));
i = (i + 1);
}
double t = exp(log_t0);
double q = t;
int64_t a = 0;
while (1) {
int64_t b = (k - (2 * a));
if (b < 2) {
break;
}
double ratio = (((double)((b * (b - 1)))) / ((2.0 * ((double)((a + 1)))) * ((double)((((n - k) + a) + 1)))));
t = (t * ratio);
q = (q + t);
a = (a + 1);
}
printf("%.10f\n", (1.0 - q));
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.10f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
func.func private @log(f64) -> f64
func.func private @exp(f64) -> f64
func.func @main() -> i32 {
%0 = arith.constant 20000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 1000000 : i32
%3 = arith.extsi %2 : i32 to i64
%4 = arith.constant 1.0 : f32
%5 = arith.sitofp %3 : i64 to f64
%7 = arith.extf %4 : f32 to f64
%6 = arith.divf %7, %5 : f64
%8 = arith.constant 0.0 : f32
%9 = arith.extf %8 : f32 to f64
%10 = llvm.mlir.constant(1 : i64) : i64
%11 = llvm.alloca %10 x f64 : (i64) -> !llvm.ptr
llvm.store %9, %11 : f64, !llvm.ptr
%12 = arith.constant 0 : i32
%13 = arith.extsi %12 : i32 to i64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%16 = llvm.load %15 : !llvm.ptr -> i64
%17 = arith.cmpi slt, %16, %1 : i64
cf.cond_br %17, ^bb1, ^bb2
^bb1:
%18 = llvm.load %11 : !llvm.ptr -> f64
%19 = arith.constant 1.0 : f32
%20 = llvm.load %15 : !llvm.ptr -> i64
%21 = arith.sitofp %20 : i64 to f64
%22 = arith.mulf %21, %6 : f64
%24 = arith.extf %19 : f32 to f64
%23 = arith.subf %24, %22 : f64
%25 = math.log %23 : f64
%26 = arith.addf %18, %25 : f64
llvm.store %26, %11 : f64, !llvm.ptr
%27 = llvm.load %15 : !llvm.ptr -> i64
%28 = arith.constant 1 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.addi %27, %30 : i64
llvm.store %29, %15 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%31 = llvm.load %11 : !llvm.ptr -> f64
%32 = math.exp %31 : f64
%33 = llvm.mlir.constant(1 : i64) : i64
%34 = llvm.alloca %33 x f64 : (i64) -> !llvm.ptr
llvm.store %32, %34 : f64, !llvm.ptr
%35 = llvm.load %34 : !llvm.ptr -> f64
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x f64 : (i64) -> !llvm.ptr
llvm.store %35, %37 : f64, !llvm.ptr
%38 = arith.constant 0 : i32
%39 = arith.extsi %38 : i32 to i64
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
llvm.store %39, %41 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%42 = arith.constant 1 : i1
cf.cond_br %42, ^bb4, ^bb5
^bb4:
%43 = arith.constant 2 : i32
%44 = llvm.load %41 : !llvm.ptr -> i64
%46 = arith.extsi %43 : i32 to i64
%45 = arith.muli %46, %44 : i64
%47 = arith.subi %1, %45 : i64
%48 = arith.constant 2 : i32
%50 = arith.extsi %48 : i32 to i64
%49 = arith.cmpi slt, %47, %50 : i64
cf.cond_br %49, ^bb6, ^bb7
^bb6:
cf.br ^bb5
^bb7:
cf.br ^bb8
^bb8:
%51 = arith.constant 1 : i32
%53 = arith.extsi %51 : i32 to i64
%52 = arith.subi %47, %53 : i64
%54 = arith.muli %47, %52 : i64
%55 = arith.sitofp %54 : i64 to f64
%56 = arith.constant 2.0 : f32
%57 = llvm.load %41 : !llvm.ptr -> i64
%58 = arith.constant 1 : i32
%60 = arith.extsi %58 : i32 to i64
%59 = arith.addi %57, %60 : i64
%61 = arith.sitofp %59 : i64 to f64
%63 = arith.extf %56 : f32 to f64
%62 = arith.mulf %63, %61 : f64
%64 = arith.subi %3, %1 : i64
%65 = llvm.load %41 : !llvm.ptr -> i64
%66 = arith.addi %64, %65 : i64
%67 = arith.constant 1 : i32
%69 = arith.extsi %67 : i32 to i64
%68 = arith.addi %66, %69 : i64
%70 = arith.sitofp %68 : i64 to f64
%71 = arith.mulf %62, %70 : f64
%72 = arith.divf %55, %71 : f64
%73 = llvm.load %34 : !llvm.ptr -> f64
%74 = arith.mulf %73, %72 : f64
llvm.store %74, %34 : f64, !llvm.ptr
%75 = llvm.load %37 : !llvm.ptr -> f64
%76 = llvm.load %34 : !llvm.ptr -> f64
%77 = arith.addf %75, %76 : f64
llvm.store %77, %37 : f64, !llvm.ptr
%78 = llvm.load %41 : !llvm.ptr -> i64
%79 = arith.constant 1 : i32
%81 = arith.extsi %79 : i32 to i64
%80 = arith.addi %78, %81 : i64
llvm.store %80, %41 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%82 = llvm.mlir.addressof @str_0 : !llvm.ptr
%83 = arith.constant 1.0 : f32
%84 = llvm.load %37 : !llvm.ptr -> f64
%86 = arith.extf %83 : f32 to f64
%85 = arith.subf %86, %84 : f64
%87 = llvm.call @printf(%82, %85) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%88 = arith.constant 0 : i32
func.return %88 : i32
}
}