# Project Euler 991
# Equation a/(b+c) + b/(c+a) + c/(a+c) = 4
# Parameterized number theory search over two branches.
extern {
function sqrt(x: f64) -> f64
}
function gcd(a0: i32, b0: i32) -> i32 {
let mut a: i32 = a0
let mut b: i32 = b0
while b != 0 {
let t: i32 = a % b
a = b
b = t
}
if a < 0 {
return -a
}
return a
}
function isqrt_ll(n: i64) -> i32 {
if n < 0 {
return 0
}
let mut r: i64 = sqrt(n as f64) as i64
while r * r > n {
r = r - 1
}
while (r + 1) * (r + 1) <= n {
r = r + 1
}
return r as i32
}
function main() -> i32 {
let limit: i64 = 10000000
let mut total: i64 = 0
# Plus branch: s = 6m^2 - n^2 + mn, n < 2m so s > 4m^2, m <= sqrt(limit/4)
let m_max: i32 = isqrt_ll(limit / 4) + 2
for m in 1..(m_max + 1) {
let n_min: i32 = isqrt_ll(3 * m * m) + 1
let n_max: i32 = 2 * m - 1
for n in n_min..(n_max + 1) {
if gcd(m, n) != 1 {
continue
}
let a: i64 = 4 * m * m - (n as i64) * (n as i64)
let c: i64 = (n as i64) * (n as i64) - 3 * m * m
let b: i64 = 5 * m * m - (n as i64) * (n as i64) + (m as i64) * (n as i64)
let s: i64 = a + b + c
if a <= 0 || b <= 0 || c <= 0 {
continue
}
if s <= limit {
let count: i64 = limit / s
total = total + s * count * (count + 1) / 2
}
}
}
# Minus branch: k = 2m - n, s = k(5m - k)
let alpha: f64 = 2.0 + sqrt(3.0)
let beta: f64 = (5.0 + sqrt(21.0)) / 2.0
let mut k: i32 = 1
while true {
let mut low: i32 = (alpha * (k as f64)) as i32 + 1
while (2 * low - k) * (2 * low - k) <= 3 * low * low {
low = low + 1
}
let mut high_pos: i32 = (beta * (k as f64)) as i32
while high_pos > 0 {
if -(high_pos as i64) * (high_pos as i64) + 5 * (high_pos as i64) * (k as i64) - (k as i64) * (k as i64) > 0 {
break
}
high_pos = high_pos - 1
}
let high_sum: i64 = (limit + (k as i64) * (k as i64)) / (5 * k)
let high: i32 = if high_pos < (high_sum as i32) { high_pos } else { high_sum as i32 }
if low > (high_sum as i32) {
break
}
for m in low..(high + 1) {
if gcd(m, k) != 1 {
continue
}
let n: i32 = 2 * m - k
let a: i64 = 4 * m * m - (n as i64) * (n as i64)
let c: i64 = (n as i64) * (n as i64) - 3 * m * m
let b: i64 = 5 * m * m - (n as i64) * (n as i64) - (m as i64) * (n as i64)
let s: i64 = a + b + c
if a <= 0 || b <= 0 || c <= 0 {
continue
}
if s <= limit {
let count: i64 = limit / s
total = total + s * count * (count + 1) / 2
}
}
k = k + 1
}
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 gcd_i32_i32(int32_t a0, int32_t b0);
int32_t isqrt_ll_i64(int64_t n);
int32_t main(void);
int32_t gcd_i32_i32(int32_t a0, int32_t b0) {
int32_t a = a0;
int32_t b = b0;
while (b != 0) {
int32_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
if (a < 0) {
return (-a);
}
return a;
}
int32_t isqrt_ll_i64(int64_t n) {
if (n < 0) {
return 0;
}
int64_t r = ((int64_t)(sqrt(((double)(n)))));
while ((r * r) > n) {
r = (r - 1);
}
while (((r + 1) * (r + 1)) <= n) {
r = (r + 1);
}
return ((int32_t)(r));
}
int32_t main(void) {
int64_t limit = 10000000;
int64_t total = 0;
int32_t m_max = (isqrt_ll_i64(FLOW_CHECKED_DIV((limit), (4))) + 2);
int32_t __flow_step_1 = 1;
for (int32_t m = 1; (1 <= (m_max + 1)) ? m < (m_max + 1) : m > (m_max + 1); m += (1 <= (m_max + 1)) ? 1 : -1) {
int32_t n_min = (isqrt_ll_i64(((3 * m) * m)) + 1);
int32_t n_max = ((2 * m) - 1);
int32_t __flow_step_2 = 1;
for (int32_t n = n_min; (n_min <= (n_max + 1)) ? n < (n_max + 1) : n > (n_max + 1); n += (n_min <= (n_max + 1)) ? 1 : -1) {
if (gcd_i32_i32(m, n) != 1) {
continue;
}
int64_t a = (((4 * m) * m) - (((int64_t)(n)) * ((int64_t)(n))));
int64_t c = ((((int64_t)(n)) * ((int64_t)(n))) - ((3 * m) * m));
int64_t b = ((((5 * m) * m) - (((int64_t)(n)) * ((int64_t)(n)))) + (((int64_t)(m)) * ((int64_t)(n))));
int64_t s = ((a + b) + c);
if (((a <= 0 || b <= 0) || c <= 0)) {
continue;
}
if (s <= limit) {
int64_t count = FLOW_CHECKED_DIV((limit), (s));
total = (total + FLOW_CHECKED_DIV((((s * count) * (count + 1))), (2)));
}
}
}
double alpha = (2.0 + sqrt(3.0));
double beta = ((5.0 + sqrt(21.0)) / 2.0);
int32_t k = 1;
while (1) {
int32_t low = (((int32_t)((alpha * ((double)(k))))) + 1);
while ((((2 * low) - k) * ((2 * low) - k)) <= ((3 * low) * low)) {
low = (low + 1);
}
int32_t high_pos = ((int32_t)((beta * ((double)(k)))));
while (high_pos > 0) {
if (((((-((int64_t)(high_pos))) * ((int64_t)(high_pos))) + ((5 * ((int64_t)(high_pos))) * ((int64_t)(k)))) - (((int64_t)(k)) * ((int64_t)(k)))) > 0) {
break;
}
high_pos = (high_pos - 1);
}
int64_t high_sum = FLOW_CHECKED_DIV(((limit + (((int64_t)(k)) * ((int64_t)(k))))), ((5 * k)));
int32_t high = ((high_pos < ((int32_t)(high_sum))) ? (high_pos) : (((int32_t)(high_sum))));
if (low > ((int32_t)(high_sum))) {
break;
}
int32_t __flow_step_3 = 1;
for (int32_t m = low; (low <= (high + 1)) ? m < (high + 1) : m > (high + 1); m += (low <= (high + 1)) ? 1 : -1) {
if (gcd_i32_i32(m, k) != 1) {
continue;
}
int32_t n = ((2 * m) - k);
int64_t a = (((4 * m) * m) - (((int64_t)(n)) * ((int64_t)(n))));
int64_t c = ((((int64_t)(n)) * ((int64_t)(n))) - ((3 * m) * m));
int64_t b = ((((5 * m) * m) - (((int64_t)(n)) * ((int64_t)(n)))) - (((int64_t)(m)) * ((int64_t)(n))));
int64_t s = ((a + b) + c);
if (((a <= 0 || b <= 0) || c <= 0)) {
continue;
}
if (s <= limit) {
int64_t count = FLOW_CHECKED_DIV((limit), (s));
total = (total + FLOW_CHECKED_DIV((((s * count) * (count + 1))), (2)));
}
}
k = (k + 1);
}
printf("%lld\n", total);
return 0;
}