Problem 527
Randomized Binary Search — R(10^10)-B(10^10) to 8 d.p.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | ? |
| Space complexity | O(n) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 527
# Randomized Binary Search — R(10^10)-B(10^10) to 8 d.p.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function log(x: f64) -> f64
}
const CAP: i64 = 1048576
const EULER_GAMMA: f64 = 0.5772156649015328606
function harmonic(n: i64) -> f64 {
if n <= 0 { return 0.0 }
if n < 2000000 {
let mut s: f64 = 0.0
let mut k: i64 = 1
while k <= n {
s = s + 1.0 / (k as f64)
k = k + 1
}
return s
}
let inv: f64 = 1.0 / (n as f64)
let inv2: f64 = inv * inv
let inv4: f64 = inv2 * inv2
let inv6: f64 = inv4 * inv2
let inv8: f64 = inv4 * inv4
return log(n as f64) + EULER_GAMMA + 0.5 * inv
- (1.0 / 12.0) * inv2
+ (1.0 / 120.0) * inv4
- (1.0 / 252.0) * inv6
+ (1.0 / 240.0) * inv8
}
function R(n: i64) -> f64 {
let hn: f64 = harmonic(n)
return 2.0 * ((n + 1) as f64) / (n as f64) * hn - 3.0
}
function hslot(key: i64, keys: ptr<i64>, used: ptr<i8>) -> i64 {
let mut h: i64 = key % CAP
if h < 0 { h = h + CAP }
while used[h] == 1 && keys[h] != key {
h = h + 1
if h == CAP { h = 0 }
}
return h
}
function B(n: i64, keys: ptr<i64>, vals: ptr<f64>, used: ptr<i8>) -> f64 {
if n <= 1 { return n as f64 }
let s: i64 = hslot(n, keys, used)
if used[s] == 1 { return vals[s] }
let g: i64 = (n + 1) / 2
let left: i64 = g - 1
let right: i64 = n - g
let val: f64 = 1.0
+ ((left as f64) / (n as f64)) * B(left, keys, vals, used)
+ ((right as f64) / (n as f64)) * B(right, keys, vals, used)
used[s] = 1
keys[s] = n
vals[s] = val
return val
}
function main() -> i32 {
let keys: ptr<i64> = calloc(CAP, 8)
let vals: ptr<f64> = calloc(CAP, 8)
let used: ptr<i8> = calloc(CAP, 1)
if keys == null || vals == null || used == null { return 1 }
let n: i64 = 10000000000
let ans: f64 = R(n) - B(n, keys, vals, used)
printf("%.8f\n", ans)
free(keys); free(vals); free(used)
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 harmonic_i64(int64_t n);
double R_i64(int64_t n);
int64_t hslot_i64_ptr_i64_ptr_i8(int64_t key, int64_t* keys, int8_t* used);
double B_i64_ptr_i64_ptr_f64_ptr_i8(int64_t n, int64_t* keys, double* vals, int8_t* used);
int32_t main(void);
static const int64_t CAP = 1048576;
static const double EULER_GAMMA = 0.5772156649015328606;
double harmonic_i64(int64_t n) {
if (n <= 0) {
return 0.0;
}
if (n < 2000000) {
double s = 0.0;
int64_t k = 1;
while (k <= n) {
s = (s + (1.0 / ((double)(k))));
k = (k + 1);
}
return s;
}
double inv = (1.0 / ((double)(n)));
double inv2 = (inv * inv);
double inv4 = (inv2 * inv2);
double inv6 = (inv4 * inv2);
double inv8 = (inv4 * inv4);
return ((((((log(((double)(n))) + EULER_GAMMA) + (0.5 * inv)) - ((1.0 / 12.0) * inv2)) + ((1.0 / 120.0) * inv4)) - ((1.0 / 252.0) * inv6)) + ((1.0 / 240.0) * inv8));
}
double R_i64(int64_t n) {
double hn = harmonic_i64(n);
return ((((2.0 * ((double)((n + 1)))) / ((double)(n))) * hn) - 3.0);
}
int64_t hslot_i64_ptr_i64_ptr_i8(int64_t key, int64_t* keys, int8_t* used) {
int64_t h = FLOW_CHECKED_MOD((key), (CAP));
if (h < 0) {
h = (h + CAP);
}
while ((used[h] == 1 && keys[h] != key)) {
h = (h + 1);
if (h == CAP) {
h = 0;
}
}
return h;
}
double B_i64_ptr_i64_ptr_f64_ptr_i8(int64_t n, int64_t* keys, double* vals, int8_t* used) {
if (n <= 1) {
return ((double)(n));
}
int64_t s = hslot_i64_ptr_i64_ptr_i8(n, keys, used);
if (used[s] == 1) {
return vals[s];
}
int64_t g = FLOW_CHECKED_DIV(((n + 1)), (2));
int64_t left = (g - 1);
int64_t right = (n - g);
double val = ((1.0 + ((((double)(left)) / ((double)(n))) * B_i64_ptr_i64_ptr_f64_ptr_i8(left, keys, vals, used))) + ((((double)(right)) / ((double)(n))) * B_i64_ptr_i64_ptr_f64_ptr_i8(right, keys, vals, used)));
used[s] = 1;
keys[s] = n;
vals[s] = val;
return val;
}
int32_t main(void) {
int64_t* keys = (int64_t*)(calloc(CAP, 8));
double* vals = (double*)(calloc(CAP, 8));
int8_t* used = (int8_t*)(calloc(CAP, 1));
if (((keys == NULL || vals == NULL) || used == NULL)) {
return 1;
}
int64_t n = 10000000000;
double ans = (R_i64(n) - B_i64_ptr_i64_ptr_f64_ptr_i8(n, keys, vals, used));
printf("%.8f\n", ans);
free(keys);
free(vals);
free(used);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.8f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func private @log(f64) -> f64
// Constant: CAP
llvm.mlir.global internal constant @CAP(1048576 : i64) : i64
// Constant: EULER_GAMMA
llvm.mlir.global internal constant @EULER_GAMMA(0.5772156649015328606 : f64) : f64
func.func @harmonic(%arg0: i64) -> f64 {
%0 = arith.constant 0 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi sle, %arg0, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%3 = arith.constant 0.0 : f32
%4 = arith.extf %3 : f32 to f64
func.return %4 : f64
^bb1:
cf.br ^bb2
^bb2:
%5 = arith.constant 2000000 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.cmpi slt, %arg0, %7 : i64
cf.cond_br %6, ^bb3, ^bb4
^bb3:
%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 : i32
%13 = arith.extsi %12 : i32 to i64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%16 = llvm.load %15 : !llvm.ptr -> i64
%17 = arith.cmpi sle, %16, %arg0 : i64
cf.cond_br %17, ^bb7, ^bb8
^bb7:
%18 = llvm.load %11 : !llvm.ptr -> f64
%19 = arith.constant 1.0 : f32
%20 = llvm.load %15 : !llvm.ptr -> i64
%21 = arith.sitofp %20 : i64 to f64
%23 = arith.extf %19 : f32 to f64
%22 = arith.divf %23, %21 : f64
%24 = arith.addf %18, %22 : f64
llvm.store %24, %11 : f64, !llvm.ptr
%25 = llvm.load %15 : !llvm.ptr -> i64
%26 = arith.constant 1 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.addi %25, %28 : i64
llvm.store %27, %15 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%29 = llvm.load %11 : !llvm.ptr -> f64
func.return %29 : f64
^bb4:
cf.br ^bb5
^bb5:
%30 = arith.constant 1.0 : f32
%31 = arith.sitofp %arg0 : i64 to f64
%33 = arith.extf %30 : f32 to f64
%32 = arith.divf %33, %31 : f64
%34 = arith.mulf %32, %32 : f64
%35 = arith.mulf %34, %34 : f64
%36 = arith.mulf %35, %34 : f64
%37 = arith.mulf %35, %35 : f64
%38 = arith.sitofp %arg0 : i64 to f64
%39 = math.log %38 : f64
%40 = llvm.mlir.addressof @EULER_GAMMA : !llvm.ptr
%41 = llvm.load %40 : !llvm.ptr -> f64
%42 = arith.addf %39, %41 : f64
%43 = arith.constant 0.5 : f32
%45 = arith.extf %43 : f32 to f64
%44 = arith.mulf %45, %32 : f64
%46 = arith.addf %42, %44 : f64
%47 = arith.constant 1.0 : f32
%48 = arith.constant 12.0 : f32
%49 = arith.divf %47, %48 : f32
%51 = arith.extf %49 : f32 to f64
%50 = arith.mulf %51, %34 : f64
%52 = arith.subf %46, %50 : f64
%53 = arith.constant 1.0 : f32
%54 = arith.constant 120.0 : f32
%55 = arith.divf %53, %54 : f32
%57 = arith.extf %55 : f32 to f64
%56 = arith.mulf %57, %35 : f64
%58 = arith.addf %52, %56 : f64
%59 = arith.constant 1.0 : f32
%60 = arith.constant 252.0 : f32
%61 = arith.divf %59, %60 : f32
%63 = arith.extf %61 : f32 to f64
%62 = arith.mulf %63, %36 : f64
%64 = arith.subf %58, %62 : f64
%65 = arith.constant 1.0 : f32
%66 = arith.constant 240.0 : f32
%67 = arith.divf %65, %66 : f32
%69 = arith.extf %67 : f32 to f64
%68 = arith.mulf %69, %37 : f64
%70 = arith.addf %64, %68 : f64
func.return %70 : f64
}
func.func @R(%arg0: i64) -> f64 {
%71 = func.call @harmonic(%arg0) : (i64) -> f64
%72 = arith.constant 2.0 : f32
%73 = arith.constant 1 : i32
%75 = arith.extsi %73 : i32 to i64
%74 = arith.addi %arg0, %75 : i64
%76 = arith.sitofp %74 : i64 to f64
%78 = arith.extf %72 : f32 to f64
%77 = arith.mulf %78, %76 : f64
%79 = arith.sitofp %arg0 : i64 to f64
%80 = arith.divf %77, %79 : f64
%81 = arith.mulf %80, %71 : f64
%82 = arith.constant 3.0 : f32
%84 = arith.extf %82 : f32 to f64
%83 = arith.subf %81, %84 : f64
func.return %83 : f64
}
func.func @hslot(%arg0: i64, %arg1: !llvm.ptr, %arg2: !llvm.ptr) -> i64 {
%85 = llvm.mlir.addressof @CAP : !llvm.ptr
%86 = llvm.load %85 : !llvm.ptr -> i64
%87 = arith.remsi %arg0, %86 : i64
%88 = llvm.mlir.constant(1 : i64) : i64
%89 = llvm.alloca %88 x i64 : (i64) -> !llvm.ptr
llvm.store %87, %89 : i64, !llvm.ptr
%90 = llvm.load %89 : !llvm.ptr -> i64
%91 = arith.constant 0 : i32
%93 = arith.extsi %91 : i32 to i64
%92 = arith.cmpi slt, %90, %93 : i64
cf.cond_br %92, ^bb9, ^bb10
^bb9:
%94 = llvm.load %89 : !llvm.ptr -> i64
%95 = llvm.mlir.addressof @CAP : !llvm.ptr
%96 = llvm.load %95 : !llvm.ptr -> i64
%97 = arith.addi %94, %96 : i64
llvm.store %97, %89 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
cf.br ^bb12
^bb12:
%99 = llvm.load %89 : !llvm.ptr -> i64
%100 = llvm.getelementptr %arg2[%99] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%98 = llvm.load %100 : !llvm.ptr -> i8
%101 = arith.constant 1 : i32
%103 = arith.extsi %98 : i8 to i32
%102 = arith.cmpi eq, %103, %101 : i32
%104 = scf.if %102 -> (i1) {
%106 = llvm.load %89 : !llvm.ptr -> i64
%107 = llvm.getelementptr %arg1[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%105 = llvm.load %107 : !llvm.ptr -> i64
%108 = arith.cmpi ne, %105, %arg0 : i64
scf.yield %108 : i1
} else {
%109 = arith.constant false
scf.yield %109 : i1
}
cf.cond_br %104, ^bb13, ^bb14
^bb13:
%110 = llvm.load %89 : !llvm.ptr -> i64
%111 = arith.constant 1 : i32
%113 = arith.extsi %111 : i32 to i64
%112 = arith.addi %110, %113 : i64
llvm.store %112, %89 : i64, !llvm.ptr
%114 = llvm.load %89 : !llvm.ptr -> i64
%115 = llvm.mlir.addressof @CAP : !llvm.ptr
%116 = llvm.load %115 : !llvm.ptr -> i64
%117 = arith.cmpi eq, %114, %116 : i64
cf.cond_br %117, ^bb15, ^bb16
^bb15:
%118 = arith.constant 0 : i32
%119 = arith.extsi %118 : i32 to i64
llvm.store %119, %89 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
cf.br ^bb12
^bb14:
%120 = llvm.load %89 : !llvm.ptr -> i64
func.return %120 : i64
}
func.func @B(%arg0: i64, %arg1: !llvm.ptr, %arg2: !llvm.ptr, %arg3: !llvm.ptr) -> f64 {
%121 = arith.constant 1 : i32
%123 = arith.extsi %121 : i32 to i64
%122 = arith.cmpi sle, %arg0, %123 : i64
cf.cond_br %122, ^bb18, ^bb19
^bb18:
%124 = arith.sitofp %arg0 : i64 to f64
func.return %124 : f64
^bb19:
cf.br ^bb20
^bb20:
%125 = func.call @hslot(%arg0, %arg1, %arg3) : (i64, !llvm.ptr, !llvm.ptr) -> i64
%127 = llvm.getelementptr %arg3[%125] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%126 = llvm.load %127 : !llvm.ptr -> i8
%128 = arith.constant 1 : i32
%130 = arith.extsi %126 : i8 to i32
%129 = arith.cmpi eq, %130, %128 : i32
cf.cond_br %129, ^bb21, ^bb22
^bb21:
%132 = llvm.getelementptr %arg2[%125] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%131 = llvm.load %132 : !llvm.ptr -> f64
func.return %131 : f64
^bb22:
cf.br ^bb23
^bb23:
%133 = arith.constant 1 : i32
%135 = arith.extsi %133 : i32 to i64
%134 = arith.addi %arg0, %135 : i64
%136 = arith.constant 2 : i32
%138 = arith.extsi %136 : i32 to i64
%137 = arith.divsi %134, %138 : i64
%139 = arith.constant 1 : i32
%141 = arith.extsi %139 : i32 to i64
%140 = arith.subi %137, %141 : i64
%142 = arith.subi %arg0, %137 : i64
%143 = arith.constant 1.0 : f32
%144 = arith.sitofp %140 : i64 to f64
%145 = arith.sitofp %arg0 : i64 to f64
%146 = arith.divf %144, %145 : f64
%147 = func.call @B(%140, %arg1, %arg2, %arg3) : (i64, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> f64
%148 = arith.mulf %146, %147 : f64
%150 = arith.extf %143 : f32 to f64
%149 = arith.addf %150, %148 : f64
%151 = arith.sitofp %142 : i64 to f64
%152 = arith.sitofp %arg0 : i64 to f64
%153 = arith.divf %151, %152 : f64
%154 = func.call @B(%142, %arg1, %arg2, %arg3) : (i64, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> f64
%155 = arith.mulf %153, %154 : f64
%156 = arith.addf %149, %155 : f64
%157 = arith.constant 1 : i32
%158 = arith.trunci %157 : i32 to i8
%159 = llvm.getelementptr %arg3[%125] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %158, %159 : i8, !llvm.ptr
%160 = llvm.getelementptr %arg1[%125] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %arg0, %160 : i64, !llvm.ptr
%161 = llvm.getelementptr %arg2[%125] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %156, %161 : f64, !llvm.ptr
func.return %156 : f64
}
func.func @main() -> i32 {
%163 = llvm.mlir.addressof @CAP : !llvm.ptr
%164 = llvm.load %163 : !llvm.ptr -> i64
%165 = arith.constant 8 : i32
%166 = arith.extsi %165 : i32 to i64
%162 = func.call @calloc(%164, %166) : (i64, i64) -> !llvm.ptr
%168 = llvm.mlir.addressof @CAP : !llvm.ptr
%169 = llvm.load %168 : !llvm.ptr -> i64
%170 = arith.constant 8 : i32
%171 = arith.extsi %170 : i32 to i64
%167 = func.call @calloc(%169, %171) : (i64, i64) -> !llvm.ptr
%173 = llvm.mlir.addressof @CAP : !llvm.ptr
%174 = llvm.load %173 : !llvm.ptr -> i64
%175 = arith.constant 1 : i32
%176 = arith.extsi %175 : i32 to i64
%172 = func.call @calloc(%174, %176) : (i64, i64) -> !llvm.ptr
%177 = llvm.mlir.zero : !llvm.ptr
%178 = llvm.icmp "eq" %162, %177 : !llvm.ptr
%179 = scf.if %178 -> (i1) {
%180 = arith.constant true
scf.yield %180 : i1
} else {
%181 = llvm.mlir.zero : !llvm.ptr
%182 = llvm.icmp "eq" %167, %181 : !llvm.ptr
scf.yield %182 : i1
}
%183 = scf.if %179 -> (i1) {
%184 = arith.constant true
scf.yield %184 : i1
} else {
%185 = llvm.mlir.zero : !llvm.ptr
%186 = llvm.icmp "eq" %172, %185 : !llvm.ptr
scf.yield %186 : i1
}
cf.cond_br %183, ^bb24, ^bb25
^bb24:
%187 = arith.constant 1 : i32
func.return %187 : i32
^bb25:
cf.br ^bb26
^bb26:
%188 = arith.constant 5705032704 : i32
%189 = arith.extsi %188 : i32 to i64
%190 = func.call @R(%189) : (i64) -> f64
%191 = func.call @B(%189, %162, %167, %172) : (i64, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> f64
%192 = arith.subf %190, %191 : f64
%193 = llvm.mlir.addressof @str_0 : !llvm.ptr
%194 = llvm.call @printf(%193, %192) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
func.call @free(%162) : (!llvm.ptr) -> ()
func.call @free(%167) : (!llvm.ptr) -> ()
func.call @free(%172) : (!llvm.ptr) -> ()
%198 = arith.constant 0 : i32
func.return %198 : i32
}
}