# Project Euler 261
# Sum of pivots k <= 10^10 for pivoted square sums.
import euler.nt { isqrt }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
# Multiply two non-neg i64 into hi:lo (unsigned interpretation of bits)
function mul128(a: i64, b: i64, hi: ptr<i64>, lo: ptr<i64>) -> void {
let mask: i64 = 4294967295
let a0: i64 = a & mask
let a1: i64 = (a >> 32) & mask
let b0: i64 = b & mask
let b1: i64 = (b >> 32) & mask
let p0: i64 = a0 * b0
let p1: i64 = a0 * b1
let p2: i64 = a1 * b0
let p3: i64 = a1 * b1
let mut mid: i64 = (p0 >> 32) + (p1 & mask) + (p2 & mask)
lo[0] = (p0 & mask) | ((mid & mask) << 32)
hi[0] = p3 + (p1 >> 32) + (p2 >> 32) + (mid >> 32)
}
function add128(ahi: i64, alo: i64, bhi: i64, blo: i64, hi: ptr<i64>, lo: ptr<i64>) -> void {
let mask: i64 = 4294967295
# add low as unsigned - use careful
let mut lo_sum: i64 = alo + blo
let mut carry: i64 = 0
# detect unsigned overflow: if both nonneg and sum < either, or mixed signs hard
# Since pell numbers stay positive and < 2^63 for final x,y we only need intermediates
# that may exceed 2^63. Use unsigned compare via bits.
let alo_u_hi: i64 = (alo >> 32) & mask
let alo_u_lo: i64 = alo & mask
let blo_u_hi: i64 = (blo >> 32) & mask
let blo_u_lo: i64 = blo & mask
let s_lo: i64 = alo_u_lo + blo_u_lo
let c1: i64 = s_lo >> 32
let s_hi: i64 = alo_u_hi + blo_u_hi + c1
carry = s_hi >> 32
lo[0] = (s_lo & mask) | ((s_hi & mask) << 32)
hi[0] = ahi + bhi + carry
}
function set_add(keys: ptr<i64>, used: ptr<i8>, cap: i64, key: i64) -> bool {
let mut h: i64 = key % cap
if h < 0 { h = -h }
while used[h] != 0 {
if keys[h] == key { return false }
h = h + 1
if h == cap { h = 0 }
}
used[h] = 1
keys[h] = key
return true
}
function main() -> i32 {
let LIMIT: i64 = 10000000000
let mmax: i64 = (isqrt(1 + 2 * LIMIT) - 1) / 2
let spf: ptr<i32> = calloc(mmax + 3, 4)
let mut i: i64 = 0
while i <= mmax + 2 {
spf[i] = i as i32
i = i + 1
}
i = 2
while i * i <= mmax + 2 {
if (spf[i] as i64) == i {
let mut j: i64 = i * i
while j <= mmax + 2 {
if (spf[j] as i64) == j {
spf[j] = i as i32
}
j = j + i
}
}
i = i + 1
}
# pell cache as parallel arrays keyed by D via hashmap
let PCAP: i64 = 200003
let p_used: ptr<i8> = calloc(PCAP, 1)
let p_D: ptr<i64> = calloc(PCAP, 8)
let p_x: ptr<i64> = calloc(PCAP, 8)
let p_y: ptr<i64> = calloc(PCAP, 8)
let SCAP: i64 = 150001
let s_used: ptr<i8> = calloc(SCAP, 1)
let s_keys: ptr<i64> = calloc(SCAP, 8)
let mut sum: i64 = 0
let hi1: ptr<i64> = calloc(1, 8)
let lo1: ptr<i64> = calloc(1, 8)
let hi2: ptr<i64> = calloc(1, 8)
let lo2: ptr<i64> = calloc(1, 8)
let hi3: ptr<i64> = calloc(1, 8)
let lo3: ptr<i64> = calloc(1, 8)
let mut m: i64 = 1
while m <= mmax {
# squarefree_and_sqrt(m)
let mut n: i64 = m
let mut s: i64 = 1
let mut p: i64 = 1
while n > 1 {
let pr: i64 = spf[n] as i64
let mut e: i64 = 0
while n % pr == 0 {
n = n / pr
e = e + 1
}
if e % 2 == 1 { s = s * pr }
let mut ee: i64 = e / 2
let mut pp: i64 = 1
let mut t: i64 = 0
while t < ee {
pp = pp * pr
t = t + 1
}
p = p * pp
}
n = m + 1
let mut g: i64 = 1
let mut r: i64 = 1
while n > 1 {
let pr: i64 = spf[n] as i64
let mut e: i64 = 0
while n % pr == 0 {
n = n / pr
e = e + 1
}
if e % 2 == 1 { g = g * pr }
let mut ee: i64 = e / 2
let mut pp: i64 = 1
let mut t: i64 = 0
while t < ee {
pp = pp * pr
t = t + 1
}
r = r * pp
}
let D: i64 = s * g
# get or compute pell fundamental
let mut h: i64 = D % PCAP
if h < 0 { h = -h }
while p_used[h] != 0 && p_D[h] != D {
h = h + 1
if h == PCAP { h = 0 }
}
let mut x1: i64 = 0
let mut y1: i64 = 0
if p_used[h] != 0 {
x1 = p_x[h]
y1 = p_y[h]
} else {
let a0: i64 = isqrt(D)
let mut mm: i64 = 0
let mut dd: i64 = 1
let mut a: i64 = a0
let mut num1: i64 = 1
let mut num: i64 = a
let mut den1: i64 = 0
let mut den: i64 = 1
while num * num - D * den * den != 1 {
mm = dd * a - mm
dd = (D - mm * mm) / dd
a = (a0 + mm) / dd
let num2: i64 = num1
num1 = num
let den2: i64 = den1
den1 = den
num = a * num1 + num2
den = a * den1 + den2
}
x1 = num
y1 = den
p_used[h] = 1
p_D[h] = D
p_x[h] = x1
p_y[h] = y1
}
let mut x: i64 = g * r
let mut y: i64 = p
while true {
let q: i64 = y
let numerator: i64 = s * p * (p + q)
let k_floor: i64 = numerator / 2
if k_floor > LIMIT && q > p { break }
if numerator % 2 == 0 {
let k: i64 = numerator / 2
if k <= LIMIT && k >= 2 * m * (m + 1) {
let u: i64 = x / g
let tt: i64 = g * r * u
if (tt - m - 1) % 2 == 0 {
let nn: i64 = (tt - m - 1) / 2
if nn >= k {
if set_add(s_keys, s_used, SCAP, k) {
sum = sum + k
}
}
}
}
}
# x,y = x*x1 + y*y1*D, x*y1 + y*x1
# use 128-bit for safety then take low if fits
mul128(x, x1, hi1, lo1)
mul128(y, y1, hi2, lo2)
mul128(lo2[0], D, hi3, lo3)
# add lo1+lo3 -> newx, need hi
add128(hi1[0], lo1[0], hi3[0], lo3[0], hi1, lo1)
let newx: i64 = lo1[0]
mul128(x, y1, hi1, lo1)
mul128(y, x1, hi2, lo2)
add128(hi1[0], lo1[0], hi2[0], lo2[0], hi1, lo1)
let newy: i64 = lo1[0]
x = newx
y = newy
if x < 0 || y < 0 { break }
}
m = m + 1
}
printf("%lld\n", sum)
free(spf); free(p_used); free(p_D); free(p_x); free(p_y); free(s_used); free(s_keys)
free(hi1); free(lo1); free(hi2); free(lo2); free(hi3); free(lo3)
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);
void mul128_i64_i64_ptr_i64_ptr_i64(int64_t a, int64_t b, int64_t* hi, int64_t* lo);
void add128_i64_i64_i64_i64_ptr_i64_ptr_i64(int64_t ahi, int64_t alo, int64_t bhi, int64_t blo, int64_t* hi, int64_t* lo);
bool set_add_ptr_i64_ptr_i8_i64_i64(int64_t* keys, int8_t* used, int64_t cap, int64_t key);
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;
}
void mul128_i64_i64_ptr_i64_ptr_i64(int64_t a, int64_t b, int64_t* hi, int64_t* lo) {
int64_t mask = 4294967295;
int64_t a0 = (a & mask);
int64_t a1 = (FLOW_CHECKED_SHR((a), (32)) & mask);
int64_t b0 = (b & mask);
int64_t b1 = (FLOW_CHECKED_SHR((b), (32)) & mask);
int64_t p0 = (a0 * b0);
int64_t p1 = (a0 * b1);
int64_t p2 = (a1 * b0);
int64_t p3 = (a1 * b1);
int64_t mid = ((FLOW_CHECKED_SHR((p0), (32)) + (p1 & mask)) + (p2 & mask));
lo[0] = ((p0 & mask) | FLOW_CHECKED_SHL(((mid & mask)), (32)));
hi[0] = (((p3 + FLOW_CHECKED_SHR((p1), (32))) + FLOW_CHECKED_SHR((p2), (32))) + FLOW_CHECKED_SHR((mid), (32)));
}
void add128_i64_i64_i64_i64_ptr_i64_ptr_i64(int64_t ahi, int64_t alo, int64_t bhi, int64_t blo, int64_t* hi, int64_t* lo) {
int64_t mask = 4294967295;
int64_t lo_sum = (alo + blo);
int64_t carry = 0;
int64_t alo_u_hi = (FLOW_CHECKED_SHR((alo), (32)) & mask);
int64_t alo_u_lo = (alo & mask);
int64_t blo_u_hi = (FLOW_CHECKED_SHR((blo), (32)) & mask);
int64_t blo_u_lo = (blo & mask);
int64_t s_lo = (alo_u_lo + blo_u_lo);
int64_t c1 = FLOW_CHECKED_SHR((s_lo), (32));
int64_t s_hi = ((alo_u_hi + blo_u_hi) + c1);
carry = FLOW_CHECKED_SHR((s_hi), (32));
lo[0] = ((s_lo & mask) | FLOW_CHECKED_SHL(((s_hi & mask)), (32)));
hi[0] = ((ahi + bhi) + carry);
}
bool set_add_ptr_i64_ptr_i8_i64_i64(int64_t* keys, int8_t* used, int64_t cap, int64_t key) {
int64_t h = FLOW_CHECKED_MOD((key), (cap));
if (h < 0) {
h = (-h);
}
while (used[h] != 0) {
if (keys[h] == key) {
return 0;
}
h = (h + 1);
if (h == cap) {
h = 0;
}
}
used[h] = 1;
keys[h] = key;
return 1;
}
int32_t main(void) {
int64_t LIMIT = 10000000000;
int64_t mmax = FLOW_CHECKED_DIV(((isqrt_i64((1 + (2 * LIMIT))) - 1)), (2));
int32_t* spf = (int32_t*)(calloc((mmax + 3), 4));
int64_t i = 0;
while (i <= (mmax + 2)) {
spf[i] = ((int32_t)(i));
i = (i + 1);
}
i = 2;
while ((i * i) <= (mmax + 2)) {
if (((int64_t)(spf[i])) == i) {
int64_t j = (i * i);
while (j <= (mmax + 2)) {
if (((int64_t)(spf[j])) == j) {
spf[j] = ((int32_t)(i));
}
j = (j + i);
}
}
i = (i + 1);
}
int64_t PCAP = 200003;
int8_t* p_used = (int8_t*)(calloc(PCAP, 1));
int64_t* p_D = (int64_t*)(calloc(PCAP, 8));
int64_t* p_x = (int64_t*)(calloc(PCAP, 8));
int64_t* p_y = (int64_t*)(calloc(PCAP, 8));
int64_t SCAP = 150001;
int8_t* s_used = (int8_t*)(calloc(SCAP, 1));
int64_t* s_keys = (int64_t*)(calloc(SCAP, 8));
int64_t sum = 0;
int64_t* hi1 = (int64_t*)(calloc(1, 8));
int64_t* lo1 = (int64_t*)(calloc(1, 8));
int64_t* hi2 = (int64_t*)(calloc(1, 8));
int64_t* lo2 = (int64_t*)(calloc(1, 8));
int64_t* hi3 = (int64_t*)(calloc(1, 8));
int64_t* lo3 = (int64_t*)(calloc(1, 8));
int64_t m = 1;
while (m <= mmax) {
int64_t n = m;
int64_t s = 1;
int64_t p = 1;
while (n > 1) {
int64_t pr = ((int64_t)(spf[n]));
int64_t e = 0;
while (FLOW_CHECKED_MOD((n), (pr)) == 0) {
n = FLOW_CHECKED_DIV((n), (pr));
e = (e + 1);
}
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
s = (s * pr);
}
int64_t ee = FLOW_CHECKED_DIV((e), (2));
int64_t pp = 1;
int64_t t = 0;
while (t < ee) {
pp = (pp * pr);
t = (t + 1);
}
p = (p * pp);
}
n = (m + 1);
int64_t g = 1;
int64_t r = 1;
while (n > 1) {
int64_t pr = ((int64_t)(spf[n]));
int64_t e = 0;
while (FLOW_CHECKED_MOD((n), (pr)) == 0) {
n = FLOW_CHECKED_DIV((n), (pr));
e = (e + 1);
}
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
g = (g * pr);
}
int64_t ee = FLOW_CHECKED_DIV((e), (2));
int64_t pp = 1;
int64_t t = 0;
while (t < ee) {
pp = (pp * pr);
t = (t + 1);
}
r = (r * pp);
}
int64_t D = (s * g);
int64_t h = FLOW_CHECKED_MOD((D), (PCAP));
if (h < 0) {
h = (-h);
}
while ((p_used[h] != 0 && p_D[h] != D)) {
h = (h + 1);
if (h == PCAP) {
h = 0;
}
}
int64_t x1 = 0;
int64_t y1 = 0;
if (p_used[h] != 0) {
x1 = p_x[h];
y1 = p_y[h];
} else {
int64_t a0 = isqrt_i64(D);
int64_t mm = 0;
int64_t dd = 1;
int64_t a = a0;
int64_t num1 = 1;
int64_t num = a;
int64_t den1 = 0;
int64_t den = 1;
while (((num * num) - ((D * den) * den)) != 1) {
mm = ((dd * a) - mm);
dd = FLOW_CHECKED_DIV(((D - (mm * mm))), (dd));
a = FLOW_CHECKED_DIV(((a0 + mm)), (dd));
int64_t num2 = num1;
num1 = num;
int64_t den2 = den1;
den1 = den;
num = ((a * num1) + num2);
den = ((a * den1) + den2);
}
x1 = num;
y1 = den;
p_used[h] = 1;
p_D[h] = D;
p_x[h] = x1;
p_y[h] = y1;
}
int64_t x = (g * r);
int64_t y = p;
while (1) {
int64_t q = y;
int64_t numerator = ((s * p) * (p + q));
int64_t k_floor = FLOW_CHECKED_DIV((numerator), (2));
if ((k_floor > LIMIT && q > p)) {
break;
}
if (FLOW_CHECKED_MOD((numerator), (2)) == 0) {
int64_t k = FLOW_CHECKED_DIV((numerator), (2));
if ((k <= LIMIT && k >= ((2 * m) * (m + 1)))) {
int64_t u = FLOW_CHECKED_DIV((x), (g));
int64_t tt = ((g * r) * u);
if (FLOW_CHECKED_MOD((((tt - m) - 1)), (2)) == 0) {
int64_t nn = FLOW_CHECKED_DIV((((tt - m) - 1)), (2));
if (nn >= k) {
if (set_add_ptr_i64_ptr_i8_i64_i64(s_keys, s_used, SCAP, k)) {
sum = (sum + k);
}
}
}
}
}
mul128_i64_i64_ptr_i64_ptr_i64(x, x1, hi1, lo1);
mul128_i64_i64_ptr_i64_ptr_i64(y, y1, hi2, lo2);
mul128_i64_i64_ptr_i64_ptr_i64(lo2[0], D, hi3, lo3);
add128_i64_i64_i64_i64_ptr_i64_ptr_i64(hi1[0], lo1[0], hi3[0], lo3[0], hi1, lo1);
int64_t newx = lo1[0];
mul128_i64_i64_ptr_i64_ptr_i64(x, y1, hi1, lo1);
mul128_i64_i64_ptr_i64_ptr_i64(y, x1, hi2, lo2);
add128_i64_i64_i64_i64_ptr_i64_ptr_i64(hi1[0], lo1[0], hi2[0], lo2[0], hi1, lo1);
int64_t newy = lo1[0];
x = newx;
y = newy;
if ((x < 0 || y < 0)) {
break;
}
}
m = (m + 1);
}
printf("%lld\n", sum);
free(spf);
free(p_used);
free(p_D);
free(p_x);
free(p_y);
free(s_used);
free(s_keys);
free(hi1);
free(lo1);
free(hi2);
free(lo2);
free(hi3);
free(lo3);
return 0;
}