# Project Euler 388
# Distinct lines D(10^10); print first 9 and last 9 digits concatenated.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function icbrt128(n: i128) -> i64 {
if n < (2 as i128) { return n as i64 }
# binary search for floor(cbrt(n))
let mut lo: i64 = 1
let mut hi: i64 = 1 << 22
while lo < hi {
let mid: i64 = (lo + hi + 1) / 2
let m: i128 = mid as i128
if m * m * m <= n {
lo = mid
} else {
hi = mid - 1
}
}
return lo
}
function memo_get(keys: ptr<i64>, vals: ptr<i64>, used: ptr<i8>, cap: i64, key: i64, out: ptr<i64>) -> i32 {
let mut slot: i64 = key
if slot < 0 { slot = 0 - slot }
slot = slot & (cap - 1)
while used[slot] != 0 {
if keys[slot] == key {
out[0] = vals[slot]
return 1
}
slot = (slot + 1) & (cap - 1)
}
return 0
}
function memo_put(keys: ptr<i64>, vals: ptr<i64>, used: ptr<i8>, cap: i64, key: i64, val: i64) -> void {
let mut slot: i64 = key
if slot < 0 { slot = 0 - slot }
slot = slot & (cap - 1)
while used[slot] != 0 && keys[slot] != key {
slot = (slot + 1) & (cap - 1)
}
used[slot] = 1
keys[slot] = key
vals[slot] = val
}
function mertens(n: i64, B: i64, M_small: ptr<i64>,
keys: ptr<i64>, vals: ptr<i64>, used: ptr<i8>, cap: i64) -> i64 {
if n <= B { return M_small[n] }
let box: array<i64, 1> = [0]
if memo_get(keys, vals, used, cap, n, box) == 1 {
return box[0]
}
let mut res: i64 = 1
let mut l: i64 = 2
while l <= n {
let q: i64 = n / l
let r: i64 = n / q
res = res - (r - l + 1) * mertens(q, B, M_small, keys, vals, used, cap)
l = r + 1
}
memo_put(keys, vals, used, cap, n, res)
return res
}
function G(q: i128) -> i128 {
return q * (q * (q + (3 as i128)) + (3 as i128))
}
function main() -> i32 {
let N: i64 = 10000000000
let NN: i128 = (N as i128) * (N as i128)
let B: i64 = icbrt128(NN)
let mu: ptr<i8> = calloc(B + 1, 1)
let lp: ptr<i32> = calloc(B + 1, 4)
let primes: ptr<i32> = calloc(B / 5 + 10, 4)
let M_small: ptr<i64> = calloc(B + 1, 8)
if mu == null || lp == null || primes == null || M_small == null { return 1 }
mu[1] = 1
let mut pc: i64 = 0
let mut i: i64 = 2
while i <= B {
if lp[i] == 0 {
lp[i] = i as i32
primes[pc] = i as i32
pc = pc + 1
mu[i] = (0 - 1) as i8
}
let mut j: i64 = 0
while j < pc {
let p: i64 = primes[j] as i64
let ip: i64 = i * p
if ip > B { break }
lp[ip] = p as i32
if i % p == 0 {
mu[ip] = 0
break
}
mu[ip] = (0 - (mu[i] as i64)) as i8
j = j + 1
}
i = i + 1
}
let mut s: i64 = 0
i = 1
while i <= B {
s = s + (mu[i] as i64)
M_small[i] = s
i = i + 1
}
let cap: i64 = 8192
let keys: ptr<i64> = calloc(cap, 8)
let vals: ptr<i64> = calloc(cap, 8)
let used: ptr<i8> = calloc(cap, 1)
if keys == null || vals == null || used == null { return 1 }
# warm
let _w: i64 = mertens(N, B, M_small, keys, vals, used, cap)
let mut total: i128 = 0
# Part 1: d <= B
let mut l: i64 = 1
while l <= B {
let q: i64 = N / l
let mut r: i64 = N / q
if r > B { r = B }
let dm: i64 = M_small[r] - M_small[l - 1]
total = total + (dm as i128) * G(q as i128)
l = r + 1
}
# Part 2: d > B
let max_q: i64 = N / (B + 1)
let mut q: i64 = 1
while q <= max_q {
let mut left: i64 = N / (q + 1) + 1
let right: i64 = N / q
if left <= B { left = B + 1 }
if left <= right {
let mr: i64 = mertens(right, B, M_small, keys, vals, used, cap)
let ml: i64 = mertens(left - 1, B, M_small, keys, vals, used, cap)
total = total + ((mr - ml) as i128) * G(q as i128)
}
q = q + 1
}
let MOD9: i128 = 1000000000
let last9: i64 = (total % MOD9) as i64
if last9 < 0 { last9 = 0 - last9 } # shouldn't be negative
# first 9 digits
let mut tmp: i128 = total
if tmp < (0 as i128) { tmp = 0 as i128 - tmp }
let mut digs: i64 = 0
let mut t2: i128 = tmp
while t2 > (0 as i128) {
digs = digs + 1
t2 = t2 / (10 as i128)
}
let mut div: i128 = 1
let mut k: i64 = 0
while k < digs - 9 {
div = div * (10 as i128)
k = k + 1
}
let first9: i64 = (tmp / div) as i64
printf("%lld%09lld\n", first9, last9)
free(used)
free(vals)
free(keys)
free(M_small)
free(primes)
free(lp)
free(mu)
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 icbrt128_i128(__int128 n);
int32_t memo_get_ptr_i64_ptr_i64_ptr_i8_i64_i64_ptr_i64(int64_t* keys, int64_t* vals, int8_t* used, int64_t cap, int64_t key, int64_t* out);
void memo_put_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(int64_t* keys, int64_t* vals, int8_t* used, int64_t cap, int64_t key, int64_t val);
int64_t mertens_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i8_i64(int64_t n, int64_t B, int64_t* M_small, int64_t* keys, int64_t* vals, int8_t* used, int64_t cap);
__int128 G_i128(__int128 q);
int32_t main(void);
int64_t icbrt128_i128(__int128 n) {
if (n < ((__int128)(2))) {
return ((int64_t)(n));
}
int64_t lo = 1;
int64_t hi = FLOW_CHECKED_SHL((1), (22));
while (lo < hi) {
int64_t mid = FLOW_CHECKED_DIV((((lo + hi) + 1)), (2));
__int128 m = ((__int128)(mid));
if (((m * m) * m) <= n) {
lo = mid;
} else {
hi = (mid - 1);
}
}
return lo;
}
int32_t memo_get_ptr_i64_ptr_i64_ptr_i8_i64_i64_ptr_i64(int64_t* keys, int64_t* vals, int8_t* used, int64_t cap, int64_t key, int64_t* out) {
int64_t slot = key;
if (slot < 0) {
slot = (0 - slot);
}
slot = (slot & (cap - 1));
while (used[slot] != 0) {
if (keys[slot] == key) {
out[0] = vals[slot];
return 1;
}
slot = ((slot + 1) & (cap - 1));
}
return 0;
}
void memo_put_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(int64_t* keys, int64_t* vals, int8_t* used, int64_t cap, int64_t key, int64_t val) {
int64_t slot = key;
if (slot < 0) {
slot = (0 - slot);
}
slot = (slot & (cap - 1));
while ((used[slot] != 0 && keys[slot] != key)) {
slot = ((slot + 1) & (cap - 1));
}
used[slot] = 1;
keys[slot] = key;
vals[slot] = val;
}
int64_t mertens_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i8_i64(int64_t n, int64_t B, int64_t* M_small, int64_t* keys, int64_t* vals, int8_t* used, int64_t cap) {
if (n <= B) {
return M_small[n];
}
int64_t box[1] = { 0 };
if (memo_get_ptr_i64_ptr_i64_ptr_i8_i64_i64_ptr_i64(keys, vals, used, cap, n, box) == 1) {
return (((unsigned)(0) < 1) ? box[0] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(0), 1), flow_fault_handler("array index out of bounds"), box[0]));
}
int64_t res = 1;
int64_t l = 2;
while (l <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (l));
int64_t r = FLOW_CHECKED_DIV((n), (q));
res = (res - (((r - l) + 1) * mertens_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i8_i64(q, B, M_small, keys, vals, used, cap)));
l = (r + 1);
}
memo_put_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(keys, vals, used, cap, n, res);
return res;
}
__int128 G_i128(__int128 q) {
return (q * ((q * (q + ((__int128)(3)))) + ((__int128)(3))));
}
int32_t main(void) {
int64_t N = 10000000000;
__int128 NN = (((__int128)(N)) * ((__int128)(N)));
int64_t B = icbrt128_i128(NN);
int8_t* mu = (int8_t*)(calloc((B + 1), 1));
int32_t* lp = (int32_t*)(calloc((B + 1), 4));
int32_t* primes = (int32_t*)(calloc((FLOW_CHECKED_DIV((B), (5)) + 10), 4));
int64_t* M_small = (int64_t*)(calloc((B + 1), 8));
if ((((mu == NULL || lp == NULL) || primes == NULL) || M_small == NULL)) {
return 1;
}
mu[1] = 1;
int64_t pc = 0;
int64_t i = 2;
while (i <= B) {
if (lp[i] == 0) {
lp[i] = ((int32_t)(i));
primes[pc] = ((int32_t)(i));
pc = (pc + 1);
mu[i] = ((int8_t)((0 - 1)));
}
int64_t j = 0;
while (j < pc) {
int64_t p = ((int64_t)(primes[j]));
int64_t ip = (i * p);
if (ip > B) {
break;
}
lp[ip] = ((int32_t)(p));
if (FLOW_CHECKED_MOD((i), (p)) == 0) {
mu[ip] = 0;
break;
}
mu[ip] = ((int8_t)((0 - ((int64_t)(mu[i])))));
j = (j + 1);
}
i = (i + 1);
}
int64_t s = 0;
i = 1;
while (i <= B) {
s = (s + ((int64_t)(mu[i])));
M_small[i] = s;
i = (i + 1);
}
int64_t cap = 8192;
int64_t* keys = (int64_t*)(calloc(cap, 8));
int64_t* vals = (int64_t*)(calloc(cap, 8));
int8_t* used = (int8_t*)(calloc(cap, 1));
if (((keys == NULL || vals == NULL) || used == NULL)) {
return 1;
}
int64_t _w = mertens_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i8_i64(N, B, M_small, keys, vals, used, cap);
__int128 total = 0;
int64_t l = 1;
while (l <= B) {
int64_t q = FLOW_CHECKED_DIV((N), (l));
int64_t r = FLOW_CHECKED_DIV((N), (q));
if (r > B) {
r = B;
}
int64_t dm = (M_small[r] - M_small[(l - 1)]);
total = (total + (((__int128)(dm)) * G_i128(((__int128)(q)))));
l = (r + 1);
}
int64_t max_q = FLOW_CHECKED_DIV((N), ((B + 1)));
int64_t q = 1;
while (q <= max_q) {
int64_t left = (FLOW_CHECKED_DIV((N), ((q + 1))) + 1);
int64_t right = FLOW_CHECKED_DIV((N), (q));
if (left <= B) {
left = (B + 1);
}
if (left <= right) {
int64_t mr = mertens_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i8_i64(right, B, M_small, keys, vals, used, cap);
int64_t ml = mertens_i64_i64_ptr_i64_ptr_i64_ptr_i64_ptr_i8_i64((left - 1), B, M_small, keys, vals, used, cap);
total = (total + (((__int128)((mr - ml))) * G_i128(((__int128)(q)))));
}
q = (q + 1);
}
__int128 MOD9 = 1000000000;
int64_t last9 = ((int64_t)(FLOW_CHECKED_MOD((total), (MOD9))));
if (last9 < 0) {
last9 = (0 - last9);
}
__int128 tmp = total;
if (tmp < ((__int128)(0))) {
tmp = (((__int128)(0)) - tmp);
}
int64_t digs = 0;
__int128 t2 = tmp;
while (t2 > ((__int128)(0))) {
digs = (digs + 1);
t2 = FLOW_CHECKED_DIV((t2), (((__int128)(10))));
}
__int128 div = 1;
int64_t k = 0;
while (k < (digs - 9)) {
div = (div * ((__int128)(10)));
k = (k + 1);
}
int64_t first9 = ((int64_t)(FLOW_CHECKED_DIV((tmp), (div))));
printf("%lld%09lld\n", first9, last9);
free(used);
free(vals);
free(keys);
free(M_small);
free(primes);
free(lp);
free(mu);
return 0;
}