Problem 339

Expected black sheep E(10000), 6 decimal places.

Answer19823.542204
Output19823.542204
StatusPASS
Native helperno
Runtime0 ms
Peak memory1184 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 339
# Expected black sheep E(10000), 6 decimal places.

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

function main() -> i32 {
    let n: i64 = 10000
    let B: ptr<f64> = calloc(n + 1, 8)
    if B == null { return 1 }
    B[0] = 0.0
    let mut p: f64 = 1.0
    let mut b: i64 = 1
    while b <= n {
        let r: f64 = (2.0 * p) / (1.0 + p)
        let m: f64 = (2 * b - 1) as f64
        B[b] = B[b - 1] + (m - B[b - 1]) * r
        p = p * ((2.0 * (b as f64) - 1.0) / (2.0 * (b as f64)))
        b = b + 1
    }
    let mut q: f64 = 0.5
    let mut t: i64 = 1
    while t < n {
        q = q * ((2.0 * (t as f64) + 1.0) / (2.0 * (t as f64) + 2.0))
        t = t + 1
    }
    let r2: f64 = 2.0 * q
    let m2: f64 = (2 * n) as f64
    let S: f64 = B[n] + (m2 - B[n]) * r2
    let ans: f64 = 0.5 * (B[n - 1] + S)
    printf("%.6f\n", ans)
    free(B)
    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 n = 10000;
    double* B = (double*)(calloc((n + 1), 8));
    if (B == NULL) {
        return 1;
    }
    B[0] = 0.0;
    double p = 1.0;
    int64_t b = 1;
    while (b <= n) {
        double r = ((2.0 * p) / (1.0 + p));
        double m = ((double)(((2 * b) - 1)));
        B[b] = (B[(b - 1)] + ((m - B[(b - 1)]) * r));
        p = (p * (((2.0 * ((double)(b))) - 1.0) / (2.0 * ((double)(b)))));
        b = (b + 1);
    }
    double q = 0.5;
    int64_t t = 1;
    while (t < n) {
        q = (q * (((2.0 * ((double)(t))) + 1.0) / ((2.0 * ((double)(t))) + 2.0)));
        t = (t + 1);
    }
    double r2 = (2.0 * q);
    double m2 = ((double)((2 * n)));
    double S = (B[n] + ((m2 - B[n]) * r2));
    double ans = (0.5 * (B[(n - 1)] + S));
    printf("%.6f\n", ans);
    free(B);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.6f\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 @main() -> i32 {
    %0 = arith.constant 10000 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 1 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.addi %1, %5 : i64
    %6 = arith.constant 8 : i32
    %7 = arith.extsi %6 : i32 to i64
    %2 = func.call @calloc(%4, %7) : (i64, i64) -> !llvm.ptr
    %8 = llvm.mlir.zero : !llvm.ptr
    %9 = llvm.icmp "eq" %2, %8 : !llvm.ptr
    cf.cond_br %9, ^bb0, ^bb1
    ^bb0:
      %10 = arith.constant 1 : i32
      func.return %10 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %11 = arith.constant 0.0 : f32
    %12 = arith.constant 0 : i32
    %13 = arith.extf %11 : f32 to f64
    %14 = arith.extsi %12 : i32 to i64
    %15 = llvm.getelementptr %2[%14] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    llvm.store %13, %15 : f64, !llvm.ptr
    %16 = arith.constant 1.0 : f32
    %17 = arith.extf %16 : f32 to f64
    %18 = llvm.mlir.constant(1 : i64) : i64
    %19 = llvm.alloca %18 x f64 : (i64) -> !llvm.ptr
    llvm.store %17, %19 : f64, !llvm.ptr
    %20 = arith.constant 1 : i32
    %21 = arith.extsi %20 : i32 to i64
    %22 = llvm.mlir.constant(1 : i64) : i64
    %23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
    llvm.store %21, %23 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %24 = llvm.load %23 : !llvm.ptr -> i64
    %25 = arith.cmpi sle, %24, %1 : i64
    cf.cond_br %25, ^bb4, ^bb5
    ^bb4:
      %26 = arith.constant 2.0 : f32
      %27 = llvm.load %19 : !llvm.ptr -> f64
      %29 = arith.extf %26 : f32 to f64
      %28 = arith.mulf %29, %27 : f64
      %30 = arith.constant 1.0 : f32
      %31 = llvm.load %19 : !llvm.ptr -> f64
      %33 = arith.extf %30 : f32 to f64
      %32 = arith.addf %33, %31 : f64
      %34 = arith.divf %28, %32 : f64
      %35 = arith.constant 2 : i32
      %36 = llvm.load %23 : !llvm.ptr -> i64
      %38 = arith.extsi %35 : i32 to i64
      %37 = arith.muli %38, %36 : i64
      %39 = arith.constant 1 : i32
      %41 = arith.extsi %39 : i32 to i64
      %40 = arith.subi %37, %41 : i64
      %42 = arith.sitofp %40 : i64 to f64
      %44 = llvm.load %23 : !llvm.ptr -> i64
      %45 = arith.constant 1 : i32
      %47 = arith.extsi %45 : i32 to i64
      %46 = arith.subi %44, %47 : i64
      %48 = llvm.getelementptr %2[%46] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %43 = llvm.load %48 : !llvm.ptr -> f64
      %50 = llvm.load %23 : !llvm.ptr -> i64
      %51 = arith.constant 1 : i32
      %53 = arith.extsi %51 : i32 to i64
      %52 = arith.subi %50, %53 : i64
      %54 = llvm.getelementptr %2[%52] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %49 = llvm.load %54 : !llvm.ptr -> f64
      %55 = arith.subf %42, %49 : f64
      %56 = arith.mulf %55, %34 : f64
      %57 = arith.addf %43, %56 : f64
      %58 = llvm.load %23 : !llvm.ptr -> i64
      %59 = llvm.getelementptr %2[%58] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      llvm.store %57, %59 : f64, !llvm.ptr
      %60 = llvm.load %19 : !llvm.ptr -> f64
      %61 = arith.constant 2.0 : f32
      %62 = llvm.load %23 : !llvm.ptr -> i64
      %63 = arith.sitofp %62 : i64 to f64
      %65 = arith.extf %61 : f32 to f64
      %64 = arith.mulf %65, %63 : f64
      %66 = arith.constant 1.0 : f32
      %68 = arith.extf %66 : f32 to f64
      %67 = arith.subf %64, %68 : f64
      %69 = arith.constant 2.0 : f32
      %70 = llvm.load %23 : !llvm.ptr -> i64
      %71 = arith.sitofp %70 : i64 to f64
      %73 = arith.extf %69 : f32 to f64
      %72 = arith.mulf %73, %71 : f64
      %74 = arith.divf %67, %72 : f64
      %75 = arith.mulf %60, %74 : f64
      llvm.store %75, %19 : f64, !llvm.ptr
      %76 = llvm.load %23 : !llvm.ptr -> i64
      %77 = arith.constant 1 : i32
      %79 = arith.extsi %77 : i32 to i64
      %78 = arith.addi %76, %79 : i64
      llvm.store %78, %23 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %80 = arith.constant 0.5 : f32
    %81 = arith.extf %80 : f32 to f64
    %82 = llvm.mlir.constant(1 : i64) : i64
    %83 = llvm.alloca %82 x f64 : (i64) -> !llvm.ptr
    llvm.store %81, %83 : f64, !llvm.ptr
    %84 = arith.constant 1 : i32
    %85 = arith.extsi %84 : i32 to i64
    %86 = llvm.mlir.constant(1 : i64) : i64
    %87 = llvm.alloca %86 x i64 : (i64) -> !llvm.ptr
    llvm.store %85, %87 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %88 = llvm.load %87 : !llvm.ptr -> i64
    %89 = arith.cmpi slt, %88, %1 : i64
    cf.cond_br %89, ^bb7, ^bb8
    ^bb7:
      %90 = llvm.load %83 : !llvm.ptr -> f64
      %91 = arith.constant 2.0 : f32
      %92 = llvm.load %87 : !llvm.ptr -> i64
      %93 = arith.sitofp %92 : i64 to f64
      %95 = arith.extf %91 : f32 to f64
      %94 = arith.mulf %95, %93 : f64
      %96 = arith.constant 1.0 : f32
      %98 = arith.extf %96 : f32 to f64
      %97 = arith.addf %94, %98 : f64
      %99 = arith.constant 2.0 : f32
      %100 = llvm.load %87 : !llvm.ptr -> i64
      %101 = arith.sitofp %100 : i64 to f64
      %103 = arith.extf %99 : f32 to f64
      %102 = arith.mulf %103, %101 : f64
      %104 = arith.constant 2.0 : f32
      %106 = arith.extf %104 : f32 to f64
      %105 = arith.addf %102, %106 : f64
      %107 = arith.divf %97, %105 : f64
      %108 = arith.mulf %90, %107 : f64
      llvm.store %108, %83 : f64, !llvm.ptr
      %109 = llvm.load %87 : !llvm.ptr -> i64
      %110 = arith.constant 1 : i32
      %112 = arith.extsi %110 : i32 to i64
      %111 = arith.addi %109, %112 : i64
      llvm.store %111, %87 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %113 = arith.constant 2.0 : f32
    %114 = llvm.load %83 : !llvm.ptr -> f64
    %116 = arith.extf %113 : f32 to f64
    %115 = arith.mulf %116, %114 : f64
    %117 = arith.constant 2 : i32
    %119 = arith.extsi %117 : i32 to i64
    %118 = arith.muli %119, %1 : i64
    %120 = arith.sitofp %118 : i64 to f64
    %122 = llvm.getelementptr %2[%1] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %121 = llvm.load %122 : !llvm.ptr -> f64
    %124 = llvm.getelementptr %2[%1] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %123 = llvm.load %124 : !llvm.ptr -> f64
    %125 = arith.subf %120, %123 : f64
    %126 = arith.mulf %125, %115 : f64
    %127 = arith.addf %121, %126 : f64
    %128 = arith.constant 0.5 : f32
    %130 = arith.constant 1 : i32
    %132 = arith.extsi %130 : i32 to i64
    %131 = arith.subi %1, %132 : i64
    %133 = llvm.getelementptr %2[%131] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %129 = llvm.load %133 : !llvm.ptr -> f64
    %134 = arith.addf %129, %127 : f64
    %136 = arith.extf %128 : f32 to f64
    %135 = arith.mulf %136, %134 : f64
    %137 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %138 = llvm.call @printf(%137, %135) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    %140 = arith.constant 0 : i32
    func.return %140 : i32
  }
}