# Project Euler 919
# Fortunate Triangles: |cos(A)| = 1/4 for at least one angle.
# S(P) = sum of perimeters of all fortunate triangles with perimeter <= P.
# P = 10^7.
import euler.nt { gcd }
extern {
function sqrt(x: f64) -> f64
}
let mut total: i128 = 0
function passes_d_filter(m: i64, d: i64) -> bool {
if d == 1 { return (m % 3 != 0) && (m % 5 != 0) }
if d == 3 { return (m % 5 != 0) }
if d == 5 { return (m % 3 != 0) }
return true
}
function process(raw_a: i64, raw_b: i64, raw_c: i64, P: i64) -> void {
let mut g: i64 = gcd(raw_a, raw_b)
g = gcd(g, raw_c)
let a: i64 = raw_a / g
let b: i64 = raw_b / g
let c: i64 = raw_c / g
if a > b { return }
let per: i64 = a + b + c
if per > P { return }
let n: i64 = P / per
total = total + (per as i128) * (n as i128) * ((n + 1) as i128) / 2
}
function main() -> i32 {
let P: i64 = 10000000
total = 0
let q_max: i64 = 2 * (sqrt((P as f64)) as i64) + 10
let ds: array<i64, 4> = [1, 3, 5, 15]
# Family A: cos = +1/4
# A = 8*p*q, B = 15*q^2 - p^2 + 2*p*q, C = 15*q^2 + p^2
# Constraint: p < 5q, perimeter raw = 10*q*(p+3q)
let mut q: i64 = 1
while q <= q_max {
let qq: i64 = q * q
let max_p: i64 = 5 * q - 1
let mut di: i64 = 0
while di < 4 {
let d: i64 = ds[di]
let mut p_lim: i64 = (P * 8 * d) / (10 * q) - 3 * q
if p_lim > max_p { p_lim = max_p }
if p_lim >= d {
let mut p: i64 = d
while p <= p_lim {
let m: i64 = p / d
if passes_d_filter(m, d) {
if gcd(p, q) == 1 {
let a: i64 = 8 * p * q
let b: i64 = 15 * qq - p * p + 2 * p * q
let c: i64 = 15 * qq + p * p
if b > 0 {
process(a, b, c, P)
}
}
}
p = p + d
}
}
di = di + 1
}
q = q + 1
}
# Family B: cos = -1/4
# A = 8*p*q, B = 15*q^2 - p^2 - 2*p*q, C = 15*q^2 + p^2
# Constraint: p < 3q, perimeter raw = 6*q*(p+5q)
q = 1
while q <= q_max {
let qq: i64 = q * q
let max_p: i64 = 3 * q - 1
let mut di: i64 = 0
while di < 4 {
let d: i64 = ds[di]
let mut p_lim: i64 = (P * 8 * d) / (6 * q) - 5 * q
if p_lim > max_p { p_lim = max_p }
if p_lim >= d {
let mut p: i64 = d
while p <= p_lim {
let m: i64 = p / d
if passes_d_filter(m, d) {
if gcd(p, q) == 1 {
let a: i64 = 8 * p * q
let b: i64 = 15 * qq - p * p - 2 * p * q
let c: i64 = 15 * qq + p * p
if b > 0 {
process(a, b, c, P)
}
}
}
p = p + d
}
}
di = di + 1
}
q = q + 1
}
printf("%lld\n", total as i64)
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);
bool passes_d_filter_i64_i64(int64_t m, int64_t d);
void process_i64_i64_i64_i64(int64_t raw_a, int64_t raw_b, int64_t raw_c, int64_t P);
int32_t main(void);
/* Module statics */
static __int128 total = 0;
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;
}
bool passes_d_filter_i64_i64(int64_t m, int64_t d) {
if (d == 1) {
return (FLOW_CHECKED_MOD((m), (3)) != 0 && FLOW_CHECKED_MOD((m), (5)) != 0);
}
if (d == 3) {
return FLOW_CHECKED_MOD((m), (5)) != 0;
}
if (d == 5) {
return FLOW_CHECKED_MOD((m), (3)) != 0;
}
return 1;
}
void process_i64_i64_i64_i64(int64_t raw_a, int64_t raw_b, int64_t raw_c, int64_t P) {
int64_t g = gcd_i64_i64(raw_a, raw_b);
g = gcd_i64_i64(g, raw_c);
int64_t a = FLOW_CHECKED_DIV((raw_a), (g));
int64_t b = FLOW_CHECKED_DIV((raw_b), (g));
int64_t c = FLOW_CHECKED_DIV((raw_c), (g));
if (a > b) {
return;
}
int64_t per = ((a + b) + c);
if (per > P) {
return;
}
int64_t n = FLOW_CHECKED_DIV((P), (per));
total = (total + FLOW_CHECKED_DIV((((((__int128)(per)) * ((__int128)(n))) * ((__int128)((n + 1))))), (2)));
}
int32_t main(void) {
int64_t P = 10000000;
total = 0;
int64_t q_max = ((2 * ((int64_t)(sqrt(((double)(P)))))) + 10);
int64_t ds[4] = { 1, 3, 5, 15 };
int64_t q = 1;
while (q <= q_max) {
int64_t qq = (q * q);
int64_t max_p = ((5 * q) - 1);
int64_t di = 0;
while (di < 4) {
int64_t d = (((unsigned)(di) < 4) ? ds[di] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(di), 4), flow_fault_handler("array index out of bounds"), ds[0]));
int64_t p_lim = (FLOW_CHECKED_DIV((((P * 8) * d)), ((10 * q))) - (3 * q));
if (p_lim > max_p) {
p_lim = max_p;
}
if (p_lim >= d) {
int64_t p = d;
while (p <= p_lim) {
int64_t m = FLOW_CHECKED_DIV((p), (d));
if (passes_d_filter_i64_i64(m, d)) {
if (gcd_i64_i64(p, q) == 1) {
int64_t a = ((8 * p) * q);
int64_t b = (((15 * qq) - (p * p)) + ((2 * p) * q));
int64_t c = ((15 * qq) + (p * p));
if (b > 0) {
process_i64_i64_i64_i64(a, b, c, P);
}
}
}
p = (p + d);
}
}
di = (di + 1);
}
q = (q + 1);
}
q = 1;
while (q <= q_max) {
int64_t qq = (q * q);
int64_t max_p = ((3 * q) - 1);
int64_t di = 0;
while (di < 4) {
int64_t d = (((unsigned)(di) < 4) ? ds[di] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(di), 4), flow_fault_handler("array index out of bounds"), ds[0]));
int64_t p_lim = (FLOW_CHECKED_DIV((((P * 8) * d)), ((6 * q))) - (5 * q));
if (p_lim > max_p) {
p_lim = max_p;
}
if (p_lim >= d) {
int64_t p = d;
while (p <= p_lim) {
int64_t m = FLOW_CHECKED_DIV((p), (d));
if (passes_d_filter_i64_i64(m, d)) {
if (gcd_i64_i64(p, q) == 1) {
int64_t a = ((8 * p) * q);
int64_t b = (((15 * qq) - (p * p)) - ((2 * p) * q));
int64_t c = ((15 * qq) + (p * p));
if (b > 0) {
process_i64_i64_i64_i64(a, b, c, P);
}
}
}
p = (p + d);
}
}
di = (di + 1);
}
q = (q + 1);
}
printf("%lld\n", ((int64_t)(total)));
return 0;
}