# Project Euler 385
# A(10^9): sum of areas of integer triangles with Steiner foci at (±√13, 0).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function igcd(a0: i64, b0: i64) -> i64 {
let mut a: i64 = a0
let mut b: i64 = b0
if a < 0 { a = 0 - a }
if b < 0 { b = 0 - b }
while b != 0 {
let t: i64 = a % b
a = b
b = t
}
return a
}
function iabs(x: i64) -> i64 {
if x < 0 { return 0 - x }
return x
}
function pt_less(ax: i64, ay: i64, bx: i64, by: i64) -> i64 {
if ax < bx { return 1 }
if ax > bx { return 0 }
if ay < by { return 1 }
return 0
}
function tri_seen(sx: ptr<i64>, sy: ptr<i64>, nseen: i64,
x1: i64, y1: i64, x2: i64, y2: i64, x3: i64, y3: i64) -> i64 {
let mut i: i64 = 0
while i < nseen {
let b: i64 = i * 3
if sx[b] == x1 && sy[b] == y1 && sx[b + 1] == x2 && sy[b + 1] == y2 && sx[b + 2] == x3 && sy[b + 2] == y3 {
return 1
}
i = i + 1
}
return 0
}
function main() -> i32 {
let N: i64 = 1000000000
# Pell seeds packed: K, then pairs (s,t). We hardcode the four K groups.
let seeds_k: array<i64, 4> = [468, 117, 36, 9]
let seeds_s: array<i64, 6> = [24, 30, 12, 15, 6, 3]
let seeds_t: array<i64, 6> = [6, 12, 3, 6, 0, 0]
let seeds_cnt: array<i64, 4> = [2, 2, 1, 1]
let seeds_off: array<i64, 4> = [0, 2, 4, 5]
let sx: ptr<i64> = calloc(500 * 3, 8)
let sy: ptr<i64> = calloc(500 * 3, 8)
if sx == null || sy == null { return 1 }
let mut nseen: i64 = 0
let mut total: i128 = 0
let mut m: i64 = 0 - 12
while m <= 12 {
let mut n: i64 = 0 - 21
while n <= 21 {
if m == 0 && n == 0 {
n = n + 1
continue
}
if igcd(iabs(m), iabs(n)) != 1 {
n = n + 1
continue
}
let D: i64 = n * n + 3 * m * m
if D == 0 || 468 % D != 0 {
n = n + 1
continue
}
let K: i64 = 468 / D
let mut ki: i64 = 0
let mut found: i64 = 0
while ki < 4 {
if seeds_k[ki] == K {
found = 1
break
}
ki = ki + 1
}
if found == 0 {
n = n + 1
continue
}
let cnt: i64 = seeds_cnt[ki]
let off: i64 = seeds_off[ki]
let mut si: i64 = 0
while si < cnt {
let mut s: i64 = seeds_s[off + si]
let mut t: i64 = seeds_t[off + si]
while 1 == 1 {
let mut mx: i64 = s
if t > mx { mx = t }
if mx > 6 * N + 10 { break }
if s != 0 && t != 0 {
let mut ssign: i64 = 0
while ssign < 2 {
let mut tsign: i64 = 0
while tsign < 2 {
let mut ss: i64 = s
if ssign == 1 { ss = 0 - s }
let mut tt: i64 = t
if tsign == 1 { tt = 0 - t }
if (ss * n) % 3 == 0 {
let a: i64 = ss * m
let b: i64 = 0 - tt * n
let c: i64 = (ss * n) / 3
let d: i64 = tt * m
if ((a + c) & 1) == 0 && ((b + d) & 1) == 0 {
let x1: i64 = (a + c) / 2
let y1: i64 = (b + d) / 2
let x2: i64 = (c - a) / 2
let y2: i64 = (d - b) / 2
let x3: i64 = 0 - c
let y3: i64 = 0 - d
let mut big: i64 = iabs(x1)
if iabs(y1) > big { big = iabs(y1) }
if iabs(x2) > big { big = iabs(x2) }
if iabs(y2) > big { big = iabs(y2) }
if iabs(x3) > big { big = iabs(x3) }
if iabs(y3) > big { big = iabs(y3) }
if big <= N {
# sort three points
let mut p1x: i64 = x1
let mut p1y: i64 = y1
let mut p2x: i64 = x2
let mut p2y: i64 = y2
let mut p3x: i64 = x3
let mut p3y: i64 = y3
if pt_less(p2x, p2y, p1x, p1y) == 1 {
let tx: i64 = p1x
let ty: i64 = p1y
p1x = p2x
p1y = p2y
p2x = tx
p2y = ty
}
if pt_less(p3x, p3y, p2x, p2y) == 1 {
let tx2: i64 = p2x
let ty2: i64 = p2y
p2x = p3x
p2y = p3y
p3x = tx2
p3y = ty2
}
if pt_less(p2x, p2y, p1x, p1y) == 1 {
let tx3: i64 = p1x
let ty3: i64 = p1y
p1x = p2x
p1y = p2y
p2x = tx3
p2y = ty3
}
if tri_seen(sx, sy, nseen, p1x, p1y, p2x, p2y, p3x, p3y) == 0 {
let bidx: i64 = nseen * 3
sx[bidx] = p1x
sy[bidx] = p1y
sx[bidx + 1] = p2x
sy[bidx + 1] = p2y
sx[bidx + 2] = p3x
sy[bidx + 2] = p3y
nseen = nseen + 1
let prod: i128 = (D as i128) * (iabs(s) as i128) * (iabs(t) as i128)
total = total + prod
}
}
}
}
tsign = tsign + 1
}
ssign = ssign + 1
}
}
let ns: i64 = 2 * s + 3 * t
let nt: i64 = s + 2 * t
s = ns
t = nt
}
si = si + 1
}
n = n + 1
}
m = m + 1
}
let ans: i64 = (total / (4 as i128)) as i64
printf("%lld\n", ans)
free(sy)
free(sx)
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 igcd_i64_i64(int64_t a0, int64_t b0);
int64_t iabs_i64(int64_t x);
int64_t pt_less_i64_i64_i64_i64(int64_t ax, int64_t ay, int64_t bx, int64_t by);
int64_t tri_seen_ptr_i64_ptr_i64_i64_i64_i64_i64_i64_i64_i64(int64_t* sx, int64_t* sy, int64_t nseen, int64_t x1, int64_t y1, int64_t x2, int64_t y2, int64_t x3, int64_t y3);
int32_t main(void);
int64_t igcd_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
if (a < 0) {
a = (0 - a);
}
if (b < 0) {
b = (0 - b);
}
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t iabs_i64(int64_t x) {
if (x < 0) {
return (0 - x);
}
return x;
}
int64_t pt_less_i64_i64_i64_i64(int64_t ax, int64_t ay, int64_t bx, int64_t by) {
if (ax < bx) {
return 1;
}
if (ax > bx) {
return 0;
}
if (ay < by) {
return 1;
}
return 0;
}
int64_t tri_seen_ptr_i64_ptr_i64_i64_i64_i64_i64_i64_i64_i64(int64_t* sx, int64_t* sy, int64_t nseen, int64_t x1, int64_t y1, int64_t x2, int64_t y2, int64_t x3, int64_t y3) {
int64_t i = 0;
while (i < nseen) {
int64_t b = (i * 3);
if ((((((sx[b] == x1 && sy[b] == y1) && sx[(b + 1)] == x2) && sy[(b + 1)] == y2) && sx[(b + 2)] == x3) && sy[(b + 2)] == y3)) {
return 1;
}
i = (i + 1);
}
return 0;
}
int32_t main(void) {
int64_t N = 1000000000;
int64_t seeds_k[4] = { 468, 117, 36, 9 };
int64_t seeds_s[6] = { 24, 30, 12, 15, 6, 3 };
int64_t seeds_t[6] = { 6, 12, 3, 6, 0, 0 };
int64_t seeds_cnt[4] = { 2, 2, 1, 1 };
int64_t seeds_off[4] = { 0, 2, 4, 5 };
int64_t* sx = (int64_t*)(calloc((500 * 3), 8));
int64_t* sy = (int64_t*)(calloc((500 * 3), 8));
if ((sx == NULL || sy == NULL)) {
return 1;
}
int64_t nseen = 0;
__int128 total = 0;
int64_t m = (0 - 12);
while (m <= 12) {
int64_t n = (0 - 21);
while (n <= 21) {
if ((m == 0 && n == 0)) {
n = (n + 1);
continue;
}
if (igcd_i64_i64(iabs_i64(m), iabs_i64(n)) != 1) {
n = (n + 1);
continue;
}
int64_t D = ((n * n) + ((3 * m) * m));
if ((D == 0 || FLOW_CHECKED_MOD((468), (D)) != 0)) {
n = (n + 1);
continue;
}
int64_t K = FLOW_CHECKED_DIV((468), (D));
int64_t ki = 0;
int64_t found = 0;
while (ki < 4) {
if ((((unsigned)(ki) < 4) ? seeds_k[ki] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(ki), 4), flow_fault_handler("array index out of bounds"), seeds_k[0])) == K) {
found = 1;
break;
}
ki = (ki + 1);
}
if (found == 0) {
n = (n + 1);
continue;
}
int64_t cnt = (((unsigned)(ki) < 4) ? seeds_cnt[ki] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(ki), 4), flow_fault_handler("array index out of bounds"), seeds_cnt[0]));
int64_t off = (((unsigned)(ki) < 4) ? seeds_off[ki] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(ki), 4), flow_fault_handler("array index out of bounds"), seeds_off[0]));
int64_t si = 0;
while (si < cnt) {
int64_t s = (((unsigned)((off + si)) < 6) ? seeds_s[(off + si)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((off + si)), 6), flow_fault_handler("array index out of bounds"), seeds_s[0]));
int64_t t = (((unsigned)((off + si)) < 6) ? seeds_t[(off + si)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((off + si)), 6), flow_fault_handler("array index out of bounds"), seeds_t[0]));
while (1 == 1) {
int64_t mx = s;
if (t > mx) {
mx = t;
}
if (mx > ((6 * N) + 10)) {
break;
}
if ((s != 0 && t != 0)) {
int64_t ssign = 0;
while (ssign < 2) {
int64_t tsign = 0;
while (tsign < 2) {
int64_t ss = s;
if (ssign == 1) {
ss = (0 - s);
}
int64_t tt = t;
if (tsign == 1) {
tt = (0 - t);
}
if (FLOW_CHECKED_MOD(((ss * n)), (3)) == 0) {
int64_t a = (ss * m);
int64_t b = (0 - (tt * n));
int64_t c = FLOW_CHECKED_DIV(((ss * n)), (3));
int64_t d = (tt * m);
if ((((a + c) & 1) == 0 && ((b + d) & 1) == 0)) {
int64_t x1 = FLOW_CHECKED_DIV(((a + c)), (2));
int64_t y1 = FLOW_CHECKED_DIV(((b + d)), (2));
int64_t x2 = FLOW_CHECKED_DIV(((c - a)), (2));
int64_t y2 = FLOW_CHECKED_DIV(((d - b)), (2));
int64_t x3 = (0 - c);
int64_t y3 = (0 - d);
int64_t big = iabs_i64(x1);
if (iabs_i64(y1) > big) {
big = iabs_i64(y1);
}
if (iabs_i64(x2) > big) {
big = iabs_i64(x2);
}
if (iabs_i64(y2) > big) {
big = iabs_i64(y2);
}
if (iabs_i64(x3) > big) {
big = iabs_i64(x3);
}
if (iabs_i64(y3) > big) {
big = iabs_i64(y3);
}
if (big <= N) {
int64_t p1x = x1;
int64_t p1y = y1;
int64_t p2x = x2;
int64_t p2y = y2;
int64_t p3x = x3;
int64_t p3y = y3;
if (pt_less_i64_i64_i64_i64(p2x, p2y, p1x, p1y) == 1) {
int64_t tx = p1x;
int64_t ty = p1y;
p1x = p2x;
p1y = p2y;
p2x = tx;
p2y = ty;
}
if (pt_less_i64_i64_i64_i64(p3x, p3y, p2x, p2y) == 1) {
int64_t tx2 = p2x;
int64_t ty2 = p2y;
p2x = p3x;
p2y = p3y;
p3x = tx2;
p3y = ty2;
}
if (pt_less_i64_i64_i64_i64(p2x, p2y, p1x, p1y) == 1) {
int64_t tx3 = p1x;
int64_t ty3 = p1y;
p1x = p2x;
p1y = p2y;
p2x = tx3;
p2y = ty3;
}
if (tri_seen_ptr_i64_ptr_i64_i64_i64_i64_i64_i64_i64_i64(sx, sy, nseen, p1x, p1y, p2x, p2y, p3x, p3y) == 0) {
int64_t bidx = (nseen * 3);
sx[bidx] = p1x;
sy[bidx] = p1y;
sx[(bidx + 1)] = p2x;
sy[(bidx + 1)] = p2y;
sx[(bidx + 2)] = p3x;
sy[(bidx + 2)] = p3y;
nseen = (nseen + 1);
__int128 prod = ((((__int128)(D)) * ((__int128)(iabs_i64(s)))) * ((__int128)(iabs_i64(t))));
total = (total + prod);
}
}
}
}
tsign = (tsign + 1);
}
ssign = (ssign + 1);
}
}
int64_t ns = ((2 * s) + (3 * t));
int64_t nt = (s + (2 * t));
s = ns;
t = nt;
}
si = (si + 1);
}
n = (n + 1);
}
m = (m + 1);
}
int64_t ans = ((int64_t)(FLOW_CHECKED_DIV((total), (((__int128)(4))))));
printf("%lld\n", ans);
free(sy);
free(sx);
return 0;
}