# Project Euler 407
# Sum M(n) for 1<=n<=10^7.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function modinv(a0: i64, m: i64) -> i64 {
let mut a: i64 = a0 % m
let mut b: i64 = m
let mut x0: i64 = 1
let mut x1: i64 = 0
while b != 0 {
let q: i64 = a / b
let t: i64 = a - q * b
a = b
b = t
let tx: i64 = x0 - q * x1
x0 = x1
x1 = tx
}
if x0 < 0 { x0 = x0 + m }
return x0
}
function main() -> i32 {
let LIMIT: i64 = 10000000
let spf: ptr<i32> = calloc(LIMIT + 1, 4)
if spf == null { return 1 }
let mut i: i64 = 0
while i <= LIMIT {
spf[i] = i as i32
i = i + 1
}
i = 2
while i * i <= LIMIT {
if (spf[i] as i64) == i {
let mut j: i64 = i * i
while j <= LIMIT {
if (spf[j] as i64) == j {
spf[j] = i as i32
}
j = j + i
}
}
i = i + 1
}
let mut total: i64 = 0
let mut n: i64 = 2
while n <= LIMIT {
let mut t: i64 = n
let p0: i64 = spf[t] as i64
let mut pk: i64 = 1
while t % p0 == 0 {
t = t / p0
pk = pk * p0
}
if t == 1 {
total = total + 1
n = n + 1
continue
}
# CRT subsets of sum of e_i where e_i ≡ 1 mod q_i, 0 mod n/q_i
let mut sums: ptr<i64> = calloc(512, 8)
if sums == null { return 1 }
let mut sc: i64 = 1
sums[0] = 0
let mut min_gt: i64 = n
# first factor pk
let mut q: i64 = pk
let n_div: i64 = n / q
let e: i64 = n_div * modinv(n_div, q)
let mut v: i64 = e % n
if v > 1 && v < min_gt { min_gt = v }
sums[sc] = v
sc = sc + 1
while t > 1 {
let p: i64 = spf[t] as i64
pk = 1
while t % p == 0 {
t = t / p
pk = pk * p
}
q = pk
let nd: i64 = n / q
let ee: i64 = nd * modinv(nd, q)
let old: i64 = sc
let mut s: i64 = 0
while s < old {
v = sums[s] + ee
if v >= n { v = v - n }
if v > 1 && v < min_gt { min_gt = v }
sums[sc] = v
sc = sc + 1
s = s + 1
}
}
total = total + n + 1 - min_gt
free(sums)
n = n + 1
}
printf("%lld\n", total)
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 modinv_i64_i64(int64_t a0, int64_t m);
int32_t main(void);
int64_t modinv_i64_i64(int64_t a0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD((a0), (m));
int64_t b = m;
int64_t x0 = 1;
int64_t x1 = 0;
while (b != 0) {
int64_t q = FLOW_CHECKED_DIV((a), (b));
int64_t t = (a - (q * b));
a = b;
b = t;
int64_t tx = (x0 - (q * x1));
x0 = x1;
x1 = tx;
}
if (x0 < 0) {
x0 = (x0 + m);
}
return x0;
}
int32_t main(void) {
int64_t LIMIT = 10000000;
int32_t* spf = (int32_t*)(calloc((LIMIT + 1), 4));
if (spf == NULL) {
return 1;
}
int64_t i = 0;
while (i <= LIMIT) {
spf[i] = ((int32_t)(i));
i = (i + 1);
}
i = 2;
while ((i * i) <= LIMIT) {
if (((int64_t)(spf[i])) == i) {
int64_t j = (i * i);
while (j <= LIMIT) {
if (((int64_t)(spf[j])) == j) {
spf[j] = ((int32_t)(i));
}
j = (j + i);
}
}
i = (i + 1);
}
int64_t total = 0;
int64_t n = 2;
while (n <= LIMIT) {
int64_t t = n;
int64_t p0 = ((int64_t)(spf[t]));
int64_t pk = 1;
while (FLOW_CHECKED_MOD((t), (p0)) == 0) {
t = FLOW_CHECKED_DIV((t), (p0));
pk = (pk * p0);
}
if (t == 1) {
total = (total + 1);
n = (n + 1);
continue;
}
int64_t* sums = (int64_t*)(calloc(512, 8));
if (sums == NULL) {
return 1;
}
int64_t sc = 1;
sums[0] = 0;
int64_t min_gt = n;
int64_t q = pk;
int64_t n_div = FLOW_CHECKED_DIV((n), (q));
int64_t e = (n_div * modinv_i64_i64(n_div, q));
int64_t v = FLOW_CHECKED_MOD((e), (n));
if ((v > 1 && v < min_gt)) {
min_gt = v;
}
sums[sc] = v;
sc = (sc + 1);
while (t > 1) {
int64_t p = ((int64_t)(spf[t]));
pk = 1;
while (FLOW_CHECKED_MOD((t), (p)) == 0) {
t = FLOW_CHECKED_DIV((t), (p));
pk = (pk * p);
}
q = pk;
int64_t nd = FLOW_CHECKED_DIV((n), (q));
int64_t ee = (nd * modinv_i64_i64(nd, q));
int64_t old = sc;
int64_t s = 0;
while (s < old) {
v = (sums[s] + ee);
if (v >= n) {
v = (v - n);
}
if ((v > 1 && v < min_gt)) {
min_gt = v;
}
sums[sc] = v;
sc = (sc + 1);
s = (s + 1);
}
}
total = (((total + n) + 1) - min_gt);
free(sums);
n = (n + 1);
}
printf("%lld\n", total);
free(spf);
return 0;
}