Problem 499

St. Petersburg Lottery — p_15(10^9) via negative root of mgf equation.

Answer0.8660312
Output0.8660312
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 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 499
# St. Petersburg Lottery — p_15(10^9) via negative root of mgf equation.

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

function expm1(x: f64) -> f64 {
    return exp(x) - 1.0
}

function f_expm1(t: f64, m: i32) -> f64 {
    let mut s: f64 = 0.0
    let mut c: f64 = 0.0
    let mut pow2: f64 = 1.0
    let mut weight: f64 = 0.5
    let mut k: i32 = 0
    while k < 200 {
        let term: f64 = weight * expm1(pow2 * t)
        let y: f64 = term - c
        let tmp: f64 = s + y
        c = (tmp - s) - y
        s = tmp
        if weight < 1.0e-27 { break }
        pow2 = pow2 * 2.0
        weight = weight * 0.5
        k = k + 1
    }
    return expm1((m as f64) * t) - s
}

function solve_t(m: i32) -> f64 {
    let mut hi: f64 = -1.0e-12
    let mut f_hi: f64 = f_expm1(hi, m)
    while f_hi <= 0.0 {
        hi = hi * 0.5
        f_hi = f_expm1(hi, m)
    }
    let mut lo: f64 = hi
    let mut f_lo: f64 = f_hi
    while f_lo > 0.0 {
        lo = lo * 2.0
        f_lo = f_expm1(lo, m)
    }
    let mut it: i32 = 0
    while it < 120 {
        let mid: f64 = (lo + hi) * 0.5
        let f_mid: f64 = f_expm1(mid, m)
        if f_mid > 0.0 {
            hi = mid
        } else {
            lo = mid
        }
        it = it + 1
    }
    return (lo + hi) * 0.5
}

function main() -> i32 {
    let m: i32 = 15
    let s: i64 = 1000000000
    let t: f64 = solve_t(m)
    let p: f64 = -expm1(t * ((s - (m as i64) + 1) as f64))
    printf("%.7f\n", p)
    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 expm1_f64(double x);
double f_expm1_f64_i32(double t, int32_t m);
double solve_t_i32(int32_t m);
int32_t main(void);


double expm1_f64(double x) {
    return (exp(x) - 1.0);
}

double f_expm1_f64_i32(double t, int32_t m) {
    double s = 0.0;
    double c = 0.0;
    double pow2 = 1.0;
    double weight = 0.5;
    int32_t k = 0;
    while (k < 200) {
        double term = (weight * expm1_f64((pow2 * t)));
        double y = (term - c);
        double tmp = (s + y);
        c = ((tmp - s) - y);
        s = tmp;
        if (weight < 1.0e-27) {
            break;
        }
        pow2 = (pow2 * 2.0);
        weight = (weight * 0.5);
        k = (k + 1);
    }
    return (expm1_f64((((double)(m)) * t)) - s);
}

double solve_t_i32(int32_t m) {
    double hi = (-1.0e-12);
    double f_hi = f_expm1_f64_i32(hi, m);
    while (f_hi <= 0.0) {
        hi = (hi * 0.5);
        f_hi = f_expm1_f64_i32(hi, m);
    }
    double lo = hi;
    double f_lo = f_hi;
    while (f_lo > 0.0) {
        lo = (lo * 2.0);
        f_lo = f_expm1_f64_i32(lo, m);
    }
    int32_t it = 0;
    while (it < 120) {
        double mid = ((lo + hi) * 0.5);
        double f_mid = f_expm1_f64_i32(mid, m);
        if (f_mid > 0.0) {
            hi = mid;
        } else {
            lo = mid;
        }
        it = (it + 1);
    }
    return ((lo + hi) * 0.5);
}

int32_t main(void) {
    int32_t m = 15;
    int64_t s = 1000000000;
    double t = solve_t_i32(m);
    double p = (-expm1_f64((t * ((double)(((s - ((int64_t)(m))) + 1))))));
    printf("%.7f\n", p);
    return 0;
}

Generated MLIR

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