Problem 286

Find q such that P(exactly 20 hits in 50 shots) = 0.02.

Answer52.6494571953
Output52.6494571953
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(2^n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(2^n)O(n * s)
Space complexityO(n)O(s)
ApproachFlow solutionBinomial or DP
VerdictUnknown

Flow source

# Project Euler 286
# Find q such that P(exactly 20 hits in 50 shots) = 0.02.

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

function probability(q: f64, made: i32, distance: i32, cache: ptr<f64>, seen: ptr<i8>) -> f64 {
    let threshold: i32 = 20
    let max_distance: i32 = 50
    if made > threshold { return 0.0 }
    if distance > max_distance {
        if made == threshold { return 1.0 }
        return 0.0
    }
    let id: i32 = made * (max_distance + 1) + distance
    # cache keyed only on (made,distance) for fixed q — caller clears between q
    if seen[id] == 1 { return cache[id] }
    let chance_hit: f64 = 1.0 - (distance as f64) / q
    let chance_miss: f64 = 1.0 - chance_hit
    let result: f64 = chance_hit * probability(q, made + 1, distance + 1, cache, seen) + chance_miss * probability(q, made, distance + 1, cache, seen)
    cache[id] = result
    seen[id] = 1
    return result
}

function main() -> i32 {
    let cache: ptr<f64> = calloc(21 * 52, 8)
    let seen: ptr<i8> = calloc(21 * 52, 1)
    if cache == null || seen == null { return 1 }
    let mut low: f64 = 50.0
    let mut high: f64 = 100.0
    let accuracy: f64 = 0.0000000001
    while high - low > accuracy {
        let mid: f64 = (high + low) * 0.5
        # clear cache
        let mut i: i32 = 0
        while i < 21 * 52 {
            seen[i] = 0
            i = i + 1
        }
        if probability(mid, 0, 1, cache, seen) < 0.02 {
            high = mid
        } else {
            low = mid
        }
    }
    printf("%.10f\n", low)
    free(cache); free(seen)
    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; }

double probability_f64_i32_i32_ptr_f64_ptr_i8(double q, int32_t made, int32_t distance, double* cache, int8_t* seen);
int32_t main(void);



double probability_f64_i32_i32_ptr_f64_ptr_i8(double q, int32_t made, int32_t distance, double* cache, int8_t* seen) {
    int32_t threshold = 20;
    int32_t max_distance = 50;
    if (made > threshold) {
        return 0.0;
    }
    if (distance > max_distance) {
        if (made == threshold) {
            return 1.0;
        }
        return 0.0;
    }
    int32_t id = ((made * (max_distance + 1)) + distance);
    if (seen[id] == 1) {
        return cache[id];
    }
    double chance_hit = (1.0 - (((double)(distance)) / q));
    double chance_miss = (1.0 - chance_hit);
    double result = ((chance_hit * probability_f64_i32_i32_ptr_f64_ptr_i8(q, (made + 1), (distance + 1), cache, seen)) + (chance_miss * probability_f64_i32_i32_ptr_f64_ptr_i8(q, made, (distance + 1), cache, seen)));
    cache[id] = result;
    seen[id] = 1;
    return result;
}

int32_t main(void) {
    double* cache = (double*)(calloc((21 * 52), 8));
    int8_t* seen = (int8_t*)(calloc((21 * 52), 1));
    if ((cache == NULL || seen == NULL)) {
        return 1;
    }
    double low = 50.0;
    double high = 100.0;
    double accuracy = 0.0000000001;
    while ((high - low) > accuracy) {
        double mid = ((high + low) * 0.5);
        int32_t i = 0;
        while (i < (21 * 52)) {
            seen[i] = 0;
            i = (i + 1);
        }
        if (probability_f64_i32_i32_ptr_f64_ptr_i8(mid, 0, 1, cache, seen) < 0.02) {
            high = mid;
        } else {
            low = mid;
        }
    }
    printf("%.10f\n", low);
    free(cache);
    free(seen);
    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 @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @probability(%arg0: f64, %arg1: i32, %arg2: i32, %arg3: !llvm.ptr, %arg4: !llvm.ptr) -> f64 {
    %0 = arith.constant 20 : i32
    %1 = arith.constant 50 : i32
    %2 = arith.cmpi sgt, %arg1, %0 : i32
    cf.cond_br %2, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 0.0 : f32
      %4 = arith.extf %3 : f32 to f64
      func.return %4 : f64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %5 = arith.cmpi sgt, %arg2, %1 : i32
    cf.cond_br %5, ^bb3, ^bb4
    ^bb3:
      %6 = arith.cmpi eq, %arg1, %0 : i32
      cf.cond_br %6, ^bb6, ^bb7
      ^bb6:
        %7 = arith.constant 1.0 : f32
        %8 = arith.extf %7 : f32 to f64
        func.return %8 : f64
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %9 = arith.constant 0.0 : f32
      %10 = arith.extf %9 : f32 to f64
      func.return %10 : f64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %11 = arith.constant 1 : i32
    %12 = arith.addi %1, %11 : i32
    %13 = arith.muli %arg1, %12 : i32
    %14 = arith.addi %13, %arg2 : i32
    %16 = arith.extsi %14 : i32 to i64
    %17 = llvm.getelementptr %arg4[%16] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    %15 = llvm.load %17 : !llvm.ptr -> i8
    %18 = arith.constant 1 : i32
    %20 = arith.extsi %15 : i8 to i32
    %19 = arith.cmpi eq, %20, %18 : i32
    cf.cond_br %19, ^bb9, ^bb10
    ^bb9:
      %22 = arith.extsi %14 : i32 to i64
      %23 = llvm.getelementptr %arg3[%22] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %21 = llvm.load %23 : !llvm.ptr -> f64
      func.return %21 : f64
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %24 = arith.constant 1.0 : f32
    %25 = arith.sitofp %arg2 : i32 to f64
    %26 = arith.divf %25, %arg0 : f64
    %28 = arith.extf %24 : f32 to f64
    %27 = arith.subf %28, %26 : f64
    %29 = arith.constant 1.0 : f32
    %31 = arith.extf %29 : f32 to f64
    %30 = arith.subf %31, %27 : f64
    %33 = arith.constant 1 : i32
    %34 = arith.addi %arg1, %33 : i32
    %35 = arith.constant 1 : i32
    %36 = arith.addi %arg2, %35 : i32
    %32 = func.call @probability(%arg0, %34, %36, %arg3, %arg4) : (f64, i32, i32, !llvm.ptr, !llvm.ptr) -> f64
    %37 = arith.mulf %27, %32 : f64
    %39 = arith.constant 1 : i32
    %40 = arith.addi %arg2, %39 : i32
    %38 = func.call @probability(%arg0, %arg1, %40, %arg3, %arg4) : (f64, i32, i32, !llvm.ptr, !llvm.ptr) -> f64
    %41 = arith.mulf %30, %38 : f64
    %42 = arith.addf %37, %41 : f64
    %43 = arith.extsi %14 : i32 to i64
    %44 = llvm.getelementptr %arg3[%43] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    llvm.store %42, %44 : f64, !llvm.ptr
    %45 = arith.constant 1 : i32
    %46 = arith.trunci %45 : i32 to i8
    %47 = arith.extsi %14 : i32 to i64
    %48 = llvm.getelementptr %arg4[%47] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %46, %48 : i8, !llvm.ptr
    func.return %42 : f64
  }
  func.func @main() -> i32 {
    %50 = arith.constant 21 : i32
    %51 = arith.constant 52 : i32
    %52 = arith.muli %50, %51 : i32
    %53 = arith.constant 8 : i32
    %54 = arith.extsi %52 : i32 to i64
    %55 = arith.extsi %53 : i32 to i64
    %49 = func.call @calloc(%54, %55) : (i64, i64) -> !llvm.ptr
    %57 = arith.constant 21 : i32
    %58 = arith.constant 52 : i32
    %59 = arith.muli %57, %58 : i32
    %60 = arith.constant 1 : i32
    %61 = arith.extsi %59 : i32 to i64
    %62 = arith.extsi %60 : i32 to i64
    %56 = func.call @calloc(%61, %62) : (i64, i64) -> !llvm.ptr
    %63 = llvm.mlir.zero : !llvm.ptr
    %64 = llvm.icmp "eq" %49, %63 : !llvm.ptr
    %65 = scf.if %64 -> (i1) {
      %66 = arith.constant true
      scf.yield %66 : i1
    } else {
      %67 = llvm.mlir.zero : !llvm.ptr
      %68 = llvm.icmp "eq" %56, %67 : !llvm.ptr
      scf.yield %68 : i1
    }
    cf.cond_br %65, ^bb12, ^bb13
    ^bb12:
      %69 = arith.constant 1 : i32
      func.return %69 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %70 = arith.constant 50.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 100.0 : f32
    %75 = arith.extf %74 : f32 to f64
    %76 = llvm.mlir.constant(1 : i64) : i64
    %77 = llvm.alloca %76 x f64 : (i64) -> !llvm.ptr
    llvm.store %75, %77 : f64, !llvm.ptr
    %78 = arith.constant 0.0000000001 : f32
    %79 = arith.extf %78 : f32 to f64
    cf.br ^bb15
    ^bb15:
    %80 = llvm.load %77 : !llvm.ptr -> f64
    %81 = llvm.load %73 : !llvm.ptr -> f64
    %82 = arith.subf %80, %81 : f64
    %83 = arith.cmpf ogt, %82, %79 : f64
    cf.cond_br %83, ^bb16, ^bb17
    ^bb16:
      %84 = llvm.load %77 : !llvm.ptr -> f64
      %85 = llvm.load %73 : !llvm.ptr -> f64
      %86 = arith.addf %84, %85 : f64
      %87 = arith.constant 0.5 : f32
      %89 = arith.extf %87 : f32 to f64
      %88 = arith.mulf %86, %89 : f64
      %90 = arith.constant 0 : i32
      %91 = llvm.mlir.constant(1 : i64) : i64
      %92 = llvm.alloca %91 x i32 : (i64) -> !llvm.ptr
      llvm.store %90, %92 : i32, !llvm.ptr
      cf.br ^bb18
      ^bb18:
      %93 = llvm.load %92 : !llvm.ptr -> i32
      %94 = arith.constant 21 : i32
      %95 = arith.constant 52 : i32
      %96 = arith.muli %94, %95 : i32
      %97 = arith.cmpi slt, %93, %96 : i32
      cf.cond_br %97, ^bb19, ^bb20
      ^bb19:
        %98 = arith.constant 0 : i32
        %99 = llvm.load %92 : !llvm.ptr -> i32
        %100 = arith.trunci %98 : i32 to i8
        %101 = arith.extsi %99 : i32 to i64
        %102 = llvm.getelementptr %56[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        llvm.store %100, %102 : i8, !llvm.ptr
        %103 = llvm.load %92 : !llvm.ptr -> i32
        %104 = arith.constant 1 : i32
        %105 = arith.addi %103, %104 : i32
        llvm.store %105, %92 : i32, !llvm.ptr
        cf.br ^bb18
      ^bb20:
      %107 = arith.constant 0 : i32
      %108 = arith.constant 1 : i32
      %106 = func.call @probability(%88, %107, %108, %49, %56) : (f64, i32, i32, !llvm.ptr, !llvm.ptr) -> f64
      %109 = arith.constant 0.02 : f32
      %111 = arith.extf %109 : f32 to f64
      %110 = arith.cmpf olt, %106, %111 : f64
      cf.cond_br %110, ^bb21, ^bb22
      ^bb21:
        llvm.store %88, %77 : f64, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        llvm.store %88, %73 : f64, !llvm.ptr
        cf.br ^bb23
      ^bb23:
      cf.br ^bb15
    ^bb17:
    %112 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %113 = llvm.load %73 : !llvm.ptr -> f64
    %114 = llvm.call @printf(%112, %113) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    func.call @free(%49) : (!llvm.ptr) -> ()
    func.call @free(%56) : (!llvm.ptr) -> ()
    %117 = arith.constant 0 : i32
    func.return %117 : i32
  }
}