Problem 265
Sum of binary circle encodings S(5).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(2^n) | O(2^n) |
| Space complexity | O(1) | O(2^n) |
| Approach | Flow solution | Bitmask enumeration |
| Verdict | Optimal |
Flow source
# Project Euler 265
# Sum of binary circle encodings S(5).
function search(history: i64, sequence: i64, bits: i64) -> i64 {
let one: i64 = 1
let mask: i64 = (one << bits) - 1
let all_bits: i64 = (one << (one << bits)) - 1
if history == all_bits {
return sequence >> (bits - 1)
}
let next_value: i64 = (sequence << 1) & mask
let z: i64 = next_value
let o: i64 = next_value + 1
let mut result: i64 = 0
if (history & (one << z)) == 0 {
result = result + search(history | (one << z), sequence << 1, bits)
}
if (history & (one << o)) == 0 {
result = result + search(history | (one << o), (sequence << 1) | 1, bits)
}
return result
}
function main() -> i32 {
printf("%lld\n", search(1, 0, 5))
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_i64_i64(int64_t history, int64_t sequence, int64_t bits);
int32_t main(void);
int64_t search_i64_i64_i64(int64_t history, int64_t sequence, int64_t bits) {
int64_t one = 1;
int64_t mask = (FLOW_CHECKED_SHL((one), (bits)) - 1);
int64_t all_bits = (FLOW_CHECKED_SHL((one), (FLOW_CHECKED_SHL((one), (bits)))) - 1);
if (history == all_bits) {
return FLOW_CHECKED_SHR((sequence), ((bits - 1)));
}
int64_t next_value = (FLOW_CHECKED_SHL((sequence), (1)) & mask);
int64_t z = next_value;
int64_t o = (next_value + 1);
int64_t result = 0;
if ((history & FLOW_CHECKED_SHL((one), (z))) == 0) {
result = (result + search_i64_i64_i64((history | FLOW_CHECKED_SHL((one), (z))), FLOW_CHECKED_SHL((sequence), (1)), bits));
}
if ((history & FLOW_CHECKED_SHL((one), (o))) == 0) {
result = (result + search_i64_i64_i64((history | FLOW_CHECKED_SHL((one), (o))), (FLOW_CHECKED_SHL((sequence), (1)) | 1), bits));
}
return result;
}
int32_t main(void) {
printf("%lld\n", search_i64_i64_i64(1, 0, 5));
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 @search(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.constant 1 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.shli %1, %arg2 : i64
%3 = arith.constant 1 : i32
%5 = arith.extsi %3 : i32 to i64
%4 = arith.subi %2, %5 : i64
%6 = arith.shli %1, %arg2 : i64
%7 = arith.shli %1, %6 : i64
%8 = arith.constant 1 : i32
%10 = arith.extsi %8 : i32 to i64
%9 = arith.subi %7, %10 : i64
%11 = arith.cmpi eq, %arg0, %9 : i64
cf.cond_br %11, ^bb0, ^bb1
^bb0:
%12 = arith.constant 1 : i32
%14 = arith.extsi %12 : i32 to i64
%13 = arith.subi %arg2, %14 : i64
%15 = arith.shrsi %arg1, %13 : i64
func.return %15 : i64
^bb1:
cf.br ^bb2
^bb2:
%16 = arith.constant 1 : i32
%18 = arith.extsi %16 : i32 to i64
%17 = arith.shli %arg1, %18 : i64
%19 = arith.andi %17, %4 : i64
%20 = arith.constant 1 : i32
%22 = arith.extsi %20 : i32 to i64
%21 = arith.addi %19, %22 : i64
%23 = arith.constant 0 : i32
%24 = arith.extsi %23 : i32 to i64
%25 = llvm.mlir.constant(1 : i64) : i64
%26 = llvm.alloca %25 x i64 : (i64) -> !llvm.ptr
llvm.store %24, %26 : i64, !llvm.ptr
%27 = arith.shli %1, %19 : i64
%28 = arith.andi %arg0, %27 : i64
%29 = arith.constant 0 : i32
%31 = arith.extsi %29 : i32 to i64
%30 = arith.cmpi eq, %28, %31 : i64
cf.cond_br %30, ^bb3, ^bb4
^bb3:
%32 = llvm.load %26 : !llvm.ptr -> i64
%34 = arith.shli %1, %19 : i64
%35 = arith.ori %arg0, %34 : i64
%36 = arith.constant 1 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.shli %arg1, %38 : i64
%33 = func.call @search(%35, %37, %arg2) : (i64, i64, i64) -> i64
%39 = arith.addi %32, %33 : i64
llvm.store %39, %26 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%40 = arith.shli %1, %21 : i64
%41 = arith.andi %arg0, %40 : i64
%42 = arith.constant 0 : i32
%44 = arith.extsi %42 : i32 to i64
%43 = arith.cmpi eq, %41, %44 : i64
cf.cond_br %43, ^bb6, ^bb7
^bb6:
%45 = llvm.load %26 : !llvm.ptr -> i64
%47 = arith.shli %1, %21 : i64
%48 = arith.ori %arg0, %47 : i64
%49 = arith.constant 1 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.shli %arg1, %51 : i64
%52 = arith.constant 1 : i32
%54 = arith.extsi %52 : i32 to i64
%53 = arith.ori %50, %54 : i64
%46 = func.call @search(%48, %53, %arg2) : (i64, i64, i64) -> i64
%55 = arith.addi %45, %46 : i64
llvm.store %55, %26 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%56 = llvm.load %26 : !llvm.ptr -> i64
func.return %56 : i64
}
func.func @main() -> i32 {
%57 = llvm.mlir.addressof @str_0 : !llvm.ptr
%59 = arith.constant 1 : i32
%60 = arith.constant 0 : i32
%61 = arith.constant 5 : i32
%62 = arith.extsi %59 : i32 to i64
%63 = arith.extsi %60 : i32 to i64
%64 = arith.extsi %61 : i32 to i64
%58 = func.call @search(%62, %63, %64) : (i64, i64, i64) -> i64
%65 = llvm.call @printf(%57, %58) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%66 = arith.constant 0 : i32
func.return %66 : i32
}
}