← All problems
Problem 341
Sum of Golomb self-describing sequence values at cubes 1^3..999999^3.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n log log n)
Space complexity O(n)O(n)
Approach Flow solution Sieve or enumeration
Verdict Suboptimal
Flow source
# Project Euler 341
# Sum of Golomb self-describing sequence values at cubes 1^3..999999^3.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function ceil(x: f64) -> f64
}
function main() -> i32 {
let limit: i64 = 1000000
let cubic_limit: i64 = limit * limit * limit
let cap: i64 = 12000000
let golomb: ptr<i32> = calloc(cap, 4)
if golomb == null { return 1 }
golomb[1] = 1
let mut products: i64 = 1
let mut i: i64 = 2
while products < cubic_limit {
let gi: i32 = 1 + golomb[i - (golomb[golomb[i - 1]] as i64)]
golomb[i] = gi
products = products + (gi as i64) * i
i = i + 1
}
let gmax: i64 = i
let mut total: i64 = 0
let mut last_sums: i64 = 0
let mut sums: i64 = 1
let mut last_products: i64 = 0
products = 1
let mut index: i64 = 1
i = 1
while i < limit {
let n: i64 = i * i * i
while products < n {
index = index + 1
last_sums = sums
sums = sums + (golomb[index] as i64)
last_products = products
products = products + (golomb[index] as i64) * index
}
let from_v: i64 = last_products
let to_v: i64 = products
let ratio: f64 = ((n - from_v) as f64) / ((to_v - from_v) as f64)
let low: i64 = last_sums
let high: i64 = sums
let offset: i64 = ceil(((high - low) as f64) * ratio) as i64
total = total + offset + low
i = i + 1
}
printf("%lld\n", total)
free(golomb)
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 limit = 1000000;
int64_t cubic_limit = ((limit * limit) * limit);
int64_t cap = 12000000;
int32_t* golomb = (int32_t*)(calloc(cap, 4));
if (golomb == NULL) {
return 1;
}
golomb[1] = 1;
int64_t products = 1;
int64_t i = 2;
while (products < cubic_limit) {
int32_t gi = (1 + golomb[(i - ((int64_t)(golomb[golomb[(i - 1)]])))]);
golomb[i] = gi;
products = (products + (((int64_t)(gi)) * i));
i = (i + 1);
}
int64_t gmax = i;
int64_t total = 0;
int64_t last_sums = 0;
int64_t sums = 1;
int64_t last_products = 0;
products = 1;
int64_t index = 1;
i = 1;
while (i < limit) {
int64_t n = ((i * i) * i);
while (products < n) {
index = (index + 1);
last_sums = sums;
sums = (sums + ((int64_t)(golomb[index])));
last_products = products;
products = (products + (((int64_t)(golomb[index])) * index));
}
int64_t from_v = last_products;
int64_t to_v = products;
double ratio = (((double)((n - from_v))) / ((double)((to_v - from_v))));
int64_t low = last_sums;
int64_t high = sums;
int64_t offset = ((int64_t)(ceil((((double)((high - low))) * ratio))));
total = ((total + offset) + low);
i = (i + 1);
}
printf("%lld\n", total);
free(golomb);
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 private @ceil(f64) -> f64
func.func @main() -> i32 {
%0 = arith.constant 1000000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.muli %1, %1 : i64
%3 = arith.muli %2, %1 : i64
%4 = arith.constant 12000000 : i32
%5 = arith.extsi %4 : i32 to i64
%7 = arith.constant 4 : i32
%8 = arith.extsi %7 : i32 to i64
%6 = func.call @calloc(%5, %8) : (i64, i64) -> !llvm.ptr
%9 = llvm.mlir.zero : !llvm.ptr
%10 = llvm.icmp "eq" %6, %9 : !llvm.ptr
cf.cond_br %10, ^bb0, ^bb1
^bb0:
%11 = arith.constant 1 : i32
func.return %11 : i32
^bb1:
cf.br ^bb2
^bb2:
%12 = arith.constant 1 : i32
%13 = arith.constant 1 : i32
%14 = arith.extsi %13 : i32 to i64
%15 = llvm.getelementptr %6[%14] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %12, %15 : i32, !llvm.ptr
%16 = arith.constant 1 : i32
%17 = arith.extsi %16 : i32 to i64
%18 = llvm.mlir.constant(1 : i64) : i64
%19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
llvm.store %17, %19 : i64, !llvm.ptr
%20 = arith.constant 2 : i32
%21 = arith.extsi %20 : i32 to i64
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
llvm.store %21, %23 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%24 = llvm.load %19 : !llvm.ptr -> i64
%25 = arith.cmpi slt, %24, %3 : i64
cf.cond_br %25, ^bb4, ^bb5
^bb4:
%26 = arith.constant 1 : i32
%28 = llvm.load %23 : !llvm.ptr -> i64
%31 = llvm.load %23 : !llvm.ptr -> i64
%32 = arith.constant 1 : i32
%34 = arith.extsi %32 : i32 to i64
%33 = arith.subi %31, %34 : i64
%35 = llvm.getelementptr %6[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%30 = llvm.load %35 : !llvm.ptr -> i32
%36 = arith.extsi %30 : i32 to i64
%37 = llvm.getelementptr %6[%36] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%29 = llvm.load %37 : !llvm.ptr -> i32
%38 = arith.extsi %29 : i32 to i64
%39 = arith.subi %28, %38 : i64
%40 = llvm.getelementptr %6[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%27 = llvm.load %40 : !llvm.ptr -> i32
%41 = arith.addi %26, %27 : i32
%42 = llvm.load %23 : !llvm.ptr -> i64
%43 = llvm.getelementptr %6[%42] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %41, %43 : i32, !llvm.ptr
%44 = llvm.load %19 : !llvm.ptr -> i64
%45 = arith.extsi %41 : i32 to i64
%46 = llvm.load %23 : !llvm.ptr -> i64
%47 = arith.muli %45, %46 : i64
%48 = arith.addi %44, %47 : i64
llvm.store %48, %19 : i64, !llvm.ptr
%49 = llvm.load %23 : !llvm.ptr -> i64
%50 = arith.constant 1 : i32
%52 = arith.extsi %50 : i32 to i64
%51 = arith.addi %49, %52 : i64
llvm.store %51, %23 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%53 = llvm.load %23 : !llvm.ptr -> i64
%54 = arith.constant 0 : i32
%55 = arith.extsi %54 : i32 to i64
%56 = llvm.mlir.constant(1 : i64) : i64
%57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
llvm.store %55, %57 : i64, !llvm.ptr
%58 = arith.constant 0 : i32
%59 = arith.extsi %58 : i32 to i64
%60 = llvm.mlir.constant(1 : i64) : i64
%61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
llvm.store %59, %61 : i64, !llvm.ptr
%62 = arith.constant 1 : i32
%63 = arith.extsi %62 : i32 to i64
%64 = llvm.mlir.constant(1 : i64) : i64
%65 = llvm.alloca %64 x i64 : (i64) -> !llvm.ptr
llvm.store %63, %65 : i64, !llvm.ptr
%66 = arith.constant 0 : i32
%67 = arith.extsi %66 : i32 to i64
%68 = llvm.mlir.constant(1 : i64) : i64
%69 = llvm.alloca %68 x i64 : (i64) -> !llvm.ptr
llvm.store %67, %69 : i64, !llvm.ptr
%70 = arith.constant 1 : i32
%71 = arith.extsi %70 : i32 to i64
llvm.store %71, %19 : i64, !llvm.ptr
%72 = arith.constant 1 : i32
%73 = arith.extsi %72 : i32 to i64
%74 = llvm.mlir.constant(1 : i64) : i64
%75 = llvm.alloca %74 x i64 : (i64) -> !llvm.ptr
llvm.store %73, %75 : i64, !llvm.ptr
%76 = arith.constant 1 : i32
%77 = arith.extsi %76 : i32 to i64
llvm.store %77, %23 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%78 = llvm.load %23 : !llvm.ptr -> i64
%79 = arith.cmpi slt, %78, %1 : i64
cf.cond_br %79, ^bb7, ^bb8
^bb7:
%80 = llvm.load %23 : !llvm.ptr -> i64
%81 = llvm.load %23 : !llvm.ptr -> i64
%82 = arith.muli %80, %81 : i64
%83 = llvm.load %23 : !llvm.ptr -> i64
%84 = arith.muli %82, %83 : i64
cf.br ^bb9
^bb9:
%85 = llvm.load %19 : !llvm.ptr -> i64
%86 = arith.cmpi slt, %85, %84 : i64
cf.cond_br %86, ^bb10, ^bb11
^bb10:
%87 = llvm.load %75 : !llvm.ptr -> i64
%88 = arith.constant 1 : i32
%90 = arith.extsi %88 : i32 to i64
%89 = arith.addi %87, %90 : i64
llvm.store %89, %75 : i64, !llvm.ptr
%91 = llvm.load %65 : !llvm.ptr -> i64
llvm.store %91, %61 : i64, !llvm.ptr
%92 = llvm.load %65 : !llvm.ptr -> i64
%94 = llvm.load %75 : !llvm.ptr -> i64
%95 = llvm.getelementptr %6[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%93 = llvm.load %95 : !llvm.ptr -> i32
%96 = arith.extsi %93 : i32 to i64
%97 = arith.addi %92, %96 : i64
llvm.store %97, %65 : i64, !llvm.ptr
%98 = llvm.load %19 : !llvm.ptr -> i64
llvm.store %98, %69 : i64, !llvm.ptr
%99 = llvm.load %19 : !llvm.ptr -> i64
%101 = llvm.load %75 : !llvm.ptr -> i64
%102 = llvm.getelementptr %6[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%100 = llvm.load %102 : !llvm.ptr -> i32
%103 = arith.extsi %100 : i32 to i64
%104 = llvm.load %75 : !llvm.ptr -> i64
%105 = arith.muli %103, %104 : i64
%106 = arith.addi %99, %105 : i64
llvm.store %106, %19 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%107 = llvm.load %69 : !llvm.ptr -> i64
%108 = llvm.load %19 : !llvm.ptr -> i64
%109 = arith.subi %84, %107 : i64
%110 = arith.sitofp %109 : i64 to f64
%111 = arith.subi %108, %107 : i64
%112 = arith.sitofp %111 : i64 to f64
%113 = arith.divf %110, %112 : f64
%114 = llvm.load %61 : !llvm.ptr -> i64
%115 = llvm.load %65 : !llvm.ptr -> i64
%117 = arith.subi %115, %114 : i64
%118 = arith.sitofp %117 : i64 to f64
%119 = arith.mulf %118, %113 : f64
%116 = func.call @ceil(%119) : (f64) -> f64
%120 = arith.fptosi %116 : f64 to i64
%121 = llvm.load %57 : !llvm.ptr -> i64
%122 = arith.addi %121, %120 : i64
%123 = arith.addi %122, %114 : i64
llvm.store %123, %57 : i64, !llvm.ptr
%124 = llvm.load %23 : !llvm.ptr -> i64
%125 = arith.constant 1 : i32
%127 = arith.extsi %125 : i32 to i64
%126 = arith.addi %124, %127 : i64
llvm.store %126, %23 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%128 = llvm.mlir.addressof @str_0 : !llvm.ptr
%129 = llvm.load %57 : !llvm.ptr -> i64
%130 = llvm.call @printf(%128, %129) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%6) : (!llvm.ptr) -> ()
%132 = arith.constant 0 : i32
func.return %132 : i32
}
}