Problem 664
Infinite game: F(n) = 3 + ceil(log_phi(A_n)) via lgamma asymptotic.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(1) | O(n log log n) |
| Space complexity | O(1) | O(n) |
| Approach | Flow solution | Sieve-based totient computation |
| Verdict | Optimal |
Flow source
# Project Euler 664
# Infinite game: F(n) = 3 + ceil(log_phi(A_n)) via lgamma asymptotic.
extern {
function sqrt(x: f64) -> f64
function log(x: f64) -> f64
function lgamma(x: f64) -> f64
function floor(x: f64) -> f64
function round(x: f64) -> f64
function fabs(x: f64) -> f64
}
function ceil_guard(x: f64) -> i64 {
let k: f64 = round(x)
if fabs(x - k) < 0.0000000001 {
return k as i64
}
return (floor(x) + 1.0) as i64
}
function F(n: i64) -> i64 {
if n == 0 { return 4 }
if n == 1 { return 6 }
if n == 2 { return 9 }
let phi: f64 = (1.0 + sqrt(5.0)) / 2.0
let ln_phi: f64 = log(phi)
let ln_ln_phi: f64 = log(ln_phi)
let lnA: f64 = lgamma((n + 1) as f64) - ((n + 1) as f64) * ln_ln_phi
let log_phi_A: f64 = lnA / ln_phi
return 3 + ceil_guard(log_phi_A)
}
function main() -> i32 {
printf("%lld\n", F(1234567))
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 lgamma(double x);
int64_t ceil_guard_f64(double x);
int64_t F_i64(int64_t n);
int32_t main(void);
int64_t ceil_guard_f64(double x) {
double k = round(x);
if (fabs((x - k)) < 0.0000000001) {
return ((int64_t)(k));
}
return ((int64_t)((floor(x) + 1.0)));
}
int64_t F_i64(int64_t n) {
if (n == 0) {
return 4;
}
if (n == 1) {
return 6;
}
if (n == 2) {
return 9;
}
double phi = ((1.0 + sqrt(5.0)) / 2.0);
double ln_phi = log(phi);
double ln_ln_phi = log(ln_phi);
double lnA = (lgamma(((double)((n + 1)))) - (((double)((n + 1))) * ln_ln_phi));
double log_phi_A = (lnA / ln_phi);
return (3 + ceil_guard_f64(log_phi_A));
}
int32_t main(void) {
printf("%lld\n", F_i64(1234567));
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 @sqrt(f64) -> f64
func.func private @log(f64) -> f64
func.func private @lgamma(f64) -> f64
func.func private @floor(f64) -> f64
func.func private @round(f64) -> f64
func.func private @fabs(f64) -> f64
func.func @ceil_guard(%arg0: f64) -> i64 {
%0 = func.call @round(%arg0) : (f64) -> f64
%1 = arith.subf %arg0, %0 : f64
%2 = math.absf %1 : f64
%3 = arith.constant 0.0000000001 : f32
%5 = arith.extf %3 : f32 to f64
%4 = arith.cmpf olt, %2, %5 : f64
cf.cond_br %4, ^bb0, ^bb1
^bb0:
%6 = arith.fptosi %0 : f64 to i64
func.return %6 : i64
^bb1:
cf.br ^bb2
^bb2:
%7 = func.call @floor(%arg0) : (f64) -> f64
%8 = arith.constant 1.0 : f32
%10 = arith.extf %8 : f32 to f64
%9 = arith.addf %7, %10 : f64
%11 = arith.fptosi %9 : f64 to i64
func.return %11 : i64
}
func.func @F(%arg0: i64) -> i64 {
%12 = arith.constant 0 : i32
%14 = arith.extsi %12 : i32 to i64
%13 = arith.cmpi eq, %arg0, %14 : i64
cf.cond_br %13, ^bb3, ^bb4
^bb3:
%15 = arith.constant 4 : i32
%16 = arith.extsi %15 : i32 to i64
func.return %16 : i64
^bb4:
cf.br ^bb5
^bb5:
%17 = arith.constant 1 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.cmpi eq, %arg0, %19 : i64
cf.cond_br %18, ^bb6, ^bb7
^bb6:
%20 = arith.constant 6 : i32
%21 = arith.extsi %20 : i32 to i64
func.return %21 : i64
^bb7:
cf.br ^bb8
^bb8:
%22 = arith.constant 2 : i32
%24 = arith.extsi %22 : i32 to i64
%23 = arith.cmpi eq, %arg0, %24 : i64
cf.cond_br %23, ^bb9, ^bb10
^bb9:
%25 = arith.constant 9 : i32
%26 = arith.extsi %25 : i32 to i64
func.return %26 : i64
^bb10:
cf.br ^bb11
^bb11:
%27 = arith.constant 1.0 : f32
%28 = arith.constant 5.0 : f32
%29 = math.sqrt %28 : f32
%31 = arith.extf %27 : f32 to f64
%30 = arith.addf %31, %29 : f64
%32 = arith.constant 2.0 : f32
%34 = arith.extf %32 : f32 to f64
%33 = arith.divf %30, %34 : f64
%35 = math.log %33 : f64
%36 = math.log %35 : f64
%38 = arith.constant 1 : i32
%40 = arith.extsi %38 : i32 to i64
%39 = arith.addi %arg0, %40 : i64
%41 = arith.sitofp %39 : i64 to f64
%37 = func.call @lgamma(%41) : (f64) -> f64
%42 = arith.constant 1 : i32
%44 = arith.extsi %42 : i32 to i64
%43 = arith.addi %arg0, %44 : i64
%45 = arith.sitofp %43 : i64 to f64
%46 = arith.mulf %45, %36 : f64
%47 = arith.subf %37, %46 : f64
%48 = arith.divf %47, %35 : f64
%49 = arith.constant 3 : i32
%50 = func.call @ceil_guard(%48) : (f64) -> i64
%52 = arith.extsi %49 : i32 to i64
%51 = arith.addi %52, %50 : i64
func.return %51 : i64
}
func.func @main() -> i32 {
%53 = llvm.mlir.addressof @str_0 : !llvm.ptr
%55 = arith.constant 1234567 : i32
%56 = arith.extsi %55 : i32 to i64
%54 = func.call @F(%56) : (i64) -> i64
%57 = llvm.call @printf(%53, %54) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%58 = arith.constant 0 : i32
func.return %58 : i32
}
}