# Project Euler 283
# Sum perimeters of integer triangles with integral area/perimeter ratio <= 1000.
import euler.nt { isqrt }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function factorize(num0: i64, spf: ptr<i32>, pr: ptr<i32>, ex: ptr<i32>) -> i64 {
let mut num: i64 = num0
let mut cnt: i64 = 0
while num > 1 {
let p: i64 = spf[num] as i64
let mut e: i64 = 1
num = num / p
while num > 1 && (spf[num] as i64) == p {
e = e + 1
num = num / p
}
pr[cnt] = p as i32
ex[cnt] = e as i32
cnt = cnt + 1
}
return cnt
}
function merge_factors(ap: ptr<i32>, ae: ptr<i32>, ac: i64,
bp: ptr<i32>, be: ptr<i32>, bc: i64,
mp: ptr<i32>, me: ptr<i32>) -> i64 {
let mut i: i64 = 0
let mut j: i64 = 0
let mut c: i64 = 0
while i < ac && j < bc {
let pa: i64 = ap[i] as i64
let pb: i64 = bp[j] as i64
if pa == pb {
mp[c] = ap[i]
me[c] = (ae[i] as i64 + be[j] as i64) as i32
c = c + 1
i = i + 1
j = j + 1
} elif pa < pb {
mp[c] = ap[i]
me[c] = ae[i]
c = c + 1
i = i + 1
} else {
mp[c] = bp[j]
me[c] = be[j]
c = c + 1
j = j + 1
}
}
while i < ac {
mp[c] = ap[i]
me[c] = ae[i]
c = c + 1
i = i + 1
}
while j < bc {
mp[c] = bp[j]
me[c] = be[j]
c = c + 1
j = j + 1
}
return c
}
function main() -> i32 {
let limit_k: i64 = 1000
let max_r: i64 = 2 * limit_k
let max_n: i64 = max_r * max_r
let max_x: i64 = isqrt(3 * max_n)
let max_m: i64 = max_n + max_x * max_x
let spf: ptr<i32> = calloc(max_m + 1, 4)
let squares: ptr<i64> = calloc(max_x + 1, 8)
let fn_p: ptr<i32> = calloc(32, 4)
let fn_e: ptr<i32> = calloc(32, 4)
let fm_p: ptr<i32> = calloc(32, 4)
let fm_e: ptr<i32> = calloc(32, 4)
let mg_p: ptr<i32> = calloc(64, 4)
let mg_e: ptr<i32> = calloc(64, 4)
let divs: ptr<i64> = calloc(8192, 8)
if spf == null { return 1 }
spf[1] = 1
let mut i: i64 = 2
while i <= max_m {
spf[i] = 2
i = i + 2
}
i = 3
while i <= max_m {
if spf[i] == 0 {
spf[i] = i as i32
let ii: i64 = i * i
if ii <= max_m {
let mut j: i64 = ii
let step: i64 = i << 1
while j <= max_m {
if spf[j] == 0 { spf[j] = i as i32 }
j = j + step
}
}
}
i = i + 2
}
i = 0
while i <= max_x {
squares[i] = i * i
i = i + 1
}
let mut total: i64 = 0
let mut r: i64 = 2
while r <= max_r {
let n: i64 = r * r
let max_x_r: i64 = isqrt(3 * n)
let limit_d: i64 = 2 * n
let fn_cnt: i64 = factorize(n, spf, fn_p, fn_e)
let mut x: i64 = 1
while x <= max_x_r {
let m: i64 = squares[x] + n
let fm_cnt: i64 = factorize(m, spf, fm_p, fm_e)
let mg_cnt: i64 = merge_factors(fn_p, fn_e, fn_cnt, fm_p, fm_e, fm_cnt, mg_p, mg_e)
let mut nd: i64 = 0
divs[0] = 1
nd = 1
let mut fi: i64 = 0
while fi < mg_cnt {
let p: i64 = mg_p[fi] as i64
let e: i64 = mg_e[fi] as i64
let old_nd: i64 = nd
let mut mult: i64 = 1
let mut ee: i64 = 0
while ee <= e {
if mult > limit_d { break }
if ee > 0 {
let mut j: i64 = 0
while j < old_nd {
let v: i64 = divs[j] * mult
if v <= limit_d {
divs[nd] = v
nd = nd + 1
}
j = j + 1
}
}
mult = mult * p
ee = ee + 1
}
fi = fi + 1
}
let mut di: i64 = 0
while di < nd {
let d: i64 = divs[di]
let nd_val: i64 = n + d
if nd_val % x == 0 {
let y: i64 = nd_val / x
if y >= x {
let numz: i64 = n * (x + y)
if numz % d == 0 {
let z: i64 = numz / d
if z >= y {
total = total + 2 * (x + y + z)
}
}
}
}
di = di + 1
}
x = x + 1
}
r = r + 2
}
free(spf); free(squares); free(fn_p); free(fn_e); free(fm_p); free(fm_e)
free(mg_p); free(mg_e); free(divs)
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 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 factorize_i64_ptr_i32_ptr_i32_ptr_i32(int64_t num0, int32_t* spf, int32_t* pr, int32_t* ex);
int64_t merge_factors_ptr_i32_ptr_i32_i64_ptr_i32_ptr_i32_i64_ptr_i32_ptr_i32(int32_t* ap, int32_t* ae, int64_t ac, int32_t* bp, int32_t* be, int64_t bc, int32_t* mp, int32_t* me);
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 factorize_i64_ptr_i32_ptr_i32_ptr_i32(int64_t num0, int32_t* spf, int32_t* pr, int32_t* ex) {
int64_t num = num0;
int64_t cnt = 0;
while (num > 1) {
int64_t p = ((int64_t)(spf[num]));
int64_t e = 1;
num = FLOW_CHECKED_DIV((num), (p));
while ((num > 1 && ((int64_t)(spf[num])) == p)) {
e = (e + 1);
num = FLOW_CHECKED_DIV((num), (p));
}
pr[cnt] = ((int32_t)(p));
ex[cnt] = ((int32_t)(e));
cnt = (cnt + 1);
}
return cnt;
}
int64_t merge_factors_ptr_i32_ptr_i32_i64_ptr_i32_ptr_i32_i64_ptr_i32_ptr_i32(int32_t* ap, int32_t* ae, int64_t ac, int32_t* bp, int32_t* be, int64_t bc, int32_t* mp, int32_t* me) {
int64_t i = 0;
int64_t j = 0;
int64_t c = 0;
while ((i < ac && j < bc)) {
int64_t pa = ((int64_t)(ap[i]));
int64_t pb = ((int64_t)(bp[j]));
if (pa == pb) {
mp[c] = ap[i];
me[c] = ((int32_t)((((int64_t)(ae[i])) + ((int64_t)(be[j])))));
c = (c + 1);
i = (i + 1);
j = (j + 1);
} else if (pa < pb) {
mp[c] = ap[i];
me[c] = ae[i];
c = (c + 1);
i = (i + 1);
} else {
mp[c] = bp[j];
me[c] = be[j];
c = (c + 1);
j = (j + 1);
}
}
while (i < ac) {
mp[c] = ap[i];
me[c] = ae[i];
c = (c + 1);
i = (i + 1);
}
while (j < bc) {
mp[c] = bp[j];
me[c] = be[j];
c = (c + 1);
j = (j + 1);
}
return c;
}
int32_t main(void) {
int64_t limit_k = 1000;
int64_t max_r = (2 * limit_k);
int64_t max_n = (max_r * max_r);
int64_t max_x = isqrt_i64((3 * max_n));
int64_t max_m = (max_n + (max_x * max_x));
int32_t* spf = (int32_t*)(calloc((max_m + 1), 4));
int64_t* squares = (int64_t*)(calloc((max_x + 1), 8));
int32_t* fn_p = (int32_t*)(calloc(32, 4));
int32_t* fn_e = (int32_t*)(calloc(32, 4));
int32_t* fm_p = (int32_t*)(calloc(32, 4));
int32_t* fm_e = (int32_t*)(calloc(32, 4));
int32_t* mg_p = (int32_t*)(calloc(64, 4));
int32_t* mg_e = (int32_t*)(calloc(64, 4));
int64_t* divs = (int64_t*)(calloc(8192, 8));
if (spf == NULL) {
return 1;
}
spf[1] = 1;
int64_t i = 2;
while (i <= max_m) {
spf[i] = 2;
i = (i + 2);
}
i = 3;
while (i <= max_m) {
if (spf[i] == 0) {
spf[i] = ((int32_t)(i));
int64_t ii = (i * i);
if (ii <= max_m) {
int64_t j = ii;
int64_t step = FLOW_CHECKED_SHL((i), (1));
while (j <= max_m) {
if (spf[j] == 0) {
spf[j] = ((int32_t)(i));
}
j = (j + step);
}
}
}
i = (i + 2);
}
i = 0;
while (i <= max_x) {
squares[i] = (i * i);
i = (i + 1);
}
int64_t total = 0;
int64_t r = 2;
while (r <= max_r) {
int64_t n = (r * r);
int64_t max_x_r = isqrt_i64((3 * n));
int64_t limit_d = (2 * n);
int64_t fn_cnt = factorize_i64_ptr_i32_ptr_i32_ptr_i32(n, spf, fn_p, fn_e);
int64_t x = 1;
while (x <= max_x_r) {
int64_t m = (squares[x] + n);
int64_t fm_cnt = factorize_i64_ptr_i32_ptr_i32_ptr_i32(m, spf, fm_p, fm_e);
int64_t mg_cnt = merge_factors_ptr_i32_ptr_i32_i64_ptr_i32_ptr_i32_i64_ptr_i32_ptr_i32(fn_p, fn_e, fn_cnt, fm_p, fm_e, fm_cnt, mg_p, mg_e);
int64_t nd = 0;
divs[0] = 1;
nd = 1;
int64_t fi = 0;
while (fi < mg_cnt) {
int64_t p = ((int64_t)(mg_p[fi]));
int64_t e = ((int64_t)(mg_e[fi]));
int64_t old_nd = nd;
int64_t mult = 1;
int64_t ee = 0;
while (ee <= e) {
if (mult > limit_d) {
break;
}
if (ee > 0) {
int64_t j = 0;
while (j < old_nd) {
int64_t v = (divs[j] * mult);
if (v <= limit_d) {
divs[nd] = v;
nd = (nd + 1);
}
j = (j + 1);
}
}
mult = (mult * p);
ee = (ee + 1);
}
fi = (fi + 1);
}
int64_t di = 0;
while (di < nd) {
int64_t d = divs[di];
int64_t nd_val = (n + d);
if (FLOW_CHECKED_MOD((nd_val), (x)) == 0) {
int64_t y = FLOW_CHECKED_DIV((nd_val), (x));
if (y >= x) {
int64_t numz = (n * (x + y));
if (FLOW_CHECKED_MOD((numz), (d)) == 0) {
int64_t z = FLOW_CHECKED_DIV((numz), (d));
if (z >= y) {
total = (total + (2 * ((x + y) + z)));
}
}
}
}
di = (di + 1);
}
x = (x + 1);
}
r = (r + 2);
}
free(spf);
free(squares);
free(fn_p);
free(fn_e);
free(fm_p);
free(fm_e);
free(mg_p);
free(mg_e);
free(divs);
printf("%lld\n", total);
return 0;
}