# Project Euler 540
# Counting Primitive Pythagorean Triples — P(3141592653589793).
import euler.nt { isqrt }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const N: i64 = 3141592653589793
function icbrt(n: i64) -> i64 {
if n <= 1 { return n }
let mut r: i64 = 1
while r * r * r <= n {
let r1: i64 = r + 1
if r1 * r1 * r1 > n { break }
r = r1
}
while r * r * r > n { r = r - 1 }
return r
}
function odd_ge3_count_le(n: i64) -> i64 {
if n < 3 { return 0 }
return (n - 1) / 2
}
function raw_opposite_parity_count(limit: i64) -> i64 {
if limit < 5 { return 0 }
let mut full: i64 = (1 + isqrt(2 * limit - 1)) / 2
while (full + 1) * (full + 1) + full * full <= limit { full = full + 1 }
while full * full + (full - 1) * (full - 1) > limit { full = full - 1 }
let k: i64 = full / 2
let mut total: i64 = k * k
let mut m: i64 = 2 * k + 1
if m * m > limit { return total }
let mut y: i64 = isqrt(limit - m * m)
while m * m + 1 <= limit {
let rem: i64 = limit - m * m
while y * y > rem { y = y - 1 }
let mut nmax: i64 = y
if nmax >= m { nmax = m - 1 }
if (m & 1) != 0 {
total = total + nmax / 2
} else {
total = total + (nmax + 1) / 2
}
m = m + 1
}
return total
}
function build_small_table(cube: i64, small: ptr<i64>) -> void {
let mut x: i64 = 1
while x <= cube {
let mut total: i64 = raw_opposite_parity_count(x)
let max_d: i64 = isqrt(x)
let split: i64 = icbrt(x)
let mut d: i64 = 3
while d <= max_d && d <= split {
total = total - small[x / (d * d)]
d = d + 2
}
if d <= max_d {
let max_z: i64 = x / (d * d)
let mut z: i64 = 1
while z <= max_z {
let hi: i64 = odd_ge3_count_le(isqrt(x / z))
let lo: i64 = odd_ge3_count_le(isqrt(x / (z + 1)))
total = total - (hi - lo) * small[z]
z = z + 1
}
}
small[x] = total
x = x + 1
}
}
function main() -> i32 {
let cube: i64 = icbrt(N)
let small: ptr<i64> = calloc(cube + 1, 8)
let tail: ptr<i64> = calloc(cube + 1, 8)
let transformed: ptr<i64> = calloc(cube + 1, 8)
if small == null || tail == null || transformed == null { return 1 }
build_small_table(cube, small)
let s_max: i64 = isqrt(N / 5)
let mut t: i64 = 1
while t <= cube {
let mut s: i64 = (cube / t + 1) * t
if (s & 1) == 0 { s = s + t }
let mut acc: i64 = 0
let step: i64 = 2 * t
while s <= s_max {
acc = acc + small[N / (s * s)]
s = s + step
}
tail[t] = acc
t = t + 2
}
let mut start: i64 = cube
if (start & 1) == 0 { start = start - 1 }
t = start
while t > 0 {
let x: i64 = N / (t * t)
let mut total: i64 = raw_opposite_parity_count(x)
let mut s: i64 = 3 * t
let step: i64 = 2 * t
while s <= cube {
total = total - transformed[s]
s = s + step
}
total = total - tail[t]
transformed[t] = total
t = t - 2
}
printf("%lld\n", transformed[1])
free(small); free(tail); free(transformed)
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 icbrt_i64(int64_t n);
int64_t odd_ge3_count_le_i64(int64_t n);
int64_t raw_opposite_parity_count_i64(int64_t limit);
void build_small_table_i64_ptr_i64(int64_t cube, int64_t* small);
int32_t main(void);
static const int64_t N = 3141592653589793;
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 icbrt_i64(int64_t n) {
if (n <= 1) {
return n;
}
int64_t r = 1;
while (((r * r) * r) <= n) {
int64_t r1 = (r + 1);
if (((r1 * r1) * r1) > n) {
break;
}
r = r1;
}
while (((r * r) * r) > n) {
r = (r - 1);
}
return r;
}
int64_t odd_ge3_count_le_i64(int64_t n) {
if (n < 3) {
return 0;
}
return FLOW_CHECKED_DIV(((n - 1)), (2));
}
int64_t raw_opposite_parity_count_i64(int64_t limit) {
if (limit < 5) {
return 0;
}
int64_t full = FLOW_CHECKED_DIV(((1 + isqrt_i64(((2 * limit) - 1)))), (2));
while ((((full + 1) * (full + 1)) + (full * full)) <= limit) {
full = (full + 1);
}
while (((full * full) + ((full - 1) * (full - 1))) > limit) {
full = (full - 1);
}
int64_t k = FLOW_CHECKED_DIV((full), (2));
int64_t total = (k * k);
int64_t m = ((2 * k) + 1);
if ((m * m) > limit) {
return total;
}
int64_t y = isqrt_i64((limit - (m * m)));
while (((m * m) + 1) <= limit) {
int64_t rem = (limit - (m * m));
while ((y * y) > rem) {
y = (y - 1);
}
int64_t nmax = y;
if (nmax >= m) {
nmax = (m - 1);
}
if ((m & 1) != 0) {
total = (total + FLOW_CHECKED_DIV((nmax), (2)));
} else {
total = (total + FLOW_CHECKED_DIV(((nmax + 1)), (2)));
}
m = (m + 1);
}
return total;
}
void build_small_table_i64_ptr_i64(int64_t cube, int64_t* small) {
int64_t x = 1;
while (x <= cube) {
int64_t total = raw_opposite_parity_count_i64(x);
int64_t max_d = isqrt_i64(x);
int64_t split = icbrt_i64(x);
int64_t d = 3;
while ((d <= max_d && d <= split)) {
total = (total - small[FLOW_CHECKED_DIV((x), ((d * d)))]);
d = (d + 2);
}
if (d <= max_d) {
int64_t max_z = FLOW_CHECKED_DIV((x), ((d * d)));
int64_t z = 1;
while (z <= max_z) {
int64_t hi = odd_ge3_count_le_i64(isqrt_i64(FLOW_CHECKED_DIV((x), (z))));
int64_t lo = odd_ge3_count_le_i64(isqrt_i64(FLOW_CHECKED_DIV((x), ((z + 1)))));
total = (total - ((hi - lo) * small[z]));
z = (z + 1);
}
}
small[x] = total;
x = (x + 1);
}
}
int32_t main(void) {
int64_t cube = icbrt_i64(N);
int64_t* small = (int64_t*)(calloc((cube + 1), 8));
int64_t* tail = (int64_t*)(calloc((cube + 1), 8));
int64_t* transformed = (int64_t*)(calloc((cube + 1), 8));
if (((small == NULL || tail == NULL) || transformed == NULL)) {
return 1;
}
build_small_table_i64_ptr_i64(cube, small);
int64_t s_max = isqrt_i64(FLOW_CHECKED_DIV((N), (5)));
int64_t t = 1;
while (t <= cube) {
int64_t s = ((FLOW_CHECKED_DIV((cube), (t)) + 1) * t);
if ((s & 1) == 0) {
s = (s + t);
}
int64_t acc = 0;
int64_t step = (2 * t);
while (s <= s_max) {
acc = (acc + small[FLOW_CHECKED_DIV((N), ((s * s)))]);
s = (s + step);
}
tail[t] = acc;
t = (t + 2);
}
int64_t start = cube;
if ((start & 1) == 0) {
start = (start - 1);
}
t = start;
while (t > 0) {
int64_t x = FLOW_CHECKED_DIV((N), ((t * t)));
int64_t total = raw_opposite_parity_count_i64(x);
int64_t s = (3 * t);
int64_t step = (2 * t);
while (s <= cube) {
total = (total - transformed[s]);
s = (s + step);
}
total = (total - tail[t]);
transformed[t] = total;
t = (t - 2);
}
printf("%lld\n", transformed[1]);
free(small);
free(tail);
free(transformed);
return 0;
}