Problem 970

Kangaroo Hopping over Sixes. H(n) = 2n + 2/3 + eps, where eps = 2*Re(e^{lam*n}/lam) is a tiny correction with lam = 1 + W_1(-1/e) (Lambert W, branch 1). For n = 10^6 the asymptotic formula is exact to far more digits than needed. Precomputed lam to 70 digits, f64 is sufficient for n=10^6.

Answer44754029
Output44754029
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 970
# Kangaroo Hopping over Sixes.
# H(n) = 2n + 2/3 + eps, where eps = 2*Re(e^{lam*n}/lam) is a tiny
# correction with lam = 1 + W_1(-1/e) (Lambert W, branch 1).
# For n = 10^6 the asymptotic formula is exact to far more digits than needed.
# Precomputed lam to 70 digits, f64 is sufficient for n=10^6.

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

function my_fmod(x: f64, y: f64) -> f64 {
    let q: f64 = floor(x / y)
    return x - q * y
}

function main() -> i32 {
    let lam_re: f64 = -2.088843015613043855957086716774947500545693741036729673239112544244607
    let lam_im: f64 = 7.461489285654254556906116612186415334509094993202209240934411391411877
    let lam_abs: f64 = 7.748360310659838754659859240216219375966049464762612291981467582861770
    let lam_arg: f64 = 1.843758551210239598129985611715645443454302414441256154232297011153285

    let n: f64 = 1000000.0
    let pi: f64 = 3.141592653589793238462643383279502884197169399375105820974944592307816
    let two_pi: f64 = 2.0 * pi
    let neg_pi: f64 = 0.0 - pi

    let ln10: f64 = log(10.0)

    let mut theta: f64 = lam_im * n - lam_arg
    theta = my_fmod(theta, two_pi)
    if theta > pi {
        theta = theta - two_pi
    } else {
        if theta < neg_pi {
            theta = theta + two_pi
        }
    }

    let c: f64 = cos(theta)

    let mut a: f64 = log(2.0 / lam_abs) / ln10
    a = a + lam_re * n / ln10
    a = a + log(fabs(c)) / ln10

    let L: i64 = (floor(0.0 - a) as i64)

    let mut delta: f64 = exp((a + (L as f64)) * ln10)
    if c < 0.0 {
        delta = 0.0 - delta
    }

    let s: f64 = 2.0 / 3.0 + delta
    let mut frac: f64 = s - floor(s)

    let mut result: i64 = 0
    let mut count: i32 = 0
    let mut i: i32 = 0
    while i < 200 && count < 8 {
        frac = frac * 10.0
        let d: i64 = (floor(frac) as i64)
        frac = frac - floor(frac)
        if d != 6 {
            result = result * 10 + d
            count = count + 1
        }
        i = i + 1
    }

    printf("%lld\n", result)
    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 my_fmod_f64_f64(double x, double y);
int32_t main(void);






double my_fmod_f64_f64(double x, double y) {
    double q = floor((x / y));
    return (x - (q * y));
}

int32_t main(void) {
    double lam_re = (-2.088843015613043855957086716774947500545693741036729673239112544244607);
    double lam_im = 7.461489285654254556906116612186415334509094993202209240934411391411877;
    double lam_abs = 7.748360310659838754659859240216219375966049464762612291981467582861770;
    double lam_arg = 1.843758551210239598129985611715645443454302414441256154232297011153285;
    double n = 1000000.0;
    double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816;
    double two_pi = (2.0 * pi);
    double neg_pi = (0.0 - pi);
    double ln10 = log(10.0);
    double theta = ((lam_im * n) - lam_arg);
    theta = my_fmod_f64_f64(theta, two_pi);
    if (theta > pi) {
        theta = (theta - two_pi);
    } else {
        if (theta < neg_pi) {
            theta = (theta + two_pi);
        }
    }
    double c = cos(theta);
    double a = (log((2.0 / lam_abs)) / ln10);
    a = (a + ((lam_re * n) / ln10));
    a = (a + (log(fabs(c)) / ln10));
    int64_t L = ((int64_t)(floor((0.0 - a))));
    double delta = exp(((a + ((double)(L))) * ln10));
    if (c < 0.0) {
        delta = (0.0 - delta);
    }
    double s = ((2.0 / 3.0) + delta);
    double frac = (s - floor(s));
    int64_t result = 0;
    int32_t count = 0;
    int32_t i = 0;
    while ((i < 200 && count < 8)) {
        frac = (frac * 10.0);
        int64_t d = ((int64_t)(floor(frac)));
        frac = (frac - floor(frac));
        if (d != 6) {
            result = ((result * 10) + d);
            count = (count + 1);
        }
        i = (i + 1);
    }
    printf("%lld\n", result);
    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 @cos(f64) -> f64
  func.func private @log(f64) -> f64
  func.func private @exp(f64) -> f64
  func.func private @floor(f64) -> f64
  func.func private @fabs(f64) -> f64
  func.func @my_fmod(%arg0: f64, %arg1: f64) -> f64 {
    %1 = arith.divf %arg0, %arg1 : f64
    %0 = func.call @floor(%1) : (f64) -> f64
    %2 = arith.mulf %0, %arg1 : f64
    %3 = arith.subf %arg0, %2 : f64
    func.return %3 : f64
  }
  func.func @main() -> i32 {
    %4 = arith.constant 2.088843015613043855957086716774947500545693741036729673239112544244607 : f32
    %5 = arith.negf %4 : f32
    %6 = arith.extf %5 : f32 to f64
    %7 = arith.constant 7.461489285654254556906116612186415334509094993202209240934411391411877 : f32
    %8 = arith.extf %7 : f32 to f64
    %9 = arith.constant 7.748360310659838754659859240216219375966049464762612291981467582861770 : f32
    %10 = arith.extf %9 : f32 to f64
    %11 = arith.constant 1.843758551210239598129985611715645443454302414441256154232297011153285 : f32
    %12 = arith.extf %11 : f32 to f64
    %13 = arith.constant 1000000.0 : f32
    %14 = arith.extf %13 : f32 to f64
    %15 = arith.constant 3.141592653589793238462643383279502884197169399375105820974944592307816 : f32
    %16 = arith.extf %15 : f32 to f64
    %17 = arith.constant 2.0 : f32
    %19 = arith.extf %17 : f32 to f64
    %18 = arith.mulf %19, %16 : f64
    %20 = arith.constant 0.0 : f32
    %22 = arith.extf %20 : f32 to f64
    %21 = arith.subf %22, %16 : f64
    %23 = arith.constant 10.0 : f32
    %24 = math.log %23 : f32
    %25 = arith.extf %24 : f32 to f64
    %26 = arith.mulf %8, %14 : f64
    %27 = arith.subf %26, %12 : f64
    %28 = llvm.mlir.constant(1 : i64) : i64
    %29 = llvm.alloca %28 x f64 : (i64) -> !llvm.ptr
    llvm.store %27, %29 : f64, !llvm.ptr
    %31 = llvm.load %29 : !llvm.ptr -> f64
    %30 = func.call @my_fmod(%31, %18) : (f64, f64) -> f64
    llvm.store %30, %29 : f64, !llvm.ptr
    %32 = llvm.load %29 : !llvm.ptr -> f64
    %33 = arith.cmpf ogt, %32, %16 : f64
    cf.cond_br %33, ^bb0, ^bb1
    ^bb0:
      %34 = llvm.load %29 : !llvm.ptr -> f64
      %35 = arith.subf %34, %18 : f64
      llvm.store %35, %29 : f64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      %36 = llvm.load %29 : !llvm.ptr -> f64
      %37 = arith.cmpf olt, %36, %21 : f64
      cf.cond_br %37, ^bb3, ^bb4
      ^bb3:
        %38 = llvm.load %29 : !llvm.ptr -> f64
        %39 = arith.addf %38, %18 : f64
        llvm.store %39, %29 : f64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      cf.br ^bb2
    ^bb2:
    %40 = llvm.load %29 : !llvm.ptr -> f64
    %41 = math.cos %40 : f64
    %42 = arith.constant 2.0 : f32
    %44 = arith.extf %42 : f32 to f64
    %43 = arith.divf %44, %10 : f64
    %45 = math.log %43 : f64
    %46 = arith.divf %45, %25 : f64
    %47 = llvm.mlir.constant(1 : i64) : i64
    %48 = llvm.alloca %47 x f64 : (i64) -> !llvm.ptr
    llvm.store %46, %48 : f64, !llvm.ptr
    %49 = llvm.load %48 : !llvm.ptr -> f64
    %50 = arith.mulf %6, %14 : f64
    %51 = arith.divf %50, %25 : f64
    %52 = arith.addf %49, %51 : f64
    llvm.store %52, %48 : f64, !llvm.ptr
    %53 = llvm.load %48 : !llvm.ptr -> f64
    %54 = math.absf %41 : f64
    %55 = math.log %54 : f64
    %56 = arith.divf %55, %25 : f64
    %57 = arith.addf %53, %56 : f64
    llvm.store %57, %48 : f64, !llvm.ptr
    %59 = arith.constant 0.0 : f32
    %60 = llvm.load %48 : !llvm.ptr -> f64
    %62 = arith.extf %59 : f32 to f64
    %61 = arith.subf %62, %60 : f64
    %58 = func.call @floor(%61) : (f64) -> f64
    %63 = arith.fptosi %58 : f64 to i64
    %64 = llvm.load %48 : !llvm.ptr -> f64
    %65 = arith.sitofp %63 : i64 to f64
    %66 = arith.addf %64, %65 : f64
    %67 = arith.mulf %66, %25 : f64
    %68 = math.exp %67 : f64
    %69 = llvm.mlir.constant(1 : i64) : i64
    %70 = llvm.alloca %69 x f64 : (i64) -> !llvm.ptr
    llvm.store %68, %70 : f64, !llvm.ptr
    %71 = arith.constant 0.0 : f32
    %73 = arith.extf %71 : f32 to f64
    %72 = arith.cmpf olt, %41, %73 : f64
    cf.cond_br %72, ^bb6, ^bb7
    ^bb6:
      %74 = arith.constant 0.0 : f32
      %75 = llvm.load %70 : !llvm.ptr -> f64
      %77 = arith.extf %74 : f32 to f64
      %76 = arith.subf %77, %75 : f64
      llvm.store %76, %70 : f64, !llvm.ptr
      cf.br ^bb8
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %78 = arith.constant 2.0 : f32
    %79 = arith.constant 3.0 : f32
    %80 = arith.divf %78, %79 : f32
    %81 = llvm.load %70 : !llvm.ptr -> f64
    %83 = arith.extf %80 : f32 to f64
    %82 = arith.addf %83, %81 : f64
    %84 = func.call @floor(%82) : (f64) -> f64
    %85 = arith.subf %82, %84 : 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 : i32
    %89 = arith.extsi %88 : i32 to i64
    %90 = llvm.mlir.constant(1 : i64) : i64
    %91 = llvm.alloca %90 x i64 : (i64) -> !llvm.ptr
    llvm.store %89, %91 : i64, !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
    %95 = arith.constant 0 : i32
    %96 = llvm.mlir.constant(1 : i64) : i64
    %97 = llvm.alloca %96 x i32 : (i64) -> !llvm.ptr
    llvm.store %95, %97 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %98 = llvm.load %97 : !llvm.ptr -> i32
    %99 = arith.constant 200 : i32
    %100 = arith.cmpi slt, %98, %99 : i32
    %101 = scf.if %100 -> (i1) {
      %102 = llvm.load %94 : !llvm.ptr -> i32
      %103 = arith.constant 8 : i32
      %104 = arith.cmpi slt, %102, %103 : i32
      scf.yield %104 : i1
    } else {
      %105 = arith.constant false
      scf.yield %105 : i1
    }
    cf.cond_br %101, ^bb10, ^bb11
    ^bb10:
      %106 = llvm.load %87 : !llvm.ptr -> f64
      %107 = arith.constant 10.0 : f32
      %109 = arith.extf %107 : f32 to f64
      %108 = arith.mulf %106, %109 : f64
      llvm.store %108, %87 : f64, !llvm.ptr
      %111 = llvm.load %87 : !llvm.ptr -> f64
      %110 = func.call @floor(%111) : (f64) -> f64
      %112 = arith.fptosi %110 : f64 to i64
      %113 = llvm.load %87 : !llvm.ptr -> f64
      %115 = llvm.load %87 : !llvm.ptr -> f64
      %114 = func.call @floor(%115) : (f64) -> f64
      %116 = arith.subf %113, %114 : f64
      llvm.store %116, %87 : f64, !llvm.ptr
      %117 = arith.constant 6 : i32
      %119 = arith.extsi %117 : i32 to i64
      %118 = arith.cmpi ne, %112, %119 : i64
      cf.cond_br %118, ^bb12, ^bb13
      ^bb12:
        %120 = llvm.load %91 : !llvm.ptr -> i64
        %121 = arith.constant 10 : i32
        %123 = arith.extsi %121 : i32 to i64
        %122 = arith.muli %120, %123 : i64
        %124 = arith.addi %122, %112 : i64
        llvm.store %124, %91 : i64, !llvm.ptr
        %125 = llvm.load %94 : !llvm.ptr -> i32
        %126 = arith.constant 1 : i32
        %127 = arith.addi %125, %126 : i32
        llvm.store %127, %94 : i32, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %128 = llvm.load %97 : !llvm.ptr -> i32
      %129 = arith.constant 1 : i32
      %130 = arith.addi %128, %129 : i32
      llvm.store %130, %97 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %131 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %132 = llvm.load %91 : !llvm.ptr -> i64
    %133 = llvm.call @printf(%131, %132) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %134 = arith.constant 0 : i32
    func.return %134 : i32
  }
}