# Project Euler 611: Hallway of Square Steps
# Count n <= N where #representations n = a^2+b^2 (0<a<b) is odd.
# Uses Lucy sieve for pi(x) and chi_sum(x).
import euler.nt { isqrt }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const N: i64 = 1000000000000 # 10^12
function main() -> i32 {
let root: i64 = isqrt(N)
# Build SPF sieve up to root
let spf: ptr<i64> = calloc(root + 1, 8)
for i in 0..(root + 1) { spf[i] = i }
for i in 2..(isqrt(root) + 1) {
if spf[i] == i {
let mut j: i64 = (i as i64) * (i as i64)
while j <= root {
if spf[j] == j { spf[j] = i }
j = j + i
}
}
}
# Build pi1_small prefix: pi1[x] = count of primes <= x with p ≡ 1 (mod 4)
let pi1_small: ptr<i64> = calloc(root + 1, 8)
let mut c: i64 = 0
for x in 2..(root + 1) {
if spf[x] == x && (x & 3) == 1 { c = c + 1 }
pi1_small[x] = c
}
# Build Lucy sieve tables
# vals: descending distinct floor(N/i)
let vals: ptr<i64> = calloc(2 * root + 10, 8)
let mut m: i64 = 0
let mut i_val: i64 = 1
while i_val <= N {
let q: i64 = N / i_val
vals[m] = q
m = m + 1
i_val = N / q + 1
}
# idx_big: for v > root, idx_big[N/v] = index in vals
let idx_big: ptr<i64> = calloc(root + 1, 8)
for idx in 0..m {
let v: i64 = vals[idx]
if v > root {
idx_big[N / v] = idx
}
}
# Initialize pi and schi
let pi_tab: ptr<i64> = calloc(m, 8)
let schi_tab: ptr<i64> = calloc(m, 8)
for j in 0..m {
let v: i64 = vals[j]
if v >= 2 { pi_tab[j] = v - 1 } else { pi_tab[j] = 0 }
# S_int(v) = count(1 mod4) - count(3 mod4) for n=1..v
# = ((v+3)/4) - ((v+1)/4)
let s_int: i64 = (v + 3) / 4 - (v + 1) / 4
schi_tab[j] = s_int - 1 # exclude n=1
}
# Lucy sieve: for each prime p up to sqrt(N)
for p in 2..(root + 1) {
if spf[p] != p { continue }
let p2: i64 = (p as i64) * (p as i64)
if p2 > N { break }
# k = upper_bound(p2) = number of vals >= p2
# vals is descending, find first index where vals[idx] < p2
let mut k: i64 = 0
let mut lo_k: i64 = 0
let mut hi_k: i64 = m
while lo_k < hi_k {
let mid: i64 = (lo_k + hi_k) / 2
if vals[mid] >= p2 { lo_k = mid + 1 } else { hi_k = mid }
}
k = lo_k
if k == 0 { continue }
# base values at p-1
let base_idx: i64 = m - (p - 1)
let base_pi: i64 = pi_tab[base_idx]
let base_s: i64 = schi_tab[base_idx]
# chi(p)
let chi_p: i64
if p == 2 { chi_p = 0 } else {
if (p & 3) == 1 { chi_p = 1 } else { chi_p = -1 }
}
for j in 0..k {
let v: i64 = vals[j]
let vp: i64 = v / p
let idx_vp: i64
if vp <= root {
idx_vp = m - vp
} else {
idx_vp = idx_big[N / vp]
}
pi_tab[j] = pi_tab[j] - (pi_tab[idx_vp] - base_pi)
if chi_p != 0 {
schi_tab[j] = schi_tab[j] - chi_p * (schi_tab[idx_vp] - base_s)
}
}
}
# pi1_query function (inline)
# For x <= root: pi1_small[x]
# For x > root: (pi_tab[idx] - 1 + schi_tab[idx]) / 2
# Enumerate odd u from 1 to root
let mut total: i64 = 0
# Temp buffer for excluded primes
let excluded: ptr<i64> = calloc(20, 8)
let mut u: i64 = 1
while u <= root {
if u % 2 == 0 {
u = u + 1
continue
}
let u2: i64 = u * u
let max2: i64 = N / u2
# Factor u using SPF, compute parity and excluded primes
let mut parity: i64 = 0
let mut n_excl: i64 = 0
let mut x: i64 = u
while x > 1 {
let p: i64 = spf[x]
let mut odd_exp: i64 = 0
while x % p == 0 {
x = x / p
odd_exp = odd_exp ^ 1
}
if odd_exp == 1 && (p & 3) == 1 {
parity = parity ^ 1
excluded[n_excl] = p
n_excl = n_excl + 1
}
}
# Case A: parity is odd -> add bit_length(max2)
if parity == 1 {
# Count k >= 0 with 2^k <= max2
let mut bl: i64 = 0
let mut tmp2: i64 = max2
while tmp2 > 0 { bl = bl + 1; tmp2 = tmp2 >> 1 }
total = total + bl
}
# Case B: n = 2^k * p * u^2, p ≡ 1 (mod4), p not in excluded
let mut xv: i64 = max2
while xv >= 5 {
# pi1_query(xv)
let cnt: i64
if xv <= root {
cnt = pi1_small[xv]
} else {
let idx: i64 = idx_big[N / xv]
let pi_x: i64 = pi_tab[idx]
let s_x: i64 = schi_tab[idx]
cnt = (pi_x - 1 + s_x) / 2
}
# Subtract excluded primes <= xv
let mut ex: i64 = 0
for ei in 0..n_excl {
if excluded[ei] <= xv { ex = ex + 1 }
}
total = total + cnt - ex
xv = xv / 2
}
u = u + 1
}
printf("%lld\n", total)
free(excluded)
free(schi_tab)
free(pi_tab)
free(idx_big)
free(vals)
free(pi1_small)
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);
int32_t main(void);
static const int64_t N = 1000000000000;
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;
}
int32_t main(void) {
int64_t root = isqrt_i64(N);
int64_t* spf = (int64_t*)(calloc((root + 1), 8));
int32_t __flow_step_1 = 1;
for (int32_t i = 0; (0 <= (root + 1)) ? i < (root + 1) : i > (root + 1); i += (0 <= (root + 1)) ? 1 : -1) {
spf[i] = i;
}
int32_t __flow_step_2 = 1;
for (int32_t i = 2; (2 <= (isqrt_i64(root) + 1)) ? i < (isqrt_i64(root) + 1) : i > (isqrt_i64(root) + 1); i += (2 <= (isqrt_i64(root) + 1)) ? 1 : -1) {
if (spf[i] == i) {
int64_t j = (((int64_t)(i)) * ((int64_t)(i)));
while (j <= root) {
if (spf[j] == j) {
spf[j] = i;
}
j = (j + i);
}
}
}
int64_t* pi1_small = (int64_t*)(calloc((root + 1), 8));
int64_t c = 0;
int32_t __flow_step_3 = 1;
for (int32_t x = 2; (2 <= (root + 1)) ? x < (root + 1) : x > (root + 1); x += (2 <= (root + 1)) ? 1 : -1) {
if ((spf[x] == x && (x & 3) == 1)) {
c = (c + 1);
}
pi1_small[x] = c;
}
int64_t* vals = (int64_t*)(calloc(((2 * root) + 10), 8));
int64_t m = 0;
int64_t i_val = 1;
while (i_val <= N) {
int64_t q = FLOW_CHECKED_DIV((N), (i_val));
vals[m] = q;
m = (m + 1);
i_val = (FLOW_CHECKED_DIV((N), (q)) + 1);
}
int64_t* idx_big = (int64_t*)(calloc((root + 1), 8));
int32_t __flow_step_4 = 1;
for (int32_t idx = 0; (0 <= m) ? idx < m : idx > m; idx += (0 <= m) ? 1 : -1) {
int64_t v = vals[idx];
if (v > root) {
idx_big[FLOW_CHECKED_DIV((N), (v))] = idx;
}
}
int64_t* pi_tab = (int64_t*)(calloc(m, 8));
int64_t* schi_tab = (int64_t*)(calloc(m, 8));
int32_t __flow_step_5 = 1;
for (int32_t j = 0; (0 <= m) ? j < m : j > m; j += (0 <= m) ? 1 : -1) {
int64_t v = vals[j];
if (v >= 2) {
pi_tab[j] = (v - 1);
} else {
pi_tab[j] = 0;
}
int64_t s_int = (FLOW_CHECKED_DIV(((v + 3)), (4)) - FLOW_CHECKED_DIV(((v + 1)), (4)));
schi_tab[j] = (s_int - 1);
}
int32_t __flow_step_6 = 1;
for (int32_t p = 2; (2 <= (root + 1)) ? p < (root + 1) : p > (root + 1); p += (2 <= (root + 1)) ? 1 : -1) {
if (spf[p] != p) {
continue;
}
int64_t p2 = (((int64_t)(p)) * ((int64_t)(p)));
if (p2 > N) {
break;
}
int64_t k = 0;
int64_t lo_k = 0;
int64_t hi_k = m;
while (lo_k < hi_k) {
int64_t mid = FLOW_CHECKED_DIV(((lo_k + hi_k)), (2));
if (vals[mid] >= p2) {
lo_k = (mid + 1);
} else {
hi_k = mid;
}
}
k = lo_k;
if (k == 0) {
continue;
}
int64_t base_idx = (m - (p - 1));
int64_t base_pi = pi_tab[base_idx];
int64_t base_s = schi_tab[base_idx];
int64_t chi_p;
if (p == 2) {
chi_p = 0;
} else {
if ((p & 3) == 1) {
chi_p = 1;
} else {
chi_p = (-1);
}
}
int32_t __flow_step_7 = 1;
for (int32_t j = 0; (0 <= k) ? j < k : j > k; j += (0 <= k) ? 1 : -1) {
int64_t v = vals[j];
int64_t vp = FLOW_CHECKED_DIV((v), (p));
int64_t idx_vp;
if (vp <= root) {
idx_vp = (m - vp);
} else {
idx_vp = idx_big[FLOW_CHECKED_DIV((N), (vp))];
}
pi_tab[j] = (pi_tab[j] - (pi_tab[idx_vp] - base_pi));
if (chi_p != 0) {
schi_tab[j] = (schi_tab[j] - (chi_p * (schi_tab[idx_vp] - base_s)));
}
}
}
int64_t total = 0;
int64_t* excluded = (int64_t*)(calloc(20, 8));
int64_t u = 1;
while (u <= root) {
if (FLOW_CHECKED_MOD((u), (2)) == 0) {
u = (u + 1);
continue;
}
int64_t u2 = (u * u);
int64_t max2 = FLOW_CHECKED_DIV((N), (u2));
int64_t parity = 0;
int64_t n_excl = 0;
int64_t x = u;
while (x > 1) {
int64_t p = spf[x];
int64_t odd_exp = 0;
while (FLOW_CHECKED_MOD((x), (p)) == 0) {
x = FLOW_CHECKED_DIV((x), (p));
odd_exp = (odd_exp ^ 1);
}
if ((odd_exp == 1 && (p & 3) == 1)) {
parity = (parity ^ 1);
excluded[n_excl] = p;
n_excl = (n_excl + 1);
}
}
if (parity == 1) {
int64_t bl = 0;
int64_t tmp2 = max2;
while (tmp2 > 0) {
bl = (bl + 1);
tmp2 = FLOW_CHECKED_SHR((tmp2), (1));
}
total = (total + bl);
}
int64_t xv = max2;
while (xv >= 5) {
int64_t cnt;
if (xv <= root) {
cnt = pi1_small[xv];
} else {
int64_t idx = idx_big[FLOW_CHECKED_DIV((N), (xv))];
int64_t pi_x = pi_tab[idx];
int64_t s_x = schi_tab[idx];
cnt = FLOW_CHECKED_DIV((((pi_x - 1) + s_x)), (2));
}
int64_t ex = 0;
int32_t __flow_step_8 = 1;
for (int32_t ei = 0; (0 <= n_excl) ? ei < n_excl : ei > n_excl; ei += (0 <= n_excl) ? 1 : -1) {
if (excluded[ei] <= xv) {
ex = (ex + 1);
}
}
total = ((total + cnt) - ex);
xv = FLOW_CHECKED_DIV((xv), (2));
}
u = (u + 1);
}
printf("%lld\n", total);
free(excluded);
free(schi_tab);
free(pi_tab);
free(idx_big);
free(vals);
free(pi1_small);
free(spf);
return 0;
}