Problem 906

A Collective Decision — Condorcet winner probability P(20000).

Answer0.0195868911
Output0.0195868911
StatusPASS
Native helperno
Runtime20 ms
Peak memory1152 KB
Time complexityO(1) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 906
# A Collective Decision — Condorcet winner probability P(20000).

function agreement_probability(n: i64) -> f64 {
    if n == 1 { return 1.0 }
    let N: i64 = n - 1
    let factor: f64 = (N + 1) as f64 / ((n * n) as f64)
    let mut s: f64 = 0.0
    let mut c: f64 = 0.0
    let mut a: i64 = 0
    while a <= N {
        let bmax: i64 = N - a
        let mut term: f64 = 1.0 / ((a + 1) as f64)
        let mut y: f64 = term - c
        let mut t: f64 = s + y
        c = (t - s) - y
        s = t
        let mut g: f64 = 1.0
        let mut num: f64 = (N - a) as f64
        let mut den: f64 = N as f64
        let mut b: i64 = 1
        while b <= bmax {
            g = g * (num / den)
            num = num - 1.0
            den = den - 1.0
            term = g / ((a + b + 1) as f64)
            y = term - c
            t = s + y
            c = (t - s) - y
            s = t
            let rem: i64 = bmax - b
            if rem > 0 && term * (rem as f64) < 1e-20 {
                break
            }
            b = b + 1
        }
        a = a + 1
    }
    return factor * s
}

function main() -> i32 {
    printf("%.10f\n", agreement_probability(20000))
    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 agreement_probability_i64(int64_t n);
int32_t main(void);

double agreement_probability_i64(int64_t n) {
    if (n == 1) {
        return 1.0;
    }
    int64_t N = (n - 1);
    double factor = (((double)((N + 1))) / ((double)((n * n))));
    double s = 0.0;
    double c = 0.0;
    int64_t a = 0;
    while (a <= N) {
        int64_t bmax = (N - a);
        double term = (1.0 / ((double)((a + 1))));
        double y = (term - c);
        double t = (s + y);
        c = ((t - s) - y);
        s = t;
        double g = 1.0;
        double num = ((double)((N - a)));
        double den = ((double)(N));
        int64_t b = 1;
        while (b <= bmax) {
            g = (g * (num / den));
            num = (num - 1.0);
            den = (den - 1.0);
            term = (g / ((double)(((a + b) + 1))));
            y = (term - c);
            t = (s + y);
            c = ((t - s) - y);
            s = t;
            int64_t rem = (bmax - b);
            if ((rem > 0 && (term * ((double)(rem))) < 1e-20)) {
                break;
            }
            b = (b + 1);
        }
        a = (a + 1);
    }
    return (factor * s);
}

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