← All problems
Problem 384
Rudin–Shapiro: sum_{t=2..45} g(F_t, F_{t-1}).
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 384
# Rudin–Shapiro: sum_{t=2..45} g(F_t, F_{t-1}).
function bit_length(n0: i64) -> i32 {
let mut n: i64 = n0
let mut b: i32 = 0
while n > 0 {
b = b + 1
n = n / 2
}
return b
}
function g_index(t0: i64, c0: i64) -> i64 {
let mut t: i64 = t0
let mut c: i64 = c0
let mut acc: i64 = 0
while true {
if t == 1 {
return acc
}
let h: i64 = 1 << (bit_length(t) - 1)
let d: i64 = t - h
if d == 0 {
if c <= t / 2 {
acc = acc + t * t / 4
t = t / 2
} else {
acc = acc + t * t / 2
c = c - t / 2
}
} elif c > h {
acc = acc + h * h
t = 2 * h - d
c = c - h
} elif c > h - d {
acc = acc + h * h
t = d
c = c + d - h
} elif c <= d {
acc = acc + h * h / 2
t = d
} else {
acc = acc + h * h / 2
t = 2 * h - t
}
}
return acc
}
function main() -> i32 {
let F: ptr<i64> = calloc(46, 8)
if F == null { return 1 }
F[0] = 1
F[1] = 1
let mut i: i32 = 2
while i <= 45 {
F[i] = F[i - 1] + F[i - 2]
i = i + 1
}
let mut total: i64 = 0
let mut t: i32 = 2
while t <= 45 {
total = total + g_index(F[t], F[t - 1])
t = t + 1
}
printf("%lld\n", total)
free(F)
return 0
}
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
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 bit_length_i64(int64_t n0);
int64_t g_index_i64_i64(int64_t t0, int64_t c0);
int32_t main(void);
int32_t bit_length_i64(int64_t n0) {
int64_t n = n0;
int32_t b = 0;
while (n > 0) {
b = (b + 1);
n = FLOW_CHECKED_DIV((n), (2));
}
return b;
}
int64_t g_index_i64_i64(int64_t t0, int64_t c0) {
int64_t t = t0;
int64_t c = c0;
int64_t acc = 0;
while (1) {
if (t == 1) {
return acc;
}
int64_t h = FLOW_CHECKED_SHL((1), ((bit_length_i64(t) - 1)));
int64_t d = (t - h);
if (d == 0) {
if (c <= FLOW_CHECKED_DIV((t), (2))) {
acc = (acc + FLOW_CHECKED_DIV(((t * t)), (4)));
t = FLOW_CHECKED_DIV((t), (2));
} else {
acc = (acc + FLOW_CHECKED_DIV(((t * t)), (2)));
c = (c - FLOW_CHECKED_DIV((t), (2)));
}
} else if (c > h) {
acc = (acc + (h * h));
t = ((2 * h) - d);
c = (c - h);
} else if (c > (h - d)) {
acc = (acc + (h * h));
t = d;
c = ((c + d) - h);
} else if (c <= d) {
acc = (acc + FLOW_CHECKED_DIV(((h * h)), (2)));
t = d;
} else {
acc = (acc + FLOW_CHECKED_DIV(((h * h)), (2)));
t = ((2 * h) - t);
}
}
return acc;
}
int32_t main(void) {
int64_t* F = (int64_t*)(calloc(46, 8));
if (F == NULL) {
return 1;
}
F[0] = 1;
F[1] = 1;
int32_t i = 2;
while (i <= 45) {
F[i] = (F[(i - 1)] + F[(i - 2)]);
i = (i + 1);
}
int64_t total = 0;
int32_t t = 2;
while (t <= 45) {
total = (total + g_index_i64_i64(F[t], F[(t - 1)]));
t = (t + 1);
}
printf("%lld\n", total);
free(F);
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 @bit_length(%arg0: i64) -> i32 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %1 : i64, !llvm.ptr
%2 = arith.constant 0 : i32
%3 = llvm.mlir.constant(1 : i64) : i64
%4 = llvm.alloca %3 x i32 : (i64) -> !llvm.ptr
llvm.store %2, %4 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%5 = llvm.load %1 : !llvm.ptr -> i64
%6 = arith.constant 0 : i32
%8 = arith.extsi %6 : i32 to i64
%7 = arith.cmpi sgt, %5, %8 : i64
cf.cond_br %7, ^bb1, ^bb2
^bb1:
%9 = llvm.load %4 : !llvm.ptr -> i32
%10 = arith.constant 1 : i32
%11 = arith.addi %9, %10 : i32
llvm.store %11, %4 : i32, !llvm.ptr
%12 = llvm.load %1 : !llvm.ptr -> i64
%13 = arith.constant 2 : i32
%15 = arith.extsi %13 : i32 to i64
%14 = arith.divsi %12, %15 : i64
llvm.store %14, %1 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%16 = llvm.load %4 : !llvm.ptr -> i32
func.return %16 : i32
}
func.func @g_index(%arg0: i64, %arg1: i64) -> i64 {
%17 = llvm.mlir.constant(1 : i64) : i64
%18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %18 : i64, !llvm.ptr
%19 = llvm.mlir.constant(1 : i64) : i64
%20 = llvm.alloca %19 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %20 : i64, !llvm.ptr
%21 = arith.constant 0 : i32
%22 = arith.extsi %21 : i32 to i64
%23 = llvm.mlir.constant(1 : i64) : i64
%24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
llvm.store %22, %24 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%25 = arith.constant 1 : i1
cf.cond_br %25, ^bb4, ^bb5
^bb4:
%26 = llvm.load %18 : !llvm.ptr -> i64
%27 = arith.constant 1 : i32
%29 = arith.extsi %27 : i32 to i64
%28 = arith.cmpi eq, %26, %29 : i64
cf.cond_br %28, ^bb6, ^bb7
^bb6:
%30 = llvm.load %24 : !llvm.ptr -> i64
func.return %30 : i64
^bb7:
cf.br ^bb8
^bb8:
%31 = arith.constant 1 : i32
%33 = llvm.load %18 : !llvm.ptr -> i64
%32 = func.call @bit_length(%33) : (i64) -> i32
%34 = arith.constant 1 : i32
%35 = arith.subi %32, %34 : i32
%36 = arith.shli %31, %35 : i32
%37 = arith.extsi %36 : i32 to i64
%38 = llvm.load %18 : !llvm.ptr -> i64
%39 = arith.subi %38, %37 : i64
%40 = arith.constant 0 : i32
%42 = arith.extsi %40 : i32 to i64
%41 = arith.cmpi eq, %39, %42 : i64
cf.cond_br %41, ^bb9, ^bb10
^bb9:
%43 = llvm.load %20 : !llvm.ptr -> i64
%44 = llvm.load %18 : !llvm.ptr -> i64
%45 = arith.constant 2 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.divsi %44, %47 : i64
%48 = arith.cmpi sle, %43, %46 : i64
cf.cond_br %48, ^bb12, ^bb13
^bb12:
%49 = llvm.load %24 : !llvm.ptr -> i64
%50 = llvm.load %18 : !llvm.ptr -> i64
%51 = llvm.load %18 : !llvm.ptr -> i64
%52 = arith.muli %50, %51 : i64
%53 = arith.constant 4 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.divsi %52, %55 : i64
%56 = arith.addi %49, %54 : i64
llvm.store %56, %24 : i64, !llvm.ptr
%57 = llvm.load %18 : !llvm.ptr -> i64
%58 = arith.constant 2 : i32
%60 = arith.extsi %58 : i32 to i64
%59 = arith.divsi %57, %60 : i64
llvm.store %59, %18 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
%61 = llvm.load %24 : !llvm.ptr -> i64
%62 = llvm.load %18 : !llvm.ptr -> i64
%63 = llvm.load %18 : !llvm.ptr -> i64
%64 = arith.muli %62, %63 : i64
%65 = arith.constant 2 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.divsi %64, %67 : i64
%68 = arith.addi %61, %66 : i64
llvm.store %68, %24 : i64, !llvm.ptr
%69 = llvm.load %20 : !llvm.ptr -> i64
%70 = llvm.load %18 : !llvm.ptr -> i64
%71 = arith.constant 2 : i32
%73 = arith.extsi %71 : i32 to i64
%72 = arith.divsi %70, %73 : i64
%74 = arith.subi %69, %72 : i64
llvm.store %74, %20 : i64, !llvm.ptr
cf.br ^bb14
^bb14:
cf.br ^bb11
^bb10:
%75 = llvm.load %20 : !llvm.ptr -> i64
%76 = arith.cmpi sgt, %75, %37 : i64
cf.cond_br %76, ^bb16, ^bb15
^bb16:
%77 = llvm.load %24 : !llvm.ptr -> i64
%78 = arith.muli %37, %37 : i64
%79 = arith.addi %77, %78 : i64
llvm.store %79, %24 : i64, !llvm.ptr
%80 = arith.constant 2 : i32
%82 = arith.extsi %80 : i32 to i64
%81 = arith.muli %82, %37 : i64
%83 = arith.subi %81, %39 : i64
llvm.store %83, %18 : i64, !llvm.ptr
%84 = llvm.load %20 : !llvm.ptr -> i64
%85 = arith.subi %84, %37 : i64
llvm.store %85, %20 : i64, !llvm.ptr
cf.br ^bb11
^bb15:
%86 = llvm.load %20 : !llvm.ptr -> i64
%87 = arith.subi %37, %39 : i64
%88 = arith.cmpi sgt, %86, %87 : i64
cf.cond_br %88, ^bb18, ^bb17
^bb18:
%89 = llvm.load %24 : !llvm.ptr -> i64
%90 = arith.muli %37, %37 : i64
%91 = arith.addi %89, %90 : i64
llvm.store %91, %24 : i64, !llvm.ptr
llvm.store %39, %18 : i64, !llvm.ptr
%92 = llvm.load %20 : !llvm.ptr -> i64
%93 = arith.addi %92, %39 : i64
%94 = arith.subi %93, %37 : i64
llvm.store %94, %20 : i64, !llvm.ptr
cf.br ^bb11
^bb17:
%95 = llvm.load %20 : !llvm.ptr -> i64
%96 = arith.cmpi sle, %95, %39 : i64
cf.cond_br %96, ^bb20, ^bb19
^bb20:
%97 = llvm.load %24 : !llvm.ptr -> i64
%98 = arith.muli %37, %37 : i64
%99 = arith.constant 2 : i32
%101 = arith.extsi %99 : i32 to i64
%100 = arith.divsi %98, %101 : i64
%102 = arith.addi %97, %100 : i64
llvm.store %102, %24 : i64, !llvm.ptr
llvm.store %39, %18 : i64, !llvm.ptr
cf.br ^bb11
^bb19:
%103 = llvm.load %24 : !llvm.ptr -> i64
%104 = arith.muli %37, %37 : i64
%105 = arith.constant 2 : i32
%107 = arith.extsi %105 : i32 to i64
%106 = arith.divsi %104, %107 : i64
%108 = arith.addi %103, %106 : i64
llvm.store %108, %24 : i64, !llvm.ptr
%109 = arith.constant 2 : i32
%111 = arith.extsi %109 : i32 to i64
%110 = arith.muli %111, %37 : i64
%112 = llvm.load %18 : !llvm.ptr -> i64
%113 = arith.subi %110, %112 : i64
llvm.store %113, %18 : i64, !llvm.ptr
cf.br ^bb11
^bb11:
cf.br ^bb3
^bb5:
%114 = llvm.load %24 : !llvm.ptr -> i64
func.return %114 : i64
}
func.func @main() -> i32 {
%116 = arith.constant 46 : i32
%117 = arith.constant 8 : i32
%118 = arith.extsi %116 : i32 to i64
%119 = arith.extsi %117 : i32 to i64
%115 = func.call @calloc(%118, %119) : (i64, i64) -> !llvm.ptr
%120 = llvm.mlir.zero : !llvm.ptr
%121 = llvm.icmp "eq" %115, %120 : !llvm.ptr
cf.cond_br %121, ^bb21, ^bb22
^bb21:
%122 = arith.constant 1 : i32
func.return %122 : i32
^bb22:
cf.br ^bb23
^bb23:
%123 = arith.constant 1 : i32
%124 = arith.constant 0 : i32
%125 = arith.extsi %123 : i32 to i64
%126 = arith.extsi %124 : i32 to i64
%127 = llvm.getelementptr %115[%126] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %125, %127 : i64, !llvm.ptr
%128 = arith.constant 1 : i32
%129 = arith.constant 1 : i32
%130 = arith.extsi %128 : i32 to i64
%131 = arith.extsi %129 : i32 to i64
%132 = llvm.getelementptr %115[%131] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %130, %132 : i64, !llvm.ptr
%133 = arith.constant 2 : i32
%134 = llvm.mlir.constant(1 : i64) : i64
%135 = llvm.alloca %134 x i32 : (i64) -> !llvm.ptr
llvm.store %133, %135 : i32, !llvm.ptr
cf.br ^bb24
^bb24:
%136 = llvm.load %135 : !llvm.ptr -> i32
%137 = arith.constant 45 : i32
%138 = arith.cmpi sle, %136, %137 : i32
cf.cond_br %138, ^bb25, ^bb26
^bb25:
%140 = llvm.load %135 : !llvm.ptr -> i32
%141 = arith.constant 1 : i32
%142 = arith.subi %140, %141 : i32
%143 = arith.extsi %142 : i32 to i64
%144 = llvm.getelementptr %115[%143] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%139 = llvm.load %144 : !llvm.ptr -> i64
%146 = llvm.load %135 : !llvm.ptr -> i32
%147 = arith.constant 2 : i32
%148 = arith.subi %146, %147 : i32
%149 = arith.extsi %148 : i32 to i64
%150 = llvm.getelementptr %115[%149] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%145 = llvm.load %150 : !llvm.ptr -> i64
%151 = arith.addi %139, %145 : i64
%152 = llvm.load %135 : !llvm.ptr -> i32
%153 = arith.extsi %152 : i32 to i64
%154 = llvm.getelementptr %115[%153] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %151, %154 : i64, !llvm.ptr
%155 = llvm.load %135 : !llvm.ptr -> i32
%156 = arith.constant 1 : i32
%157 = arith.addi %155, %156 : i32
llvm.store %157, %135 : i32, !llvm.ptr
cf.br ^bb24
^bb26:
%158 = arith.constant 0 : i32
%159 = arith.extsi %158 : i32 to i64
%160 = llvm.mlir.constant(1 : i64) : i64
%161 = llvm.alloca %160 x i64 : (i64) -> !llvm.ptr
llvm.store %159, %161 : i64, !llvm.ptr
%162 = arith.constant 2 : i32
%163 = llvm.mlir.constant(1 : i64) : i64
%164 = llvm.alloca %163 x i32 : (i64) -> !llvm.ptr
llvm.store %162, %164 : i32, !llvm.ptr
cf.br ^bb27
^bb27:
%165 = llvm.load %164 : !llvm.ptr -> i32
%166 = arith.constant 45 : i32
%167 = arith.cmpi sle, %165, %166 : i32
cf.cond_br %167, ^bb28, ^bb29
^bb28:
%168 = llvm.load %161 : !llvm.ptr -> i64
%171 = llvm.load %164 : !llvm.ptr -> i32
%172 = arith.extsi %171 : i32 to i64
%173 = llvm.getelementptr %115[%172] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%170 = llvm.load %173 : !llvm.ptr -> i64
%175 = llvm.load %164 : !llvm.ptr -> i32
%176 = arith.constant 1 : i32
%177 = arith.subi %175, %176 : i32
%178 = arith.extsi %177 : i32 to i64
%179 = llvm.getelementptr %115[%178] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%174 = llvm.load %179 : !llvm.ptr -> i64
%169 = func.call @g_index(%170, %174) : (i64, i64) -> i64
%180 = arith.addi %168, %169 : i64
llvm.store %180, %161 : i64, !llvm.ptr
%181 = llvm.load %164 : !llvm.ptr -> i32
%182 = arith.constant 1 : i32
%183 = arith.addi %181, %182 : i32
llvm.store %183, %164 : i32, !llvm.ptr
cf.br ^bb27
^bb29:
%184 = llvm.mlir.addressof @str_0 : !llvm.ptr
%185 = llvm.load %161 : !llvm.ptr -> i64
%186 = llvm.call @printf(%184, %185) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%115) : (!llvm.ptr) -> ()
%188 = arith.constant 0 : i32
func.return %188 : i32
}
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
}