← All problems
Problem 334
Spilling the beans: moves = (S2_final - S2_init)/2 for 1500 bowls.
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 334
# Spilling the beans: moves = (S2_final - S2_init)/2 for 1500 bowls.
function main() -> i32 {
let mut t: i64 = 123456
let mut N: i64 = 0
let mut S1: i64 = 0
let mut S2_init: i64 = 0
let mut i: i64 = 0
while i < 1500 {
if t % 2 == 0 {
t = t / 2
} else {
t = (t / 2) ^ 926252
}
let b: i64 = (t % 2048) + 1
N = N + b
S1 = S1 + i * b
S2_init = S2_init + i * i * b
i = i + 1
}
let sumk: i64 = N * (N + 1) / 2
let sumk2: i64 = N * (N + 1) * (2 * N + 1) / 6
let mut L: i64 = (S1 - sumk) / N
let mut H: i64 = (N + 1) * L + sumk - S1
if H < L || H > L + N {
L = L + 1
H = (N + 1) * L + sumk - S1
}
let interval_S2: i64 = (N + 1) * (L * L) + 2 * L * sumk + sumk2
let S2_final: i64 = interval_S2 - H * H
printf("%lld\n", (S2_final - S2_init) / 2)
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 t = 123456;
int64_t N = 0;
int64_t S1 = 0;
int64_t S2_init = 0;
int64_t i = 0;
while (i < 1500) {
if (FLOW_CHECKED_MOD((t), (2)) == 0) {
t = FLOW_CHECKED_DIV((t), (2));
} else {
t = (FLOW_CHECKED_DIV((t), (2)) ^ 926252);
}
int64_t b = (FLOW_CHECKED_MOD((t), (2048)) + 1);
N = (N + b);
S1 = (S1 + (i * b));
S2_init = (S2_init + ((i * i) * b));
i = (i + 1);
}
int64_t sumk = FLOW_CHECKED_DIV(((N * (N + 1))), (2));
int64_t sumk2 = FLOW_CHECKED_DIV((((N * (N + 1)) * ((2 * N) + 1))), (6));
int64_t L = FLOW_CHECKED_DIV(((S1 - sumk)), (N));
int64_t H = ((((N + 1) * L) + sumk) - S1);
if ((H < L || H > (L + N))) {
L = (L + 1);
H = ((((N + 1) * L) + sumk) - S1);
}
int64_t interval_S2 = ((((N + 1) * (L * L)) + ((2 * L) * sumk)) + sumk2);
int64_t S2_final = (interval_S2 - (H * H));
printf("%lld\n", FLOW_CHECKED_DIV(((S2_final - S2_init)), (2)));
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 @main() -> i32 {
%0 = arith.constant 123456 : 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
%4 = arith.constant 0 : i32
%5 = arith.extsi %4 : i32 to i64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
llvm.store %5, %7 : i64, !llvm.ptr
%8 = arith.constant 0 : i32
%9 = arith.extsi %8 : i32 to i64
%10 = llvm.mlir.constant(1 : i64) : i64
%11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
llvm.store %9, %11 : i64, !llvm.ptr
%12 = arith.constant 0 : i32
%13 = arith.extsi %12 : i32 to i64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : i64, !llvm.ptr
%16 = arith.constant 0 : 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
cf.br ^bb0
^bb0:
%20 = llvm.load %19 : !llvm.ptr -> i64
%21 = arith.constant 1500 : i32
%23 = arith.extsi %21 : i32 to i64
%22 = arith.cmpi slt, %20, %23 : i64
cf.cond_br %22, ^bb1, ^bb2
^bb1:
%24 = llvm.load %3 : !llvm.ptr -> i64
%25 = arith.constant 2 : i32
%27 = arith.extsi %25 : i32 to i64
%26 = arith.remsi %24, %27 : i64
%28 = arith.constant 0 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.cmpi eq, %26, %30 : i64
cf.cond_br %29, ^bb3, ^bb4
^bb3:
%31 = llvm.load %3 : !llvm.ptr -> i64
%32 = arith.constant 2 : i32
%34 = arith.extsi %32 : i32 to i64
%33 = arith.divsi %31, %34 : i64
llvm.store %33, %3 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
%35 = llvm.load %3 : !llvm.ptr -> i64
%36 = arith.constant 2 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.divsi %35, %38 : i64
%39 = arith.constant 926252 : i32
%41 = arith.extsi %39 : i32 to i64
%40 = arith.xori %37, %41 : i64
llvm.store %40, %3 : i64, !llvm.ptr
cf.br ^bb5
^bb5:
%42 = llvm.load %3 : !llvm.ptr -> i64
%43 = arith.constant 2048 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.remsi %42, %45 : i64
%46 = arith.constant 1 : i32
%48 = arith.extsi %46 : i32 to i64
%47 = arith.addi %44, %48 : i64
%49 = llvm.load %7 : !llvm.ptr -> i64
%50 = arith.addi %49, %47 : i64
llvm.store %50, %7 : i64, !llvm.ptr
%51 = llvm.load %11 : !llvm.ptr -> i64
%52 = llvm.load %19 : !llvm.ptr -> i64
%53 = arith.muli %52, %47 : i64
%54 = arith.addi %51, %53 : i64
llvm.store %54, %11 : i64, !llvm.ptr
%55 = llvm.load %15 : !llvm.ptr -> i64
%56 = llvm.load %19 : !llvm.ptr -> i64
%57 = llvm.load %19 : !llvm.ptr -> i64
%58 = arith.muli %56, %57 : i64
%59 = arith.muli %58, %47 : i64
%60 = arith.addi %55, %59 : i64
llvm.store %60, %15 : i64, !llvm.ptr
%61 = llvm.load %19 : !llvm.ptr -> i64
%62 = arith.constant 1 : i32
%64 = arith.extsi %62 : i32 to i64
%63 = arith.addi %61, %64 : i64
llvm.store %63, %19 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%65 = llvm.load %7 : !llvm.ptr -> i64
%66 = llvm.load %7 : !llvm.ptr -> i64
%67 = arith.constant 1 : i32
%69 = arith.extsi %67 : i32 to i64
%68 = arith.addi %66, %69 : i64
%70 = arith.muli %65, %68 : i64
%71 = arith.constant 2 : i32
%73 = arith.extsi %71 : i32 to i64
%72 = arith.divsi %70, %73 : i64
%74 = llvm.load %7 : !llvm.ptr -> i64
%75 = llvm.load %7 : !llvm.ptr -> i64
%76 = arith.constant 1 : i32
%78 = arith.extsi %76 : i32 to i64
%77 = arith.addi %75, %78 : i64
%79 = arith.muli %74, %77 : i64
%80 = arith.constant 2 : i32
%81 = llvm.load %7 : !llvm.ptr -> i64
%83 = arith.extsi %80 : i32 to i64
%82 = arith.muli %83, %81 : i64
%84 = arith.constant 1 : i32
%86 = arith.extsi %84 : i32 to i64
%85 = arith.addi %82, %86 : i64
%87 = arith.muli %79, %85 : i64
%88 = arith.constant 6 : i32
%90 = arith.extsi %88 : i32 to i64
%89 = arith.divsi %87, %90 : i64
%91 = llvm.load %11 : !llvm.ptr -> i64
%92 = arith.subi %91, %72 : i64
%93 = llvm.load %7 : !llvm.ptr -> i64
%94 = arith.divsi %92, %93 : i64
%95 = llvm.mlir.constant(1 : i64) : i64
%96 = llvm.alloca %95 x i64 : (i64) -> !llvm.ptr
llvm.store %94, %96 : i64, !llvm.ptr
%97 = llvm.load %7 : !llvm.ptr -> i64
%98 = arith.constant 1 : i32
%100 = arith.extsi %98 : i32 to i64
%99 = arith.addi %97, %100 : i64
%101 = llvm.load %96 : !llvm.ptr -> i64
%102 = arith.muli %99, %101 : i64
%103 = arith.addi %102, %72 : i64
%104 = llvm.load %11 : !llvm.ptr -> i64
%105 = arith.subi %103, %104 : i64
%106 = llvm.mlir.constant(1 : i64) : i64
%107 = llvm.alloca %106 x i64 : (i64) -> !llvm.ptr
llvm.store %105, %107 : i64, !llvm.ptr
%108 = llvm.load %107 : !llvm.ptr -> i64
%109 = llvm.load %96 : !llvm.ptr -> i64
%110 = arith.cmpi slt, %108, %109 : i64
%111 = scf.if %110 -> (i1) {
%112 = arith.constant true
scf.yield %112 : i1
} else {
%113 = llvm.load %107 : !llvm.ptr -> i64
%114 = llvm.load %96 : !llvm.ptr -> i64
%115 = llvm.load %7 : !llvm.ptr -> i64
%116 = arith.addi %114, %115 : i64
%117 = arith.cmpi sgt, %113, %116 : i64
scf.yield %117 : i1
}
cf.cond_br %111, ^bb6, ^bb7
^bb6:
%118 = llvm.load %96 : !llvm.ptr -> i64
%119 = arith.constant 1 : i32
%121 = arith.extsi %119 : i32 to i64
%120 = arith.addi %118, %121 : i64
llvm.store %120, %96 : i64, !llvm.ptr
%122 = llvm.load %7 : !llvm.ptr -> i64
%123 = arith.constant 1 : i32
%125 = arith.extsi %123 : i32 to i64
%124 = arith.addi %122, %125 : i64
%126 = llvm.load %96 : !llvm.ptr -> i64
%127 = arith.muli %124, %126 : i64
%128 = arith.addi %127, %72 : i64
%129 = llvm.load %11 : !llvm.ptr -> i64
%130 = arith.subi %128, %129 : i64
llvm.store %130, %107 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%131 = llvm.load %7 : !llvm.ptr -> i64
%132 = arith.constant 1 : i32
%134 = arith.extsi %132 : i32 to i64
%133 = arith.addi %131, %134 : i64
%135 = llvm.load %96 : !llvm.ptr -> i64
%136 = llvm.load %96 : !llvm.ptr -> i64
%137 = arith.muli %135, %136 : i64
%138 = arith.muli %133, %137 : i64
%139 = arith.constant 2 : i32
%140 = llvm.load %96 : !llvm.ptr -> i64
%142 = arith.extsi %139 : i32 to i64
%141 = arith.muli %142, %140 : i64
%143 = arith.muli %141, %72 : i64
%144 = arith.addi %138, %143 : i64
%145 = arith.addi %144, %89 : i64
%146 = llvm.load %107 : !llvm.ptr -> i64
%147 = llvm.load %107 : !llvm.ptr -> i64
%148 = arith.muli %146, %147 : i64
%149 = arith.subi %145, %148 : i64
%150 = llvm.mlir.addressof @str_0 : !llvm.ptr
%151 = llvm.load %15 : !llvm.ptr -> i64
%152 = arith.subi %149, %151 : i64
%153 = arith.constant 2 : i32
%155 = arith.extsi %153 : i32 to i64
%154 = arith.divsi %152, %155 : i64
%156 = llvm.call @printf(%150, %154) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%157 = arith.constant 0 : i32
func.return %157 : i32
}
}