Problem 692
Siegbert and Jo: G(n)=sum H(k) via Fibonacci/Zeckendorf recurrence.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(log n) |
| Space complexity | O(n) | O(1) |
| Approach | Flow solution | Matrix exponentiation |
| Verdict | Suboptimal |
Flow source
# Project Euler 692
# Siegbert and Jo: G(n)=sum H(k) via Fibonacci/Zeckendorf recurrence.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let n: i64 = 23416728348467685
let fib: ptr<i64> = calloc(128, 8)
fib[0] = 1
fib[1] = 2
let mut flen: i64 = 2
while fib[flen - 1] <= n {
fib[flen] = fib[flen - 1] + fib[flen - 2]
flen = flen + 1
}
let prefix: ptr<i64> = calloc(flen, 8)
prefix[0] = 0
if flen > 1 { prefix[1] = 1 }
let mut i: i64 = 1
while i < flen - 1 {
prefix[i + 1] = prefix[i] + fib[i] + prefix[i - 1]
i = i + 1
}
# iterative stack-based G(x)
let mut ans: i64 = 0
let mut x: i64 = n
while x > 0 {
# largest fib[i] <= x
let mut lo: i64 = 0
let mut hi: i64 = flen - 1
while lo < hi {
let mid: i64 = (lo + hi + 1) / 2
if fib[mid] <= x { lo = mid } else { hi = mid - 1 }
}
ans = ans + prefix[lo] + fib[lo]
x = x - fib[lo]
}
printf("%lld\n", ans)
free(fib)
free(prefix)
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; }
int32_t main(void);
int32_t main(void) {
int64_t n = 23416728348467685;
int64_t* fib = (int64_t*)(calloc(128, 8));
fib[0] = 1;
fib[1] = 2;
int64_t flen = 2;
while (fib[(flen - 1)] <= n) {
fib[flen] = (fib[(flen - 1)] + fib[(flen - 2)]);
flen = (flen + 1);
}
int64_t* prefix = (int64_t*)(calloc(flen, 8));
prefix[0] = 0;
if (flen > 1) {
prefix[1] = 1;
}
int64_t i = 1;
while (i < (flen - 1)) {
prefix[(i + 1)] = ((prefix[i] + fib[i]) + prefix[(i - 1)]);
i = (i + 1);
}
int64_t ans = 0;
int64_t x = n;
while (x > 0) {
int64_t lo = 0;
int64_t hi = (flen - 1);
while (lo < hi) {
int64_t mid = FLOW_CHECKED_DIV((((lo + hi) + 1)), (2));
if (fib[mid] <= x) {
lo = mid;
} else {
hi = (mid - 1);
}
}
ans = ((ans + prefix[lo]) + fib[lo]);
x = (x - fib[lo]);
}
printf("%lld\n", ans);
free(fib);
free(prefix);
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 @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func @main() -> i32 {
%0 = arith.constant 23416724053500389 : i32
%1 = arith.extsi %0 : i32 to i64
%3 = arith.constant 128 : i32
%4 = arith.constant 8 : i32
%5 = arith.extsi %3 : i32 to i64
%6 = arith.extsi %4 : i32 to i64
%2 = func.call @calloc(%5, %6) : (i64, i64) -> !llvm.ptr
%7 = arith.constant 1 : i32
%8 = arith.constant 0 : i32
%9 = arith.extsi %7 : i32 to i64
%10 = arith.extsi %8 : i32 to i64
%11 = llvm.getelementptr %2[%10] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %9, %11 : i64, !llvm.ptr
%12 = arith.constant 2 : i32
%13 = arith.constant 1 : i32
%14 = arith.extsi %12 : i32 to i64
%15 = arith.extsi %13 : i32 to i64
%16 = llvm.getelementptr %2[%15] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %14, %16 : i64, !llvm.ptr
%17 = arith.constant 2 : i32
%18 = arith.extsi %17 : i32 to i64
%19 = llvm.mlir.constant(1 : i64) : i64
%20 = llvm.alloca %19 x i64 : (i64) -> !llvm.ptr
llvm.store %18, %20 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%22 = llvm.load %20 : !llvm.ptr -> i64
%23 = arith.constant 1 : i32
%25 = arith.extsi %23 : i32 to i64
%24 = arith.subi %22, %25 : i64
%26 = llvm.getelementptr %2[%24] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%21 = llvm.load %26 : !llvm.ptr -> i64
%27 = arith.cmpi sle, %21, %1 : i64
cf.cond_br %27, ^bb1, ^bb2
^bb1:
%29 = llvm.load %20 : !llvm.ptr -> i64
%30 = arith.constant 1 : i32
%32 = arith.extsi %30 : i32 to i64
%31 = arith.subi %29, %32 : i64
%33 = llvm.getelementptr %2[%31] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%28 = llvm.load %33 : !llvm.ptr -> i64
%35 = llvm.load %20 : !llvm.ptr -> i64
%36 = arith.constant 2 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.subi %35, %38 : i64
%39 = llvm.getelementptr %2[%37] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%34 = llvm.load %39 : !llvm.ptr -> i64
%40 = arith.addi %28, %34 : i64
%41 = llvm.load %20 : !llvm.ptr -> i64
%42 = llvm.getelementptr %2[%41] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %40, %42 : i64, !llvm.ptr
%43 = llvm.load %20 : !llvm.ptr -> i64
%44 = arith.constant 1 : i32
%46 = arith.extsi %44 : i32 to i64
%45 = arith.addi %43, %46 : i64
llvm.store %45, %20 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%48 = llvm.load %20 : !llvm.ptr -> i64
%49 = arith.constant 8 : i32
%50 = arith.extsi %49 : i32 to i64
%47 = func.call @calloc(%48, %50) : (i64, i64) -> !llvm.ptr
%51 = arith.constant 0 : i32
%52 = arith.constant 0 : i32
%53 = arith.extsi %51 : i32 to i64
%54 = arith.extsi %52 : i32 to i64
%55 = llvm.getelementptr %47[%54] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %53, %55 : i64, !llvm.ptr
%56 = llvm.load %20 : !llvm.ptr -> i64
%57 = arith.constant 1 : i32
%59 = arith.extsi %57 : i32 to i64
%58 = arith.cmpi sgt, %56, %59 : i64
cf.cond_br %58, ^bb3, ^bb4
^bb3:
%60 = arith.constant 1 : i32
%61 = arith.constant 1 : i32
%62 = arith.extsi %60 : i32 to i64
%63 = arith.extsi %61 : i32 to i64
%64 = llvm.getelementptr %47[%63] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %62, %64 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%65 = arith.constant 1 : i32
%66 = arith.extsi %65 : i32 to i64
%67 = llvm.mlir.constant(1 : i64) : i64
%68 = llvm.alloca %67 x i64 : (i64) -> !llvm.ptr
llvm.store %66, %68 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%69 = llvm.load %68 : !llvm.ptr -> i64
%70 = llvm.load %20 : !llvm.ptr -> i64
%71 = arith.constant 1 : i32
%73 = arith.extsi %71 : i32 to i64
%72 = arith.subi %70, %73 : i64
%74 = arith.cmpi slt, %69, %72 : i64
cf.cond_br %74, ^bb7, ^bb8
^bb7:
%76 = llvm.load %68 : !llvm.ptr -> i64
%77 = llvm.getelementptr %47[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%75 = llvm.load %77 : !llvm.ptr -> i64
%79 = llvm.load %68 : !llvm.ptr -> i64
%80 = llvm.getelementptr %2[%79] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%78 = llvm.load %80 : !llvm.ptr -> i64
%81 = arith.addi %75, %78 : i64
%83 = llvm.load %68 : !llvm.ptr -> i64
%84 = arith.constant 1 : i32
%86 = arith.extsi %84 : i32 to i64
%85 = arith.subi %83, %86 : i64
%87 = llvm.getelementptr %47[%85] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%82 = llvm.load %87 : !llvm.ptr -> i64
%88 = arith.addi %81, %82 : i64
%89 = llvm.load %68 : !llvm.ptr -> i64
%90 = arith.constant 1 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.addi %89, %92 : i64
%93 = llvm.getelementptr %47[%91] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %88, %93 : i64, !llvm.ptr
%94 = llvm.load %68 : !llvm.ptr -> i64
%95 = arith.constant 1 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.addi %94, %97 : i64
llvm.store %96, %68 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%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
%102 = llvm.mlir.constant(1 : i64) : i64
%103 = llvm.alloca %102 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %103 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%104 = llvm.load %103 : !llvm.ptr -> i64
%105 = arith.constant 0 : i32
%107 = arith.extsi %105 : i32 to i64
%106 = arith.cmpi sgt, %104, %107 : i64
cf.cond_br %106, ^bb10, ^bb11
^bb10:
%108 = arith.constant 0 : i32
%109 = arith.extsi %108 : i32 to i64
%110 = llvm.mlir.constant(1 : i64) : i64
%111 = llvm.alloca %110 x i64 : (i64) -> !llvm.ptr
llvm.store %109, %111 : i64, !llvm.ptr
%112 = llvm.load %20 : !llvm.ptr -> i64
%113 = arith.constant 1 : i32
%115 = arith.extsi %113 : i32 to i64
%114 = arith.subi %112, %115 : i64
%116 = llvm.mlir.constant(1 : i64) : i64
%117 = llvm.alloca %116 x i64 : (i64) -> !llvm.ptr
llvm.store %114, %117 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%118 = llvm.load %111 : !llvm.ptr -> i64
%119 = llvm.load %117 : !llvm.ptr -> i64
%120 = arith.cmpi slt, %118, %119 : i64
cf.cond_br %120, ^bb13, ^bb14
^bb13:
%121 = llvm.load %111 : !llvm.ptr -> i64
%122 = llvm.load %117 : !llvm.ptr -> i64
%123 = arith.addi %121, %122 : i64
%124 = arith.constant 1 : i32
%126 = arith.extsi %124 : i32 to i64
%125 = arith.addi %123, %126 : i64
%127 = arith.constant 2 : i32
%129 = arith.extsi %127 : i32 to i64
%128 = arith.divsi %125, %129 : i64
%131 = llvm.getelementptr %2[%128] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%130 = llvm.load %131 : !llvm.ptr -> i64
%132 = llvm.load %103 : !llvm.ptr -> i64
%133 = arith.cmpi sle, %130, %132 : i64
cf.cond_br %133, ^bb15, ^bb16
^bb15:
llvm.store %128, %111 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
%134 = arith.constant 1 : i32
%136 = arith.extsi %134 : i32 to i64
%135 = arith.subi %128, %136 : i64
llvm.store %135, %117 : i64, !llvm.ptr
cf.br ^bb17
^bb17:
cf.br ^bb12
^bb14:
%137 = llvm.load %101 : !llvm.ptr -> i64
%139 = llvm.load %111 : !llvm.ptr -> i64
%140 = llvm.getelementptr %47[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%138 = llvm.load %140 : !llvm.ptr -> i64
%141 = arith.addi %137, %138 : i64
%143 = llvm.load %111 : !llvm.ptr -> i64
%144 = llvm.getelementptr %2[%143] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%142 = llvm.load %144 : !llvm.ptr -> i64
%145 = arith.addi %141, %142 : i64
llvm.store %145, %101 : i64, !llvm.ptr
%146 = llvm.load %103 : !llvm.ptr -> i64
%148 = llvm.load %111 : !llvm.ptr -> i64
%149 = llvm.getelementptr %2[%148] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%147 = llvm.load %149 : !llvm.ptr -> i64
%150 = arith.subi %146, %147 : i64
llvm.store %150, %103 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%151 = llvm.mlir.addressof @str_0 : !llvm.ptr
%152 = llvm.load %101 : !llvm.ptr -> i64
%153 = llvm.call @printf(%151, %152) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%2) : (!llvm.ptr) -> ()
func.call @free(%47) : (!llvm.ptr) -> ()
%156 = arith.constant 0 : i32
func.return %156 : i32
}
}