# Project Euler 545
# Faulhaber's Formulas — 100000th k with D(k)=20010.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const L: i64 = 308
const TARGET: i64 = 100000
const LIMIT_N: i64 = 4000000
function isqrt(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut x: i64 = n
let mut y: i64 = (x + 1) / 2
while y < x {
x = y
y = (x + n / x) / 2
}
return x
}
function gcd(a0: i64, b0: i64) -> i64 {
let mut a: i64 = a0
let mut b: i64 = b0
while b != 0 {
let t: i64 = a % b
a = b
b = t
}
return a
}
# Miller-Rabin primality (deterministic for n < 2^32 via bases 2,7,61).
# Replaces trial division which was O(sqrt(n)) per survivor.
function mulmod_pp(a: i64, b: i64, m: i64) -> i64 {
return (a * b) % m
}
function powmod_pp(a0: i64, e0: i64, m: i64) -> i64 {
let mut r: i64 = 1
let mut a: i64 = a0 % m
let mut e: i64 = e0
while e > 0 {
if (e & 1) != 0 { r = mulmod_pp(r, a, m) }
a = mulmod_pp(a, a, m)
e = e >> 1
}
return r
}
function mr_witness(n: i64, d: i64, s: i64, a0: i64) -> i32 {
let a: i64 = a0 % n
if a == 0 { return 1 }
let mut x: i64 = powmod_pp(a, d, n)
if x == 1 { return 1 }
if x == n - 1 { return 1 }
let mut j: i64 = 0
while j < s - 1 {
x = mulmod_pp(x, x, n)
if x == n - 1 { return 1 }
j = j + 1
}
return 0
}
function is_prime_small(n: i64) -> i32 {
if n < 2 { return 0 }
if n == 2 { return 1 }
if n == 3 { return 1 }
if n == 5 { return 1 }
if n == 7 { return 1 }
if n == 61 { return 1 }
if (n & 1) == 0 { return 0 }
if n % 3 == 0 { return 0 }
if n % 5 == 0 { return 0 }
if n % 7 == 0 { return 0 }
if n % 61 == 0 { return 0 }
let mut d: i64 = n - 1
let mut s: i64 = 0
while (d & 1) == 0 { d = d >> 1; s = s + 1 }
if mr_witness(n, d, s, 2) == 0 { return 0 }
if mr_witness(n, d, s, 7) == 0 { return 0 }
if mr_witness(n, d, s, 61) == 0 { return 0 }
return 1
}
function inv_mod(a0: i64, mod: i64) -> i64 {
let mut a: i64 = a0 % mod
if a < 0 { a = a + mod }
let mut t: i64 = 0
let mut newt: i64 = 1
let mut r: i64 = mod
let mut newr: i64 = a
while newr != 0 {
let q: i64 = r / newr
let tt: i64 = newt
newt = t - q * newt
t = tt
let rr: i64 = newr
newr = r - q * newr
r = rr
}
if r != 1 { return 0 }
if t < 0 { t = t + mod }
return t
}
function main() -> i32 {
let invalid: ptr<i8> = calloc(LIMIT_N + 1, 1)
let isprime_m: ptr<i8> = calloc(LIMIT_N + 1, 1)
let small_primes: ptr<i64> = calloc(10000, 8)
if invalid == null || isprime_m == null || small_primes == null { return 1 }
invalid[0] = 1
let mut nsp: i64 = 0
let mut p: i64 = 2
while p <= 10000 {
if is_prime_small(p) == 1 {
small_primes[nsp] = p
nsp = nsp + 1
}
p = p + 1
}
let g_divs: ptr<i64> = calloc(32, 8)
let mut nd: i64 = 0
let mut d: i64 = 1
while d * d <= L {
if L % d == 0 {
g_divs[nd] = d
nd = nd + 1
if d * d != L {
g_divs[nd] = L / d
nd = nd + 1
}
}
d = d + 1
}
let mut gi: i64 = 0
while gi < nd {
let g: i64 = g_divs[gi]
let b: i64 = L / g
let mut m: i64 = 0
while m <= LIMIT_N {
isprime_m[m] = 1
m = m + 1
}
isprime_m[0] = 0
let p_max: i64 = g * LIMIT_N + 1
let lim: i64 = isqrt(p_max)
let mut qi: i64 = 0
while qi < nsp {
let q: i64 = small_primes[qi]
if q > lim { break }
if g % q == 0 {
qi = qi + 1
continue
}
let inv: i64 = inv_mod(g, q)
let mut m0: i64 = (q - inv) % q
if m0 == 0 { m0 = q }
if g * m0 + 1 == q { m0 = m0 + q }
if m0 <= LIMIT_N {
let mut mm: i64 = m0
while mm <= LIMIT_N {
isprime_m[mm] = 0
mm = mm + q
}
}
qi = qi + 1
}
m = 1
while m <= LIMIT_N {
if isprime_m[m] == 1 {
let pp: i64 = g * m + 1
if pp != 2 && pp != 3 && pp != 5 && pp != 23 && pp != 29 {
if is_prime_small(pp) == 1 {
let f: i64 = m / gcd(m, b)
if f > 1 && invalid[f] == 0 {
let mut ff: i64 = f
while ff <= LIMIT_N {
invalid[ff] = 1
ff = ff + f
}
}
}
}
}
m = m + 1
}
gi = gi + 1
}
let mut count: i64 = 0
let mut ans: i64 = 0
let mut nn: i64 = 1
while nn <= LIMIT_N {
if invalid[nn] == 0 {
count = count + 1
if count == TARGET {
ans = L * nn
break
}
}
nn = nn + 1
}
printf("%lld\n", ans)
free(invalid); free(isprime_m); free(small_primes); free(g_divs)
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 isqrt_i64(int64_t n);
int64_t gcd_i64_i64(int64_t a0, int64_t b0);
int64_t mulmod_pp_i64_i64_i64(int64_t a, int64_t b, int64_t m);
int64_t powmod_pp_i64_i64_i64(int64_t a0, int64_t e0, int64_t m);
int32_t mr_witness_i64_i64_i64_i64(int64_t n, int64_t d, int64_t s, int64_t a0);
int32_t is_prime_small_i64(int64_t n);
int64_t inv_mod_i64_i64(int64_t a0, int64_t mod);
int32_t main(void);
static const int64_t L = 308;
static const int64_t TARGET = 100000;
static const int64_t LIMIT_N = 4000000;
int64_t isqrt_i64(int64_t n) {
if (n <= 0) {
return 0;
}
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 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 mulmod_pp_i64_i64_i64(int64_t a, int64_t b, int64_t m) {
return FLOW_CHECKED_MOD(((a * b)), (m));
}
int64_t powmod_pp_i64_i64_i64(int64_t a0, int64_t e0, int64_t m) {
int64_t r = 1;
int64_t a = FLOW_CHECKED_MOD((a0), (m));
int64_t e = e0;
while (e > 0) {
if ((e & 1) != 0) {
r = mulmod_pp_i64_i64_i64(r, a, m);
}
a = mulmod_pp_i64_i64_i64(a, a, m);
e = FLOW_CHECKED_SHR((e), (1));
}
return r;
}
int32_t mr_witness_i64_i64_i64_i64(int64_t n, int64_t d, int64_t s, int64_t a0) {
int64_t a = FLOW_CHECKED_MOD((a0), (n));
if (a == 0) {
return 1;
}
int64_t x = powmod_pp_i64_i64_i64(a, d, n);
if (x == 1) {
return 1;
}
if (x == (n - 1)) {
return 1;
}
int64_t j = 0;
while (j < (s - 1)) {
x = mulmod_pp_i64_i64_i64(x, x, n);
if (x == (n - 1)) {
return 1;
}
j = (j + 1);
}
return 0;
}
int32_t is_prime_small_i64(int64_t n) {
if (n < 2) {
return 0;
}
if (n == 2) {
return 1;
}
if (n == 3) {
return 1;
}
if (n == 5) {
return 1;
}
if (n == 7) {
return 1;
}
if (n == 61) {
return 1;
}
if ((n & 1) == 0) {
return 0;
}
if (FLOW_CHECKED_MOD((n), (3)) == 0) {
return 0;
}
if (FLOW_CHECKED_MOD((n), (5)) == 0) {
return 0;
}
if (FLOW_CHECKED_MOD((n), (7)) == 0) {
return 0;
}
if (FLOW_CHECKED_MOD((n), (61)) == 0) {
return 0;
}
int64_t d = (n - 1);
int64_t s = 0;
while ((d & 1) == 0) {
d = FLOW_CHECKED_SHR((d), (1));
s = (s + 1);
}
if (mr_witness_i64_i64_i64_i64(n, d, s, 2) == 0) {
return 0;
}
if (mr_witness_i64_i64_i64_i64(n, d, s, 7) == 0) {
return 0;
}
if (mr_witness_i64_i64_i64_i64(n, d, s, 61) == 0) {
return 0;
}
return 1;
}
int64_t inv_mod_i64_i64(int64_t a0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
if (a < 0) {
a = (a + mod);
}
int64_t t = 0;
int64_t newt = 1;
int64_t r = mod;
int64_t newr = a;
while (newr != 0) {
int64_t q = FLOW_CHECKED_DIV((r), (newr));
int64_t tt = newt;
newt = (t - (q * newt));
t = tt;
int64_t rr = newr;
newr = (r - (q * newr));
r = rr;
}
if (r != 1) {
return 0;
}
if (t < 0) {
t = (t + mod);
}
return t;
}
int32_t main(void) {
int8_t* invalid = (int8_t*)(calloc((LIMIT_N + 1), 1));
int8_t* isprime_m = (int8_t*)(calloc((LIMIT_N + 1), 1));
int64_t* small_primes = (int64_t*)(calloc(10000, 8));
if (((invalid == NULL || isprime_m == NULL) || small_primes == NULL)) {
return 1;
}
invalid[0] = 1;
int64_t nsp = 0;
int64_t p = 2;
while (p <= 10000) {
if (is_prime_small_i64(p) == 1) {
small_primes[nsp] = p;
nsp = (nsp + 1);
}
p = (p + 1);
}
int64_t* g_divs = (int64_t*)(calloc(32, 8));
int64_t nd = 0;
int64_t d = 1;
while ((d * d) <= L) {
if (FLOW_CHECKED_MOD((L), (d)) == 0) {
g_divs[nd] = d;
nd = (nd + 1);
if ((d * d) != L) {
g_divs[nd] = FLOW_CHECKED_DIV((L), (d));
nd = (nd + 1);
}
}
d = (d + 1);
}
int64_t gi = 0;
while (gi < nd) {
int64_t g = g_divs[gi];
int64_t b = FLOW_CHECKED_DIV((L), (g));
int64_t m = 0;
while (m <= LIMIT_N) {
isprime_m[m] = 1;
m = (m + 1);
}
isprime_m[0] = 0;
int64_t p_max = ((g * LIMIT_N) + 1);
int64_t lim = isqrt_i64(p_max);
int64_t qi = 0;
while (qi < nsp) {
int64_t q = small_primes[qi];
if (q > lim) {
break;
}
if (FLOW_CHECKED_MOD((g), (q)) == 0) {
qi = (qi + 1);
continue;
}
int64_t inv = inv_mod_i64_i64(g, q);
int64_t m0 = FLOW_CHECKED_MOD(((q - inv)), (q));
if (m0 == 0) {
m0 = q;
}
if (((g * m0) + 1) == q) {
m0 = (m0 + q);
}
if (m0 <= LIMIT_N) {
int64_t mm = m0;
while (mm <= LIMIT_N) {
isprime_m[mm] = 0;
mm = (mm + q);
}
}
qi = (qi + 1);
}
m = 1;
while (m <= LIMIT_N) {
if (isprime_m[m] == 1) {
int64_t pp = ((g * m) + 1);
if (((((pp != 2 && pp != 3) && pp != 5) && pp != 23) && pp != 29)) {
if (is_prime_small_i64(pp) == 1) {
int64_t f = FLOW_CHECKED_DIV((m), (gcd_i64_i64(m, b)));
if ((f > 1 && invalid[f] == 0)) {
int64_t ff = f;
while (ff <= LIMIT_N) {
invalid[ff] = 1;
ff = (ff + f);
}
}
}
}
}
m = (m + 1);
}
gi = (gi + 1);
}
int64_t count = 0;
int64_t ans = 0;
int64_t nn = 1;
while (nn <= LIMIT_N) {
if (invalid[nn] == 0) {
count = (count + 1);
if (count == TARGET) {
ans = (L * nn);
break;
}
}
nn = (nn + 1);
}
printf("%lld\n", ans);
free(invalid);
free(isprime_m);
free(small_primes);
free(g_divs);
return 0;
}