Problem 297
Sum of Zeckendorf lengths for 0 < n < 10^17.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n log n) | O(n log n) |
| Space complexity | O(1) | O(log n) |
| Approach | Flow solution | Zeckendorf representation sum |
| Verdict | Optimal |
Flow source
# Project Euler 297
# Sum of Zeckendorf lengths for 0 < n < 10^17.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function search(x: i64, fib: ptr<i64>, fib_sum: ptr<i64>) -> i64 {
let mut pos: i64 = 0
while fib[pos + 1] <= x {
pos = pos + 1
}
let reduced: i64 = x - fib[pos]
if reduced == 0 {
return fib_sum[pos]
}
return fib_sum[pos] + reduced + search(reduced, fib, fib_sum)
}
function main() -> i32 {
let limit: i64 = 100000000000000000
let fib: ptr<i64> = calloc(100, 8)
let fib_sum: ptr<i64> = calloc(100, 8)
if fib == null || fib_sum == null { return 1 }
fib[0] = 1
fib[1] = 2
fib_sum[0] = 1
fib_sum[1] = 2
let mut size: i64 = 2
while fib[size - 1] < limit {
fib[size] = fib[size - 1] + fib[size - 2]
fib_sum[size] = fib_sum[size - 1] + fib_sum[size - 2] + fib[size - 2] - 1
size = size + 1
}
printf("%lld\n", search(limit - 1, fib, fib_sum))
free(fib)
free(fib_sum)
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; }
int64_t search_i64_ptr_i64_ptr_i64(int64_t x, int64_t* fib, int64_t* fib_sum);
int32_t main(void);
int64_t search_i64_ptr_i64_ptr_i64(int64_t x, int64_t* fib, int64_t* fib_sum) {
int64_t pos = 0;
while (fib[(pos + 1)] <= x) {
pos = (pos + 1);
}
int64_t reduced = (x - fib[pos]);
if (reduced == 0) {
return fib_sum[pos];
}
return ((fib_sum[pos] + reduced) + search_i64_ptr_i64_ptr_i64(reduced, fib, fib_sum));
}
int32_t main(void) {
int64_t limit = 100000000000000000;
int64_t* fib = (int64_t*)(calloc(100, 8));
int64_t* fib_sum = (int64_t*)(calloc(100, 8));
if ((fib == NULL || fib_sum == NULL)) {
return 1;
}
fib[0] = 1;
fib[1] = 2;
fib_sum[0] = 1;
fib_sum[1] = 2;
int64_t size = 2;
while (fib[(size - 1)] < limit) {
fib[size] = (fib[(size - 1)] + fib[(size - 2)]);
fib_sum[size] = (((fib_sum[(size - 1)] + fib_sum[(size - 2)]) + fib[(size - 2)]) - 1);
size = (size + 1);
}
printf("%lld\n", search_i64_ptr_i64_ptr_i64((limit - 1), fib, fib_sum));
free(fib);
free(fib_sum);
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 @search(%arg0: i64, %arg1: !llvm.ptr, %arg2: !llvm.ptr) -> i64 {
%0 = arith.constant 0 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%5 = llvm.load %3 : !llvm.ptr -> i64
%6 = arith.constant 1 : i32
%8 = arith.extsi %6 : i32 to i64
%7 = arith.addi %5, %8 : i64
%9 = llvm.getelementptr %arg1[%7] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%4 = llvm.load %9 : !llvm.ptr -> i64
%10 = arith.cmpi sle, %4, %arg0 : i64
cf.cond_br %10, ^bb1, ^bb2
^bb1:
%11 = llvm.load %3 : !llvm.ptr -> i64
%12 = arith.constant 1 : i32
%14 = arith.extsi %12 : i32 to i64
%13 = arith.addi %11, %14 : i64
llvm.store %13, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%16 = llvm.load %3 : !llvm.ptr -> i64
%17 = llvm.getelementptr %arg1[%16] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%15 = llvm.load %17 : !llvm.ptr -> i64
%18 = arith.subi %arg0, %15 : i64
%19 = arith.constant 0 : i32
%21 = arith.extsi %19 : i32 to i64
%20 = arith.cmpi eq, %18, %21 : i64
cf.cond_br %20, ^bb3, ^bb4
^bb3:
%23 = llvm.load %3 : !llvm.ptr -> i64
%24 = llvm.getelementptr %arg2[%23] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%22 = llvm.load %24 : !llvm.ptr -> i64
func.return %22 : i64
^bb4:
cf.br ^bb5
^bb5:
%26 = llvm.load %3 : !llvm.ptr -> i64
%27 = llvm.getelementptr %arg2[%26] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%25 = llvm.load %27 : !llvm.ptr -> i64
%28 = arith.addi %25, %18 : i64
%29 = func.call @search(%18, %arg1, %arg2) : (i64, !llvm.ptr, !llvm.ptr) -> i64
%30 = arith.addi %28, %29 : i64
func.return %30 : i64
}
func.func @main() -> i32 {
%31 = arith.constant 99999995705032704 : i32
%32 = arith.extsi %31 : i32 to i64
%34 = arith.constant 100 : i32
%35 = arith.constant 8 : i32
%36 = arith.extsi %34 : i32 to i64
%37 = arith.extsi %35 : i32 to i64
%33 = func.call @calloc(%36, %37) : (i64, i64) -> !llvm.ptr
%39 = arith.constant 100 : i32
%40 = arith.constant 8 : i32
%41 = arith.extsi %39 : i32 to i64
%42 = arith.extsi %40 : i32 to i64
%38 = func.call @calloc(%41, %42) : (i64, i64) -> !llvm.ptr
%43 = llvm.mlir.zero : !llvm.ptr
%44 = llvm.icmp "eq" %33, %43 : !llvm.ptr
%45 = scf.if %44 -> (i1) {
%46 = arith.constant true
scf.yield %46 : i1
} else {
%47 = llvm.mlir.zero : !llvm.ptr
%48 = llvm.icmp "eq" %38, %47 : !llvm.ptr
scf.yield %48 : i1
}
cf.cond_br %45, ^bb6, ^bb7
^bb6:
%49 = arith.constant 1 : i32
func.return %49 : i32
^bb7:
cf.br ^bb8
^bb8:
%50 = arith.constant 1 : i32
%51 = arith.constant 0 : i32
%52 = arith.extsi %50 : i32 to i64
%53 = arith.extsi %51 : i32 to i64
%54 = llvm.getelementptr %33[%53] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %52, %54 : i64, !llvm.ptr
%55 = arith.constant 2 : i32
%56 = arith.constant 1 : i32
%57 = arith.extsi %55 : i32 to i64
%58 = arith.extsi %56 : i32 to i64
%59 = llvm.getelementptr %33[%58] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %57, %59 : i64, !llvm.ptr
%60 = arith.constant 1 : i32
%61 = arith.constant 0 : i32
%62 = arith.extsi %60 : i32 to i64
%63 = arith.extsi %61 : i32 to i64
%64 = llvm.getelementptr %38[%63] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %62, %64 : i64, !llvm.ptr
%65 = arith.constant 2 : i32
%66 = arith.constant 1 : i32
%67 = arith.extsi %65 : i32 to i64
%68 = arith.extsi %66 : i32 to i64
%69 = llvm.getelementptr %38[%68] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %67, %69 : i64, !llvm.ptr
%70 = arith.constant 2 : i32
%71 = arith.extsi %70 : i32 to i64
%72 = llvm.mlir.constant(1 : i64) : i64
%73 = llvm.alloca %72 x i64 : (i64) -> !llvm.ptr
llvm.store %71, %73 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%75 = llvm.load %73 : !llvm.ptr -> i64
%76 = arith.constant 1 : i32
%78 = arith.extsi %76 : i32 to i64
%77 = arith.subi %75, %78 : i64
%79 = llvm.getelementptr %33[%77] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%74 = llvm.load %79 : !llvm.ptr -> i64
%80 = arith.cmpi slt, %74, %32 : i64
cf.cond_br %80, ^bb10, ^bb11
^bb10:
%82 = llvm.load %73 : !llvm.ptr -> i64
%83 = arith.constant 1 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.subi %82, %85 : i64
%86 = llvm.getelementptr %33[%84] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%81 = llvm.load %86 : !llvm.ptr -> i64
%88 = llvm.load %73 : !llvm.ptr -> i64
%89 = arith.constant 2 : i32
%91 = arith.extsi %89 : i32 to i64
%90 = arith.subi %88, %91 : i64
%92 = llvm.getelementptr %33[%90] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%87 = llvm.load %92 : !llvm.ptr -> i64
%93 = arith.addi %81, %87 : i64
%94 = llvm.load %73 : !llvm.ptr -> i64
%95 = llvm.getelementptr %33[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %93, %95 : i64, !llvm.ptr
%97 = llvm.load %73 : !llvm.ptr -> i64
%98 = arith.constant 1 : i32
%100 = arith.extsi %98 : i32 to i64
%99 = arith.subi %97, %100 : i64
%101 = llvm.getelementptr %38[%99] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%96 = llvm.load %101 : !llvm.ptr -> i64
%103 = llvm.load %73 : !llvm.ptr -> i64
%104 = arith.constant 2 : i32
%106 = arith.extsi %104 : i32 to i64
%105 = arith.subi %103, %106 : i64
%107 = llvm.getelementptr %38[%105] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%102 = llvm.load %107 : !llvm.ptr -> i64
%108 = arith.addi %96, %102 : i64
%110 = llvm.load %73 : !llvm.ptr -> i64
%111 = arith.constant 2 : i32
%113 = arith.extsi %111 : i32 to i64
%112 = arith.subi %110, %113 : i64
%114 = llvm.getelementptr %33[%112] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%109 = llvm.load %114 : !llvm.ptr -> i64
%115 = arith.addi %108, %109 : i64
%116 = arith.constant 1 : i32
%118 = arith.extsi %116 : i32 to i64
%117 = arith.subi %115, %118 : i64
%119 = llvm.load %73 : !llvm.ptr -> i64
%120 = llvm.getelementptr %38[%119] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %117, %120 : i64, !llvm.ptr
%121 = llvm.load %73 : !llvm.ptr -> i64
%122 = arith.constant 1 : i32
%124 = arith.extsi %122 : i32 to i64
%123 = arith.addi %121, %124 : i64
llvm.store %123, %73 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%125 = llvm.mlir.addressof @str_0 : !llvm.ptr
%127 = arith.constant 1 : i32
%129 = arith.extsi %127 : i32 to i64
%128 = arith.subi %32, %129 : i64
%126 = func.call @search(%128, %33, %38) : (i64, !llvm.ptr, !llvm.ptr) -> i64
%130 = llvm.call @printf(%125, %126) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%33) : (!llvm.ptr) -> ()
func.call @free(%38) : (!llvm.ptr) -> ()
%133 = arith.constant 0 : i32
func.return %133 : i32
}
}