Problem 959

Asymmetric Random Walk: f(89, 97). f(a,b) = 1/G where G = sum_{k>=0} C((a+b)k, ak) / 2^{(a+b)k}, computed in log space via lgamma.

Answer0.857162085
Output0.857162085
StatusPASS
Native helperno
Runtime0 ms
Peak memory1088 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)?
Space complexityO(1)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 959
# Asymmetric Random Walk: f(89, 97).
# f(a,b) = 1/G where G = sum_{k>=0} C((a+b)k, ak) / 2^{(a+b)k},
# computed in log space via lgamma.

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

function gcd_l(x: i64, y: i64) -> i64 {
    let mut a: i64 = x
    let mut b: i64 = y
    while b != 0 {
        let t: i64 = a % b
        a = b
        b = t
    }
    if a < 0 {
        return -a
    }
    return a
}

function f_ab(a0: i64, b0: i64) -> f64 {
    let g: i64 = gcd_l(a0, b0)
    let mut a: i64 = a0 / g
    let mut b: i64 = b0 / g

    if a == b {
        return 0.0
    }
    if a > b {
        let t: i64 = a
        a = b
        b = t
    }

    let m: i64 = a + b
    let log2: f64 = log(2.0)

    let mut S: f64 = 0.0
    let mut k: i64 = 0
    while true {
        let mk: i64 = m * k
        let ak: i64 = a * k
        let bk: i64 = b * k

        let log_t: f64 = lgamma((mk + 1) as f64)
                     - lgamma((ak + 1) as f64)
                     - lgamma((bk + 1) as f64)
                     - (mk as f64) * log2

        if log_t < -700.0 {
            break
        }

        let t: f64 = exp(log_t)
        S = S + t

        if t < 1e-17 * S {
            break
        }
        k = k + 1
        if k > 200000 {
            break
        }
    }

    return 1.0 / S
}

function main() -> i32 {
    printf("%.9f\n", f_ab(89, 97))
    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);
int64_t gcd_l_i64_i64(int64_t x, int64_t y);
double f_ab_i64_i64(int64_t a0, int64_t b0);
int32_t main(void);




int64_t gcd_l_i64_i64(int64_t x, int64_t y) {
    int64_t a = x;
    int64_t b = y;
    while (b != 0) {
        int64_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
    }
    if (a < 0) {
        return (-a);
    }
    return a;
}

double f_ab_i64_i64(int64_t a0, int64_t b0) {
    int64_t g = gcd_l_i64_i64(a0, b0);
    int64_t a = FLOW_CHECKED_DIV((a0), (g));
    int64_t b = FLOW_CHECKED_DIV((b0), (g));
    if (a == b) {
        return 0.0;
    }
    if (a > b) {
        int64_t t = a;
        a = b;
        b = t;
    }
    int64_t m = (a + b);
    double log2 = log(2.0);
    double S = 0.0;
    int64_t k = 0;
    while (1) {
        int64_t mk = (m * k);
        int64_t ak = (a * k);
        int64_t bk = (b * k);
        double log_t = (((lgamma(((double)((mk + 1)))) - lgamma(((double)((ak + 1))))) - lgamma(((double)((bk + 1))))) - (((double)(mk)) * log2));
        if (log_t < (-700.0)) {
            break;
        }
        double t = exp(log_t);
        S = (S + t);
        if (t < (1e-17 * S)) {
            break;
        }
        k = (k + 1);
        if (k > 200000) {
            break;
        }
    }
    return (1.0 / S);
}

int32_t main(void) {
    printf("%.9f\n", f_ab_i64_i64(89, 97));
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.9f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @lgamma(f64) -> f64
  func.func private @log(f64) -> f64
  func.func private @exp(f64) -> f64
  func.func @gcd_l(%arg0: i64, %arg1: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %1 : i64, !llvm.ptr
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %3 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %4 = llvm.load %3 : !llvm.ptr -> i64
    %5 = arith.constant 0 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.cmpi ne, %4, %7 : i64
    cf.cond_br %6, ^bb1, ^bb2
    ^bb1:
      %8 = llvm.load %1 : !llvm.ptr -> i64
      %9 = llvm.load %3 : !llvm.ptr -> i64
      %10 = arith.remsi %8, %9 : i64
      %11 = llvm.load %3 : !llvm.ptr -> i64
      llvm.store %11, %1 : i64, !llvm.ptr
      llvm.store %10, %3 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %12 = llvm.load %1 : !llvm.ptr -> i64
    %13 = arith.constant 0 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.cmpi slt, %12, %15 : i64
    cf.cond_br %14, ^bb3, ^bb4
    ^bb3:
      %16 = llvm.load %1 : !llvm.ptr -> i64
      %18 = arith.constant 0 : i64
      %17 = arith.subi %18, %16 : i64
      func.return %17 : i64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %19 = llvm.load %1 : !llvm.ptr -> i64
    func.return %19 : i64
  }
  func.func @f_ab(%arg0: i64, %arg1: i64) -> f64 {
    %20 = func.call @gcd_l(%arg0, %arg1) : (i64, i64) -> i64
    %21 = arith.divsi %arg0, %20 : i64
    %22 = llvm.mlir.constant(1 : i64) : i64
    %23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
    llvm.store %21, %23 : i64, !llvm.ptr
    %24 = arith.divsi %arg1, %20 : i64
    %25 = llvm.mlir.constant(1 : i64) : i64
    %26 = llvm.alloca %25 x i64 : (i64) -> !llvm.ptr
    llvm.store %24, %26 : i64, !llvm.ptr
    %27 = llvm.load %23 : !llvm.ptr -> i64
    %28 = llvm.load %26 : !llvm.ptr -> i64
    %29 = arith.cmpi eq, %27, %28 : i64
    cf.cond_br %29, ^bb6, ^bb7
    ^bb6:
      %30 = arith.constant 0.0 : f32
      %31 = arith.extf %30 : f32 to f64
      func.return %31 : f64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %32 = llvm.load %23 : !llvm.ptr -> i64
    %33 = llvm.load %26 : !llvm.ptr -> i64
    %34 = arith.cmpi sgt, %32, %33 : i64
    cf.cond_br %34, ^bb9, ^bb10
    ^bb9:
      %35 = llvm.load %23 : !llvm.ptr -> i64
      %36 = llvm.load %26 : !llvm.ptr -> i64
      llvm.store %36, %23 : i64, !llvm.ptr
      llvm.store %35, %26 : i64, !llvm.ptr
      cf.br ^bb11
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %37 = llvm.load %23 : !llvm.ptr -> i64
    %38 = llvm.load %26 : !llvm.ptr -> i64
    %39 = arith.addi %37, %38 : i64
    %40 = arith.constant 2.0 : f32
    %41 = math.log %40 : f32
    %42 = arith.extf %41 : f32 to f64
    %43 = arith.constant 0.0 : f32
    %44 = arith.extf %43 : f32 to f64
    %45 = llvm.mlir.constant(1 : i64) : i64
    %46 = llvm.alloca %45 x f64 : (i64) -> !llvm.ptr
    llvm.store %44, %46 : f64, !llvm.ptr
    %47 = arith.constant 0 : i32
    %48 = arith.extsi %47 : i32 to i64
    %49 = llvm.mlir.constant(1 : i64) : i64
    %50 = llvm.alloca %49 x i64 : (i64) -> !llvm.ptr
    llvm.store %48, %50 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %51 = arith.constant 1 : i1
    cf.cond_br %51, ^bb13, ^bb14
    ^bb13:
      %52 = llvm.load %50 : !llvm.ptr -> i64
      %53 = arith.muli %39, %52 : i64
      %54 = llvm.load %23 : !llvm.ptr -> i64
      %55 = llvm.load %50 : !llvm.ptr -> i64
      %56 = arith.muli %54, %55 : i64
      %57 = llvm.load %26 : !llvm.ptr -> i64
      %58 = llvm.load %50 : !llvm.ptr -> i64
      %59 = arith.muli %57, %58 : i64
      %61 = arith.constant 1 : i32
      %63 = arith.extsi %61 : i32 to i64
      %62 = arith.addi %53, %63 : i64
      %64 = arith.sitofp %62 : i64 to f64
      %60 = func.call @lgamma(%64) : (f64) -> f64
      %66 = arith.constant 1 : i32
      %68 = arith.extsi %66 : i32 to i64
      %67 = arith.addi %56, %68 : i64
      %69 = arith.sitofp %67 : i64 to f64
      %65 = func.call @lgamma(%69) : (f64) -> f64
      %70 = arith.subf %60, %65 : f64
      %72 = arith.constant 1 : i32
      %74 = arith.extsi %72 : i32 to i64
      %73 = arith.addi %59, %74 : i64
      %75 = arith.sitofp %73 : i64 to f64
      %71 = func.call @lgamma(%75) : (f64) -> f64
      %76 = arith.subf %70, %71 : f64
      %77 = arith.sitofp %53 : i64 to f64
      %78 = arith.mulf %77, %42 : f64
      %79 = arith.subf %76, %78 : f64
      %80 = arith.constant 700.0 : f32
      %81 = arith.negf %80 : f32
      %83 = arith.extf %81 : f32 to f64
      %82 = arith.cmpf olt, %79, %83 : f64
      cf.cond_br %82, ^bb15, ^bb16
      ^bb15:
        cf.br ^bb14
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %84 = math.exp %79 : f64
      %85 = llvm.load %46 : !llvm.ptr -> f64
      %86 = arith.addf %85, %84 : f64
      llvm.store %86, %46 : f64, !llvm.ptr
      %87 = arith.constant 0 : f32
      %88 = llvm.load %46 : !llvm.ptr -> f64
      %90 = arith.extf %87 : f32 to f64
      %89 = arith.mulf %90, %88 : f64
      %91 = arith.cmpf olt, %84, %89 : f64
      cf.cond_br %91, ^bb18, ^bb19
      ^bb18:
        cf.br ^bb14
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %92 = llvm.load %50 : !llvm.ptr -> i64
      %93 = arith.constant 1 : i32
      %95 = arith.extsi %93 : i32 to i64
      %94 = arith.addi %92, %95 : i64
      llvm.store %94, %50 : i64, !llvm.ptr
      %96 = llvm.load %50 : !llvm.ptr -> i64
      %97 = arith.constant 200000 : i32
      %99 = arith.extsi %97 : i32 to i64
      %98 = arith.cmpi sgt, %96, %99 : i64
      cf.cond_br %98, ^bb21, ^bb22
      ^bb21:
        cf.br ^bb14
      ^bb22:
        cf.br ^bb23
      ^bb23:
      cf.br ^bb12
    ^bb14:
    %100 = arith.constant 1.0 : f32
    %101 = llvm.load %46 : !llvm.ptr -> f64
    %103 = arith.extf %100 : f32 to f64
    %102 = arith.divf %103, %101 : f64
    func.return %102 : f64
  }
  func.func @main() -> i32 {
    %104 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %106 = arith.constant 89 : i32
    %107 = arith.constant 97 : i32
    %108 = arith.extsi %106 : i32 to i64
    %109 = arith.extsi %107 : i32 to i64
    %105 = func.call @f_ab(%108, %109) : (i64, i64) -> f64
    %110 = llvm.call @printf(%104, %105) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %111 = arith.constant 0 : i32
    func.return %111 : i32
  }
}