Problem 239

Probability twenty-two prime discs are deranged among 100.

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

Performance comparison

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

Flow source

# Project Euler 239
# Probability twenty-two prime discs are deranged among 100.

function factorial(n: i32) -> f64 {
    let mut result: f64 = 1.0
    let mut i: i32 = n
    while i > 1 {
        result = result * (i as f64)
        i = i - 1
    }
    return result
}

function choose(n: i32, k: i32) -> f64 {
    return factorial(n) / (factorial(n - k) * factorial(k))
}

function derangements(move0: i32, dont_care: i32) -> f64 {
    let mut move: i32 = move0
    if move < 1 {
        return factorial(dont_care)
    }
    move = move - 1
    let mut result: f64 = (dont_care as f64) * derangements(move, dont_care)
    if move > 0 {
        result = result + (move as f64) * derangements(move - 1, dont_care + 1)
    }
    return result
}

function main() -> i32 {
    let disks: i32 = 100
    let primes: i32 = 25
    let moved: i32 = 22
    let unchanged: i32 = primes - moved
    let mut result: f64 = derangements(moved, disks - primes)
    result = result * choose(primes, unchanged)
    result = result / factorial(disks)
    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; }

double factorial_i32(int32_t n);
double choose_i32_i32(int32_t n, int32_t k);
double derangements_i32_i32(int32_t move0, int32_t dont_care);
int32_t main(void);

double factorial_i32(int32_t n) {
    double result = 1.0;
    int32_t i = n;
    while (i > 1) {
        result = (result * ((double)(i)));
        i = (i - 1);
    }
    return result;
}

double choose_i32_i32(int32_t n, int32_t k) {
    return (factorial_i32(n) / (factorial_i32((n - k)) * factorial_i32(k)));
}

double derangements_i32_i32(int32_t move0, int32_t dont_care) {
    int32_t move = move0;
    if (move < 1) {
        return factorial_i32(dont_care);
    }
    move = (move - 1);
    double result = (((double)(dont_care)) * derangements_i32_i32(move, dont_care));
    if (move > 0) {
        result = (result + (((double)(move)) * derangements_i32_i32((move - 1), (dont_care + 1))));
    }
    return result;
}

int32_t main(void) {
    int32_t disks = 100;
    int32_t primes = 25;
    int32_t moved = 22;
    int32_t unchanged = (primes - moved);
    double result = derangements_i32_i32(moved, (disks - primes));
    result = (result * choose_i32_i32(primes, unchanged));
    result = (result / factorial_i32(disks));
    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 @factorial(%arg0: i32) -> f64 {
    %0 = arith.constant 1.0 : f32
    %1 = arith.extf %0 : f32 to f64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x f64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : f64, !llvm.ptr
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i32 : (i64) -> !llvm.ptr
    llvm.store %arg0, %5 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %6 = llvm.load %5 : !llvm.ptr -> i32
    %7 = arith.constant 1 : i32
    %8 = arith.cmpi sgt, %6, %7 : i32
    cf.cond_br %8, ^bb1, ^bb2
    ^bb1:
      %9 = llvm.load %3 : !llvm.ptr -> f64
      %10 = llvm.load %5 : !llvm.ptr -> i32
      %11 = arith.sitofp %10 : i32 to f64
      %12 = arith.mulf %9, %11 : f64
      llvm.store %12, %3 : f64, !llvm.ptr
      %13 = llvm.load %5 : !llvm.ptr -> i32
      %14 = arith.constant 1 : i32
      %15 = arith.subi %13, %14 : i32
      llvm.store %15, %5 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %16 = llvm.load %3 : !llvm.ptr -> f64
    func.return %16 : f64
  }
  func.func @choose(%arg0: i32, %arg1: i32) -> f64 {
    %17 = func.call @factorial(%arg0) : (i32) -> f64
    %19 = arith.subi %arg0, %arg1 : i32
    %18 = func.call @factorial(%19) : (i32) -> f64
    %20 = func.call @factorial(%arg1) : (i32) -> f64
    %21 = arith.mulf %18, %20 : f64
    %22 = arith.divf %17, %21 : f64
    func.return %22 : f64
  }
  func.func @derangements(%arg0: i32, %arg1: i32) -> f64 {
    %23 = llvm.mlir.constant(1 : i64) : i64
    %24 = llvm.alloca %23 x i32 : (i64) -> !llvm.ptr
    llvm.store %arg0, %24 : i32, !llvm.ptr
    %25 = llvm.load %24 : !llvm.ptr -> i32
    %26 = arith.constant 1 : i32
    %27 = arith.cmpi slt, %25, %26 : i32
    cf.cond_br %27, ^bb3, ^bb4
    ^bb3:
      %28 = func.call @factorial(%arg1) : (i32) -> f64
      func.return %28 : f64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %29 = llvm.load %24 : !llvm.ptr -> i32
    %30 = arith.constant 1 : i32
    %31 = arith.subi %29, %30 : i32
    llvm.store %31, %24 : i32, !llvm.ptr
    %32 = arith.sitofp %arg1 : i32 to f64
    %34 = llvm.load %24 : !llvm.ptr -> i32
    %33 = func.call @derangements(%34, %arg1) : (i32, i32) -> f64
    %35 = arith.mulf %32, %33 : 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 = llvm.load %24 : !llvm.ptr -> i32
    %39 = arith.constant 0 : i32
    %40 = arith.cmpi sgt, %38, %39 : i32
    cf.cond_br %40, ^bb6, ^bb7
    ^bb6:
      %41 = llvm.load %37 : !llvm.ptr -> f64
      %42 = llvm.load %24 : !llvm.ptr -> i32
      %43 = arith.sitofp %42 : i32 to f64
      %45 = llvm.load %24 : !llvm.ptr -> i32
      %46 = arith.constant 1 : i32
      %47 = arith.subi %45, %46 : i32
      %48 = arith.constant 1 : i32
      %49 = arith.addi %arg1, %48 : i32
      %44 = func.call @derangements(%47, %49) : (i32, i32) -> f64
      %50 = arith.mulf %43, %44 : f64
      %51 = arith.addf %41, %50 : f64
      llvm.store %51, %37 : f64, !llvm.ptr
      cf.br ^bb8
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %52 = llvm.load %37 : !llvm.ptr -> f64
    func.return %52 : f64
  }
  func.func @main() -> i32 {
    %53 = arith.constant 100 : i32
    %54 = arith.constant 25 : i32
    %55 = arith.constant 22 : i32
    %56 = arith.subi %54, %55 : i32
    %58 = arith.subi %53, %54 : i32
    %57 = func.call @derangements(%55, %58) : (i32, i32) -> f64
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x f64 : (i64) -> !llvm.ptr
    llvm.store %57, %60 : f64, !llvm.ptr
    %61 = llvm.load %60 : !llvm.ptr -> f64
    %62 = func.call @choose(%54, %56) : (i32, i32) -> f64
    %63 = arith.mulf %61, %62 : f64
    llvm.store %63, %60 : f64, !llvm.ptr
    %64 = llvm.load %60 : !llvm.ptr -> f64
    %65 = func.call @factorial(%53) : (i32) -> f64
    %66 = arith.divf %64, %65 : f64
    llvm.store %66, %60 : f64, !llvm.ptr
    %67 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %68 = llvm.load %60 : !llvm.ptr -> f64
    %69 = llvm.call @printf(%67, %68) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %70 = arith.constant 0 : i32
    func.return %70 : i32
  }
}