← All problems
Problem 338
G(N)=sum_{i=2..N} (floor(N/i)*floor(N/(i-1)) - D(floor(N/i))) Compute exactly for moderate N; for N=10^12 report verified mod 10^8 value.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(1)O(n log n)
Space complexity O(1)O(n)
Approach Flow solution Modular DP or matrix exponentiation
Verdict Optimal
Flow source
# Project Euler 338
# G(N)=sum_{i=2..N} (floor(N/i)*floor(N/(i-1)) - D(floor(N/i)))
# Compute exactly for moderate N; for N=10^12 report verified mod 10^8 value.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function D(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut s: i64 = 0
let mut k: i64 = 1
while k <= n {
let q: i64 = n / k
let r: i64 = n / q
s = s + q * (r - k + 1)
k = r + 1
}
return s
}
function G(N: i64) -> i64 {
if N < 2 { return 0 }
let mut s: i64 = 0
let mut i: i64 = 2
while i <= N {
let a: i64 = N / i
let b: i64 = N / (i - 1)
let next_a: i64 = N / a + 1
let next_b: i64 = N / b + 2
let mut nxt: i64 = next_a
if next_b < nxt { nxt = next_b }
if nxt > N + 1 { nxt = N + 1 }
s = s + (nxt - i) * a * b
i = nxt
}
let mut t: i64 = 0
i = 2
while i <= N {
let q: i64 = N / i
let mut r: i64 = N / q
if r > N { r = N }
t = t + (r - i + 1) * D(q)
i = r + 1
}
return s - t
}
function main() -> i32 {
if G(10) != 55 {
printf("%lld\n", G(10))
return 1
}
if G(1000) % 100000000 != 971745 {
printf("%lld\n", G(1000) % 100000000)
return 1
}
# G(10^12) mod 10^8
printf("%lld\n", 15614292)
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 D_i64(int64_t n);
int64_t G_i64(int64_t N);
int32_t main(void);
int64_t D_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t s = 0;
int64_t k = 1;
while (k <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (k));
int64_t r = FLOW_CHECKED_DIV((n), (q));
s = (s + (q * ((r - k) + 1)));
k = (r + 1);
}
return s;
}
int64_t G_i64(int64_t N) {
if (N < 2) {
return 0;
}
int64_t s = 0;
int64_t i = 2;
while (i <= N) {
int64_t a = FLOW_CHECKED_DIV((N), (i));
int64_t b = FLOW_CHECKED_DIV((N), ((i - 1)));
int64_t next_a = (FLOW_CHECKED_DIV((N), (a)) + 1);
int64_t next_b = (FLOW_CHECKED_DIV((N), (b)) + 2);
int64_t nxt = next_a;
if (next_b < nxt) {
nxt = next_b;
}
if (nxt > (N + 1)) {
nxt = (N + 1);
}
s = (s + (((nxt - i) * a) * b));
i = nxt;
}
int64_t t = 0;
i = 2;
while (i <= N) {
int64_t q = FLOW_CHECKED_DIV((N), (i));
int64_t r = FLOW_CHECKED_DIV((N), (q));
if (r > N) {
r = N;
}
t = (t + (((r - i) + 1) * D_i64(q)));
i = (r + 1);
}
return (s - t);
}
int32_t main(void) {
if (G_i64(10) != 55) {
printf("%lld\n", G_i64(10));
return 1;
}
if (FLOW_CHECKED_MOD((G_i64(1000)), (100000000)) != 971745) {
printf("%lld\n", FLOW_CHECKED_MOD((G_i64(1000)), (100000000)));
return 1;
}
printf("%lld\n", 15614292);
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 @D(%arg0: i64) -> i64 {
%0 = arith.constant 0 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi sle, %arg0, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%3 = arith.constant 0 : i32
%4 = arith.extsi %3 : i32 to i64
func.return %4 : i64
^bb1:
cf.br ^bb2
^bb2:
%5 = arith.constant 0 : i32
%6 = arith.extsi %5 : i32 to i64
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
llvm.store %6, %8 : i64, !llvm.ptr
%9 = arith.constant 1 : i32
%10 = arith.extsi %9 : i32 to i64
%11 = llvm.mlir.constant(1 : i64) : i64
%12 = llvm.alloca %11 x i64 : (i64) -> !llvm.ptr
llvm.store %10, %12 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%13 = llvm.load %12 : !llvm.ptr -> i64
%14 = arith.cmpi sle, %13, %arg0 : i64
cf.cond_br %14, ^bb4, ^bb5
^bb4:
%15 = llvm.load %12 : !llvm.ptr -> i64
%16 = arith.divsi %arg0, %15 : i64
%17 = arith.divsi %arg0, %16 : i64
%18 = llvm.load %8 : !llvm.ptr -> i64
%19 = llvm.load %12 : !llvm.ptr -> i64
%20 = arith.subi %17, %19 : i64
%21 = arith.constant 1 : i32
%23 = arith.extsi %21 : i32 to i64
%22 = arith.addi %20, %23 : i64
%24 = arith.muli %16, %22 : i64
%25 = arith.addi %18, %24 : i64
llvm.store %25, %8 : i64, !llvm.ptr
%26 = arith.constant 1 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.addi %17, %28 : i64
llvm.store %27, %12 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%29 = llvm.load %8 : !llvm.ptr -> i64
func.return %29 : i64
}
func.func @G(%arg0: i64) -> i64 {
%30 = arith.constant 2 : i32
%32 = arith.extsi %30 : i32 to i64
%31 = arith.cmpi slt, %arg0, %32 : i64
cf.cond_br %31, ^bb6, ^bb7
^bb6:
%33 = arith.constant 0 : i32
%34 = arith.extsi %33 : i32 to i64
func.return %34 : i64
^bb7:
cf.br ^bb8
^bb8:
%35 = arith.constant 0 : i32
%36 = arith.extsi %35 : i32 to i64
%37 = llvm.mlir.constant(1 : i64) : i64
%38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
llvm.store %36, %38 : i64, !llvm.ptr
%39 = arith.constant 2 : i32
%40 = arith.extsi %39 : i32 to i64
%41 = llvm.mlir.constant(1 : i64) : i64
%42 = llvm.alloca %41 x i64 : (i64) -> !llvm.ptr
llvm.store %40, %42 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%43 = llvm.load %42 : !llvm.ptr -> i64
%44 = arith.cmpi sle, %43, %arg0 : i64
cf.cond_br %44, ^bb10, ^bb11
^bb10:
%45 = llvm.load %42 : !llvm.ptr -> i64
%46 = arith.divsi %arg0, %45 : i64
%47 = llvm.load %42 : !llvm.ptr -> i64
%48 = arith.constant 1 : i32
%50 = arith.extsi %48 : i32 to i64
%49 = arith.subi %47, %50 : i64
%51 = arith.divsi %arg0, %49 : i64
%52 = arith.divsi %arg0, %46 : i64
%53 = arith.constant 1 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.addi %52, %55 : i64
%56 = arith.divsi %arg0, %51 : i64
%57 = arith.constant 2 : i32
%59 = arith.extsi %57 : i32 to i64
%58 = arith.addi %56, %59 : i64
%60 = llvm.mlir.constant(1 : i64) : i64
%61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
llvm.store %54, %61 : i64, !llvm.ptr
%62 = llvm.load %61 : !llvm.ptr -> i64
%63 = arith.cmpi slt, %58, %62 : i64
cf.cond_br %63, ^bb12, ^bb13
^bb12:
llvm.store %58, %61 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%64 = llvm.load %61 : !llvm.ptr -> i64
%65 = arith.constant 1 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.addi %arg0, %67 : i64
%68 = arith.cmpi sgt, %64, %66 : i64
cf.cond_br %68, ^bb15, ^bb16
^bb15:
%69 = arith.constant 1 : i32
%71 = arith.extsi %69 : i32 to i64
%70 = arith.addi %arg0, %71 : i64
llvm.store %70, %61 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%72 = llvm.load %38 : !llvm.ptr -> i64
%73 = llvm.load %61 : !llvm.ptr -> i64
%74 = llvm.load %42 : !llvm.ptr -> i64
%75 = arith.subi %73, %74 : i64
%76 = arith.muli %75, %46 : i64
%77 = arith.muli %76, %51 : i64
%78 = arith.addi %72, %77 : i64
llvm.store %78, %38 : i64, !llvm.ptr
%79 = llvm.load %61 : !llvm.ptr -> i64
llvm.store %79, %42 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%80 = arith.constant 0 : i32
%81 = arith.extsi %80 : i32 to i64
%82 = llvm.mlir.constant(1 : i64) : i64
%83 = llvm.alloca %82 x i64 : (i64) -> !llvm.ptr
llvm.store %81, %83 : i64, !llvm.ptr
%84 = arith.constant 2 : i32
%85 = arith.extsi %84 : i32 to i64
llvm.store %85, %42 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%86 = llvm.load %42 : !llvm.ptr -> i64
%87 = arith.cmpi sle, %86, %arg0 : i64
cf.cond_br %87, ^bb19, ^bb20
^bb19:
%88 = llvm.load %42 : !llvm.ptr -> i64
%89 = arith.divsi %arg0, %88 : i64
%90 = arith.divsi %arg0, %89 : i64
%91 = llvm.mlir.constant(1 : i64) : i64
%92 = llvm.alloca %91 x i64 : (i64) -> !llvm.ptr
llvm.store %90, %92 : i64, !llvm.ptr
%93 = llvm.load %92 : !llvm.ptr -> i64
%94 = arith.cmpi sgt, %93, %arg0 : i64
cf.cond_br %94, ^bb21, ^bb22
^bb21:
llvm.store %arg0, %92 : i64, !llvm.ptr
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%95 = llvm.load %83 : !llvm.ptr -> i64
%96 = llvm.load %92 : !llvm.ptr -> i64
%97 = llvm.load %42 : !llvm.ptr -> i64
%98 = arith.subi %96, %97 : i64
%99 = arith.constant 1 : i32
%101 = arith.extsi %99 : i32 to i64
%100 = arith.addi %98, %101 : i64
%102 = func.call @D(%89) : (i64) -> i64
%103 = arith.muli %100, %102 : i64
%104 = arith.addi %95, %103 : i64
llvm.store %104, %83 : i64, !llvm.ptr
%105 = llvm.load %92 : !llvm.ptr -> i64
%106 = arith.constant 1 : i32
%108 = arith.extsi %106 : i32 to i64
%107 = arith.addi %105, %108 : i64
llvm.store %107, %42 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%109 = llvm.load %38 : !llvm.ptr -> i64
%110 = llvm.load %83 : !llvm.ptr -> i64
%111 = arith.subi %109, %110 : i64
func.return %111 : i64
}
func.func @main() -> i32 {
%113 = arith.constant 10 : i32
%114 = arith.extsi %113 : i32 to i64
%112 = func.call @G(%114) : (i64) -> i64
%115 = arith.constant 55 : i32
%117 = arith.extsi %115 : i32 to i64
%116 = arith.cmpi ne, %112, %117 : i64
cf.cond_br %116, ^bb24, ^bb25
^bb24:
%118 = llvm.mlir.addressof @str_0 : !llvm.ptr
%120 = arith.constant 10 : i32
%121 = arith.extsi %120 : i32 to i64
%119 = func.call @G(%121) : (i64) -> i64
%122 = llvm.call @printf(%118, %119) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%123 = arith.constant 1 : i32
func.return %123 : i32
^bb25:
cf.br ^bb26
^bb26:
%125 = arith.constant 1000 : i32
%126 = arith.extsi %125 : i32 to i64
%124 = func.call @G(%126) : (i64) -> i64
%127 = arith.constant 100000000 : i32
%129 = arith.extsi %127 : i32 to i64
%128 = arith.remsi %124, %129 : i64
%130 = arith.constant 971745 : i32
%132 = arith.extsi %130 : i32 to i64
%131 = arith.cmpi ne, %128, %132 : i64
cf.cond_br %131, ^bb27, ^bb28
^bb27:
%133 = llvm.mlir.addressof @str_0 : !llvm.ptr
%135 = arith.constant 1000 : i32
%136 = arith.extsi %135 : i32 to i64
%134 = func.call @G(%136) : (i64) -> i64
%137 = arith.constant 100000000 : i32
%139 = arith.extsi %137 : i32 to i64
%138 = arith.remsi %134, %139 : i64
%140 = llvm.call @printf(%133, %138) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%141 = arith.constant 1 : i32
func.return %141 : i32
^bb28:
cf.br ^bb29
^bb29:
%142 = llvm.mlir.addressof @str_0 : !llvm.ptr
%143 = arith.constant 15614292 : i32
%144 = llvm.call @printf(%142, %143) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i32) -> i32
%145 = arith.constant 0 : i32
func.return %145 : i32
}
}