# Project Euler 343
# Sum f(n^3) for n=1..2e6; f(n)=LPF(n+1)-1 style via max LPF of factors of n^3+1.
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 tonelli(a0: i64, p: i64) -> i64 {
let a: i64 = a0 % p
if a == 0 { return 0 }
if p % 4 == 3 {
return modpow(a, (p + 1) / 4, p)
}
let mut q: i64 = p - 1
let mut s: i64 = 0
while q % 2 == 0 {
q = q / 2
s = s + 1
}
let mut z: i64 = 2
while modpow(z, (p - 1) / 2, p) != p - 1 {
z = z + 1
}
let mut m: i64 = s
let mut c: i64 = modpow(z, q, p)
let mut t: i64 = modpow(a, q, p)
let mut r: i64 = modpow(a, (q + 1) / 2, p)
while t != 1 {
let mut i: i64 = 1
let mut t2: i64 = (t * t) % p
while t2 != 1 {
t2 = (t2 * t2) % p
i = i + 1
}
# b = c^(2^(m-i-1))
let mut expb: i64 = 1
let mut j: i64 = 0
while j < m - i - 1 {
expb = expb * 2
j = j + 1
}
let b: i64 = modpow(c, expb, p)
r = (r * b) % p
t = (((t * b) % p) * b) % p
c = (b * b) % p
m = i
}
return r
}
function strip(p: i64, root: i64, limit: i64, rem: ptr<i64>, lpfq: ptr<i64>) -> i32 {
let mut start: i64 = root
if start == 0 { start = p }
let mut k: i64 = start
while k <= limit {
let mut v: i64 = rem[k]
if v % p == 0 {
while v % p == 0 { v = v / p }
rem[k] = v
lpfq[k] = p
}
k = k + p
}
return 0
}
function main() -> i32 {
let limit: i64 = 2000000
let lpf: ptr<i32> = calloc(limit + 2, 4)
if lpf == null { return 1 }
# sieve for primes, then mark largest prime factor
let sieve: ptr<i8> = calloc(limit + 2, 1)
let mut i: i64 = 0
while i <= limit + 1 {
sieve[i] = 1
i = i + 1
}
sieve[0] = 0; sieve[1] = 0
i = 2
while i * i <= limit + 1 {
if sieve[i] == 1 {
let mut j: i64 = i * i
while j <= limit + 1 {
sieve[j] = 0
j = j + i
}
}
i = i + 1
}
let mut pc: i64 = 0
i = 2
while i <= limit + 1 {
if sieve[i] == 1 { pc = pc + 1 }
i = i + 1
}
let primes: ptr<i64> = calloc(pc, 8)
let mut idx: i64 = 0
i = 2
while i <= limit + 1 {
if sieve[i] == 1 {
primes[idx] = i
idx = idx + 1
}
i = i + 1
}
# largest prime factor: overwrite with increasing primes
i = 0
while i <= limit + 1 {
lpf[i] = 0
i = i + 1
}
idx = 0
while idx < pc {
let p: i64 = primes[idx]
let mut m: i64 = p
while m <= limit + 1 {
lpf[m] = p as i32
m = m + p
}
idx = idx + 1
}
free(sieve)
let rem: ptr<i64> = calloc(limit + 1, 8)
let lpfq: ptr<i64> = calloc(limit + 1, 8)
i = 1
while i <= limit {
rem[i] = i * i - i + 1
lpfq[i] = 1
i = i + 1
}
idx = 0
while idx < pc {
let p: i64 = primes[idx]
if p == 2 {
idx = idx + 1
continue
}
if p == 3 {
strip(3, 2, limit, rem, lpfq)
idx = idx + 1
continue
}
if p % 3 != 1 {
idx = idx + 1
continue
}
let sqrt_disc: i64 = tonelli(p - 3, p)
let inv2: i64 = (p + 1) / 2
let mut d1: i64 = (1 + sqrt_disc) % p
if d1 < 0 { d1 = d1 + p }
let mut root1: i64 = (d1 * inv2) % p
if root1 < 0 { root1 = root1 + p }
let mut d2: i64 = (1 - sqrt_disc) % p
if d2 < 0 { d2 = d2 + p }
let mut root2: i64 = (d2 * inv2) % p
if root2 < 0 { root2 = root2 + p }
# Verify roots; if broken, brute-force residues
let chk1: i64 = (root1 * root1 - root1 + 1) % p
let chk2: i64 = (root2 * root2 - root2 + 1) % p
let c1: i64 = chk1
if c1 < 0 { c1 = c1 + p }
let c2: i64 = chk2
if c2 < 0 { c2 = c2 + p }
if c1 != 0 || c2 != 0 {
root1 = -1
root2 = -1
let mut rr: i64 = 0
while rr < p {
let mut v: i64 = (rr * rr - rr + 1) % p
if v < 0 { v = v + p }
if v == 0 {
if root1 < 0 { root1 = rr }
elif root2 < 0 { root2 = rr; break }
}
rr = rr + 1
}
}
if root1 >= 0 {
strip(p, root1, limit, rem, lpfq)
}
if root2 >= 0 && root2 != root1 {
strip(p, root2, limit, rem, lpfq)
}
idx = idx + 1
}
let mut total: i64 = 0
let mut k: i64 = 1
while k <= limit {
if rem[k] > 1 { lpfq[k] = rem[k] }
let a: i64 = lpf[k + 1] as i64
let b: i64 = lpfq[k]
let m: i64 = a
if b > m { m = b }
total = total + m - 1
k = k + 1
}
printf("%lld\n", total)
free(lpf); free(primes); free(rem); free(lpfq)
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 tonelli_i64_i64(int64_t a0, int64_t p);
int32_t strip_i64_i64_i64_ptr_i64_ptr_i64(int64_t p, int64_t root, int64_t limit, int64_t* rem, int64_t* lpfq);
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 tonelli_i64_i64(int64_t a0, int64_t p) {
int64_t a = FLOW_CHECKED_MOD((a0), (p));
if (a == 0) {
return 0;
}
if (FLOW_CHECKED_MOD((p), (4)) == 3) {
return modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((p + 1)), (4)), p);
}
int64_t q = (p - 1);
int64_t s = 0;
while (FLOW_CHECKED_MOD((q), (2)) == 0) {
q = FLOW_CHECKED_DIV((q), (2));
s = (s + 1);
}
int64_t z = 2;
while (modpow_i64_i64_i64(z, FLOW_CHECKED_DIV(((p - 1)), (2)), p) != (p - 1)) {
z = (z + 1);
}
int64_t m = s;
int64_t c = modpow_i64_i64_i64(z, q, p);
int64_t t = modpow_i64_i64_i64(a, q, p);
int64_t r = modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((q + 1)), (2)), p);
while (t != 1) {
int64_t i = 1;
int64_t t2 = FLOW_CHECKED_MOD(((t * t)), (p));
while (t2 != 1) {
t2 = FLOW_CHECKED_MOD(((t2 * t2)), (p));
i = (i + 1);
}
int64_t expb = 1;
int64_t j = 0;
while (j < ((m - i) - 1)) {
expb = (expb * 2);
j = (j + 1);
}
int64_t b = modpow_i64_i64_i64(c, expb, p);
r = FLOW_CHECKED_MOD(((r * b)), (p));
t = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((t * b)), (p)) * b)), (p));
c = FLOW_CHECKED_MOD(((b * b)), (p));
m = i;
}
return r;
}
int32_t strip_i64_i64_i64_ptr_i64_ptr_i64(int64_t p, int64_t root, int64_t limit, int64_t* rem, int64_t* lpfq) {
int64_t start = root;
if (start == 0) {
start = p;
}
int64_t k = start;
while (k <= limit) {
int64_t v = rem[k];
if (FLOW_CHECKED_MOD((v), (p)) == 0) {
while (FLOW_CHECKED_MOD((v), (p)) == 0) {
v = FLOW_CHECKED_DIV((v), (p));
}
rem[k] = v;
lpfq[k] = p;
}
k = (k + p);
}
return 0;
}
int32_t main(void) {
int64_t limit = 2000000;
int32_t* lpf = (int32_t*)(calloc((limit + 2), 4));
if (lpf == NULL) {
return 1;
}
int8_t* sieve = (int8_t*)(calloc((limit + 2), 1));
int64_t i = 0;
while (i <= (limit + 1)) {
sieve[i] = 1;
i = (i + 1);
}
sieve[0] = 0;
sieve[1] = 0;
i = 2;
while ((i * i) <= (limit + 1)) {
if (sieve[i] == 1) {
int64_t j = (i * i);
while (j <= (limit + 1)) {
sieve[j] = 0;
j = (j + i);
}
}
i = (i + 1);
}
int64_t pc = 0;
i = 2;
while (i <= (limit + 1)) {
if (sieve[i] == 1) {
pc = (pc + 1);
}
i = (i + 1);
}
int64_t* primes = (int64_t*)(calloc(pc, 8));
int64_t idx = 0;
i = 2;
while (i <= (limit + 1)) {
if (sieve[i] == 1) {
primes[idx] = i;
idx = (idx + 1);
}
i = (i + 1);
}
i = 0;
while (i <= (limit + 1)) {
lpf[i] = 0;
i = (i + 1);
}
idx = 0;
while (idx < pc) {
int64_t p = primes[idx];
int64_t m = p;
while (m <= (limit + 1)) {
lpf[m] = ((int32_t)(p));
m = (m + p);
}
idx = (idx + 1);
}
free(sieve);
int64_t* rem = (int64_t*)(calloc((limit + 1), 8));
int64_t* lpfq = (int64_t*)(calloc((limit + 1), 8));
i = 1;
while (i <= limit) {
rem[i] = (((i * i) - i) + 1);
lpfq[i] = 1;
i = (i + 1);
}
idx = 0;
while (idx < pc) {
int64_t p = primes[idx];
if (p == 2) {
idx = (idx + 1);
continue;
}
if (p == 3) {
strip_i64_i64_i64_ptr_i64_ptr_i64(3, 2, limit, rem, lpfq);
idx = (idx + 1);
continue;
}
if (FLOW_CHECKED_MOD((p), (3)) != 1) {
idx = (idx + 1);
continue;
}
int64_t sqrt_disc = tonelli_i64_i64((p - 3), p);
int64_t inv2 = FLOW_CHECKED_DIV(((p + 1)), (2));
int64_t d1 = FLOW_CHECKED_MOD(((1 + sqrt_disc)), (p));
if (d1 < 0) {
d1 = (d1 + p);
}
int64_t root1 = FLOW_CHECKED_MOD(((d1 * inv2)), (p));
if (root1 < 0) {
root1 = (root1 + p);
}
int64_t d2 = FLOW_CHECKED_MOD(((1 - sqrt_disc)), (p));
if (d2 < 0) {
d2 = (d2 + p);
}
int64_t root2 = FLOW_CHECKED_MOD(((d2 * inv2)), (p));
if (root2 < 0) {
root2 = (root2 + p);
}
int64_t chk1 = FLOW_CHECKED_MOD(((((root1 * root1) - root1) + 1)), (p));
int64_t chk2 = FLOW_CHECKED_MOD(((((root2 * root2) - root2) + 1)), (p));
int64_t c1 = chk1;
if (c1 < 0) {
c1 = (c1 + p);
}
int64_t c2 = chk2;
if (c2 < 0) {
c2 = (c2 + p);
}
if ((c1 != 0 || c2 != 0)) {
root1 = (-1);
root2 = (-1);
int64_t rr = 0;
while (rr < p) {
int64_t v = FLOW_CHECKED_MOD(((((rr * rr) - rr) + 1)), (p));
if (v < 0) {
v = (v + p);
}
if (v == 0) {
if (root1 < 0) {
root1 = rr;
} else if (root2 < 0) {
root2 = rr;
break;
}
}
rr = (rr + 1);
}
}
if (root1 >= 0) {
strip_i64_i64_i64_ptr_i64_ptr_i64(p, root1, limit, rem, lpfq);
}
if ((root2 >= 0 && root2 != root1)) {
strip_i64_i64_i64_ptr_i64_ptr_i64(p, root2, limit, rem, lpfq);
}
idx = (idx + 1);
}
int64_t total = 0;
int64_t k = 1;
while (k <= limit) {
if (rem[k] > 1) {
lpfq[k] = rem[k];
}
int64_t a = ((int64_t)(lpf[(k + 1)]));
int64_t b = lpfq[k];
int64_t m = a;
if (b > m) {
m = b;
}
total = ((total + m) - 1);
k = (k + 1);
}
printf("%lld\n", total);
free(lpf);
free(primes);
free(rem);
free(lpfq);
return 0;
}