f(k)/(k^2 phi(k)) is multiplicative with value p^(3(e-1)) * (p^3 - chi4(p)) at p^e, where chi4 is the nonprincipal character mod 4 (count solutions of a rank-6 quadratic form mod p). Its Dirichlet series is zeta(s-3)/L(s,chi4), so g = Id^3 * (mu.chi4) and G(n) = sum_d mu(d)chi4(d) * S3(floor(n/d)) with S3(m) = (m(m+1)/2)^2. Compute M(x) = sum_{d<=x} mu(d)chi4(d) by a linear sieve up to K = 4*10^7 plus the Mertens-style recursion from (mu.chi4)*chi4 = epsilon, then sum over quotient blocks. O(n^(2/3)) total.
# Project Euler 715: Sextuplet Norms
#
# f(k)/(k^2 phi(k)) is multiplicative with value p^(3(e-1)) * (p^3 - chi4(p))
# at p^e, where chi4 is the nonprincipal character mod 4 (count solutions of
# a rank-6 quadratic form mod p). Its Dirichlet series is zeta(s-3)/L(s,chi4),
# so g = Id^3 * (mu.chi4) and G(n) = sum_d mu(d)chi4(d) * S3(floor(n/d)) with
# S3(m) = (m(m+1)/2)^2. Compute M(x) = sum_{d<=x} mu(d)chi4(d) by a linear
# sieve up to K = 4*10^7 plus the Mertens-style recursion from
# (mu.chi4)*chi4 = epsilon, then sum over quotient blocks. O(n^(2/3)) total.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
# C(t) = sum_{d<=t} chi4(d), period 4 pattern 1,1,0,0
function cfun(t: i64) -> i64 {
let r: i64 = t % 4
if r == 1 || r == 2 {
return 1
}
return 0
}
function main() -> i32 {
let n: i64 = 1000000000000
let kk: i64 = 40000000
let md: i64 = 1000000007
let inv2: i64 = 500000004
# linear sieve for mu up to kk
let comp: ptr<i8> = calloc(kk + 1, 1)
let mu: ptr<i8> = calloc(kk + 1, 1)
let primes: ptr<i32> = calloc(2500000, 4)
mu[1] = 1
let mut np: i64 = 0
let mut i: i64 = 2
while i <= kk {
if comp[i] == 0 {
primes[np] = i as i32
np = np + 1
mu[i] = 0 - 1
}
let mut j: i64 = 0
while j < np {
let p: i64 = primes[j] as i64
let ip: i64 = i * p
if ip > kk {
break
}
comp[ip] = 1
if i % p == 0 {
mu[ip] = 0
break
}
mu[ip] = 0 - mu[i]
j = j + 1
}
i = i + 1
}
free(comp)
free(primes)
# prefix sums of h(d) = mu(d) * chi4(d)
let msmall: ptr<i32> = calloc(kk + 1, 4)
let mut acc: i64 = 0
i = 1
while i <= kk {
if i % 2 == 1 {
let m: i64 = mu[i] as i64
if i % 4 == 1 {
acc = acc + m
} else {
acc = acc - m
}
}
msmall[i] = acc as i32
i = i + 1
}
free(mu)
# M(n/ii) for ii = 1..imax via M(x) = 1 - sum_{d>=2} chi4(d) M(x/d)
let imax: i64 = n / kk
let mbig: ptr<i64> = calloc(imax + 2, 8)
let mut ii: i64 = imax
while ii >= 1 {
let x: i64 = n / ii
if x <= kk {
mbig[ii] = msmall[x] as i64
} else {
let mut s: i64 = 1
let mut d: i64 = 2
while d <= x {
let q: i64 = x / d
let d2: i64 = x / q
let w: i64 = cfun(d2) - cfun(d - 1)
if w != 0 {
if q <= kk {
s = s - w * (msmall[q] as i64)
} else {
s = s - w * mbig[ii * d]
}
}
d = d2 + 1
}
mbig[ii] = s
}
ii = ii - 1
}
# G(n) = sum over quotient blocks of (M(hi) - M(lo-1)) * S3(q) mod md
let mut total: i64 = 0
let mut prev: i64 = 0
let mut lo: i64 = 1
while lo <= n {
let q: i64 = n / lo
let hi: i64 = n / q
let mut m2: i64 = 0
if hi <= kk {
m2 = msmall[hi] as i64
} else {
m2 = mbig[n / hi]
}
let mut w: i64 = (m2 - prev) % md
if w < 0 {
w = w + md
}
let a: i64 = q % md
let b: i64 = (q + 1) % md
let s1: i64 = a * b % md * inv2 % md
let s3: i64 = s1 * s1 % md
total = (total + w * s3) % md
prev = m2
lo = hi + 1
}
free(msmall)
free(mbig)
printf("%lld\n", total)
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 cfun_i64(int64_t t);
int32_t main(void);
int64_t cfun_i64(int64_t t) {
int64_t r = FLOW_CHECKED_MOD((t), (4));
if ((r == 1 || r == 2)) {
return 1;
}
return 0;
}
int32_t main(void) {
int64_t n = 1000000000000;
int64_t kk = 40000000;
int64_t md = 1000000007;
int64_t inv2 = 500000004;
int8_t* comp = (int8_t*)(calloc((kk + 1), 1));
int8_t* mu = (int8_t*)(calloc((kk + 1), 1));
int32_t* primes = (int32_t*)(calloc(2500000, 4));
mu[1] = 1;
int64_t np = 0;
int64_t i = 2;
while (i <= kk) {
if (comp[i] == 0) {
primes[np] = ((int32_t)(i));
np = (np + 1);
mu[i] = (0 - 1);
}
int64_t j = 0;
while (j < np) {
int64_t p = ((int64_t)(primes[j]));
int64_t ip = (i * p);
if (ip > kk) {
break;
}
comp[ip] = 1;
if (FLOW_CHECKED_MOD((i), (p)) == 0) {
mu[ip] = 0;
break;
}
mu[ip] = (0 - mu[i]);
j = (j + 1);
}
i = (i + 1);
}
free(comp);
free(primes);
int32_t* msmall = (int32_t*)(calloc((kk + 1), 4));
int64_t acc = 0;
i = 1;
while (i <= kk) {
if (FLOW_CHECKED_MOD((i), (2)) == 1) {
int64_t m = ((int64_t)(mu[i]));
if (FLOW_CHECKED_MOD((i), (4)) == 1) {
acc = (acc + m);
} else {
acc = (acc - m);
}
}
msmall[i] = ((int32_t)(acc));
i = (i + 1);
}
free(mu);
int64_t imax = FLOW_CHECKED_DIV((n), (kk));
int64_t* mbig = (int64_t*)(calloc((imax + 2), 8));
int64_t ii = imax;
while (ii >= 1) {
int64_t x = FLOW_CHECKED_DIV((n), (ii));
if (x <= kk) {
mbig[ii] = ((int64_t)(msmall[x]));
} else {
int64_t s = 1;
int64_t d = 2;
while (d <= x) {
int64_t q = FLOW_CHECKED_DIV((x), (d));
int64_t d2 = FLOW_CHECKED_DIV((x), (q));
int64_t w = (cfun_i64(d2) - cfun_i64((d - 1)));
if (w != 0) {
if (q <= kk) {
s = (s - (w * ((int64_t)(msmall[q]))));
} else {
s = (s - (w * mbig[(ii * d)]));
}
}
d = (d2 + 1);
}
mbig[ii] = s;
}
ii = (ii - 1);
}
int64_t total = 0;
int64_t prev = 0;
int64_t lo = 1;
while (lo <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (lo));
int64_t hi = FLOW_CHECKED_DIV((n), (q));
int64_t m2 = 0;
if (hi <= kk) {
m2 = ((int64_t)(msmall[hi]));
} else {
m2 = mbig[FLOW_CHECKED_DIV((n), (hi))];
}
int64_t w = FLOW_CHECKED_MOD(((m2 - prev)), (md));
if (w < 0) {
w = (w + md);
}
int64_t a = FLOW_CHECKED_MOD((q), (md));
int64_t b = FLOW_CHECKED_MOD(((q + 1)), (md));
int64_t s1 = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((a * b)), (md)) * inv2)), (md));
int64_t s3 = FLOW_CHECKED_MOD(((s1 * s1)), (md));
total = FLOW_CHECKED_MOD(((total + (w * s3))), (md));
prev = m2;
lo = (hi + 1);
}
free(msmall);
free(mbig);
printf("%lld\n", total);
return 0;
}