# Project Euler 583
# Heron Envelopes — sum perimeters of valid flap envelopes with P <= 10^7.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function gcd(a0: i64, b0: i64) -> i64 {
let mut a: i64 = a0
let mut b: i64 = b0
while b != 0 {
let t: i64 = a % b
a = b
b = t
}
return a
}
function isqrt(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut x: i64 = n
let mut y: i64 = (x + 1) / 2
while y < x {
x = y
y = (x + n / x) / 2
}
return x
}
function is_square(n: i64) -> i32 {
if n < 0 { return 0 }
let r: i64 = isqrt(n)
if r * r == n { return 1 }
return 0
}
function S(p: i64) -> i64 {
let N: i64 = p / 2
let counts: ptr<i32> = calloc(N + 1, 4)
if counts == null { return -1 }
let mmax_leg: i64 = isqrt(2 * N) + 1
let mut m: i64 = 2
while m <= mmax_leg {
let mm: i64 = m * m
let mut n: i64 = 1
if (m & 1) != 0 { n = 2 }
while n < m {
if gcd(m, n) == 1 {
let mut a0: i64 = mm - n * n
let mut b0: i64 = 2 * m * n
if a0 > b0 {
let t: i64 = a0
a0 = b0
b0 = t
}
if b0 <= N {
let mut aa: i64 = a0
let mut bb: i64 = b0
while bb <= N {
counts[aa] = counts[aa] + 1
counts[bb] = counts[bb] + 1
aa = aa + a0
bb = bb + b0
}
}
}
n = n + 2
}
m = m + 1
}
let offset: ptr<i64> = calloc(N + 2, 8)
let mut total_e: i64 = 0
let mut i: i64 = 0
while i <= N {
offset[i] = total_e
total_e = total_e + (counts[i] as i64)
i = i + 1
}
offset[N + 1] = total_e
let vals: ptr<i32> = calloc(total_e + 1, 4)
let cursor: ptr<i64> = calloc(N + 1, 8)
if vals == null || cursor == null { return -1 }
i = 0
while i <= N {
cursor[i] = offset[i]
i = i + 1
}
m = 2
while m <= mmax_leg {
let mm: i64 = m * m
let mut n: i64 = 1
if (m & 1) != 0 { n = 2 }
while n < m {
if gcd(m, n) == 1 {
let mut a0: i64 = mm - n * n
let mut b0: i64 = 2 * m * n
if a0 > b0 {
let t: i64 = a0
a0 = b0
b0 = t
}
if b0 <= N {
let mut aa: i64 = a0
let mut bb: i64 = b0
while bb <= N {
vals[cursor[aa]] = bb as i32
cursor[aa] = cursor[aa] + 1
vals[cursor[bb]] = aa as i32
cursor[bb] = cursor[bb] + 1
aa = aa + a0
bb = bb + b0
}
}
}
n = n + 2
}
m = m + 1
}
let mut ans: i64 = 0
let mmax_hyp: i64 = isqrt(N) + 1
m = 2
while m <= mmax_hyp {
let mm: i64 = m * m
let mut n: i64 = 1
if (m & 1) != 0 { n = 2 }
while n < m {
if gcd(m, n) == 1 {
let mut a0: i64 = mm - n * n
let mut b0: i64 = 2 * m * n
let c0: i64 = mm + n * n
if c0 <= N {
if a0 > b0 {
let t: i64 = a0
a0 = b0
b0 = t
}
let mut a: i64 = a0
let mut b: i64 = b0
let mut c: i64 = c0
while c <= N {
let mut pass: i64 = 0
while pass < 2 {
let mut AA: i64 = a
let mut TT: i64 = b
if pass == 1 {
AA = b
TT = a
}
let start: i64 = offset[AA]
let end: i64 = offset[AA + 1]
if start != end {
let h_max: i64 = N - AA - c
if h_max > TT {
let lo: i64 = 2 * TT + 1
let hi: i64 = TT + h_max
let fourA2: i64 = (AA * AA) << 2
let per_base: i64 = 2 * (AA + c)
let mut q: i64 = start
while q < end {
let u: i64 = vals[q] as i64
if u >= lo && u <= hi {
let h: i64 = u - TT
if h > TT && h <= h_max {
if is_square(fourA2 + h * h) != 0 {
ans = ans + per_base + 2 * h
}
}
}
q = q + 1
}
}
}
pass = pass + 1
}
a = a + a0
b = b + b0
c = c + c0
}
}
}
n = n + 2
}
m = m + 1
}
free(counts)
free(offset)
free(vals)
free(cursor)
return ans
}
function main() -> i32 {
printf("%lld\n", S(10000000))
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 isqrt_i64(int64_t n);
int32_t is_square_i64(int64_t n);
int64_t S_i64(int64_t p);
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 isqrt_i64(int64_t n) {
if (n <= 0) {
return 0;
}
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;
}
int32_t is_square_i64(int64_t n) {
if (n < 0) {
return 0;
}
int64_t r = isqrt_i64(n);
if ((r * r) == n) {
return 1;
}
return 0;
}
int64_t S_i64(int64_t p) {
int64_t N = FLOW_CHECKED_DIV((p), (2));
int32_t* counts = (int32_t*)(calloc((N + 1), 4));
if (counts == NULL) {
return (-1);
}
int64_t mmax_leg = (isqrt_i64((2 * N)) + 1);
int64_t m = 2;
while (m <= mmax_leg) {
int64_t mm = (m * m);
int64_t n = 1;
if ((m & 1) != 0) {
n = 2;
}
while (n < m) {
if (gcd_i64_i64(m, n) == 1) {
int64_t a0 = (mm - (n * n));
int64_t b0 = ((2 * m) * n);
if (a0 > b0) {
int64_t t = a0;
a0 = b0;
b0 = t;
}
if (b0 <= N) {
int64_t aa = a0;
int64_t bb = b0;
while (bb <= N) {
counts[aa] = (counts[aa] + 1);
counts[bb] = (counts[bb] + 1);
aa = (aa + a0);
bb = (bb + b0);
}
}
}
n = (n + 2);
}
m = (m + 1);
}
int64_t* offset = (int64_t*)(calloc((N + 2), 8));
int64_t total_e = 0;
int64_t i = 0;
while (i <= N) {
offset[i] = total_e;
total_e = (total_e + ((int64_t)(counts[i])));
i = (i + 1);
}
offset[(N + 1)] = total_e;
int32_t* vals = (int32_t*)(calloc((total_e + 1), 4));
int64_t* cursor = (int64_t*)(calloc((N + 1), 8));
if ((vals == NULL || cursor == NULL)) {
return (-1);
}
i = 0;
while (i <= N) {
cursor[i] = offset[i];
i = (i + 1);
}
m = 2;
while (m <= mmax_leg) {
int64_t mm = (m * m);
int64_t n = 1;
if ((m & 1) != 0) {
n = 2;
}
while (n < m) {
if (gcd_i64_i64(m, n) == 1) {
int64_t a0 = (mm - (n * n));
int64_t b0 = ((2 * m) * n);
if (a0 > b0) {
int64_t t = a0;
a0 = b0;
b0 = t;
}
if (b0 <= N) {
int64_t aa = a0;
int64_t bb = b0;
while (bb <= N) {
vals[cursor[aa]] = ((int32_t)(bb));
cursor[aa] = (cursor[aa] + 1);
vals[cursor[bb]] = ((int32_t)(aa));
cursor[bb] = (cursor[bb] + 1);
aa = (aa + a0);
bb = (bb + b0);
}
}
}
n = (n + 2);
}
m = (m + 1);
}
int64_t ans = 0;
int64_t mmax_hyp = (isqrt_i64(N) + 1);
m = 2;
while (m <= mmax_hyp) {
int64_t mm = (m * m);
int64_t n = 1;
if ((m & 1) != 0) {
n = 2;
}
while (n < m) {
if (gcd_i64_i64(m, n) == 1) {
int64_t a0 = (mm - (n * n));
int64_t b0 = ((2 * m) * n);
int64_t c0 = (mm + (n * n));
if (c0 <= N) {
if (a0 > b0) {
int64_t t = a0;
a0 = b0;
b0 = t;
}
int64_t a = a0;
int64_t b = b0;
int64_t c = c0;
while (c <= N) {
int64_t pass = 0;
while (pass < 2) {
int64_t AA = a;
int64_t TT = b;
if (pass == 1) {
AA = b;
TT = a;
}
int64_t start = offset[AA];
int64_t end = offset[(AA + 1)];
if (start != end) {
int64_t h_max = ((N - AA) - c);
if (h_max > TT) {
int64_t lo = ((2 * TT) + 1);
int64_t hi = (TT + h_max);
int64_t fourA2 = FLOW_CHECKED_SHL(((AA * AA)), (2));
int64_t per_base = (2 * (AA + c));
int64_t q = start;
while (q < end) {
int64_t u = ((int64_t)(vals[q]));
if ((u >= lo && u <= hi)) {
int64_t h = (u - TT);
if ((h > TT && h <= h_max)) {
if (is_square_i64((fourA2 + (h * h))) != 0) {
ans = ((ans + per_base) + (2 * h));
}
}
}
q = (q + 1);
}
}
}
pass = (pass + 1);
}
a = (a + a0);
b = (b + b0);
c = (c + c0);
}
}
}
n = (n + 2);
}
m = (m + 1);
}
free(counts);
free(offset);
free(vals);
free(cursor);
return ans;
}
int32_t main(void) {
printf("%lld\n", S_i64(10000000));
return 0;
}