Divisors of 2n^2: F(N) = sum over n <= N of the number of divisors of 2n^2 that are at most n, for N = 10^12. Method: F(N) counts pairs (d, n) with d <= n and d | 2n^2. Writing d = g*a, n = g*b with gcd(a, b) = 1 turns the condition into a | 2g, splitting F into two 3D lattice counts over (a, b, k) with parity and ordering constraints. Mobius inversion over odd e removes the gcd condition, and each count reduces to sums of the divisor summatory function D(x) and its partial sums via the hyperbola method, with D(x) for x <= 10^8 served from a sieved prefix table of tau. Runs in a few seconds.
# Project Euler 735
# Divisors of 2n^2: F(N) = sum over n <= N of the number of divisors of
# 2n^2 that are at most n, for N = 10^12.
# Method: F(N) counts pairs (d, n) with d <= n and d | 2n^2. Writing
# d = g*a, n = g*b with gcd(a, b) = 1 turns the condition into a | 2g,
# splitting F into two 3D lattice counts over (a, b, k) with parity and
# ordering constraints. Mobius inversion over odd e removes the gcd
# condition, and each count reduces to sums of the divisor summatory
# function D(x) and its partial sums via the hyperbola method, with
# D(x) for x <= 10^8 served from a sieved prefix table of tau. Runs in
# a few seconds.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function sqrt(x: f64) -> f64
}
function isqrt(x: i64) -> i64 {
if x <= 0 { return 0 }
let mut r: i64 = sqrt(x as f64) as i64
while r > 0 && r * r > x { r = r - 1 }
while (r + 1) * (r + 1) <= x { r = r + 1 }
return r
}
# Divisor summatory D(x) = sum_{k<=x} floor(x/k), hyperbola method.
function bigd(x: i64) -> i64 {
if x <= 0 { return 0 }
let r: i64 = isqrt(x)
let mut s: i64 = 0
for i in 1..(r + 1) { s = s + x / i }
return 2 * s - r * r
}
function dval(x: i64, tab: ptr<i32>, y: i64) -> i64 {
if x <= 0 { return 0 }
if x <= y { return tab[x] as i64 }
return bigd(x)
}
# P(x, l) = sum_{b=1..l} floor(x/b)
function pval(x: i64, l: i64, tab: ptr<i32>, y: i64) -> i64 {
if l <= 0 || x <= 0 { return 0 }
if l >= x { return dval(x, tab, y) }
if l * l <= x {
let mut s: i64 = 0
for b in 1..(l + 1) { s = s + x / b }
return s
}
let k: i64 = x / (l + 1)
let mut s: i64 = dval(x, tab, y)
for i in 1..(k + 1) { s = s - (x / i - l) }
return s
}
# T1(m) = #{(a,b,k): a odd, a <= b, a*b*k <= m}
function t1(m: i64, tab: ptr<i32>, y: i64) -> i64 {
let mut s: i64 = 0
let mut a: i64 = 1
while a * a <= m {
let x: i64 = m / a
s = s + dval(x, tab, y) - pval(x, a - 1, tab, y)
a = a + 2
}
return s
}
# T2(m) = #{(c,b,k): b odd, 2c < b, c*b*k <= m}
function t2(m: i64, tab: ptr<i32>, y: i64) -> i64 {
let mut s: i64 = 0
let mut c: i64 = 1
while c * (2 * c + 1) <= m {
let x: i64 = m / c
let x2: i64 = x / 2
s = s + dval(x, tab, y) - dval(x2, tab, y)
s = s - (pval(x, 2 * c, tab, y) - pval(x2, c, tab, y))
c = c + 1
}
return s
}
function solve(n: i64, y: i64) -> i64 {
# Prefix sums of tau up to y; D(1e8) < 2^31 so i32 holds each entry.
let tab: ptr<i32> = calloc(y + 1, 4)
if tab == null { return -1 }
for d in 1..(y + 1) {
let mut mm: i64 = d
while mm <= y {
tab[mm] = tab[mm] + 1
mm = mm + d
}
}
let mut acc: i64 = 0
for i in 1..(y + 1) {
acc = acc + (tab[i] as i64)
tab[i] = acc as i32
}
# Mobius values up to sqrt(n).
let lim: i64 = isqrt(n)
let mu: ptr<i32> = calloc(lim + 1, 4)
let comp: ptr<i32> = calloc(lim + 1, 4)
if mu == null || comp == null { return -1 }
for i in 1..(lim + 1) { mu[i] = 1 }
let mut p: i64 = 2
while p <= lim {
if comp[p] == 0 {
let mut m: i64 = p
while m <= lim {
if m > p { comp[m] = 1 }
mu[m] = 0 - mu[m]
m = m + p
}
let p2: i64 = p * p
let mut m2: i64 = p2
while m2 <= lim {
mu[m2] = 0
m2 = m2 + p2
}
}
p = p + 1
}
let mut total: i64 = 0
let mut e: i64 = 1
while e * e <= n {
let mv: i64 = mu[e] as i64
if mv != 0 {
let m: i64 = n / (e * e)
total = total + mv * (t1(m, tab, y) + t2(m, tab, y))
}
e = e + 2
}
free(tab)
free(mu)
free(comp)
return total
}
function main() -> i32 {
printf("%lld\n", solve(1000000000000, 100000000))
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 isqrt_i64(int64_t x);
int64_t bigd_i64(int64_t x);
int64_t dval_i64_ptr_i32_i64(int64_t x, int32_t* tab, int64_t y);
int64_t pval_i64_i64_ptr_i32_i64(int64_t x, int64_t l, int32_t* tab, int64_t y);
int64_t t1_i64_ptr_i32_i64(int64_t m, int32_t* tab, int64_t y);
int64_t t2_i64_ptr_i32_i64(int64_t m, int32_t* tab, int64_t y);
int64_t solve_i64_i64(int64_t n, int64_t y);
int32_t main(void);
int64_t isqrt_i64(int64_t x) {
if (x <= 0) {
return 0;
}
int64_t r = ((int64_t)(sqrt(((double)(x)))));
while ((r > 0 && (r * r) > x)) {
r = (r - 1);
}
while (((r + 1) * (r + 1)) <= x) {
r = (r + 1);
}
return r;
}
int64_t bigd_i64(int64_t x) {
if (x <= 0) {
return 0;
}
int64_t r = isqrt_i64(x);
int64_t s = 0;
int32_t __flow_step_1 = 1;
for (int32_t i = 1; (1 <= (r + 1)) ? i < (r + 1) : i > (r + 1); i += (1 <= (r + 1)) ? 1 : -1) {
s = (s + FLOW_CHECKED_DIV((x), (i)));
}
return ((2 * s) - (r * r));
}
int64_t dval_i64_ptr_i32_i64(int64_t x, int32_t* tab, int64_t y) {
if (x <= 0) {
return 0;
}
if (x <= y) {
return ((int64_t)(tab[x]));
}
return bigd_i64(x);
}
int64_t pval_i64_i64_ptr_i32_i64(int64_t x, int64_t l, int32_t* tab, int64_t y) {
if ((l <= 0 || x <= 0)) {
return 0;
}
if (l >= x) {
return dval_i64_ptr_i32_i64(x, tab, y);
}
if ((l * l) <= x) {
int64_t s = 0;
int32_t __flow_step_2 = 1;
for (int32_t b = 1; (1 <= (l + 1)) ? b < (l + 1) : b > (l + 1); b += (1 <= (l + 1)) ? 1 : -1) {
s = (s + FLOW_CHECKED_DIV((x), (b)));
}
return s;
}
int64_t k = FLOW_CHECKED_DIV((x), ((l + 1)));
int64_t s = dval_i64_ptr_i32_i64(x, tab, y);
int32_t __flow_step_3 = 1;
for (int32_t i = 1; (1 <= (k + 1)) ? i < (k + 1) : i > (k + 1); i += (1 <= (k + 1)) ? 1 : -1) {
s = (s - (FLOW_CHECKED_DIV((x), (i)) - l));
}
return s;
}
int64_t t1_i64_ptr_i32_i64(int64_t m, int32_t* tab, int64_t y) {
int64_t s = 0;
int64_t a = 1;
while ((a * a) <= m) {
int64_t x = FLOW_CHECKED_DIV((m), (a));
s = ((s + dval_i64_ptr_i32_i64(x, tab, y)) - pval_i64_i64_ptr_i32_i64(x, (a - 1), tab, y));
a = (a + 2);
}
return s;
}
int64_t t2_i64_ptr_i32_i64(int64_t m, int32_t* tab, int64_t y) {
int64_t s = 0;
int64_t c = 1;
while ((c * ((2 * c) + 1)) <= m) {
int64_t x = FLOW_CHECKED_DIV((m), (c));
int64_t x2 = FLOW_CHECKED_DIV((x), (2));
s = ((s + dval_i64_ptr_i32_i64(x, tab, y)) - dval_i64_ptr_i32_i64(x2, tab, y));
s = (s - (pval_i64_i64_ptr_i32_i64(x, (2 * c), tab, y) - pval_i64_i64_ptr_i32_i64(x2, c, tab, y)));
c = (c + 1);
}
return s;
}
int64_t solve_i64_i64(int64_t n, int64_t y) {
int32_t* tab = (int32_t*)(calloc((y + 1), 4));
if (tab == NULL) {
return (-1);
}
int32_t __flow_step_4 = 1;
for (int32_t d = 1; (1 <= (y + 1)) ? d < (y + 1) : d > (y + 1); d += (1 <= (y + 1)) ? 1 : -1) {
int64_t mm = d;
while (mm <= y) {
tab[mm] = (tab[mm] + 1);
mm = (mm + d);
}
}
int64_t acc = 0;
int32_t __flow_step_5 = 1;
for (int32_t i = 1; (1 <= (y + 1)) ? i < (y + 1) : i > (y + 1); i += (1 <= (y + 1)) ? 1 : -1) {
acc = (acc + ((int64_t)(tab[i])));
tab[i] = ((int32_t)(acc));
}
int64_t lim = isqrt_i64(n);
int32_t* mu = (int32_t*)(calloc((lim + 1), 4));
int32_t* comp = (int32_t*)(calloc((lim + 1), 4));
if ((mu == NULL || comp == NULL)) {
return (-1);
}
int32_t __flow_step_6 = 1;
for (int32_t i = 1; (1 <= (lim + 1)) ? i < (lim + 1) : i > (lim + 1); i += (1 <= (lim + 1)) ? 1 : -1) {
mu[i] = 1;
}
int64_t p = 2;
while (p <= lim) {
if (comp[p] == 0) {
int64_t m = p;
while (m <= lim) {
if (m > p) {
comp[m] = 1;
}
mu[m] = (0 - mu[m]);
m = (m + p);
}
int64_t p2 = (p * p);
int64_t m2 = p2;
while (m2 <= lim) {
mu[m2] = 0;
m2 = (m2 + p2);
}
}
p = (p + 1);
}
int64_t total = 0;
int64_t e = 1;
while ((e * e) <= n) {
int64_t mv = ((int64_t)(mu[e]));
if (mv != 0) {
int64_t m = FLOW_CHECKED_DIV((n), ((e * e)));
total = (total + (mv * (t1_i64_ptr_i32_i64(m, tab, y) + t2_i64_ptr_i32_i64(m, tab, y))));
}
e = (e + 2);
}
free(tab);
free(mu);
free(comp);
return total;
}
int32_t main(void) {
printf("%lld\n", solve_i64_i64(1000000000000, 100000000));
return 0;
}