Problem 568

Reciprocal Games II First 7 significant digits of D(n)=H_n/2^n for n=123456789.

Answer4228020
Output4228020
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(1) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 568
# Reciprocal Games II
# First 7 significant digits of D(n)=H_n/2^n for n=123456789.

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

function harmonic_asymp(n: i64) -> f64 {
    let x: f64 = n as f64
    let inv: f64 = 1.0 / x
    let inv2: f64 = inv * inv
    let gamma: f64 = 0.5772156649015329
    return log(x) + gamma + 0.5 * inv - inv2 / 12.0 + (inv2 * inv2) / 120.0
        - (inv2 * inv2 * inv2) / 252.0
}

function solve(n: i64) -> i64 {
    let H: f64 = harmonic_asymp(n)
    let L: f64 = log10(H) - (n as f64) * log10(2.0)
    let e: f64 = floor(L)
    let frac: f64 = L - e
    let ln10: f64 = log(10.0)
    let mantissa: f64 = exp(frac * ln10)
    let digits: i64 = floor(mantissa * 1000000.0 + 1e-9) as i64
    if digits >= 10000000 {
        return digits / 10
    }
    return digits
}

function main() -> i32 {
    printf("%lld\n", solve(123456789))
    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_asymp_i64(int64_t n);
int64_t solve_i64(int64_t n);
int32_t main(void);





double harmonic_asymp_i64(int64_t n) {
    double x = ((double)(n));
    double inv = (1.0 / x);
    double inv2 = (inv * inv);
    double gamma = 0.5772156649015329;
    return (((((log(x) + gamma) + (0.5 * inv)) - (inv2 / 12.0)) + ((inv2 * inv2) / 120.0)) - (((inv2 * inv2) * inv2) / 252.0));
}

int64_t solve_i64(int64_t n) {
    double H = harmonic_asymp_i64(n);
    double L = (log10(H) - (((double)(n)) * log10(2.0)));
    double e = floor(L);
    double frac = (L - e);
    double ln10 = log(10.0);
    double mantissa = exp((frac * ln10));
    int64_t digits = ((int64_t)(floor(((mantissa * 1000000.0) + 1e-9))));
    if (digits >= 10000000) {
        return FLOW_CHECKED_DIV((digits), (10));
    }
    return digits;
}

int32_t main(void) {
    printf("%lld\n", solve_i64(123456789));
    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 @log10(f64) -> f64
  func.func private @exp(f64) -> f64
  func.func private @floor(f64) -> f64
  func.func @harmonic_asymp(%arg0: i64) -> f64 {
    %0 = arith.sitofp %arg0 : i64 to f64
    %1 = arith.constant 1.0 : f32
    %3 = arith.extf %1 : f32 to f64
    %2 = arith.divf %3, %0 : f64
    %4 = arith.mulf %2, %2 : f64
    %5 = arith.constant 0.5772156649015329 : f32
    %6 = arith.extf %5 : f32 to f64
    %7 = math.log %0 : f64
    %8 = arith.addf %7, %6 : f64
    %9 = arith.constant 0.5 : f32
    %11 = arith.extf %9 : f32 to f64
    %10 = arith.mulf %11, %2 : f64
    %12 = arith.addf %8, %10 : f64
    %13 = arith.constant 12.0 : f32
    %15 = arith.extf %13 : f32 to f64
    %14 = arith.divf %4, %15 : f64
    %16 = arith.subf %12, %14 : f64
    %17 = arith.mulf %4, %4 : f64
    %18 = arith.constant 120.0 : f32
    %20 = arith.extf %18 : f32 to f64
    %19 = arith.divf %17, %20 : f64
    %21 = arith.addf %16, %19 : f64
    %22 = arith.mulf %4, %4 : f64
    %23 = arith.mulf %22, %4 : f64
    %24 = arith.constant 252.0 : f32
    %26 = arith.extf %24 : f32 to f64
    %25 = arith.divf %23, %26 : f64
    %27 = arith.subf %21, %25 : f64
    func.return %27 : f64
  }
  func.func @solve(%arg0: i64) -> i64 {
    %28 = func.call @harmonic_asymp(%arg0) : (i64) -> f64
    %29 = func.call @log10(%28) : (f64) -> f64
    %30 = arith.sitofp %arg0 : i64 to f64
    %32 = arith.constant 2.0 : f32
    %33 = arith.extf %32 : f32 to f64
    %31 = func.call @log10(%33) : (f64) -> f64
    %34 = arith.mulf %30, %31 : f64
    %35 = arith.subf %29, %34 : f64
    %36 = func.call @floor(%35) : (f64) -> f64
    %37 = arith.subf %35, %36 : f64
    %38 = arith.constant 10.0 : f32
    %39 = math.log %38 : f32
    %40 = arith.extf %39 : f32 to f64
    %41 = arith.mulf %37, %40 : f64
    %42 = math.exp %41 : f64
    %44 = arith.constant 1000000.0 : f32
    %46 = arith.extf %44 : f32 to f64
    %45 = arith.mulf %42, %46 : f64
    %47 = arith.constant 0.000000001 : f32
    %49 = arith.extf %47 : f32 to f64
    %48 = arith.addf %45, %49 : f64
    %43 = func.call @floor(%48) : (f64) -> f64
    %50 = arith.fptosi %43 : f64 to i64
    %51 = arith.constant 10000000 : i32
    %53 = arith.extsi %51 : i32 to i64
    %52 = arith.cmpi sge, %50, %53 : i64
    cf.cond_br %52, ^bb0, ^bb1
    ^bb0:
      %54 = arith.constant 10 : i32
      %56 = arith.extsi %54 : i32 to i64
      %55 = arith.divsi %50, %56 : i64
      func.return %55 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    func.return %50 : i64
  }
  func.func @main() -> i32 {
    %57 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %59 = arith.constant 123456789 : i32
    %60 = arith.extsi %59 : i32 to i64
    %58 = func.call @solve(%60) : (i64) -> i64
    %61 = llvm.call @printf(%57, %58) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %62 = arith.constant 0 : i32
    func.return %62 : i32
  }
}