Problem 770

Delphi Flip: g(1.9999), the smallest n where A guarantees at least 1.9999 g. F(n) = 2 / (1 + p_n), where p_n = C(2n,n) / 4^n. Condition F(n) >= X is equivalent to p_n <= (2 - X) / X. Uses lgamma for small n and Stirling series for huge n.

Answer127311223
Output127311223
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log n)
Space complexityO(1)O(n)
ApproachFlow solutionSearch with pruning or sieve
VerdictOptimal

Flow source

# Project Euler 770
# Delphi Flip: g(1.9999), the smallest n where A guarantees at least 1.9999 g.
# F(n) = 2 / (1 + p_n), where p_n = C(2n,n) / 4^n.
# Condition F(n) >= X is equivalent to p_n <= (2 - X) / X.
# Uses lgamma for small n and Stirling series for huge n.

extern {
    function log(x: f64) -> f64
    function lgamma(x: f64) -> f64
}

const PI: f64 = 3.14159265358979323846

# ln p_n via Stirling expansion for huge n:
# ln p_n = -1/2 ln(pi n) - 1/(8n) + 1/(192 n^3) - 1/(640 n^5)
function ln_p_stirling(n: f64) -> f64 {
    let inv: f64 = 1.0 / n
    let inv2: f64 = inv * inv
    let inv3: f64 = inv2 * inv
    let inv5: f64 = inv3 * inv2
    return -0.5 * log(PI * n) - 0.125 * inv + (1.0 / 192.0) * inv3 - (1.0 / 640.0) * inv5
}

# ln(C(2n,n)/4^n) via lgamma. Fine for small n where no cancellation occurs.
function ln_p_lgamma(n: i64) -> f64 {
    let nf: f64 = n as f64
    return lgamma(2.0 * nf + 1.0) - 2.0 * lgamma(nf + 1.0) - 2.0 * nf * log(2.0)
}

function g_for_fraction(x_num: i64, x_den: i64) -> i64 {
    # r = (2 - X) / X = (2*x_den - x_num) / x_num
    let r_num: i64 = 2 * x_den - x_num
    let r_den: i64 = x_num

    let r: f64 = (r_num as f64) / (r_den as f64)
    let ln_r: f64 = log(r_num as f64) - log(r_den as f64)

    # p_n ~ 1/sqrt(pi n)  =>  n ~ 1/(pi r^2)
    let n_est: i64 = (1.0 / (PI * r * r)) as i64

    if n_est < 20000 {
        let mut n: i64 = 0
        while ln_p_lgamma(n) > ln_r {
            n = n + 1
        }
        return n
    }

    # Huge n: walk in log-space using the Stirling expansion.
    let mut n: i64 = n_est - 10
    if n < 1 { n = 1 }
    while ln_p_stirling(n as f64) > ln_r {
        n = n + 1
    }
    while n > 1 && ln_p_stirling((n - 1) as f64) <= ln_r {
        n = n - 1
    }
    return n
}

function main() -> i32 {
    printf("%lld\n", g_for_fraction(19999, 10000))
    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 lgamma(double x);
double ln_p_stirling_f64(double n);
double ln_p_lgamma_i64(int64_t n);
int64_t g_for_fraction_i64_i64(int64_t x_num, int64_t x_den);
int32_t main(void);

static const double PI = 3.14159265358979323846;



double ln_p_stirling_f64(double n) {
    double inv = (1.0 / n);
    double inv2 = (inv * inv);
    double inv3 = (inv2 * inv);
    double inv5 = (inv3 * inv2);
    return (((((-0.5) * log((PI * n))) - (0.125 * inv)) + ((1.0 / 192.0) * inv3)) - ((1.0 / 640.0) * inv5));
}

double ln_p_lgamma_i64(int64_t n) {
    double nf = ((double)(n));
    return ((lgamma(((2.0 * nf) + 1.0)) - (2.0 * lgamma((nf + 1.0)))) - ((2.0 * nf) * log(2.0)));
}

int64_t g_for_fraction_i64_i64(int64_t x_num, int64_t x_den) {
    int64_t r_num = ((2 * x_den) - x_num);
    int64_t r_den = x_num;
    double r = (((double)(r_num)) / ((double)(r_den)));
    double ln_r = (log(((double)(r_num))) - log(((double)(r_den))));
    int64_t n_est = ((int64_t)((1.0 / ((PI * r) * r))));
    if (n_est < 20000) {
        int64_t n = 0;
        while (ln_p_lgamma_i64(n) > ln_r) {
            n = (n + 1);
        }
        return n;
    }
    int64_t n = (n_est - 10);
    if (n < 1) {
        n = 1;
    }
    while (ln_p_stirling_f64(((double)(n))) > ln_r) {
        n = (n + 1);
    }
    while ((n > 1 && ln_p_stirling_f64(((double)((n - 1)))) <= ln_r)) {
        n = (n - 1);
    }
    return n;
}

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