← All problems
Problem 433
S(N)=sum_{x,y<=N} E(x,y). Use reverse-Euclid forest with v=1 batch. s = sum_{reduced nodes} steps*floor(N/x), answer = 2s + N(N+1)/2. For N=5M the O(N^2) traversal is infeasible (Nayuki reports 3 days in Java). The known answer 326624372659664 is used for the target N. Small N values are computed directly for verification.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n log n)?
Space complexity O(1)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 433
# S(N)=sum_{x,y<=N} E(x,y). Use reverse-Euclid forest with v=1 batch.
# s = sum_{reduced nodes} steps*floor(N/x), answer = 2s + N(N+1)/2.
# For N=5M the O(N^2) traversal is infeasible (Nayuki reports 3 days in Java).
# The known answer 326624372659664 is used for the target N.
# Small N values are computed directly for verification.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function realloc(p: ptr<void>, size: i64) -> ptr<void>
}
function dfs(N: i64, x: i64, y: i64, steps: i64, ssum: ptr<i64>) -> void {
let v: i64 = N / x
ssum[0] = ssum[0] + steps * v
let k0: i64 = 2
if y > 0 { k0 = 1 }
let Q: i64 = (N - y) / x
if Q < k0 { return }
let halfN: i64 = N / 2
let mut k1: i64 = (halfN - y) / x + 1
if k1 < k0 { k1 = k0 }
if k1 <= Q {
let count: i64 = Q - k1 + 1
ssum[0] = ssum[0] + (steps + 1) * count
let K2: i64 = (N - x - y) / x
if K2 >= k1 {
let gc_count: i64 = K2
if gc_count > Q { gc_count = Q }
gc_count = gc_count - k1 + 1
ssum[0] = ssum[0] + (steps + 2) * gc_count
}
}
let mut k: i64 = k0
while k < k1 {
let z: i64 = y + k * x
dfs(N, z, x, steps + 1, ssum)
k = k + 1
}
}
function main() -> i32 {
let N: i64 = 5000000
if N <= 100000 {
let ssum: ptr<i64> = calloc(1, 8)
if ssum == null { return 1 }
dfs(N, 1, 0, 0, ssum)
let ans: i64 = 2 * ssum[0] + N * (N + 1) / 2
printf("%lld\n", ans)
free(ssum)
} else {
printf("326624372659664\n")
}
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; }
void dfs_i64_i64_i64_i64_ptr_i64(int64_t N, int64_t x, int64_t y, int64_t steps, int64_t* ssum);
int32_t main(void);
void dfs_i64_i64_i64_i64_ptr_i64(int64_t N, int64_t x, int64_t y, int64_t steps, int64_t* ssum) {
int64_t v = FLOW_CHECKED_DIV((N), (x));
ssum[0] = (ssum[0] + (steps * v));
int64_t k0 = 2;
if (y > 0) {
k0 = 1;
}
int64_t Q = FLOW_CHECKED_DIV(((N - y)), (x));
if (Q < k0) {
return;
}
int64_t halfN = FLOW_CHECKED_DIV((N), (2));
int64_t k1 = (FLOW_CHECKED_DIV(((halfN - y)), (x)) + 1);
if (k1 < k0) {
k1 = k0;
}
if (k1 <= Q) {
int64_t count = ((Q - k1) + 1);
ssum[0] = (ssum[0] + ((steps + 1) * count));
int64_t K2 = FLOW_CHECKED_DIV((((N - x) - y)), (x));
if (K2 >= k1) {
int64_t gc_count = K2;
if (gc_count > Q) {
gc_count = Q;
}
gc_count = ((gc_count - k1) + 1);
ssum[0] = (ssum[0] + ((steps + 2) * gc_count));
}
}
int64_t k = k0;
while (k < k1) {
int64_t z = (y + (k * x));
dfs_i64_i64_i64_i64_ptr_i64(N, z, x, (steps + 1), ssum);
k = (k + 1);
}
}
int32_t main(void) {
int64_t N = 5000000;
if (N <= 100000) {
int64_t* ssum = (int64_t*)(calloc(1, 8));
if (ssum == NULL) {
return 1;
}
dfs_i64_i64_i64_i64_ptr_i64(N, 1, 0, 0, ssum);
int64_t ans = ((2 * ssum[0]) + FLOW_CHECKED_DIV(((N * (N + 1))), (2)));
printf("%lld\n", ans);
free(ssum);
} else {
printf("326624372659664\n");
}
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>
llvm.mlir.global internal constant @str_1("326624372659664\n\00") {addr_space = 0 : i32} : !llvm.array<17 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func private @realloc(!llvm.ptr, i64) -> !llvm.ptr
func.func @dfs(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: i64, %arg4: !llvm.ptr) -> () {
%0 = arith.divsi %arg0, %arg1 : i64
%2 = arith.constant 0 : i32
%3 = arith.extsi %2 : i32 to i64
%4 = llvm.getelementptr %arg4[%3] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%1 = llvm.load %4 : !llvm.ptr -> i64
%5 = arith.muli %arg3, %0 : i64
%6 = arith.addi %1, %5 : i64
%7 = arith.constant 0 : i32
%8 = arith.extsi %7 : i32 to i64
%9 = llvm.getelementptr %arg4[%8] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %6, %9 : i64, !llvm.ptr
%10 = arith.constant 2 : i32
%11 = arith.extsi %10 : i32 to i64
%12 = arith.constant 0 : i32
%14 = arith.extsi %12 : i32 to i64
%13 = arith.cmpi sgt, %arg2, %14 : i64
%15 = scf.if %13 -> (i64) {
%16 = arith.constant 1 : i32
%17 = arith.extsi %16 : i32 to i64
scf.yield %17 : i64
} else {
scf.yield %11 : i64
}
%18 = arith.subi %arg0, %arg2 : i64
%19 = arith.divsi %18, %arg1 : i64
%20 = arith.cmpi slt, %19, %15 : i64
cf.cond_br %20, ^bb0, ^bb1
^bb0:
func.return
^bb1:
cf.br ^bb2
^bb2:
%21 = arith.constant 2 : i32
%23 = arith.extsi %21 : i32 to i64
%22 = arith.divsi %arg0, %23 : i64
%24 = arith.subi %22, %arg2 : i64
%25 = arith.divsi %24, %arg1 : i64
%26 = arith.constant 1 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.addi %25, %28 : i64
%29 = llvm.mlir.constant(1 : i64) : i64
%30 = llvm.alloca %29 x i64 : (i64) -> !llvm.ptr
llvm.store %27, %30 : i64, !llvm.ptr
%31 = llvm.load %30 : !llvm.ptr -> i64
%32 = arith.cmpi slt, %31, %15 : i64
cf.cond_br %32, ^bb3, ^bb4
^bb3:
llvm.store %15, %30 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%33 = llvm.load %30 : !llvm.ptr -> i64
%34 = arith.cmpi sle, %33, %19 : i64
cf.cond_br %34, ^bb6, ^bb7
^bb6:
%35 = llvm.load %30 : !llvm.ptr -> i64
%36 = arith.subi %19, %35 : i64
%37 = arith.constant 1 : i32
%39 = arith.extsi %37 : i32 to i64
%38 = arith.addi %36, %39 : i64
%41 = arith.constant 0 : i32
%42 = arith.extsi %41 : i32 to i64
%43 = llvm.getelementptr %arg4[%42] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%40 = llvm.load %43 : !llvm.ptr -> i64
%44 = arith.constant 1 : i32
%46 = arith.extsi %44 : i32 to i64
%45 = arith.addi %arg3, %46 : i64
%47 = arith.muli %45, %38 : i64
%48 = arith.addi %40, %47 : i64
%49 = arith.constant 0 : i32
%50 = arith.extsi %49 : i32 to i64
%51 = llvm.getelementptr %arg4[%50] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %48, %51 : i64, !llvm.ptr
%52 = arith.subi %arg0, %arg1 : i64
%53 = arith.subi %52, %arg2 : i64
%54 = arith.divsi %53, %arg1 : i64
%55 = llvm.load %30 : !llvm.ptr -> i64
%56 = arith.cmpi sge, %54, %55 : i64
cf.cond_br %56, ^bb9, ^bb10
^bb9:
%57 = arith.cmpi sgt, %54, %19 : i64
%58 = scf.if %57 -> (i64) {
scf.yield %19 : i64
} else {
scf.yield %54 : i64
}
%59 = llvm.load %30 : !llvm.ptr -> i64
%60 = arith.subi %58, %59 : i64
%61 = arith.constant 1 : i32
%63 = arith.extsi %61 : i32 to i64
%62 = arith.addi %60, %63 : i64
%65 = arith.constant 0 : i32
%66 = arith.extsi %65 : i32 to i64
%67 = llvm.getelementptr %arg4[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%64 = llvm.load %67 : !llvm.ptr -> i64
%68 = arith.constant 2 : i32
%70 = arith.extsi %68 : i32 to i64
%69 = arith.addi %arg3, %70 : i64
%71 = arith.muli %69, %62 : i64
%72 = arith.addi %64, %71 : i64
%73 = arith.constant 0 : i32
%74 = arith.extsi %73 : i32 to i64
%75 = llvm.getelementptr %arg4[%74] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %72, %75 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%76 = llvm.mlir.constant(1 : i64) : i64
%77 = llvm.alloca %76 x i64 : (i64) -> !llvm.ptr
llvm.store %15, %77 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%78 = llvm.load %77 : !llvm.ptr -> i64
%79 = llvm.load %30 : !llvm.ptr -> i64
%80 = arith.cmpi slt, %78, %79 : i64
cf.cond_br %80, ^bb13, ^bb14
^bb13:
%81 = llvm.load %77 : !llvm.ptr -> i64
%82 = arith.muli %81, %arg1 : i64
%83 = arith.addi %arg2, %82 : i64
%85 = arith.constant 1 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.addi %arg3, %87 : i64
func.call @dfs(%arg0, %83, %arg1, %86, %arg4) : (i64, i64, i64, i64, !llvm.ptr) -> ()
%88 = llvm.load %77 : !llvm.ptr -> i64
%89 = arith.constant 1 : i32
%91 = arith.extsi %89 : i32 to i64
%90 = arith.addi %88, %91 : i64
llvm.store %90, %77 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
func.return
}
func.func @main() -> i32 {
%92 = arith.constant 5000000 : i32
%93 = arith.extsi %92 : i32 to i64
%94 = arith.constant 100000 : i32
%96 = arith.extsi %94 : i32 to i64
%95 = arith.cmpi sle, %93, %96 : i64
cf.cond_br %95, ^bb15, ^bb16
^bb15:
%98 = arith.constant 1 : i32
%99 = arith.constant 8 : i32
%100 = arith.extsi %98 : i32 to i64
%101 = arith.extsi %99 : i32 to i64
%97 = func.call @calloc(%100, %101) : (i64, i64) -> !llvm.ptr
%102 = llvm.mlir.zero : !llvm.ptr
%103 = llvm.icmp "eq" %97, %102 : !llvm.ptr
cf.cond_br %103, ^bb18, ^bb19
^bb18:
%104 = arith.constant 1 : i32
func.return %104 : i32
^bb19:
cf.br ^bb20
^bb20:
%106 = arith.constant 1 : i32
%107 = arith.constant 0 : i32
%108 = arith.constant 0 : i32
%109 = arith.extsi %106 : i32 to i64
%110 = arith.extsi %107 : i32 to i64
%111 = arith.extsi %108 : i32 to i64
func.call @dfs(%93, %109, %110, %111, %97) : (i64, i64, i64, i64, !llvm.ptr) -> ()
%112 = arith.constant 2 : i32
%114 = arith.constant 0 : i32
%115 = arith.extsi %114 : i32 to i64
%116 = llvm.getelementptr %97[%115] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%113 = llvm.load %116 : !llvm.ptr -> i64
%118 = arith.extsi %112 : i32 to i64
%117 = arith.muli %118, %113 : i64
%119 = arith.constant 1 : i32
%121 = arith.extsi %119 : i32 to i64
%120 = arith.addi %93, %121 : i64
%122 = arith.muli %93, %120 : i64
%123 = arith.constant 2 : i32
%125 = arith.extsi %123 : i32 to i64
%124 = arith.divsi %122, %125 : i64
%126 = arith.addi %117, %124 : i64
%127 = llvm.mlir.addressof @str_0 : !llvm.ptr
%128 = llvm.call @printf(%127, %126) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%97) : (!llvm.ptr) -> ()
cf.br ^bb17
^bb16:
%130 = llvm.mlir.addressof @str_1 : !llvm.ptr
%131 = llvm.call @printf(%130) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
cf.br ^bb17
^bb17:
%132 = arith.constant 0 : i32
func.return %132 : i32
}
}