Problem 267

Probability of >= 10^9 after 1000 tosses with optimal f.

Answer0.999992836187
Output0.999992836187
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n * s^2)
Space complexityO(1)O(s^2)
ApproachFlow solutionMarkov chain or DP over states
VerdictUnknown

Flow source

# Project Euler 267
# Probability of >= 10^9 after 1000 tosses with optimal f.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
    function pow(x: f64, y: f64) -> f64
    function log(x: f64) -> f64
    function exp(x: f64) -> f64
    function round(x: f64) -> f64
}

function main() -> i32 {
    # min heads needed is 432 (known from analysis / search)
    let min_heads: i64 = 432
    let n: i64 = 1000
    # compute sum_{h=min_heads}^n C(n,h) / 2^n via recursive ratio
    # start with C(n,0)/2^n = 2^{-n}, build up - better: start at h=min
    # term = C(n,k)/2^n for k = min_heads
    # Compute using log-space then iterative multiply for precision.
    # Use double DP: probability of exactly k heads via recursive product.
    let mut term: f64 = 1.0
    # C(n,0)/2^n
    let mut i: i64 = 0
    while i < n {
        term = term * 0.5
        i = i + 1
    }
    let mut total: f64 = 0.0
    # accumulate C(n,h)/2^n for h=0..n, but only add when h>=min
    # Actually start from h=0 term and walk up
    i = 0
    while i <= n {
        if i >= min_heads {
            total = total + term
        }
        # term_{i+1} = term_i * (n-i)/(i+1)
        if i < n {
            term = term * ((n - i) as f64) / ((i + 1) as f64)
        }
        i = i + 1
    }
    printf("%.12f\n", total)
    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 min_heads = 432;
    int64_t n = 1000;
    double term = 1.0;
    int64_t i = 0;
    while (i < n) {
        term = (term * 0.5);
        i = (i + 1);
    }
    double total = 0.0;
    i = 0;
    while (i <= n) {
        if (i >= min_heads) {
            total = (total + term);
        }
        if (i < n) {
            term = ((term * ((double)((n - i)))) / ((double)((i + 1))));
        }
        i = (i + 1);
    }
    printf("%.12f\n", total);
    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 @pow(f64, f64) -> f64
  func.func private @log(f64) -> f64
  func.func private @exp(f64) -> f64
  func.func private @round(f64) -> f64
  func.func @main() -> i32 {
    %0 = arith.constant 432 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = arith.constant 1000 : i32
    %3 = arith.extsi %2 : i32 to i64
    %4 = arith.constant 1.0 : f32
    %5 = arith.extf %4 : f32 to f64
    %6 = llvm.mlir.constant(1 : i64) : i64
    %7 = llvm.alloca %6 x f64 : (i64) -> !llvm.ptr
    llvm.store %5, %7 : f64, !llvm.ptr
    %8 = arith.constant 0 : 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 = arith.cmpi slt, %12, %3 : i64
    cf.cond_br %13, ^bb1, ^bb2
    ^bb1:
      %14 = llvm.load %7 : !llvm.ptr -> f64
      %15 = arith.constant 0.5 : f32
      %17 = arith.extf %15 : f32 to f64
      %16 = arith.mulf %14, %17 : f64
      llvm.store %16, %7 : f64, !llvm.ptr
      %18 = llvm.load %11 : !llvm.ptr -> i64
      %19 = arith.constant 1 : i32
      %21 = arith.extsi %19 : i32 to i64
      %20 = arith.addi %18, %21 : i64
      llvm.store %20, %11 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %22 = arith.constant 0.0 : f32
    %23 = arith.extf %22 : f32 to f64
    %24 = llvm.mlir.constant(1 : i64) : i64
    %25 = llvm.alloca %24 x f64 : (i64) -> !llvm.ptr
    llvm.store %23, %25 : f64, !llvm.ptr
    %26 = arith.constant 0 : i32
    %27 = arith.extsi %26 : i32 to i64
    llvm.store %27, %11 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %28 = llvm.load %11 : !llvm.ptr -> i64
    %29 = arith.cmpi sle, %28, %3 : i64
    cf.cond_br %29, ^bb4, ^bb5
    ^bb4:
      %30 = llvm.load %11 : !llvm.ptr -> i64
      %31 = arith.cmpi sge, %30, %1 : i64
      cf.cond_br %31, ^bb6, ^bb7
      ^bb6:
        %32 = llvm.load %25 : !llvm.ptr -> f64
        %33 = llvm.load %7 : !llvm.ptr -> f64
        %34 = arith.addf %32, %33 : f64
        llvm.store %34, %25 : f64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %35 = llvm.load %11 : !llvm.ptr -> i64
      %36 = arith.cmpi slt, %35, %3 : i64
      cf.cond_br %36, ^bb9, ^bb10
      ^bb9:
        %37 = llvm.load %7 : !llvm.ptr -> f64
        %38 = llvm.load %11 : !llvm.ptr -> i64
        %39 = arith.subi %3, %38 : i64
        %40 = arith.sitofp %39 : i64 to f64
        %41 = arith.mulf %37, %40 : f64
        %42 = llvm.load %11 : !llvm.ptr -> i64
        %43 = arith.constant 1 : i32
        %45 = arith.extsi %43 : i32 to i64
        %44 = arith.addi %42, %45 : i64
        %46 = arith.sitofp %44 : i64 to f64
        %47 = arith.divf %41, %46 : f64
        llvm.store %47, %7 : f64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %48 = llvm.load %11 : !llvm.ptr -> i64
      %49 = arith.constant 1 : i32
      %51 = arith.extsi %49 : i32 to i64
      %50 = arith.addi %48, %51 : i64
      llvm.store %50, %11 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %52 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %53 = llvm.load %25 : !llvm.ptr -> f64
    %54 = llvm.call @printf(%52, %53) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %55 = arith.constant 0 : i32
    func.return %55 : i32
  }
}