Problem 567
Reciprocal Games I S(m) = 4*H_m - 2*sum 2^{-i}/i - (J_A(m)+J_B(m)), m=123456789.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | ? |
| Space complexity | O(1) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 567
# Reciprocal Games I
# S(m) = 4*H_m - 2*sum 2^{-i}/i - (J_A(m)+J_B(m)), m=123456789.
extern {
function log(x: f64) -> f64
function ldexp(x: f64, exp: i32) -> f64
}
function harmonic(n: i64) -> f64 {
if n < 1000000 {
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 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 + (inv2 * inv2 * inv2 * inv2) / 240.0
}
function pow2_over_i_sum(m: i64) -> f64 {
if m <= 60 {
let mut s: f64 = 0.0
let mut i: i64 = 1
while i <= m {
s = s + ldexp(1.0, (0 - i) as i32) / (i as f64)
i = i + 1
}
return s
}
return log(2.0)
}
function j_a(n: i64) -> f64 {
let h: f64 = harmonic(n)
let mut s: f64 = 0.0
let mut j: i64 = 0
while j <= 80 {
s = s + ldexp(1.0, (0 - j) as i32) / ((n - j) as f64)
j = j + 1
}
# H_n / 2^n underflows for huge n
if n < 1000 {
s = s - ldexp(h, (0 - n) as i32)
}
return s
}
function j_b(n: i64) -> f64 {
let N: i64 = n - 1
let K: i64 = 25
let mut inv: f64 = 1.0
let mut edge: f64 = 1.0
let mut j: i64 = 1
while j <= K {
inv = inv * (j as f64) / ((N - j + 1) as f64)
edge = edge + inv
j = j + 1
}
return (2.0 * edge) / (n as f64)
}
function S(m: i64) -> f64 {
let h: f64 = harmonic(m)
let p: f64 = pow2_over_i_sum(m)
return 4.0 * h - 2.0 * p - (j_a(m) + j_b(m))
}
function main() -> i32 {
printf("%.8f\n", S(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 ldexp(double x, int32_t exp);
double harmonic_i64(int64_t n);
double pow2_over_i_sum_i64(int64_t m);
double j_a_i64(int64_t n);
double j_b_i64(int64_t n);
double S_i64(int64_t m);
int32_t main(void);
double harmonic_i64(int64_t n) {
if (n < 1000000) {
double s = 0.0;
int64_t k = 1;
while (k <= n) {
s = (s + (1.0 / ((double)(k))));
k = (k + 1);
}
return s;
}
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)) + ((((inv2 * inv2) * inv2) * inv2) / 240.0));
}
double pow2_over_i_sum_i64(int64_t m) {
if (m <= 60) {
double s = 0.0;
int64_t i = 1;
while (i <= m) {
s = (s + (ldexp(1.0, ((int32_t)((0 - i)))) / ((double)(i))));
i = (i + 1);
}
return s;
}
return log(2.0);
}
double j_a_i64(int64_t n) {
double h = harmonic_i64(n);
double s = 0.0;
int64_t j = 0;
while (j <= 80) {
s = (s + (ldexp(1.0, ((int32_t)((0 - j)))) / ((double)((n - j)))));
j = (j + 1);
}
if (n < 1000) {
s = (s - ldexp(h, ((int32_t)((0 - n)))));
}
return s;
}
double j_b_i64(int64_t n) {
int64_t N = (n - 1);
int64_t K = 25;
double inv = 1.0;
double edge = 1.0;
int64_t j = 1;
while (j <= K) {
inv = ((inv * ((double)(j))) / ((double)(((N - j) + 1))));
edge = (edge + inv);
j = (j + 1);
}
return ((2.0 * edge) / ((double)(n)));
}
double S_i64(int64_t m) {
double h = harmonic_i64(m);
double p = pow2_over_i_sum_i64(m);
return (((4.0 * h) - (2.0 * p)) - (j_a_i64(m) + j_b_i64(m)));
}
int32_t main(void) {
printf("%.8f\n", S_i64(123456789));
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 @log(f64) -> f64
func.func private @ldexp(f64, i32) -> f64
func.func @harmonic(%arg0: i64) -> f64 {
%0 = arith.constant 1000000 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi slt, %arg0, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%3 = arith.constant 0.0 : f32
%4 = arith.extf %3 : f32 to f64
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x f64 : (i64) -> !llvm.ptr
llvm.store %4, %6 : f64, !llvm.ptr
%7 = arith.constant 1 : i32
%8 = arith.extsi %7 : i32 to i64
%9 = llvm.mlir.constant(1 : i64) : i64
%10 = llvm.alloca %9 x i64 : (i64) -> !llvm.ptr
llvm.store %8, %10 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%11 = llvm.load %10 : !llvm.ptr -> i64
%12 = arith.cmpi sle, %11, %arg0 : i64
cf.cond_br %12, ^bb4, ^bb5
^bb4:
%13 = llvm.load %6 : !llvm.ptr -> f64
%14 = arith.constant 1.0 : f32
%15 = llvm.load %10 : !llvm.ptr -> i64
%16 = arith.sitofp %15 : i64 to f64
%18 = arith.extf %14 : f32 to f64
%17 = arith.divf %18, %16 : f64
%19 = arith.addf %13, %17 : f64
llvm.store %19, %6 : f64, !llvm.ptr
%20 = llvm.load %10 : !llvm.ptr -> i64
%21 = arith.constant 1 : i32
%23 = arith.extsi %21 : i32 to i64
%22 = arith.addi %20, %23 : i64
llvm.store %22, %10 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%24 = llvm.load %6 : !llvm.ptr -> f64
func.return %24 : f64
^bb1:
cf.br ^bb2
^bb2:
%25 = arith.sitofp %arg0 : i64 to f64
%26 = arith.constant 1.0 : f32
%28 = arith.extf %26 : f32 to f64
%27 = arith.divf %28, %25 : f64
%29 = arith.mulf %27, %27 : f64
%30 = arith.constant 0.5772156649015329 : f32
%31 = arith.extf %30 : f32 to f64
%32 = math.log %25 : f64
%33 = arith.addf %32, %31 : f64
%34 = arith.constant 0.5 : f32
%36 = arith.extf %34 : f32 to f64
%35 = arith.mulf %36, %27 : f64
%37 = arith.addf %33, %35 : f64
%38 = arith.constant 12.0 : f32
%40 = arith.extf %38 : f32 to f64
%39 = arith.divf %29, %40 : f64
%41 = arith.subf %37, %39 : f64
%42 = arith.mulf %29, %29 : f64
%43 = arith.constant 120.0 : f32
%45 = arith.extf %43 : f32 to f64
%44 = arith.divf %42, %45 : f64
%46 = arith.addf %41, %44 : f64
%47 = arith.mulf %29, %29 : f64
%48 = arith.mulf %47, %29 : f64
%49 = arith.constant 252.0 : f32
%51 = arith.extf %49 : f32 to f64
%50 = arith.divf %48, %51 : f64
%52 = arith.subf %46, %50 : f64
%53 = arith.mulf %29, %29 : f64
%54 = arith.mulf %53, %29 : f64
%55 = arith.mulf %54, %29 : f64
%56 = arith.constant 240.0 : f32
%58 = arith.extf %56 : f32 to f64
%57 = arith.divf %55, %58 : f64
%59 = arith.addf %52, %57 : f64
func.return %59 : f64
}
func.func @pow2_over_i_sum(%arg0: i64) -> f64 {
%60 = arith.constant 60 : i32
%62 = arith.extsi %60 : i32 to i64
%61 = arith.cmpi sle, %arg0, %62 : i64
cf.cond_br %61, ^bb6, ^bb7
^bb6:
%63 = arith.constant 0.0 : f32
%64 = arith.extf %63 : f32 to f64
%65 = llvm.mlir.constant(1 : i64) : i64
%66 = llvm.alloca %65 x f64 : (i64) -> !llvm.ptr
llvm.store %64, %66 : f64, !llvm.ptr
%67 = arith.constant 1 : i32
%68 = arith.extsi %67 : i32 to i64
%69 = llvm.mlir.constant(1 : i64) : i64
%70 = llvm.alloca %69 x i64 : (i64) -> !llvm.ptr
llvm.store %68, %70 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%71 = llvm.load %70 : !llvm.ptr -> i64
%72 = arith.cmpi sle, %71, %arg0 : i64
cf.cond_br %72, ^bb10, ^bb11
^bb10:
%73 = llvm.load %66 : !llvm.ptr -> f64
%75 = arith.constant 1.0 : f32
%76 = arith.constant 0 : i32
%77 = llvm.load %70 : !llvm.ptr -> i64
%79 = arith.extsi %76 : i32 to i64
%78 = arith.subi %79, %77 : i64
%80 = arith.trunci %78 : i64 to i32
%81 = arith.extf %75 : f32 to f64
%74 = func.call @ldexp(%81, %80) : (f64, i32) -> f64
%82 = llvm.load %70 : !llvm.ptr -> i64
%83 = arith.sitofp %82 : i64 to f64
%84 = arith.divf %74, %83 : f64
%85 = arith.addf %73, %84 : f64
llvm.store %85, %66 : f64, !llvm.ptr
%86 = llvm.load %70 : !llvm.ptr -> i64
%87 = arith.constant 1 : i32
%89 = arith.extsi %87 : i32 to i64
%88 = arith.addi %86, %89 : i64
llvm.store %88, %70 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%90 = llvm.load %66 : !llvm.ptr -> f64
func.return %90 : f64
^bb7:
cf.br ^bb8
^bb8:
%91 = arith.constant 2.0 : f32
%92 = math.log %91 : f32
func.return %92 : f64
}
func.func @j_a(%arg0: i64) -> f64 {
%93 = func.call @harmonic(%arg0) : (i64) -> f64
%94 = arith.constant 0.0 : f32
%95 = arith.extf %94 : f32 to f64
%96 = llvm.mlir.constant(1 : i64) : i64
%97 = llvm.alloca %96 x f64 : (i64) -> !llvm.ptr
llvm.store %95, %97 : f64, !llvm.ptr
%98 = arith.constant 0 : i32
%99 = arith.extsi %98 : i32 to i64
%100 = llvm.mlir.constant(1 : i64) : i64
%101 = llvm.alloca %100 x i64 : (i64) -> !llvm.ptr
llvm.store %99, %101 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%102 = llvm.load %101 : !llvm.ptr -> i64
%103 = arith.constant 80 : i32
%105 = arith.extsi %103 : i32 to i64
%104 = arith.cmpi sle, %102, %105 : i64
cf.cond_br %104, ^bb13, ^bb14
^bb13:
%106 = llvm.load %97 : !llvm.ptr -> f64
%108 = arith.constant 1.0 : f32
%109 = arith.constant 0 : i32
%110 = llvm.load %101 : !llvm.ptr -> i64
%112 = arith.extsi %109 : i32 to i64
%111 = arith.subi %112, %110 : i64
%113 = arith.trunci %111 : i64 to i32
%114 = arith.extf %108 : f32 to f64
%107 = func.call @ldexp(%114, %113) : (f64, i32) -> f64
%115 = llvm.load %101 : !llvm.ptr -> i64
%116 = arith.subi %arg0, %115 : i64
%117 = arith.sitofp %116 : i64 to f64
%118 = arith.divf %107, %117 : f64
%119 = arith.addf %106, %118 : f64
llvm.store %119, %97 : f64, !llvm.ptr
%120 = llvm.load %101 : !llvm.ptr -> i64
%121 = arith.constant 1 : i32
%123 = arith.extsi %121 : i32 to i64
%122 = arith.addi %120, %123 : i64
llvm.store %122, %101 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%124 = arith.constant 1000 : i32
%126 = arith.extsi %124 : i32 to i64
%125 = arith.cmpi slt, %arg0, %126 : i64
cf.cond_br %125, ^bb15, ^bb16
^bb15:
%127 = llvm.load %97 : !llvm.ptr -> f64
%129 = arith.constant 0 : i32
%131 = arith.extsi %129 : i32 to i64
%130 = arith.subi %131, %arg0 : i64
%132 = arith.trunci %130 : i64 to i32
%128 = func.call @ldexp(%93, %132) : (f64, i32) -> f64
%133 = arith.subf %127, %128 : f64
llvm.store %133, %97 : f64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%134 = llvm.load %97 : !llvm.ptr -> f64
func.return %134 : f64
}
func.func @j_b(%arg0: i64) -> f64 {
%135 = arith.constant 1 : i32
%137 = arith.extsi %135 : i32 to i64
%136 = arith.subi %arg0, %137 : i64
%138 = arith.constant 25 : i32
%139 = arith.extsi %138 : i32 to i64
%140 = arith.constant 1.0 : f32
%141 = arith.extf %140 : f32 to f64
%142 = llvm.mlir.constant(1 : i64) : i64
%143 = llvm.alloca %142 x f64 : (i64) -> !llvm.ptr
llvm.store %141, %143 : f64, !llvm.ptr
%144 = arith.constant 1.0 : f32
%145 = arith.extf %144 : f32 to f64
%146 = llvm.mlir.constant(1 : i64) : i64
%147 = llvm.alloca %146 x f64 : (i64) -> !llvm.ptr
llvm.store %145, %147 : f64, !llvm.ptr
%148 = arith.constant 1 : i32
%149 = arith.extsi %148 : i32 to i64
%150 = llvm.mlir.constant(1 : i64) : i64
%151 = llvm.alloca %150 x i64 : (i64) -> !llvm.ptr
llvm.store %149, %151 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%152 = llvm.load %151 : !llvm.ptr -> i64
%153 = arith.cmpi sle, %152, %139 : i64
cf.cond_br %153, ^bb19, ^bb20
^bb19:
%154 = llvm.load %143 : !llvm.ptr -> f64
%155 = llvm.load %151 : !llvm.ptr -> i64
%156 = arith.sitofp %155 : i64 to f64
%157 = arith.mulf %154, %156 : f64
%158 = llvm.load %151 : !llvm.ptr -> i64
%159 = arith.subi %136, %158 : i64
%160 = arith.constant 1 : i32
%162 = arith.extsi %160 : i32 to i64
%161 = arith.addi %159, %162 : i64
%163 = arith.sitofp %161 : i64 to f64
%164 = arith.divf %157, %163 : f64
llvm.store %164, %143 : f64, !llvm.ptr
%165 = llvm.load %147 : !llvm.ptr -> f64
%166 = llvm.load %143 : !llvm.ptr -> f64
%167 = arith.addf %165, %166 : f64
llvm.store %167, %147 : f64, !llvm.ptr
%168 = llvm.load %151 : !llvm.ptr -> i64
%169 = arith.constant 1 : i32
%171 = arith.extsi %169 : i32 to i64
%170 = arith.addi %168, %171 : i64
llvm.store %170, %151 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%172 = arith.constant 2.0 : f32
%173 = llvm.load %147 : !llvm.ptr -> f64
%175 = arith.extf %172 : f32 to f64
%174 = arith.mulf %175, %173 : f64
%176 = arith.sitofp %arg0 : i64 to f64
%177 = arith.divf %174, %176 : f64
func.return %177 : f64
}
func.func @S(%arg0: i64) -> f64 {
%178 = func.call @harmonic(%arg0) : (i64) -> f64
%179 = func.call @pow2_over_i_sum(%arg0) : (i64) -> f64
%180 = arith.constant 4.0 : f32
%182 = arith.extf %180 : f32 to f64
%181 = arith.mulf %182, %178 : f64
%183 = arith.constant 2.0 : f32
%185 = arith.extf %183 : f32 to f64
%184 = arith.mulf %185, %179 : f64
%186 = arith.subf %181, %184 : f64
%187 = func.call @j_a(%arg0) : (i64) -> f64
%188 = func.call @j_b(%arg0) : (i64) -> f64
%189 = arith.addf %187, %188 : f64
%190 = arith.subf %186, %189 : f64
func.return %190 : f64
}
func.func @main() -> i32 {
%191 = llvm.mlir.addressof @str_0 : !llvm.ptr
%193 = arith.constant 123456789 : i32
%194 = arith.extsi %193 : i32 to i64
%192 = func.call @S(%194) : (i64) -> f64
%195 = llvm.call @printf(%191, %192) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%196 = arith.constant 0 : i32
func.return %196 : i32
}
}