# Project Euler 784
# Reciprocal Pairs: F(2e6).
# For each r, count divisors d of (r-1)(r+1) with d <= r-1 and sum base+d+n/d.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function malloc(n: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function bitlen_lsb(x: i64) -> i32 {
let c: i32 = 0
let mut v: i64 = x & -x
while v != 0 {
v = v >> 1
c = c + 1
}
return c
}
function main() -> i32 {
let N: i64 = 2000000
let spf: ptr<i64> = calloc(N + 2, 8)
let primes: ptr<i64> = malloc((N + 2) * 8)
let mut pc: i64 = 0
spf[1] = 1
for i in 2..(N + 2) {
if spf[i] == 0 {
spf[i] = i
primes[pc] = i
pc = pc + 1
}
for j in 0..pc {
let p: i64 = primes[j]
let ip: i64 = i * p
if ip > N + 1 || p > spf[i] {
break
}
spf[ip] = p
}
}
let mut total: i64 = 0
let mut r: i64 = 2
while r < N {
let mut kmax: i64 = N - r
if kmax > r - 1 {
kmax = r - 1
}
if kmax <= 0 {
r = r + 1
continue
}
let n_val: i64 = r * r - 1
let base: i64 = 2 * r
if kmax == 1 {
total = total + base + 1 + n_val
r = r + 1
continue
}
let mut a: i64 = r - 1
let mut b: i64 = r + 1
let fac_p: ptr<i64> = malloc(64 * 8)
let fac_e: ptr<i64> = malloc(64 * 8)
let mut nf: i64 = 0
if (r & 1) != 0 {
let ea: i32 = bitlen_lsb(a) - 1
let eb: i32 = bitlen_lsb(b) - 1
let e2: i32 = ea + eb
a = a >> ea
b = b >> eb
if 2 <= kmax {
fac_p[nf] = 2
fac_e[nf] = e2 as i64
nf = nf + 1
}
}
let mut x: i64 = a
while x > 1 {
let p: i64 = spf[x]
if p > kmax {
break
}
let mut e: i64 = 0
while x > 1 && spf[x] == p {
x = x / p
e = e + 1
}
fac_p[nf] = p
fac_e[nf] = e
nf = nf + 1
}
x = b
while x > 1 {
let p: i64 = spf[x]
if p > kmax {
break
}
let mut e: i64 = 0
while x > 1 && spf[x] == p {
x = x / p
e = e + 1
}
fac_p[nf] = p
fac_e[nf] = e
nf = nf + 1
}
let divs: ptr<i64> = malloc(4096 * 8)
let newd: ptr<i64> = malloc(4096 * 8)
let mut nd: i64 = 1
divs[0] = 1
for fi in 0..nf {
let p: i64 = fac_p[fi]
let e: i64 = fac_e[fi]
let prev_n: i64 = nd
let mut pow_p: i64 = 1
let mut nn: i64 = 0
for ee in 0..(e + 1) {
for di in 0..prev_n {
let v: i64 = divs[di] * pow_p
if v <= kmax {
newd[nn] = v
nn = nn + 1
}
}
pow_p = pow_p * p
if pow_p > kmax {
break
}
}
for i in 0..nn {
divs[i] = newd[i]
}
nd = nn
}
for i in 0..nd {
total = total + base + divs[i] + n_val / divs[i]
}
free(fac_p)
free(fac_e)
free(divs)
free(newd)
r = r + 1
}
free(spf)
free(primes)
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; }
int32_t bitlen_lsb_i64(int64_t x);
int32_t main(void);
int32_t bitlen_lsb_i64(int64_t x) {
int32_t c = 0;
int64_t v = (x & (-x));
while (v != 0) {
v = FLOW_CHECKED_SHR((v), (1));
c = (c + 1);
}
return c;
}
int32_t main(void) {
int64_t N = 2000000;
int64_t* spf = (int64_t*)(calloc((N + 2), 8));
int64_t* primes = (int64_t*)(malloc(((N + 2) * 8)));
int64_t pc = 0;
spf[1] = 1;
int32_t __flow_step_1 = 1;
for (int32_t i = 2; (2 <= (N + 2)) ? i < (N + 2) : i > (N + 2); i += (2 <= (N + 2)) ? 1 : -1) {
if (spf[i] == 0) {
spf[i] = i;
primes[pc] = i;
pc = (pc + 1);
}
int32_t __flow_step_2 = 1;
for (int32_t j = 0; (0 <= pc) ? j < pc : j > pc; j += (0 <= pc) ? 1 : -1) {
int64_t p = primes[j];
int64_t ip = (i * p);
if ((ip > (N + 1) || p > spf[i])) {
break;
}
spf[ip] = p;
}
}
int64_t total = 0;
int64_t r = 2;
while (r < N) {
int64_t kmax = (N - r);
if (kmax > (r - 1)) {
kmax = (r - 1);
}
if (kmax <= 0) {
r = (r + 1);
continue;
}
int64_t n_val = ((r * r) - 1);
int64_t base = (2 * r);
if (kmax == 1) {
total = (((total + base) + 1) + n_val);
r = (r + 1);
continue;
}
int64_t a = (r - 1);
int64_t b = (r + 1);
int64_t* fac_p = (int64_t*)(malloc((64 * 8)));
int64_t* fac_e = (int64_t*)(malloc((64 * 8)));
int64_t nf = 0;
if ((r & 1) != 0) {
int32_t ea = (bitlen_lsb_i64(a) - 1);
int32_t eb = (bitlen_lsb_i64(b) - 1);
int32_t e2 = (ea + eb);
a = FLOW_CHECKED_SHR((a), (ea));
b = FLOW_CHECKED_SHR((b), (eb));
if (2 <= kmax) {
fac_p[nf] = 2;
fac_e[nf] = ((int64_t)(e2));
nf = (nf + 1);
}
}
int64_t x = a;
while (x > 1) {
int64_t p = spf[x];
if (p > kmax) {
break;
}
int64_t e = 0;
while ((x > 1 && spf[x] == p)) {
x = FLOW_CHECKED_DIV((x), (p));
e = (e + 1);
}
fac_p[nf] = p;
fac_e[nf] = e;
nf = (nf + 1);
}
x = b;
while (x > 1) {
int64_t p = spf[x];
if (p > kmax) {
break;
}
int64_t e = 0;
while ((x > 1 && spf[x] == p)) {
x = FLOW_CHECKED_DIV((x), (p));
e = (e + 1);
}
fac_p[nf] = p;
fac_e[nf] = e;
nf = (nf + 1);
}
int64_t* divs = (int64_t*)(malloc((4096 * 8)));
int64_t* newd = (int64_t*)(malloc((4096 * 8)));
int64_t nd = 1;
divs[0] = 1;
int32_t __flow_step_3 = 1;
for (int32_t fi = 0; (0 <= nf) ? fi < nf : fi > nf; fi += (0 <= nf) ? 1 : -1) {
int64_t p = fac_p[fi];
int64_t e = fac_e[fi];
int64_t prev_n = nd;
int64_t pow_p = 1;
int64_t nn = 0;
int32_t __flow_step_4 = 1;
for (int32_t ee = 0; (0 <= (e + 1)) ? ee < (e + 1) : ee > (e + 1); ee += (0 <= (e + 1)) ? 1 : -1) {
int32_t __flow_step_5 = 1;
for (int32_t di = 0; (0 <= prev_n) ? di < prev_n : di > prev_n; di += (0 <= prev_n) ? 1 : -1) {
int64_t v = (divs[di] * pow_p);
if (v <= kmax) {
newd[nn] = v;
nn = (nn + 1);
}
}
pow_p = (pow_p * p);
if (pow_p > kmax) {
break;
}
}
int32_t __flow_step_6 = 1;
for (int32_t i = 0; (0 <= nn) ? i < nn : i > nn; i += (0 <= nn) ? 1 : -1) {
divs[i] = newd[i];
}
nd = nn;
}
int32_t __flow_step_7 = 1;
for (int32_t i = 0; (0 <= nd) ? i < nd : i > nd; i += (0 <= nd) ? 1 : -1) {
total = (((total + base) + divs[i]) + FLOW_CHECKED_DIV((n_val), (divs[i])));
}
free(fac_p);
free(fac_e);
free(divs);
free(newd);
r = (r + 1);
}
free(spf);
free(primes);
printf("%lld\n", total);
return 0;
}