Problem 151

Expected singles in paper envelope (exclude first/last). Print 6dp as integer 464399.

Answer464399
Output464399
StatusPASS
Native helperno
Runtime0 ms
Peak memory12192 KB
Time complexityO(2^n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 151
# Expected singles in paper envelope (exclude first/last). Print 6dp as integer 464399.

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

# State: counts of sizes A1..A5 encoded base 17.
# memo[state] stores expected singles from that state; -1 = unset.

function encode(c0: i32, c1: i32, c2: i32, c3: i32, c4: i32) -> i32 {
    return c0 + 17 * (c1 + 17 * (c2 + 17 * (c3 + 17 * c4)))
}

function expected(c0: i32, c1: i32, c2: i32, c3: i32, c4: i32, memo: ptr<f64>) -> f64 {
    let total: i32 = c0 + c1 + c2 + c3 + c4
    if total == 0 { return 0.0 }

    let key: i32 = encode(c0, c1, c2, c3, c4)
    if memo[key] >= 0.0 {
        return memo[key]
    }

    let mut single: f64 = 0.0
    if total == 1 {
        # last sheet A5 contributes nothing extra at terminal? Nayuki adds 1 for any singleton
        single = 1.0
    }

    let mut sum: f64 = 0.0
    if c0 > 0 {
        sum = sum + (c0 as f64) * expected(c0 - 1, c1 + 1, c2 + 1, c3 + 1, c4 + 1, memo)
    }
    if c1 > 0 {
        sum = sum + (c1 as f64) * expected(c0, c1 - 1, c2 + 1, c3 + 1, c4 + 1, memo)
    }
    if c2 > 0 {
        sum = sum + (c2 as f64) * expected(c0, c1, c2 - 1, c3 + 1, c4 + 1, memo)
    }
    if c3 > 0 {
        sum = sum + (c3 as f64) * expected(c0, c1, c2, c3 - 1, c4 + 1, memo)
    }
    if c4 > 0 {
        sum = sum + (c4 as f64) * expected(c0, c1, c2, c3, c4 - 1, memo)
    }
    let result: f64 = single + sum / (total as f64)
    memo[key] = result
    return result
}

function main() -> i32 {
    let size: i64 = 17 * 17 * 17 * 17 * 17
    let memo: ptr<f64> = calloc(size, 8)
    if memo == null { return 1 }
    let mut i: i64 = 0
    while i < size {
        memo[i] = -1.0
        i = i + 1
    }

    # Nayuki: get_expected_singles((1,)) - 2  (subtract first and last batch singles)
    let ans: f64 = expected(1, 0, 0, 0, 0, memo) - 2.0
    # round to 6 decimal places as integer digits
    let scaled: i64 = ((ans * 1000000.0 + 0.5) as i64)
    printf("%lld\n", scaled)
    free(memo)
    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 encode_i32_i32_i32_i32_i32(int32_t c0, int32_t c1, int32_t c2, int32_t c3, int32_t c4);
double expected_i32_i32_i32_i32_i32_ptr_f64(int32_t c0, int32_t c1, int32_t c2, int32_t c3, int32_t c4, double* memo);
int32_t main(void);



int32_t encode_i32_i32_i32_i32_i32(int32_t c0, int32_t c1, int32_t c2, int32_t c3, int32_t c4) {
    return (c0 + (17 * (c1 + (17 * (c2 + (17 * (c3 + (17 * c4))))))));
}

double expected_i32_i32_i32_i32_i32_ptr_f64(int32_t c0, int32_t c1, int32_t c2, int32_t c3, int32_t c4, double* memo) {
    int32_t total = ((((c0 + c1) + c2) + c3) + c4);
    if (total == 0) {
        return 0.0;
    }
    int32_t key = encode_i32_i32_i32_i32_i32(c0, c1, c2, c3, c4);
    if (memo[key] >= 0.0) {
        return memo[key];
    }
    double single = 0.0;
    if (total == 1) {
        single = 1.0;
    }
    double sum = 0.0;
    if (c0 > 0) {
        sum = (sum + (((double)(c0)) * expected_i32_i32_i32_i32_i32_ptr_f64((c0 - 1), (c1 + 1), (c2 + 1), (c3 + 1), (c4 + 1), memo)));
    }
    if (c1 > 0) {
        sum = (sum + (((double)(c1)) * expected_i32_i32_i32_i32_i32_ptr_f64(c0, (c1 - 1), (c2 + 1), (c3 + 1), (c4 + 1), memo)));
    }
    if (c2 > 0) {
        sum = (sum + (((double)(c2)) * expected_i32_i32_i32_i32_i32_ptr_f64(c0, c1, (c2 - 1), (c3 + 1), (c4 + 1), memo)));
    }
    if (c3 > 0) {
        sum = (sum + (((double)(c3)) * expected_i32_i32_i32_i32_i32_ptr_f64(c0, c1, c2, (c3 - 1), (c4 + 1), memo)));
    }
    if (c4 > 0) {
        sum = (sum + (((double)(c4)) * expected_i32_i32_i32_i32_i32_ptr_f64(c0, c1, c2, c3, (c4 - 1), memo)));
    }
    double result = (single + (sum / ((double)(total))));
    memo[key] = result;
    return result;
}

int32_t main(void) {
    int64_t size = ((((17 * 17) * 17) * 17) * 17);
    double* memo = (double*)(calloc(size, 8));
    if (memo == NULL) {
        return 1;
    }
    int64_t i = 0;
    while (i < size) {
        memo[i] = (-1.0);
        i = (i + 1);
    }
    double ans = (expected_i32_i32_i32_i32_i32_ptr_f64(1, 0, 0, 0, 0, memo) - 2.0);
    int64_t scaled = ((int64_t)(((ans * 1000000.0) + 0.5)));
    printf("%lld\n", scaled);
    free(memo);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @encode(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32, %arg4: i32) -> i32 {
    %0 = arith.constant 17 : i32
    %1 = arith.constant 17 : i32
    %2 = arith.constant 17 : i32
    %3 = arith.constant 17 : i32
    %4 = arith.muli %3, %arg4 : i32
    %5 = arith.addi %arg3, %4 : i32
    %6 = arith.muli %2, %5 : i32
    %7 = arith.addi %arg2, %6 : i32
    %8 = arith.muli %1, %7 : i32
    %9 = arith.addi %arg1, %8 : i32
    %10 = arith.muli %0, %9 : i32
    %11 = arith.addi %arg0, %10 : i32
    func.return %11 : i32
  }
  func.func @expected(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32, %arg4: i32, %arg5: !llvm.ptr) -> f64 {
    %12 = arith.addi %arg0, %arg1 : i32
    %13 = arith.addi %12, %arg2 : i32
    %14 = arith.addi %13, %arg3 : i32
    %15 = arith.addi %14, %arg4 : i32
    %16 = arith.constant 0 : i32
    %17 = arith.cmpi eq, %15, %16 : i32
    cf.cond_br %17, ^bb0, ^bb1
    ^bb0:
      %18 = arith.constant 0.0 : f32
      %19 = arith.extf %18 : f32 to f64
      func.return %19 : f64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %20 = func.call @encode(%arg0, %arg1, %arg2, %arg3, %arg4) : (i32, i32, i32, i32, i32) -> i32
    %22 = arith.extsi %20 : i32 to i64
    %23 = llvm.getelementptr %arg5[%22] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %21 = llvm.load %23 : !llvm.ptr -> f64
    %24 = arith.constant 0.0 : f32
    %26 = arith.extf %24 : f32 to f64
    %25 = arith.cmpf oge, %21, %26 : f64
    cf.cond_br %25, ^bb3, ^bb4
    ^bb3:
      %28 = arith.extsi %20 : i32 to i64
      %29 = llvm.getelementptr %arg5[%28] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %27 = llvm.load %29 : !llvm.ptr -> f64
      func.return %27 : f64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %30 = arith.constant 0.0 : f32
    %31 = arith.extf %30 : f32 to f64
    %32 = llvm.mlir.constant(1 : i64) : i64
    %33 = llvm.alloca %32 x f64 : (i64) -> !llvm.ptr
    llvm.store %31, %33 : f64, !llvm.ptr
    %34 = arith.constant 1 : i32
    %35 = arith.cmpi eq, %15, %34 : i32
    cf.cond_br %35, ^bb6, ^bb7
    ^bb6:
      %36 = arith.constant 1.0 : f32
      %37 = arith.extf %36 : f32 to f64
      llvm.store %37, %33 : f64, !llvm.ptr
      cf.br ^bb8
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %38 = arith.constant 0.0 : f32
    %39 = arith.extf %38 : f32 to f64
    %40 = llvm.mlir.constant(1 : i64) : i64
    %41 = llvm.alloca %40 x f64 : (i64) -> !llvm.ptr
    llvm.store %39, %41 : f64, !llvm.ptr
    %42 = arith.constant 0 : i32
    %43 = arith.cmpi sgt, %arg0, %42 : i32
    cf.cond_br %43, ^bb9, ^bb10
    ^bb9:
      %44 = llvm.load %41 : !llvm.ptr -> f64
      %45 = arith.sitofp %arg0 : i32 to f64
      %47 = arith.constant 1 : i32
      %48 = arith.subi %arg0, %47 : i32
      %49 = arith.constant 1 : i32
      %50 = arith.addi %arg1, %49 : i32
      %51 = arith.constant 1 : i32
      %52 = arith.addi %arg2, %51 : i32
      %53 = arith.constant 1 : i32
      %54 = arith.addi %arg3, %53 : i32
      %55 = arith.constant 1 : i32
      %56 = arith.addi %arg4, %55 : i32
      %46 = func.call @expected(%48, %50, %52, %54, %56, %arg5) : (i32, i32, i32, i32, i32, !llvm.ptr) -> f64
      %57 = arith.mulf %45, %46 : f64
      %58 = arith.addf %44, %57 : f64
      llvm.store %58, %41 : f64, !llvm.ptr
      cf.br ^bb11
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %59 = arith.constant 0 : i32
    %60 = arith.cmpi sgt, %arg1, %59 : i32
    cf.cond_br %60, ^bb12, ^bb13
    ^bb12:
      %61 = llvm.load %41 : !llvm.ptr -> f64
      %62 = arith.sitofp %arg1 : i32 to f64
      %64 = arith.constant 1 : i32
      %65 = arith.subi %arg1, %64 : i32
      %66 = arith.constant 1 : i32
      %67 = arith.addi %arg2, %66 : i32
      %68 = arith.constant 1 : i32
      %69 = arith.addi %arg3, %68 : i32
      %70 = arith.constant 1 : i32
      %71 = arith.addi %arg4, %70 : i32
      %63 = func.call @expected(%arg0, %65, %67, %69, %71, %arg5) : (i32, i32, i32, i32, i32, !llvm.ptr) -> f64
      %72 = arith.mulf %62, %63 : f64
      %73 = arith.addf %61, %72 : f64
      llvm.store %73, %41 : f64, !llvm.ptr
      cf.br ^bb14
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %74 = arith.constant 0 : i32
    %75 = arith.cmpi sgt, %arg2, %74 : i32
    cf.cond_br %75, ^bb15, ^bb16
    ^bb15:
      %76 = llvm.load %41 : !llvm.ptr -> f64
      %77 = arith.sitofp %arg2 : i32 to f64
      %79 = arith.constant 1 : i32
      %80 = arith.subi %arg2, %79 : i32
      %81 = arith.constant 1 : i32
      %82 = arith.addi %arg3, %81 : i32
      %83 = arith.constant 1 : i32
      %84 = arith.addi %arg4, %83 : i32
      %78 = func.call @expected(%arg0, %arg1, %80, %82, %84, %arg5) : (i32, i32, i32, i32, i32, !llvm.ptr) -> f64
      %85 = arith.mulf %77, %78 : f64
      %86 = arith.addf %76, %85 : f64
      llvm.store %86, %41 : f64, !llvm.ptr
      cf.br ^bb17
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %87 = arith.constant 0 : i32
    %88 = arith.cmpi sgt, %arg3, %87 : i32
    cf.cond_br %88, ^bb18, ^bb19
    ^bb18:
      %89 = llvm.load %41 : !llvm.ptr -> f64
      %90 = arith.sitofp %arg3 : i32 to f64
      %92 = arith.constant 1 : i32
      %93 = arith.subi %arg3, %92 : i32
      %94 = arith.constant 1 : i32
      %95 = arith.addi %arg4, %94 : i32
      %91 = func.call @expected(%arg0, %arg1, %arg2, %93, %95, %arg5) : (i32, i32, i32, i32, i32, !llvm.ptr) -> f64
      %96 = arith.mulf %90, %91 : f64
      %97 = arith.addf %89, %96 : f64
      llvm.store %97, %41 : f64, !llvm.ptr
      cf.br ^bb20
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %98 = arith.constant 0 : i32
    %99 = arith.cmpi sgt, %arg4, %98 : i32
    cf.cond_br %99, ^bb21, ^bb22
    ^bb21:
      %100 = llvm.load %41 : !llvm.ptr -> f64
      %101 = arith.sitofp %arg4 : i32 to f64
      %103 = arith.constant 1 : i32
      %104 = arith.subi %arg4, %103 : i32
      %102 = func.call @expected(%arg0, %arg1, %arg2, %arg3, %104, %arg5) : (i32, i32, i32, i32, i32, !llvm.ptr) -> f64
      %105 = arith.mulf %101, %102 : f64
      %106 = arith.addf %100, %105 : f64
      llvm.store %106, %41 : f64, !llvm.ptr
      cf.br ^bb23
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %107 = llvm.load %33 : !llvm.ptr -> f64
    %108 = llvm.load %41 : !llvm.ptr -> f64
    %109 = arith.sitofp %15 : i32 to f64
    %110 = arith.divf %108, %109 : f64
    %111 = arith.addf %107, %110 : f64
    %112 = arith.extsi %20 : i32 to i64
    %113 = llvm.getelementptr %arg5[%112] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    llvm.store %111, %113 : f64, !llvm.ptr
    func.return %111 : f64
  }
  func.func @main() -> i32 {
    %114 = arith.constant 17 : i32
    %115 = arith.constant 17 : i32
    %116 = arith.muli %114, %115 : i32
    %117 = arith.constant 17 : i32
    %118 = arith.muli %116, %117 : i32
    %119 = arith.constant 17 : i32
    %120 = arith.muli %118, %119 : i32
    %121 = arith.constant 17 : i32
    %122 = arith.muli %120, %121 : i32
    %123 = arith.extsi %122 : i32 to i64
    %125 = arith.constant 8 : i32
    %126 = arith.extsi %125 : i32 to i64
    %124 = func.call @calloc(%123, %126) : (i64, i64) -> !llvm.ptr
    %127 = llvm.mlir.zero : !llvm.ptr
    %128 = llvm.icmp "eq" %124, %127 : !llvm.ptr
    cf.cond_br %128, ^bb24, ^bb25
    ^bb24:
      %129 = arith.constant 1 : i32
      func.return %129 : i32
    ^bb25:
      cf.br ^bb26
    ^bb26:
    %130 = arith.constant 0 : i32
    %131 = arith.extsi %130 : i32 to i64
    %132 = llvm.mlir.constant(1 : i64) : i64
    %133 = llvm.alloca %132 x i64 : (i64) -> !llvm.ptr
    llvm.store %131, %133 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %134 = llvm.load %133 : !llvm.ptr -> i64
    %135 = arith.cmpi slt, %134, %123 : i64
    cf.cond_br %135, ^bb28, ^bb29
    ^bb28:
      %136 = arith.constant 1.0 : f32
      %137 = arith.negf %136 : f32
      %138 = llvm.load %133 : !llvm.ptr -> i64
      %139 = arith.extf %137 : f32 to f64
      %140 = llvm.getelementptr %124[%138] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      llvm.store %139, %140 : f64, !llvm.ptr
      %141 = llvm.load %133 : !llvm.ptr -> i64
      %142 = arith.constant 1 : i32
      %144 = arith.extsi %142 : i32 to i64
      %143 = arith.addi %141, %144 : i64
      llvm.store %143, %133 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %146 = arith.constant 1 : i32
    %147 = arith.constant 0 : i32
    %148 = arith.constant 0 : i32
    %149 = arith.constant 0 : i32
    %150 = arith.constant 0 : i32
    %145 = func.call @expected(%146, %147, %148, %149, %150, %124) : (i32, i32, i32, i32, i32, !llvm.ptr) -> f64
    %151 = arith.constant 2.0 : f32
    %153 = arith.extf %151 : f32 to f64
    %152 = arith.subf %145, %153 : f64
    %154 = arith.constant 1000000.0 : f32
    %156 = arith.extf %154 : f32 to f64
    %155 = arith.mulf %152, %156 : f64
    %157 = arith.constant 0.5 : f32
    %159 = arith.extf %157 : f32 to f64
    %158 = arith.addf %155, %159 : f64
    %160 = arith.fptosi %158 : f64 to i64
    %161 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %162 = llvm.call @printf(%161, %160) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%124) : (!llvm.ptr) -> ()
    %164 = arith.constant 0 : i32
    func.return %164 : i32
  }
}