# Project Euler 397
# Count triangles on y=x^2/k with a 45-degree angle: F(10^6, 10^9).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
# Floor division toward -inf (C/Flow truncate toward 0).
function floordiv(a: i64, b: i64) -> i64 {
let q: i64 = a / b
let r: i64 = a % b
if r != 0 {
if (a < 0 && b > 0) || (a > 0 && b < 0) {
return q - 1
}
}
return q
}
function count_left(X: i64, s: i64, t: i64) -> i64 {
let mut L: i64 = t - X
if L < (0 - X) { L = 0 - X }
let mut U: i64 = s + X
if U > X { U = X }
let u2: i64 = floordiv(s - 1, 2)
if u2 < U { U = u2 }
if U < L { return 0 }
return U - L + 1
}
function count_mid(X: i64, p: i64, q: i64) -> i64 {
let mut L: i64 = p - X
if L < (0 - X) { L = 0 - X }
let qx: i64 = q - X
if qx > L { L = qx }
let l2: i64 = floordiv(p, 2) + 1
if l2 > L { L = l2 }
let mut U: i64 = p + X
if U > X { U = X }
let u2: i64 = floordiv(q - 1, 2)
if u2 < U { U = u2 }
if U < L { return 0 }
return U - L + 1
}
function main() -> i32 {
let K: i64 = 1000000
let X: i64 = 1000000000
let spf: ptr<i32> = calloc(K + 1, 4)
let mut i: i64 = 0
while i <= K {
spf[i] = i as i32
i = i + 1
}
i = 2
while i * i <= K {
if (spf[i] as i64) == i {
let mut j: i64 = i * i
while j <= K {
if (spf[j] as i64) == j { spf[j] = i as i32 }
j = j + i
}
}
i = i + 1
}
let twoX: i64 = 2 * X
let mut count_A: i64 = 0
let mut count_B: i64 = 0
let mut overlap_AB: i64 = 0
let mut overlap_AC: i64 = 0
let fac_p: ptr<i32> = calloc(64, 4)
let fac_e: ptr<i32> = calloc(64, 4)
let divs: ptr<i64> = calloc(4096, 8)
let new_divs: ptr<i64> = calloc(4096, 8)
let mut k: i64 = 1
while k <= K {
let D: i64 = 2 * k * k
# factor 2*k^2 from spf of k
let mut x: i64 = k
let mut fc: i64 = 0
let mut has2: i64 = 0
while x > 1 {
let p: i64 = spf[x] as i64
let mut e: i64 = 0
while x % p == 0 {
x = x / p
e = e + 1
}
if p == 2 {
fac_p[fc] = 2
fac_e[fc] = (2 * e + 1) as i32
has2 = 1
} else {
fac_p[fc] = p as i32
fac_e[fc] = (2 * e) as i32
}
fc = fc + 1
}
if has2 == 0 {
fac_p[fc] = 2
fac_e[fc] = 1
fc = fc + 1
}
divs[0] = 1
let mut dc: i64 = 1
let mut fi: i64 = 0
while fi < fc {
let p: i64 = fac_p[fi] as i64
let expv: i64 = fac_e[fi] as i64
let mut pe: i64 = 1
let mut ndc: i64 = 0
let mut di: i64 = 0
while di < dc {
let base: i64 = divs[di]
pe = 1
let mut e: i64 = 0
while e <= expv {
new_divs[ndc] = base * pe
ndc = ndc + 1
if e < expv { pe = pe * p }
e = e + 1
}
di = di + 1
}
di = 0
while di < ndc {
divs[di] = new_divs[di]
di = di + 1
}
dc = ndc
fi = fi + 1
}
let mut di: i64 = 0
while di < dc {
let d: i64 = divs[di]
let u: i64 = D / d
let s: i64 = k - u
let t: i64 = d - k
if s >= (0 - twoX) && s <= twoX && t >= (0 - twoX) && t <= twoX {
count_A = count_A + count_left(X, s, t)
let denom: i64 = s + k
if denom != 0 {
let num: i64 = k * (s - k)
if num % denom == 0 {
let q: i64 = num / denom
if q >= (0 - twoX) && q <= twoX && ((s + t - q) & 1) == 0 {
let a: i64 = (s + t - q) / 2
let b: i64 = (s + q - t) / 2
let c: i64 = (t + q - s) / 2
if a >= (0 - X) && a < b && b < c && c <= X {
overlap_AB = overlap_AB + 1
}
}
}
}
let denom2: i64 = k - t
if denom2 != 0 {
let num2: i64 = k * (k + t)
if num2 % denom2 == 0 {
let q2: i64 = num2 / denom2
if q2 >= (0 - twoX) && q2 <= twoX && ((s + t - q2) & 1) == 0 {
let a: i64 = (s + t - q2) / 2
let b: i64 = (s + q2 - t) / 2
let c: i64 = (t + q2 - s) / 2
if a >= (0 - X) && a < b && b < c && c <= X {
overlap_AC = overlap_AC + 1
}
}
}
}
}
let p: i64 = 0 - (k + u)
let q: i64 = k + d
if p >= (0 - twoX) && p <= twoX && q <= twoX {
count_B = count_B + count_mid(X, p, q)
}
di = di + 1
}
k = k + 1
}
let total: i64 = 2 * count_A + count_B - 2 * overlap_AB - overlap_AC
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 floordiv_i64_i64(int64_t a, int64_t b);
int64_t count_left_i64_i64_i64(int64_t X, int64_t s, int64_t t);
int64_t count_mid_i64_i64_i64(int64_t X, int64_t p, int64_t q);
int32_t main(void);
int64_t floordiv_i64_i64(int64_t a, int64_t b) {
int64_t q = FLOW_CHECKED_DIV((a), (b));
int64_t r = FLOW_CHECKED_MOD((a), (b));
if (r != 0) {
if (((a < 0 && b > 0) || (a > 0 && b < 0))) {
return (q - 1);
}
}
return q;
}
int64_t count_left_i64_i64_i64(int64_t X, int64_t s, int64_t t) {
int64_t L = (t - X);
if (L < (0 - X)) {
L = (0 - X);
}
int64_t U = (s + X);
if (U > X) {
U = X;
}
int64_t u2 = floordiv_i64_i64((s - 1), 2);
if (u2 < U) {
U = u2;
}
if (U < L) {
return 0;
}
return ((U - L) + 1);
}
int64_t count_mid_i64_i64_i64(int64_t X, int64_t p, int64_t q) {
int64_t L = (p - X);
if (L < (0 - X)) {
L = (0 - X);
}
int64_t qx = (q - X);
if (qx > L) {
L = qx;
}
int64_t l2 = (floordiv_i64_i64(p, 2) + 1);
if (l2 > L) {
L = l2;
}
int64_t U = (p + X);
if (U > X) {
U = X;
}
int64_t u2 = floordiv_i64_i64((q - 1), 2);
if (u2 < U) {
U = u2;
}
if (U < L) {
return 0;
}
return ((U - L) + 1);
}
int32_t main(void) {
int64_t K = 1000000;
int64_t X = 1000000000;
int32_t* spf = (int32_t*)(calloc((K + 1), 4));
int64_t i = 0;
while (i <= K) {
spf[i] = ((int32_t)(i));
i = (i + 1);
}
i = 2;
while ((i * i) <= K) {
if (((int64_t)(spf[i])) == i) {
int64_t j = (i * i);
while (j <= K) {
if (((int64_t)(spf[j])) == j) {
spf[j] = ((int32_t)(i));
}
j = (j + i);
}
}
i = (i + 1);
}
int64_t twoX = (2 * X);
int64_t count_A = 0;
int64_t count_B = 0;
int64_t overlap_AB = 0;
int64_t overlap_AC = 0;
int32_t* fac_p = (int32_t*)(calloc(64, 4));
int32_t* fac_e = (int32_t*)(calloc(64, 4));
int64_t* divs = (int64_t*)(calloc(4096, 8));
int64_t* new_divs = (int64_t*)(calloc(4096, 8));
int64_t k = 1;
while (k <= K) {
int64_t D = ((2 * k) * k);
int64_t x = k;
int64_t fc = 0;
int64_t has2 = 0;
while (x > 1) {
int64_t p = ((int64_t)(spf[x]));
int64_t e = 0;
while (FLOW_CHECKED_MOD((x), (p)) == 0) {
x = FLOW_CHECKED_DIV((x), (p));
e = (e + 1);
}
if (p == 2) {
fac_p[fc] = 2;
fac_e[fc] = ((int32_t)(((2 * e) + 1)));
has2 = 1;
} else {
fac_p[fc] = ((int32_t)(p));
fac_e[fc] = ((int32_t)((2 * e)));
}
fc = (fc + 1);
}
if (has2 == 0) {
fac_p[fc] = 2;
fac_e[fc] = 1;
fc = (fc + 1);
}
divs[0] = 1;
int64_t dc = 1;
int64_t fi = 0;
while (fi < fc) {
int64_t p = ((int64_t)(fac_p[fi]));
int64_t expv = ((int64_t)(fac_e[fi]));
int64_t pe = 1;
int64_t ndc = 0;
int64_t di = 0;
while (di < dc) {
int64_t base = divs[di];
pe = 1;
int64_t e = 0;
while (e <= expv) {
new_divs[ndc] = (base * pe);
ndc = (ndc + 1);
if (e < expv) {
pe = (pe * p);
}
e = (e + 1);
}
di = (di + 1);
}
di = 0;
while (di < ndc) {
divs[di] = new_divs[di];
di = (di + 1);
}
dc = ndc;
fi = (fi + 1);
}
int64_t di = 0;
while (di < dc) {
int64_t d = divs[di];
int64_t u = FLOW_CHECKED_DIV((D), (d));
int64_t s = (k - u);
int64_t t = (d - k);
if ((((s >= (0 - twoX) && s <= twoX) && t >= (0 - twoX)) && t <= twoX)) {
count_A = (count_A + count_left_i64_i64_i64(X, s, t));
int64_t denom = (s + k);
if (denom != 0) {
int64_t num = (k * (s - k));
if (FLOW_CHECKED_MOD((num), (denom)) == 0) {
int64_t q = FLOW_CHECKED_DIV((num), (denom));
if (((q >= (0 - twoX) && q <= twoX) && (((s + t) - q) & 1) == 0)) {
int64_t a = FLOW_CHECKED_DIV((((s + t) - q)), (2));
int64_t b = FLOW_CHECKED_DIV((((s + q) - t)), (2));
int64_t c = FLOW_CHECKED_DIV((((t + q) - s)), (2));
if ((((a >= (0 - X) && a < b) && b < c) && c <= X)) {
overlap_AB = (overlap_AB + 1);
}
}
}
}
int64_t denom2 = (k - t);
if (denom2 != 0) {
int64_t num2 = (k * (k + t));
if (FLOW_CHECKED_MOD((num2), (denom2)) == 0) {
int64_t q2 = FLOW_CHECKED_DIV((num2), (denom2));
if (((q2 >= (0 - twoX) && q2 <= twoX) && (((s + t) - q2) & 1) == 0)) {
int64_t a = FLOW_CHECKED_DIV((((s + t) - q2)), (2));
int64_t b = FLOW_CHECKED_DIV((((s + q2) - t)), (2));
int64_t c = FLOW_CHECKED_DIV((((t + q2) - s)), (2));
if ((((a >= (0 - X) && a < b) && b < c) && c <= X)) {
overlap_AC = (overlap_AC + 1);
}
}
}
}
}
int64_t p = (0 - (k + u));
int64_t q = (k + d);
if (((p >= (0 - twoX) && p <= twoX) && q <= twoX)) {
count_B = (count_B + count_mid_i64_i64_i64(X, p, q));
}
di = (di + 1);
}
k = (k + 1);
}
int64_t total = ((((2 * count_A) + count_B) - (2 * overlap_AB)) - overlap_AC);
printf("%lld\n", total);
return 0;
}