# Project Euler 264
# Sum perimeters of lattice triangles with circumcenter O and orthocenter (5,0).
import euler.nt { isqrt }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function sqrt(x: f64) -> f64
}
function gcd_abs(a0: i64, b0: i64) -> i64 {
let mut a: i64 = a0
let mut b: i64 = b0
if a < 0 { a = -a }
if b < 0 { b = -b }
while b != 0 {
let t: i64 = a % b
a = b
b = t
}
return a
}
function mod_inverse(a0: i64, m: i64) -> i64 {
let mut a: i64 = a0 % m
if a < 0 { a = a + m }
let mut b: i64 = m
let mut x0: i64 = 1
let mut x1: i64 = 0
while b != 0 {
let q: i64 = a / b
let t: i64 = a % b
a = b
b = t
let tx: i64 = x0 - q * x1
x0 = x1
x1 = tx
}
if x0 < 0 { x0 = x0 + m }
return x0
}
function hypot_f(ax: i64, ay: i64, bx: i64, by: i64) -> f64 {
let dx: f64 = (ax - bx) as f64
let dy: f64 = (ay - by) as f64
return sqrt(dx * dx + dy * dy)
}
function tri_key(ax: i64, ay: i64, bx: i64, by: i64, cx: i64, cy: i64) -> i64 {
# pack sorted coords into hash (simple dedup key)
let mut a: i64 = ax * 1000003 + ay
let mut b: i64 = bx * 1000003 + by
let mut c: i64 = cx * 1000003 + cy
if a > b { let t: i64 = a; a = b; b = t }
if b > c { let t2: i64 = b; b = c; c = t2 }
if a > b { let t3: i64 = a; a = b; b = t3 }
return a * 1000000007 + b * 1000003 + c
}
function main() -> i32 {
let limit_p: i64 = 100000
let HX: i64 = 5
let HY: i64 = 0
let d_max: f64 = (limit_p as f64) / 2.0
let m_limit: i64 = (40.0 * d_max + 100.0) as i64
let pq_limit: i64 = isqrt(m_limit) + 1
let r_max: f64 = (limit_p as f64) / 4.0
let r2_max: f64 = r_max * r_max
let max_tri: i64 = 500000
let seen: ptr<i64> = calloc(max_tri, 8)
let seen_cnt: ptr<i64> = calloc(1, 8)
let mut total: f64 = 0.0
# d=0 special case: H=(5,0), b=-c on circle radius 5
let n0: i64 = 25
let mut x: i64 = -5
while x <= 5 {
let y2: i64 = n0 - x * x
if y2 >= 0 {
let y: i64 = isqrt(y2)
if y * y == y2 {
let mut yv: i64 = -y
while yv <= y {
if yv != 0 {
let bx: i64 = x
let by: i64 = yv
let cx: i64 = -x
let cy: i64 = -yv
if !(bx == cx && by == cy) {
let per: f64 = hypot_f(HX, HY, bx, by) + hypot_f(bx, by, cx, cy) + hypot_f(cx, cy, HX, HY)
if per <= (limit_p as f64) + 0.000000001 {
let key: i64 = tri_key(HX, HY, bx, by, cx, cy)
let mut found: bool = false
let sc: i64 = seen_cnt[0]
let mut si: i64 = 0
while si < sc {
if seen[si] == key { found = true; break }
si = si + 1
}
if !found {
seen[sc] = key
seen_cnt[0] = sc + 1
total = total + per
}
}
}
}
if yv == y { break }
if yv == -y { yv = y } else { yv = yv + 1 }
}
}
}
x = x + 1
}
let mut p: i64 = 0
while p <= pq_limit {
let p2: i64 = p * p
if p2 > m_limit { break }
let q_max: i64 = isqrt(m_limit - p2)
let mut q: i64 = -q_max
while q <= q_max {
if p == 0 {
if q != 1 { q = q + 1; continue }
}
if p == 0 && q == 0 { q = q + 1; continue }
if gcd_abs(p, q) != 1 { q = q + 1; continue }
let m: i64 = p2 + q * q
if m == 0 || m > m_limit { q = q + 1; continue }
let a40: i64 = (40 * p) % m
let b100: i64 = 100 % m
let d0: i64 = gcd_abs(a40, m)
if b100 % d0 != 0 { q = q + 1; continue }
let m1: i64 = m / d0
let a1: i64 = a40 / d0
let b1: i64 = b100 / d0
let mut g0: i64 = 0
if m1 > 1 {
let inv: i64 = mod_inverse(a1 % m1, m1)
g0 = (b1 * inv) % m1
}
let g_abs_max: i64 = (d_max / sqrt(m as f64)) as i64 + 2
if g_abs_max <= 0 { q = q + 1; continue }
let mut g_abs_min: i64 = 1
if p > 0 {
if m > 100 {
g_abs_min = (m - 100 + 40 * p - 1) / (40 * p)
if g_abs_min < 1 { g_abs_min = 1 }
}
}
if g_abs_min > g_abs_max { q = q + 1; continue }
let k_min: i64 = (-g_abs_max - g0 + m1 - 1) / m1
let k_max: i64 = (g_abs_max - g0) / m1
let mut k: i64 = k_min
while k <= k_max {
let g: i64 = g0 + k * m1
if g != 0 {
let ag: i64 = g
if ag < 0 { ag = -ag }
if ag >= g_abs_min && ag <= g_abs_max {
let gg: i64 = g
let M: i64 = 3 * gg * gg * m - 40 * gg * p + 100
if M > 0 && M % m == 0 {
let t2: i64 = M / m
let t: i64 = isqrt(t2)
if t * t == t2 {
if ((g * p + t * q) & 1) == 0 && ((g * q - t * p) & 1) == 0 {
let ax: i64 = HX - g * p
let ay: i64 = HY - g * q
let bx: i64 = (g * p + t * q) / 2
let by: i64 = (g * q - t * p) / 2
let cx: i64 = (g * p - t * q) / 2
let cy: i64 = (g * q + t * p) / 2
if !((ax == bx && ay == by) || (ax == cx && ay == cy) || (bx == cx && by == cy)) {
let n: i64 = ax * ax + ay * ay
if (n as f64) <= r2_max + 0.000000001 {
let per: f64 = hypot_f(ax, ay, bx, by) + hypot_f(bx, by, cx, cy) + hypot_f(cx, cy, ax, ay)
if per <= (limit_p as f64) + 0.000000001 {
let key: i64 = tri_key(ax, ay, bx, by, cx, cy)
let mut found: bool = false
let sc: i64 = seen_cnt[0]
let mut si: i64 = 0
while si < sc {
if seen[si] == key { found = true; break }
si = si + 1
}
if !found {
seen[sc] = key
seen_cnt[0] = sc + 1
total = total + per
}
}
}
}
}
}
}
}
}
k = k + 1
}
q = q + 1
}
p = p + 1
}
free(seen); free(seen_cnt)
printf("%.4f\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 gcd_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t gcd_abs_i64_i64(int64_t a0, int64_t b0);
int64_t mod_inverse_i64_i64(int64_t a0, int64_t m);
double hypot_f_i64_i64_i64_i64(int64_t ax, int64_t ay, int64_t bx, int64_t by);
int64_t tri_key_i64_i64_i64_i64_i64_i64(int64_t ax, int64_t ay, int64_t bx, int64_t by, int64_t cx, int64_t cy);
int32_t main(void);
int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t lcm_i64_i64(int64_t a, int64_t b) {
if ((a == 0 || b == 0)) {
return 0;
}
return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}
int64_t isqrt_i64(int64_t n) {
if (n < 2) {
return n;
}
int64_t x = n;
int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
while (y < x) {
x = y;
y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
}
return x;
}
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
int64_t result = 0;
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
result = FLOW_CHECKED_MOD(((result + a)), (mod));
}
a = FLOW_CHECKED_MOD(((a * 2)), (mod));
b = FLOW_CHECKED_DIV((b), (2));
}
return result;
}
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
if (mod == 1) {
return 0;
}
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
result = mulmod_i64_i64_i64(result, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return result;
}
bool is_prime_i64(int64_t n) {
if (n < 2) {
return 0;
}
if (n < 4) {
return 1;
}
if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
return 0;
}
int64_t i = 5;
while ((i * i) <= n) {
if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
return 0;
}
i = (i + 6);
}
return 1;
}
int64_t gcd_abs_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
if (a < 0) {
a = (-a);
}
if (b < 0) {
b = (-b);
}
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t mod_inverse_i64_i64(int64_t a0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD((a0), (m));
if (a < 0) {
a = (a + m);
}
int64_t b = m;
int64_t x0 = 1;
int64_t x1 = 0;
while (b != 0) {
int64_t q = FLOW_CHECKED_DIV((a), (b));
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
int64_t tx = (x0 - (q * x1));
x0 = x1;
x1 = tx;
}
if (x0 < 0) {
x0 = (x0 + m);
}
return x0;
}
double hypot_f_i64_i64_i64_i64(int64_t ax, int64_t ay, int64_t bx, int64_t by) {
double dx = ((double)((ax - bx)));
double dy = ((double)((ay - by)));
return sqrt(((dx * dx) + (dy * dy)));
}
int64_t tri_key_i64_i64_i64_i64_i64_i64(int64_t ax, int64_t ay, int64_t bx, int64_t by, int64_t cx, int64_t cy) {
int64_t a = ((ax * 1000003) + ay);
int64_t b = ((bx * 1000003) + by);
int64_t c = ((cx * 1000003) + cy);
if (a > b) {
int64_t t = a;
a = b;
b = t;
}
if (b > c) {
int64_t t2 = b;
b = c;
c = t2;
}
if (a > b) {
int64_t t3 = a;
a = b;
b = t3;
}
return (((a * 1000000007) + (b * 1000003)) + c);
}
int32_t main(void) {
int64_t limit_p = 100000;
int64_t HX = 5;
int64_t HY = 0;
double d_max = (((double)(limit_p)) / 2.0);
int64_t m_limit = ((int64_t)(((40.0 * d_max) + 100.0)));
int64_t pq_limit = (isqrt_i64(m_limit) + 1);
double r_max = (((double)(limit_p)) / 4.0);
double r2_max = (r_max * r_max);
int64_t max_tri = 500000;
int64_t* seen = (int64_t*)(calloc(max_tri, 8));
int64_t* seen_cnt = (int64_t*)(calloc(1, 8));
double total = 0.0;
int64_t n0 = 25;
int64_t x = (-5);
while (x <= 5) {
int64_t y2 = (n0 - (x * x));
if (y2 >= 0) {
int64_t y = isqrt_i64(y2);
if ((y * y) == y2) {
int64_t yv = (-y);
while (yv <= y) {
if (yv != 0) {
int64_t bx = x;
int64_t by = yv;
int64_t cx = (-x);
int64_t cy = (-yv);
if ((!((bx == cx && by == cy)))) {
double per = ((hypot_f_i64_i64_i64_i64(HX, HY, bx, by) + hypot_f_i64_i64_i64_i64(bx, by, cx, cy)) + hypot_f_i64_i64_i64_i64(cx, cy, HX, HY));
if (per <= (((double)(limit_p)) + 0.000000001)) {
int64_t key = tri_key_i64_i64_i64_i64_i64_i64(HX, HY, bx, by, cx, cy);
bool found = 0;
int64_t sc = seen_cnt[0];
int64_t si = 0;
while (si < sc) {
if (seen[si] == key) {
found = 1;
break;
}
si = (si + 1);
}
if ((!(found))) {
seen[sc] = key;
seen_cnt[0] = (sc + 1);
total = (total + per);
}
}
}
}
if (yv == y) {
break;
}
if (yv == (-y)) {
yv = y;
} else {
yv = (yv + 1);
}
}
}
}
x = (x + 1);
}
int64_t p = 0;
while (p <= pq_limit) {
int64_t p2 = (p * p);
if (p2 > m_limit) {
break;
}
int64_t q_max = isqrt_i64((m_limit - p2));
int64_t q = (-q_max);
while (q <= q_max) {
if (p == 0) {
if (q != 1) {
q = (q + 1);
continue;
}
}
if ((p == 0 && q == 0)) {
q = (q + 1);
continue;
}
if (gcd_abs_i64_i64(p, q) != 1) {
q = (q + 1);
continue;
}
int64_t m = (p2 + (q * q));
if ((m == 0 || m > m_limit)) {
q = (q + 1);
continue;
}
int64_t a40 = FLOW_CHECKED_MOD(((40 * p)), (m));
int64_t b100 = FLOW_CHECKED_MOD((100), (m));
int64_t d0 = gcd_abs_i64_i64(a40, m);
if (FLOW_CHECKED_MOD((b100), (d0)) != 0) {
q = (q + 1);
continue;
}
int64_t m1 = FLOW_CHECKED_DIV((m), (d0));
int64_t a1 = FLOW_CHECKED_DIV((a40), (d0));
int64_t b1 = FLOW_CHECKED_DIV((b100), (d0));
int64_t g0 = 0;
if (m1 > 1) {
int64_t inv = mod_inverse_i64_i64(FLOW_CHECKED_MOD((a1), (m1)), m1);
g0 = FLOW_CHECKED_MOD(((b1 * inv)), (m1));
}
int64_t g_abs_max = (((int64_t)((d_max / sqrt(((double)(m)))))) + 2);
if (g_abs_max <= 0) {
q = (q + 1);
continue;
}
int64_t g_abs_min = 1;
if (p > 0) {
if (m > 100) {
g_abs_min = FLOW_CHECKED_DIV(((((m - 100) + (40 * p)) - 1)), ((40 * p)));
if (g_abs_min < 1) {
g_abs_min = 1;
}
}
}
if (g_abs_min > g_abs_max) {
q = (q + 1);
continue;
}
int64_t k_min = FLOW_CHECKED_DIV((((((-g_abs_max) - g0) + m1) - 1)), (m1));
int64_t k_max = FLOW_CHECKED_DIV(((g_abs_max - g0)), (m1));
int64_t k = k_min;
while (k <= k_max) {
int64_t g = (g0 + (k * m1));
if (g != 0) {
int64_t ag = g;
if (ag < 0) {
ag = (-ag);
}
if ((ag >= g_abs_min && ag <= g_abs_max)) {
int64_t gg = g;
int64_t M = (((((3 * gg) * gg) * m) - ((40 * gg) * p)) + 100);
if ((M > 0 && FLOW_CHECKED_MOD((M), (m)) == 0)) {
int64_t t2 = FLOW_CHECKED_DIV((M), (m));
int64_t t = isqrt_i64(t2);
if ((t * t) == t2) {
if (((((g * p) + (t * q)) & 1) == 0 && (((g * q) - (t * p)) & 1) == 0)) {
int64_t ax = (HX - (g * p));
int64_t ay = (HY - (g * q));
int64_t bx = FLOW_CHECKED_DIV((((g * p) + (t * q))), (2));
int64_t by = FLOW_CHECKED_DIV((((g * q) - (t * p))), (2));
int64_t cx = FLOW_CHECKED_DIV((((g * p) - (t * q))), (2));
int64_t cy = FLOW_CHECKED_DIV((((g * q) + (t * p))), (2));
if ((!((((ax == bx && ay == by) || (ax == cx && ay == cy)) || (bx == cx && by == cy))))) {
int64_t n = ((ax * ax) + (ay * ay));
if (((double)(n)) <= (r2_max + 0.000000001)) {
double per = ((hypot_f_i64_i64_i64_i64(ax, ay, bx, by) + hypot_f_i64_i64_i64_i64(bx, by, cx, cy)) + hypot_f_i64_i64_i64_i64(cx, cy, ax, ay));
if (per <= (((double)(limit_p)) + 0.000000001)) {
int64_t key = tri_key_i64_i64_i64_i64_i64_i64(ax, ay, bx, by, cx, cy);
bool found = 0;
int64_t sc = seen_cnt[0];
int64_t si = 0;
while (si < sc) {
if (seen[si] == key) {
found = 1;
break;
}
si = (si + 1);
}
if ((!(found))) {
seen[sc] = key;
seen_cnt[0] = (sc + 1);
total = (total + per);
}
}
}
}
}
}
}
}
}
k = (k + 1);
}
q = (q + 1);
}
p = (p + 1);
}
free(seen);
free(seen_cnt);
printf("%.4f\n", total);
return 0;
}