Problem 901

Well Drilling — optimal increasing depths via admissibility boundary. E[T] = d1 + 1 + sum exp(-d_n) under d_{n+1} = exp(d_n - d_{n-1}).

Answer2.364497769
Output2.364497769
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 901
# Well Drilling — optimal increasing depths via admissibility boundary.
# E[T] = d1 + 1 + sum exp(-d_n) under d_{n+1} = exp(d_n - d_{n-1}).

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

function is_admissible(first: f64) -> i32 {
    let mut prev: f64 = 0.0
    let mut cur: f64 = first
    let mut step: i32 = 0
    while step < 200 {
        let gap: f64 = cur - prev
        if gap <= 0.0 { return 0 }
        if cur > 80.0 || gap > 80.0 { return 1 }
        prev = cur
        cur = exp(gap)
        step = step + 1
    }
    return 1
}

function expected_cost(first: f64) -> f64 {
    let mut prev: f64 = 0.0
    let mut cur: f64 = first
    let mut total: f64 = first + 1.0
    let mut step: i32 = 0
    while step < 200 {
        total = total + exp(0.0 - cur)
        if cur > 80.0 { break }
        let nxt: f64 = exp(cur - prev)
        prev = cur
        cur = nxt
        step = step + 1
    }
    return total
}

function upper_branch_boundary() -> f64 {
    let mut prev_x: f64 = 0.6
    let mut prev_ok: i32 = is_admissible(prev_x)
    let mut x: f64 = 0.61
    let mut low: f64 = 0.0
    let mut high: f64 = 0.0
    let mut found: i32 = 0
    while x <= 1.5 {
        let ok: i32 = is_admissible(x)
        if prev_ok == 0 && ok == 1 {
            low = prev_x
            high = x
            found = 1
            break
        }
        prev_x = x
        prev_ok = ok
        x = x + 0.01
    }
    if found == 0 { return 0.0 }
    let mut i: i32 = 0
    while i < 90 {
        let mid: f64 = (low + high) * 0.5
        if is_admissible(mid) == 1 {
            high = mid
        } else {
            low = mid
        }
        i = i + 1
    }
    return high
}

function main() -> i32 {
    printf("%.9f\n", expected_cost(upper_branch_boundary()))
    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 is_admissible_f64(double first);
double expected_cost_f64(double first);
double upper_branch_boundary(void);
int32_t main(void);


int32_t is_admissible_f64(double first) {
    double prev = 0.0;
    double cur = first;
    int32_t step = 0;
    while (step < 200) {
        double gap = (cur - prev);
        if (gap <= 0.0) {
            return 0;
        }
        if ((cur > 80.0 || gap > 80.0)) {
            return 1;
        }
        prev = cur;
        cur = exp(gap);
        step = (step + 1);
    }
    return 1;
}

double expected_cost_f64(double first) {
    double prev = 0.0;
    double cur = first;
    double total = (first + 1.0);
    int32_t step = 0;
    while (step < 200) {
        total = (total + exp((0.0 - cur)));
        if (cur > 80.0) {
            break;
        }
        double nxt = exp((cur - prev));
        prev = cur;
        cur = nxt;
        step = (step + 1);
    }
    return total;
}

double upper_branch_boundary(void) {
    double prev_x = 0.6;
    int32_t prev_ok = is_admissible_f64(prev_x);
    double x = 0.61;
    double low = 0.0;
    double high = 0.0;
    int32_t found = 0;
    while (x <= 1.5) {
        int32_t ok = is_admissible_f64(x);
        if ((prev_ok == 0 && ok == 1)) {
            low = prev_x;
            high = x;
            found = 1;
            break;
        }
        prev_x = x;
        prev_ok = ok;
        x = (x + 0.01);
    }
    if (found == 0) {
        return 0.0;
    }
    int32_t i = 0;
    while (i < 90) {
        double mid = ((low + high) * 0.5);
        if (is_admissible_f64(mid) == 1) {
            high = mid;
        } else {
            low = mid;
        }
        i = (i + 1);
    }
    return high;
}

int32_t main(void) {
    printf("%.9f\n", expected_cost_f64(upper_branch_boundary()));
    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 @exp(f64) -> f64
  func.func @is_admissible(%arg0: f64) -> i32 {
    %0 = arith.constant 0.0 : f32
    %1 = arith.extf %0 : f32 to f64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x f64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : f64, !llvm.ptr
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x f64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %5 : f64, !llvm.ptr
    %6 = arith.constant 0 : i32
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i32 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %9 = llvm.load %8 : !llvm.ptr -> i32
    %10 = arith.constant 200 : i32
    %11 = arith.cmpi slt, %9, %10 : i32
    cf.cond_br %11, ^bb1, ^bb2
    ^bb1:
      %12 = llvm.load %5 : !llvm.ptr -> f64
      %13 = llvm.load %3 : !llvm.ptr -> f64
      %14 = arith.subf %12, %13 : f64
      %15 = arith.constant 0.0 : f32
      %17 = arith.extf %15 : f32 to f64
      %16 = arith.cmpf ole, %14, %17 : f64
      cf.cond_br %16, ^bb3, ^bb4
      ^bb3:
        %18 = arith.constant 0 : i32
        func.return %18 : i32
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %19 = llvm.load %5 : !llvm.ptr -> f64
      %20 = arith.constant 80.0 : f32
      %22 = arith.extf %20 : f32 to f64
      %21 = arith.cmpf ogt, %19, %22 : f64
      %23 = scf.if %21 -> (i1) {
        %24 = arith.constant true
        scf.yield %24 : i1
      } else {
        %25 = arith.constant 80.0 : f32
        %27 = arith.extf %25 : f32 to f64
        %26 = arith.cmpf ogt, %14, %27 : f64
        scf.yield %26 : i1
      }
      cf.cond_br %23, ^bb6, ^bb7
      ^bb6:
        %28 = arith.constant 1 : i32
        func.return %28 : i32
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %29 = llvm.load %5 : !llvm.ptr -> f64
      llvm.store %29, %3 : f64, !llvm.ptr
      %30 = math.exp %14 : f64
      llvm.store %30, %5 : f64, !llvm.ptr
      %31 = llvm.load %8 : !llvm.ptr -> i32
      %32 = arith.constant 1 : i32
      %33 = arith.addi %31, %32 : i32
      llvm.store %33, %8 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %34 = arith.constant 1 : i32
    func.return %34 : i32
  }
  func.func @expected_cost(%arg0: f64) -> f64 {
    %35 = arith.constant 0.0 : f32
    %36 = arith.extf %35 : f32 to f64
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x f64 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : f64, !llvm.ptr
    %39 = llvm.mlir.constant(1 : i64) : i64
    %40 = llvm.alloca %39 x f64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %40 : f64, !llvm.ptr
    %41 = arith.constant 1.0 : f32
    %43 = arith.extf %41 : f32 to f64
    %42 = arith.addf %arg0, %43 : f64
    %44 = llvm.mlir.constant(1 : i64) : i64
    %45 = llvm.alloca %44 x f64 : (i64) -> !llvm.ptr
    llvm.store %42, %45 : f64, !llvm.ptr
    %46 = arith.constant 0 : i32
    %47 = llvm.mlir.constant(1 : i64) : i64
    %48 = llvm.alloca %47 x i32 : (i64) -> !llvm.ptr
    llvm.store %46, %48 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %49 = llvm.load %48 : !llvm.ptr -> i32
    %50 = arith.constant 200 : i32
    %51 = arith.cmpi slt, %49, %50 : i32
    cf.cond_br %51, ^bb10, ^bb11
    ^bb10:
      %52 = llvm.load %45 : !llvm.ptr -> f64
      %53 = arith.constant 0.0 : f32
      %54 = llvm.load %40 : !llvm.ptr -> f64
      %56 = arith.extf %53 : f32 to f64
      %55 = arith.subf %56, %54 : f64
      %57 = math.exp %55 : f64
      %58 = arith.addf %52, %57 : f64
      llvm.store %58, %45 : f64, !llvm.ptr
      %59 = llvm.load %40 : !llvm.ptr -> f64
      %60 = arith.constant 80.0 : f32
      %62 = arith.extf %60 : f32 to f64
      %61 = arith.cmpf ogt, %59, %62 : f64
      cf.cond_br %61, ^bb12, ^bb13
      ^bb12:
        cf.br ^bb11
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %63 = llvm.load %40 : !llvm.ptr -> f64
      %64 = llvm.load %38 : !llvm.ptr -> f64
      %65 = arith.subf %63, %64 : f64
      %66 = math.exp %65 : f64
      %67 = llvm.load %40 : !llvm.ptr -> f64
      llvm.store %67, %38 : f64, !llvm.ptr
      llvm.store %66, %40 : f64, !llvm.ptr
      %68 = llvm.load %48 : !llvm.ptr -> i32
      %69 = arith.constant 1 : i32
      %70 = arith.addi %68, %69 : i32
      llvm.store %70, %48 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %71 = llvm.load %45 : !llvm.ptr -> f64
    func.return %71 : f64
  }
  func.func @upper_branch_boundary() -> f64 {
    %72 = arith.constant 0.6 : f32
    %73 = arith.extf %72 : f32 to f64
    %74 = llvm.mlir.constant(1 : i64) : i64
    %75 = llvm.alloca %74 x f64 : (i64) -> !llvm.ptr
    llvm.store %73, %75 : f64, !llvm.ptr
    %77 = llvm.load %75 : !llvm.ptr -> f64
    %76 = func.call @is_admissible(%77) : (f64) -> i32
    %78 = llvm.mlir.constant(1 : i64) : i64
    %79 = llvm.alloca %78 x i32 : (i64) -> !llvm.ptr
    llvm.store %76, %79 : i32, !llvm.ptr
    %80 = arith.constant 0.61 : 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 0.0 : f32
    %85 = arith.extf %84 : f32 to f64
    %86 = llvm.mlir.constant(1 : i64) : i64
    %87 = llvm.alloca %86 x f64 : (i64) -> !llvm.ptr
    llvm.store %85, %87 : f64, !llvm.ptr
    %88 = arith.constant 0.0 : f32
    %89 = arith.extf %88 : f32 to f64
    %90 = llvm.mlir.constant(1 : i64) : i64
    %91 = llvm.alloca %90 x f64 : (i64) -> !llvm.ptr
    llvm.store %89, %91 : f64, !llvm.ptr
    %92 = arith.constant 0 : i32
    %93 = llvm.mlir.constant(1 : i64) : i64
    %94 = llvm.alloca %93 x i32 : (i64) -> !llvm.ptr
    llvm.store %92, %94 : i32, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %95 = llvm.load %83 : !llvm.ptr -> f64
    %96 = arith.constant 1.5 : f32
    %98 = arith.extf %96 : f32 to f64
    %97 = arith.cmpf ole, %95, %98 : f64
    cf.cond_br %97, ^bb16, ^bb17
    ^bb16:
      %100 = llvm.load %83 : !llvm.ptr -> f64
      %99 = func.call @is_admissible(%100) : (f64) -> i32
      %101 = llvm.load %79 : !llvm.ptr -> i32
      %102 = arith.constant 0 : i32
      %103 = arith.cmpi eq, %101, %102 : i32
      %104 = scf.if %103 -> (i1) {
        %105 = arith.constant 1 : i32
        %106 = arith.cmpi eq, %99, %105 : i32
        scf.yield %106 : i1
      } else {
        %107 = arith.constant false
        scf.yield %107 : i1
      }
      cf.cond_br %104, ^bb18, ^bb19
      ^bb18:
        %108 = llvm.load %75 : !llvm.ptr -> f64
        llvm.store %108, %87 : f64, !llvm.ptr
        %109 = llvm.load %83 : !llvm.ptr -> f64
        llvm.store %109, %91 : f64, !llvm.ptr
        %110 = arith.constant 1 : i32
        llvm.store %110, %94 : i32, !llvm.ptr
        cf.br ^bb17
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %111 = llvm.load %83 : !llvm.ptr -> f64
      llvm.store %111, %75 : f64, !llvm.ptr
      llvm.store %99, %79 : i32, !llvm.ptr
      %112 = llvm.load %83 : !llvm.ptr -> f64
      %113 = arith.constant 0.01 : f32
      %115 = arith.extf %113 : f32 to f64
      %114 = arith.addf %112, %115 : f64
      llvm.store %114, %83 : f64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %116 = llvm.load %94 : !llvm.ptr -> i32
    %117 = arith.constant 0 : i32
    %118 = arith.cmpi eq, %116, %117 : i32
    cf.cond_br %118, ^bb21, ^bb22
    ^bb21:
      %119 = arith.constant 0.0 : f32
      %120 = arith.extf %119 : f32 to f64
      func.return %120 : f64
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %121 = arith.constant 0 : i32
    %122 = llvm.mlir.constant(1 : i64) : i64
    %123 = llvm.alloca %122 x i32 : (i64) -> !llvm.ptr
    llvm.store %121, %123 : i32, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %124 = llvm.load %123 : !llvm.ptr -> i32
    %125 = arith.constant 90 : i32
    %126 = arith.cmpi slt, %124, %125 : i32
    cf.cond_br %126, ^bb25, ^bb26
    ^bb25:
      %127 = llvm.load %87 : !llvm.ptr -> f64
      %128 = llvm.load %91 : !llvm.ptr -> f64
      %129 = arith.addf %127, %128 : f64
      %130 = arith.constant 0.5 : f32
      %132 = arith.extf %130 : f32 to f64
      %131 = arith.mulf %129, %132 : f64
      %133 = func.call @is_admissible(%131) : (f64) -> i32
      %134 = arith.constant 1 : i32
      %135 = arith.cmpi eq, %133, %134 : i32
      cf.cond_br %135, ^bb27, ^bb28
      ^bb27:
        llvm.store %131, %91 : f64, !llvm.ptr
        cf.br ^bb29
      ^bb28:
        llvm.store %131, %87 : f64, !llvm.ptr
        cf.br ^bb29
      ^bb29:
      %136 = llvm.load %123 : !llvm.ptr -> i32
      %137 = arith.constant 1 : i32
      %138 = arith.addi %136, %137 : i32
      llvm.store %138, %123 : i32, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %139 = llvm.load %91 : !llvm.ptr -> f64
    func.return %139 : f64
  }
  func.func @main() -> i32 {
    %140 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %142 = func.call @upper_branch_boundary() : () -> f64
    %141 = func.call @expected_cost(%142) : (f64) -> f64
    %143 = llvm.call @printf(%140, %141) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %144 = arith.constant 0 : i32
    func.return %144 : i32
  }
}