# Project Euler 681: Maximal Area
# Uses Brahmagupta's formula. SP(n) = sum of perimeters of cyclic quadrilaterals
# with integer area k <= n.
import euler.nt { isqrt }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function sqrt(x: f64) -> f64
}
# Hardware-sqrt integer square root (faster than Newton iteration).
function isq(n: i64) -> i64 {
if n <= 0 { return 0 }
let r: i64 = sqrt(n as f64) as i64
while r > 0 && r * r > n { r = r - 1 }
while (r + 1) * (r + 1) <= n { r = r + 1 }
return r
}
function sift_down(a: ptr<i64>, start: i64, end: i64) -> void {
let mut root: i64 = start
while 2 * root + 1 <= end {
let mut child: i64 = 2 * root + 1
if child + 1 <= end {
if a[child] < a[child + 1] { child = child + 1 }
}
if a[root] < a[child] {
let t: i64 = a[root]
a[root] = a[child]
a[child] = t
root = child
} else {
return
}
}
}
function heapsort_i64(a: ptr<i64>, n: i64) -> void {
if n < 2 { return }
let mut start: i64 = n / 2 - 1
while start >= 0 {
sift_down(a, start, n - 1)
start = start - 1
}
let mut end: i64 = n - 1
while end > 0 {
let t: i64 = a[0]
a[0] = a[end]
a[end] = t
end = end - 1
sift_down(a, 0, end)
}
}
function main() -> i32 {
let n: i64 = 1000000
# Build SPF sieve up to n
let spf: ptr<i64> = calloc(n + 1, 8)
if spf == null { return 1 }
for i in 0..(n + 1) { spf[i] = i }
let root: i64 = isqrt(n)
for i in 2..(root + 1) {
if spf[i] == i {
let start: i64 = i * i
let mut j: i64 = start
while j <= n {
if spf[j] == j { spf[j] = i }
j = j + i
}
}
}
# Divisor buffer (max divisors of k^2 is small, ~240 for k<=10^6)
let max_divs: i64 = 2000
let divs: ptr<i64> = calloc(max_divs, 8)
if divs == null { return 1 }
# Temp factorization buffer
let max_primes: i64 = 20
let fac_p: ptr<i64> = calloc(max_primes, 8)
let fac_e: ptr<i64> = calloc(max_primes, 8)
let mut total: i64 = 0
let mut k: i64 = 1
while k <= n {
let k2: i64 = k * k
# Factorize k
let mut nfac: i64 = 0
let mut x: i64 = k
while x > 1 {
let p: i64 = spf[x]
let mut e: i64 = 0
while x % p == 0 {
x = x / p
e = e + 1
}
fac_p[nfac] = p
fac_e[nfac] = e
nfac = nfac + 1
}
# Generate all divisors of k^2 that are <= k
# k^2 has exponents 2*e for each prime
divs[0] = 1
let mut ndivs: i64 = 1
for fi in 0..nfac {
let p: i64 = fac_p[fi]
let e2: i64 = 2 * fac_e[fi]
let old_count: i64 = ndivs
# Generate powers p^1, p^2, ..., p^(2e)
let mut pe: i64 = 1
for _ in 0..e2 {
pe = pe * p
for di in 0..old_count {
let v: i64 = divs[di] * pe
if v <= k {
divs[ndivs] = v
ndivs = ndivs + 1
if ndivs >= max_divs { return 1 }
}
}
}
}
# Sort divs (heapsort — O(d log d) vs O(d^2) insertion sort).
heapsort_i64(divs, ndivs)
let dlen: i64 = ndivs
# Iterate over T, W, V
for ti in 0..dlen {
let T: i64 = divs[ti]
if T * T > k { break }
let k2_div_T: i64 = k2 / T
for wi in ti..dlen {
let W: i64 = divs[wi]
if W * W * W > k2_div_T { break }
if k2_div_T % W != 0 { continue }
let R: i64 = k2_div_T / W
let mut vmax: i64 = isq(R)
if vmax < W { break }
if vmax > k { vmax = k }
let S: i64 = W + T
# disc = S^2 + 4*R
let disc: i64 = S * S + 4 * R
let rt: i64 = isq(disc)
let mut vmin: i64 = (rt - S) / 2 + 1
if vmin < W { vmin = W }
if vmin > vmax { continue }
# Binary search for first divs[vi] >= vmin
let mut lo: i64 = wi
let mut hi: i64 = dlen
while lo < hi {
let mid: i64 = (lo + hi) / 2
if divs[mid] < vmin { lo = mid + 1 }
else { hi = mid }
}
for vi in lo..dlen {
let V: i64 = divs[vi]
if V > vmax { break }
if R % V != 0 { continue }
let U: i64 = R / V
if U < V { continue }
if U >= V + S { continue }
let p: i64 = U + V + S
if p % 2 != 0 { continue }
total = total + p
}
}
}
k = k + 1
}
printf("%lld\n", total)
free(fac_e)
free(fac_p)
free(divs)
free(spf)
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 isq_i64(int64_t n);
void sift_down_ptr_i64_i64_i64(int64_t* a, int64_t start, int64_t end);
void heapsort_i64_ptr_i64_i64(int64_t* a, int64_t n);
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 isq_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t r = ((int64_t)(sqrt(((double)(n)))));
while ((r > 0 && (r * r) > n)) {
r = (r - 1);
}
while (((r + 1) * (r + 1)) <= n) {
r = (r + 1);
}
return r;
}
void sift_down_ptr_i64_i64_i64(int64_t* a, int64_t start, int64_t end) {
int64_t root = start;
while (((2 * root) + 1) <= end) {
int64_t child = ((2 * root) + 1);
if ((child + 1) <= end) {
if (a[child] < a[(child + 1)]) {
child = (child + 1);
}
}
if (a[root] < a[child]) {
int64_t t = a[root];
a[root] = a[child];
a[child] = t;
root = child;
} else {
return;
}
}
}
void heapsort_i64_ptr_i64_i64(int64_t* a, int64_t n) {
if (n < 2) {
return;
}
int64_t start = (FLOW_CHECKED_DIV((n), (2)) - 1);
while (start >= 0) {
sift_down_ptr_i64_i64_i64(a, start, (n - 1));
start = (start - 1);
}
int64_t end = (n - 1);
while (end > 0) {
int64_t t = a[0];
a[0] = a[end];
a[end] = t;
end = (end - 1);
sift_down_ptr_i64_i64_i64(a, 0, end);
}
}
int32_t main(void) {
int64_t n = 1000000;
int64_t* spf = (int64_t*)(calloc((n + 1), 8));
if (spf == NULL) {
return 1;
}
int32_t __flow_step_1 = 1;
for (int32_t i = 0; (0 <= (n + 1)) ? i < (n + 1) : i > (n + 1); i += (0 <= (n + 1)) ? 1 : -1) {
spf[i] = i;
}
int64_t root = isqrt_i64(n);
int32_t __flow_step_2 = 1;
for (int32_t i = 2; (2 <= (root + 1)) ? i < (root + 1) : i > (root + 1); i += (2 <= (root + 1)) ? 1 : -1) {
if (spf[i] == i) {
int64_t start = (i * i);
int64_t j = start;
while (j <= n) {
if (spf[j] == j) {
spf[j] = i;
}
j = (j + i);
}
}
}
int64_t max_divs = 2000;
int64_t* divs = (int64_t*)(calloc(max_divs, 8));
if (divs == NULL) {
return 1;
}
int64_t max_primes = 20;
int64_t* fac_p = (int64_t*)(calloc(max_primes, 8));
int64_t* fac_e = (int64_t*)(calloc(max_primes, 8));
int64_t total = 0;
int64_t k = 1;
while (k <= n) {
int64_t k2 = (k * k);
int64_t nfac = 0;
int64_t x = k;
while (x > 1) {
int64_t p = spf[x];
int64_t e = 0;
while (FLOW_CHECKED_MOD((x), (p)) == 0) {
x = FLOW_CHECKED_DIV((x), (p));
e = (e + 1);
}
fac_p[nfac] = p;
fac_e[nfac] = e;
nfac = (nfac + 1);
}
divs[0] = 1;
int64_t ndivs = 1;
int32_t __flow_step_3 = 1;
for (int32_t fi = 0; (0 <= nfac) ? fi < nfac : fi > nfac; fi += (0 <= nfac) ? 1 : -1) {
int64_t p = fac_p[fi];
int64_t e2 = (2 * fac_e[fi]);
int64_t old_count = ndivs;
int64_t pe = 1;
int32_t __flow_step_4 = 1;
for (int32_t _ = 0; (0 <= e2) ? _ < e2 : _ > e2; _ += (0 <= e2) ? 1 : -1) {
pe = (pe * p);
int32_t __flow_step_5 = 1;
for (int32_t di = 0; (0 <= old_count) ? di < old_count : di > old_count; di += (0 <= old_count) ? 1 : -1) {
int64_t v = (divs[di] * pe);
if (v <= k) {
divs[ndivs] = v;
ndivs = (ndivs + 1);
if (ndivs >= max_divs) {
return 1;
}
}
}
}
}
heapsort_i64_ptr_i64_i64(divs, ndivs);
int64_t dlen = ndivs;
int32_t __flow_step_6 = 1;
for (int32_t ti = 0; (0 <= dlen) ? ti < dlen : ti > dlen; ti += (0 <= dlen) ? 1 : -1) {
int64_t T = divs[ti];
if ((T * T) > k) {
break;
}
int64_t k2_div_T = FLOW_CHECKED_DIV((k2), (T));
int32_t __flow_step_7 = 1;
for (int32_t wi = ti; (ti <= dlen) ? wi < dlen : wi > dlen; wi += (ti <= dlen) ? 1 : -1) {
int64_t W = divs[wi];
if (((W * W) * W) > k2_div_T) {
break;
}
if (FLOW_CHECKED_MOD((k2_div_T), (W)) != 0) {
continue;
}
int64_t R = FLOW_CHECKED_DIV((k2_div_T), (W));
int64_t vmax = isq_i64(R);
if (vmax < W) {
break;
}
if (vmax > k) {
vmax = k;
}
int64_t S = (W + T);
int64_t disc = ((S * S) + (4 * R));
int64_t rt = isq_i64(disc);
int64_t vmin = (FLOW_CHECKED_DIV(((rt - S)), (2)) + 1);
if (vmin < W) {
vmin = W;
}
if (vmin > vmax) {
continue;
}
int64_t lo = wi;
int64_t hi = dlen;
while (lo < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (divs[mid] < vmin) {
lo = (mid + 1);
} else {
hi = mid;
}
}
int32_t __flow_step_8 = 1;
for (int32_t vi = lo; (lo <= dlen) ? vi < dlen : vi > dlen; vi += (lo <= dlen) ? 1 : -1) {
int64_t V = divs[vi];
if (V > vmax) {
break;
}
if (FLOW_CHECKED_MOD((R), (V)) != 0) {
continue;
}
int64_t U = FLOW_CHECKED_DIV((R), (V));
if (U < V) {
continue;
}
if (U >= (V + S)) {
continue;
}
int64_t p = ((U + V) + S);
if (FLOW_CHECKED_MOD((p), (2)) != 0) {
continue;
}
total = (total + p);
}
}
}
k = (k + 1);
}
printf("%lld\n", total);
free(fac_e);
free(fac_p);
free(divs);
free(spf);
return 0;
}