# Project Euler 365
# sum M(10^18, 10^9, pqr) over primes 1000<p<q<r<5000 (Lucas + CRT).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function modpow(base: i64, exp: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base % mod
let mut e: i64 = exp
while e > 0 {
if e % 2 == 1 { r = (r * b) % mod }
b = (b * b) % mod
e = e / 2
}
return r
}
function lucas(n0: i64, k0: i64, p: i64) -> i64 {
let fact: ptr<i64> = calloc(p, 8)
let invfact: ptr<i64> = calloc(p, 8)
if fact == null || invfact == null { return 0 }
fact[0] = 1
let mut i: i64 = 1
while i < p {
fact[i] = (fact[i - 1] * i) % p
i = i + 1
}
invfact[p - 1] = modpow(fact[p - 1], p - 2, p)
i = p - 1
while i > 0 {
invfact[i - 1] = (invfact[i] * i) % p
i = i - 1
}
let mut res: i64 = 1
let mut n: i64 = n0
let mut k: i64 = k0
while k > 0 || n > 0 {
let ni: i64 = n % p
let ki: i64 = k % p
if ki > ni {
free(invfact)
free(fact)
return 0
}
res = (res * fact[ni] % p * invfact[ki] % p * invfact[ni - ki]) % p
n = n / p
k = k / p
}
free(invfact)
free(fact)
return res
}
function main() -> i32 {
let HI: i32 = 5000
let sieve: ptr<i8> = calloc(HI as i64, 1)
if sieve == null { return 1 }
let mut i: i32 = 0
while i < HI {
sieve[i] = 1
i = i + 1
}
sieve[0] = 0
sieve[1] = 0
let mut p: i32 = 2
while p * p < HI {
if sieve[p] == 1 {
let mut m: i32 = p * p
while m < HI {
sieve[m] = 0
m = m + p
}
}
p = p + 1
}
let primes: ptr<i32> = calloc(1000, 4)
if primes == null { return 1 }
let mut L: i32 = 0
p = 1001
while p < HI {
if sieve[p] == 1 {
primes[L] = p
L = L + 1
}
p = p + 1
}
let N: i64 = 1000000000000000000
let K: i64 = 1000000000
let residues: ptr<i32> = calloc(L as i64, 4)
if residues == null { return 1 }
i = 0
while i < L {
residues[i] = lucas(N, K, primes[i] as i64) as i32
i = i + 1
}
# inv_rows[i][j-i-1] = p_i^{-1} mod p_j for j>i
# Store flat: for each i, row of length L-i-1
let invflat: ptr<i32> = calloc((L as i64) * (L as i64), 4)
if invflat == null { return 1 }
i = 0
while i < L {
let mut j: i32 = i + 1
while j < L {
let pi: i64 = primes[i] as i64
let pj: i64 = primes[j] as i64
invflat[i * L + j] = modpow(pi, pj - 2, pj) as i32
j = j + 1
}
i = i + 1
}
let mut total: i64 = 0
i = 0
while i < L - 2 {
let pi: i64 = primes[i] as i64
let a: i64 = residues[i] as i64
let mut j: i32 = i + 1
while j < L - 1 {
let q: i64 = primes[j] as i64
let b: i64 = residues[j] as i64
let inv_pq: i64 = invflat[i * L + j] as i64
let t1: i64 = ((b - a) % q + q) % q * inv_pq % q
let x1: i64 = a + pi * t1
let pq: i64 = pi * q
let mut k: i32 = j + 1
while k < L {
let r: i64 = primes[k] as i64
let c: i64 = residues[k] as i64
let inv_p_r: i64 = invflat[i * L + k] as i64
let inv_q_r: i64 = invflat[j * L + k] as i64
let inv_pq_r: i64 = (inv_p_r * inv_q_r) % r
let t2: i64 = ((c - x1) % r + r) % r * inv_pq_r % r
total = total + x1 + pq * t2
k = k + 1
}
j = j + 1
}
i = i + 1
}
printf("%lld\n", total)
free(invflat)
free(residues)
free(primes)
free(sieve)
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 modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int64_t lucas_i64_i64_i64(int64_t n0, int64_t k0, int64_t p);
int32_t main(void);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = FLOW_CHECKED_MOD(((r * b)), (mod));
}
b = FLOW_CHECKED_MOD(((b * b)), (mod));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t lucas_i64_i64_i64(int64_t n0, int64_t k0, int64_t p) {
int64_t* fact = (int64_t*)(calloc(p, 8));
int64_t* invfact = (int64_t*)(calloc(p, 8));
if ((fact == NULL || invfact == NULL)) {
return 0;
}
fact[0] = 1;
int64_t i = 1;
while (i < p) {
fact[i] = FLOW_CHECKED_MOD(((fact[(i - 1)] * i)), (p));
i = (i + 1);
}
invfact[(p - 1)] = modpow_i64_i64_i64(fact[(p - 1)], (p - 2), p);
i = (p - 1);
while (i > 0) {
invfact[(i - 1)] = FLOW_CHECKED_MOD(((invfact[i] * i)), (p));
i = (i - 1);
}
int64_t res = 1;
int64_t n = n0;
int64_t k = k0;
while ((k > 0 || n > 0)) {
int64_t ni = FLOW_CHECKED_MOD((n), (p));
int64_t ki = FLOW_CHECKED_MOD((k), (p));
if (ki > ni) {
free(invfact);
free(fact);
return 0;
}
res = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((res * fact[ni])), (p)) * invfact[ki])), (p)) * invfact[(ni - ki)])), (p));
n = FLOW_CHECKED_DIV((n), (p));
k = FLOW_CHECKED_DIV((k), (p));
}
free(invfact);
free(fact);
return res;
}
int32_t main(void) {
int32_t HI = 5000;
int8_t* sieve = (int8_t*)(calloc(((int64_t)(HI)), 1));
if (sieve == NULL) {
return 1;
}
int32_t i = 0;
while (i < HI) {
sieve[i] = 1;
i = (i + 1);
}
sieve[0] = 0;
sieve[1] = 0;
int32_t p = 2;
while ((p * p) < HI) {
if (sieve[p] == 1) {
int32_t m = (p * p);
while (m < HI) {
sieve[m] = 0;
m = (m + p);
}
}
p = (p + 1);
}
int32_t* primes = (int32_t*)(calloc(1000, 4));
if (primes == NULL) {
return 1;
}
int32_t L = 0;
p = 1001;
while (p < HI) {
if (sieve[p] == 1) {
primes[L] = p;
L = (L + 1);
}
p = (p + 1);
}
int64_t N = 1000000000000000000;
int64_t K = 1000000000;
int32_t* residues = (int32_t*)(calloc(((int64_t)(L)), 4));
if (residues == NULL) {
return 1;
}
i = 0;
while (i < L) {
residues[i] = ((int32_t)(lucas_i64_i64_i64(N, K, ((int64_t)(primes[i])))));
i = (i + 1);
}
int32_t* invflat = (int32_t*)(calloc((((int64_t)(L)) * ((int64_t)(L))), 4));
if (invflat == NULL) {
return 1;
}
i = 0;
while (i < L) {
int32_t j = (i + 1);
while (j < L) {
int64_t pi = ((int64_t)(primes[i]));
int64_t pj = ((int64_t)(primes[j]));
invflat[((i * L) + j)] = ((int32_t)(modpow_i64_i64_i64(pi, (pj - 2), pj)));
j = (j + 1);
}
i = (i + 1);
}
int64_t total = 0;
i = 0;
while (i < (L - 2)) {
int64_t pi = ((int64_t)(primes[i]));
int64_t a = ((int64_t)(residues[i]));
int32_t j = (i + 1);
while (j < (L - 1)) {
int64_t q = ((int64_t)(primes[j]));
int64_t b = ((int64_t)(residues[j]));
int64_t inv_pq = ((int64_t)(invflat[((i * L) + j)]));
int64_t t1 = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((b - a)), (q)) + q)), (q)) * inv_pq)), (q));
int64_t x1 = (a + (pi * t1));
int64_t pq = (pi * q);
int32_t k = (j + 1);
while (k < L) {
int64_t r = ((int64_t)(primes[k]));
int64_t c = ((int64_t)(residues[k]));
int64_t inv_p_r = ((int64_t)(invflat[((i * L) + k)]));
int64_t inv_q_r = ((int64_t)(invflat[((j * L) + k)]));
int64_t inv_pq_r = FLOW_CHECKED_MOD(((inv_p_r * inv_q_r)), (r));
int64_t t2 = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((c - x1)), (r)) + r)), (r)) * inv_pq_r)), (r));
total = ((total + x1) + (pq * t2));
k = (k + 1);
}
j = (j + 1);
}
i = (i + 1);
}
printf("%lld\n", total);
free(invflat);
free(residues);
free(primes);
free(sieve);
return 0;
}