# Project Euler 530
# GCD of Divisors — F(10^15) via Dirichlet hyperbola.
import euler.nt { isqrt }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const CAP: i64 = 2097152
function divisor_summatory(n: i64) -> i64 {
let s: i64 = isqrt(n)
let mut acc: i64 = 0
let mut i: i64 = 1
while i <= s {
acc = acc + n / i
i = i + 1
}
return 2 * acc - s * s
}
function hslot(key: i64, keys: ptr<i64>, used: ptr<i8>) -> i64 {
let mut h: i64 = key % CAP
if h < 0 { h = h + CAP }
while used[h] == 1 && keys[h] != key {
h = h + 1
if h == CAP { h = 0 }
}
return h
}
function D_cached(x: i64, keys: ptr<i64>, vals: ptr<i64>, used: ptr<i8>) -> i64 {
let s: i64 = hslot(x, keys, used)
if used[s] == 1 { return vals[s] }
let v: i64 = divisor_summatory(x)
used[s] = 1
keys[s] = x
vals[s] = v
return v
}
function main() -> i32 {
let N: i64 = 1000000000000000
let k: i64 = isqrt(N)
let l: i64 = N / k
let tmax: i64 = isqrt(l)
let phi: ptr<i32> = calloc(k + 1, 4)
let tau: ptr<i16> = calloc(k + 1, 2)
let lp: ptr<i32> = calloc(k + 1, 4)
let expv: ptr<i8> = calloc(k + 1, 1)
let primes: ptr<i32> = calloc(k / 5 + 10, 4)
let Phi: ptr<i64> = calloc(k + 1, 8)
if phi == null || tau == null || lp == null || expv == null || primes == null || Phi == null {
return 1
}
phi[1] = 1
tau[1] = 1
let mut pn: i64 = 0
let mut i: i64 = 2
while i <= k {
if lp[i] == 0 {
lp[i] = i as i32
primes[pn] = i as i32
pn = pn + 1
phi[i] = (i - 1) as i32
tau[i] = 2
expv[i] = 1
}
let mut pi: i64 = 0
while pi < pn {
let p: i64 = primes[pi] as i64
let ip: i64 = i * p
if ip > k { break }
lp[ip] = p as i32
if i % p == 0 {
phi[ip] = (phi[i] as i64 * p) as i32
let e: i64 = (expv[i] as i64) + 1
expv[ip] = e as i8
tau[ip] = ((tau[i] as i64) / ((expv[i] as i64) + 1) * (e + 1)) as i16
break
} else {
phi[ip] = (phi[i] as i64 * (p - 1)) as i32
expv[ip] = 1
tau[ip] = ((tau[i] as i64) * 2) as i16
}
pi = pi + 1
}
i = i + 1
}
let mut running: i64 = 0
i = 1
while i <= k {
running = running + (phi[i] as i64)
Phi[i] = running
i = i + 1
}
let phi_small: ptr<i32> = calloc(tmax + 1, 4)
if phi_small == null { return 1 }
i = 1
while i <= tmax {
phi_small[i] = phi[i]
i = i + 1
}
let mut sum1: i64 = 0
let mut sum_tau: i64 = 0
i = 1
while i <= k {
let ti: i64 = tau[i] as i64
sum_tau = sum_tau + ti
let m: i64 = isqrt(N / i)
sum1 = sum1 + ti * Phi[m]
i = i + 1
}
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 }
let s0: i64 = hslot(k, keys, used)
used[s0] = 1
keys[s0] = k
vals[s0] = sum_tau
let mut sum2: i64 = 0
let mut t: i64 = 1
while t <= tmax {
sum2 = sum2 + (phi_small[t] as i64) * D_cached(N / (t * t), keys, vals, used)
t = t + 1
}
let result: i64 = sum1 + sum2 - sum_tau * Phi[tmax]
printf("%lld\n", result)
free(phi); free(tau); free(lp); free(expv); free(primes); free(Phi)
free(phi_small); free(keys); free(vals); free(used)
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 gcd_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t divisor_summatory_i64(int64_t n);
int64_t hslot_i64_ptr_i64_ptr_i8(int64_t key, int64_t* keys, int8_t* used);
int64_t D_cached_i64_ptr_i64_ptr_i64_ptr_i8(int64_t x, int64_t* keys, int64_t* vals, int8_t* used);
int32_t main(void);
static const int64_t CAP = 2097152;
int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t lcm_i64_i64(int64_t a, int64_t b) {
if ((a == 0 || b == 0)) {
return 0;
}
return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}
int64_t isqrt_i64(int64_t n) {
if (n < 2) {
return n;
}
int64_t x = n;
int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
while (y < x) {
x = y;
y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
}
return x;
}
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
int64_t result = 0;
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
result = FLOW_CHECKED_MOD(((result + a)), (mod));
}
a = FLOW_CHECKED_MOD(((a * 2)), (mod));
b = FLOW_CHECKED_DIV((b), (2));
}
return result;
}
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
if (mod == 1) {
return 0;
}
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
result = mulmod_i64_i64_i64(result, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return result;
}
bool is_prime_i64(int64_t n) {
if (n < 2) {
return 0;
}
if (n < 4) {
return 1;
}
if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
return 0;
}
int64_t i = 5;
while ((i * i) <= n) {
if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
return 0;
}
i = (i + 6);
}
return 1;
}
int64_t divisor_summatory_i64(int64_t n) {
int64_t s = isqrt_i64(n);
int64_t acc = 0;
int64_t i = 1;
while (i <= s) {
acc = (acc + FLOW_CHECKED_DIV((n), (i)));
i = (i + 1);
}
return ((2 * acc) - (s * s));
}
int64_t hslot_i64_ptr_i64_ptr_i8(int64_t key, int64_t* keys, int8_t* used) {
int64_t h = FLOW_CHECKED_MOD((key), (CAP));
if (h < 0) {
h = (h + CAP);
}
while ((used[h] == 1 && keys[h] != key)) {
h = (h + 1);
if (h == CAP) {
h = 0;
}
}
return h;
}
int64_t D_cached_i64_ptr_i64_ptr_i64_ptr_i8(int64_t x, int64_t* keys, int64_t* vals, int8_t* used) {
int64_t s = hslot_i64_ptr_i64_ptr_i8(x, keys, used);
if (used[s] == 1) {
return vals[s];
}
int64_t v = divisor_summatory_i64(x);
used[s] = 1;
keys[s] = x;
vals[s] = v;
return v;
}
int32_t main(void) {
int64_t N = 1000000000000000;
int64_t k = isqrt_i64(N);
int64_t l = FLOW_CHECKED_DIV((N), (k));
int64_t tmax = isqrt_i64(l);
int32_t* phi = (int32_t*)(calloc((k + 1), 4));
int16_t* tau = (int16_t*)(calloc((k + 1), 2));
int32_t* lp = (int32_t*)(calloc((k + 1), 4));
int8_t* expv = (int8_t*)(calloc((k + 1), 1));
int32_t* primes = (int32_t*)(calloc((FLOW_CHECKED_DIV((k), (5)) + 10), 4));
int64_t* Phi = (int64_t*)(calloc((k + 1), 8));
if ((((((phi == NULL || tau == NULL) || lp == NULL) || expv == NULL) || primes == NULL) || Phi == NULL)) {
return 1;
}
phi[1] = 1;
tau[1] = 1;
int64_t pn = 0;
int64_t i = 2;
while (i <= k) {
if (lp[i] == 0) {
lp[i] = ((int32_t)(i));
primes[pn] = ((int32_t)(i));
pn = (pn + 1);
phi[i] = ((int32_t)((i - 1)));
tau[i] = 2;
expv[i] = 1;
}
int64_t pi = 0;
while (pi < pn) {
int64_t p = ((int64_t)(primes[pi]));
int64_t ip = (i * p);
if (ip > k) {
break;
}
lp[ip] = ((int32_t)(p));
if (FLOW_CHECKED_MOD((i), (p)) == 0) {
phi[ip] = ((int32_t)((((int64_t)(phi[i])) * p)));
int64_t e = (((int64_t)(expv[i])) + 1);
expv[ip] = ((int8_t)(e));
tau[ip] = ((int16_t)((FLOW_CHECKED_DIV((((int64_t)(tau[i]))), ((((int64_t)(expv[i])) + 1))) * (e + 1))));
break;
} else {
phi[ip] = ((int32_t)((((int64_t)(phi[i])) * (p - 1))));
expv[ip] = 1;
tau[ip] = ((int16_t)((((int64_t)(tau[i])) * 2)));
}
pi = (pi + 1);
}
i = (i + 1);
}
int64_t running = 0;
i = 1;
while (i <= k) {
running = (running + ((int64_t)(phi[i])));
Phi[i] = running;
i = (i + 1);
}
int32_t* phi_small = (int32_t*)(calloc((tmax + 1), 4));
if (phi_small == NULL) {
return 1;
}
i = 1;
while (i <= tmax) {
phi_small[i] = phi[i];
i = (i + 1);
}
int64_t sum1 = 0;
int64_t sum_tau = 0;
i = 1;
while (i <= k) {
int64_t ti = ((int64_t)(tau[i]));
sum_tau = (sum_tau + ti);
int64_t m = isqrt_i64(FLOW_CHECKED_DIV((N), (i)));
sum1 = (sum1 + (ti * Phi[m]));
i = (i + 1);
}
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 s0 = hslot_i64_ptr_i64_ptr_i8(k, keys, used);
used[s0] = 1;
keys[s0] = k;
vals[s0] = sum_tau;
int64_t sum2 = 0;
int64_t t = 1;
while (t <= tmax) {
sum2 = (sum2 + (((int64_t)(phi_small[t])) * D_cached_i64_ptr_i64_ptr_i64_ptr_i8(FLOW_CHECKED_DIV((N), ((t * t))), keys, vals, used)));
t = (t + 1);
}
int64_t result = ((sum1 + sum2) - (sum_tau * Phi[tmax]));
printf("%lld\n", result);
free(phi);
free(tau);
free(lp);
free(expv);
free(primes);
free(Phi);
free(phi_small);
free(keys);
free(vals);
free(used);
return 0;
}