# Project Euler 926
# Total Roundness — R(10^7!) mod 10^9+7.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function sqrt(x: f64) -> f64
}
const MOD: i64 = 1000000007
function primes_upto(n: i64, out_count: ptr<i64>) -> ptr<i64> {
if n < 2 {
out_count[0] = 0
return null
}
let size: i64 = n / 2 + 1
let mut sieve: ptr<i8> = calloc(size, 1)
for i in 0..size {
sieve[i] = 1
}
sieve[0] = 0
let limit: i64 = (sqrt(n as f64)) as i64
for i in 1..(limit / 2 + 1) {
if sieve[i] != 0 {
let p: i64 = 2 * i + 1
let mut j: i64 = (p * p) / 2
while j < size {
sieve[j] = 0
j = j + p
}
}
}
let mut cnt: i64 = 1
for i in 1..size {
if sieve[i] != 0 { cnt = cnt + 1 }
}
let mut primes: ptr<i64> = calloc(cnt, 8)
primes[0] = 2
let mut k: i64 = 1
for i in 1..size {
if sieve[i] != 0 {
primes[k] = 2 * i + 1
k = k + 1
}
}
if primes[k - 1] > n { k = k - 1 }
free(sieve)
out_count[0] = k
return primes
}
function v_p_factorial(n: i64, p: i64) -> i64 {
let mut e: i64 = 0
let mut pp: i64 = p
while pp <= n {
e = e + n / pp
if pp > n / p { break }
pp = pp * p
}
return e
}
function apply_exp(D: ptr<i64>, e: i64, c: i64) -> void {
for k in 1..(e + 1) {
let mult: i64 = (e / k) + 1
let mut m: i64 = 1
for i in 0..c {
m = ((m as i128) * (mult as i128) % (MOD as i128)) as i64
}
D[k] = ((D[k] as i128) * (m as i128) % (MOD as i128)) as i64
}
}
function total_roundness_factorial(n: i64) -> i64 {
if n <= 1 { return 0 }
let emax: i64 = v_p_factorial(n, 2)
let mut D: ptr<i64> = calloc(emax + 1, 8)
for i in 0..(emax + 1) {
D[i] = 1
}
let mut pc: i64 = 0
let mut primes: ptr<i64> = primes_upto(n, &pc)
let mut sq: i64 = (sqrt(n as f64)) as i64
while (sq + 1) * (sq + 1) <= n { sq = sq + 1 }
while sq * sq > n { sq = sq - 1 }
let mut split: i64 = 0
while split < pc && primes[split] <= sq { split = split + 1 }
for i in 0..split {
let e: i64 = v_p_factorial(n, primes[i])
apply_exp(D, e, 1)
}
let mut counts: ptr<i64> = calloc(sq + 1, 8)
for i in split..pc {
counts[n / primes[i]] = counts[n / primes[i]] + 1
}
for e in 1..(sq + 1) {
if counts[e] != 0 { apply_exp(D, e, counts[e]) }
}
let mut total: i64 = 0
for k in 1..(emax + 1) {
total = (total + D[k]) % MOD
}
free(D)
free(primes)
free(counts)
return (total - emax % MOD + MOD) % MOD
}
function main() -> i32 {
printf("%lld\n", total_roundness_factorial(10000000))
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* primes_upto_i64_ptr_i64(int64_t n, int64_t* out_count);
int64_t v_p_factorial_i64_i64(int64_t n, int64_t p);
void apply_exp_ptr_i64_i64_i64(int64_t* D, int64_t e, int64_t c);
int64_t total_roundness_factorial_i64(int64_t n);
int32_t main(void);
static const int64_t MOD = 1000000007;
int64_t* primes_upto_i64_ptr_i64(int64_t n, int64_t* out_count) {
if (n < 2) {
out_count[0] = 0;
return NULL;
}
int64_t size = (FLOW_CHECKED_DIV((n), (2)) + 1);
int8_t* sieve = (int8_t*)(calloc(size, 1));
int32_t __flow_step_1 = 1;
for (int32_t i = 0; (0 <= size) ? i < size : i > size; i += (0 <= size) ? 1 : -1) {
sieve[i] = 1;
}
sieve[0] = 0;
int64_t limit = ((int64_t)(sqrt(((double)(n)))));
int32_t __flow_step_2 = 1;
for (int32_t i = 1; (1 <= (FLOW_CHECKED_DIV((limit), (2)) + 1)) ? i < (FLOW_CHECKED_DIV((limit), (2)) + 1) : i > (FLOW_CHECKED_DIV((limit), (2)) + 1); i += (1 <= (FLOW_CHECKED_DIV((limit), (2)) + 1)) ? 1 : -1) {
if (sieve[i] != 0) {
int64_t p = ((2 * i) + 1);
int64_t j = FLOW_CHECKED_DIV(((p * p)), (2));
while (j < size) {
sieve[j] = 0;
j = (j + p);
}
}
}
int64_t cnt = 1;
int32_t __flow_step_3 = 1;
for (int32_t i = 1; (1 <= size) ? i < size : i > size; i += (1 <= size) ? 1 : -1) {
if (sieve[i] != 0) {
cnt = (cnt + 1);
}
}
int64_t* primes = (int64_t*)(calloc(cnt, 8));
primes[0] = 2;
int64_t k = 1;
int32_t __flow_step_4 = 1;
for (int32_t i = 1; (1 <= size) ? i < size : i > size; i += (1 <= size) ? 1 : -1) {
if (sieve[i] != 0) {
primes[k] = ((2 * i) + 1);
k = (k + 1);
}
}
if (primes[(k - 1)] > n) {
k = (k - 1);
}
free(sieve);
out_count[0] = k;
return primes;
}
int64_t v_p_factorial_i64_i64(int64_t n, int64_t p) {
int64_t e = 0;
int64_t pp = p;
while (pp <= n) {
e = (e + FLOW_CHECKED_DIV((n), (pp)));
if (pp > FLOW_CHECKED_DIV((n), (p))) {
break;
}
pp = (pp * p);
}
return e;
}
void apply_exp_ptr_i64_i64_i64(int64_t* D, int64_t e, int64_t c) {
int32_t __flow_step_5 = 1;
for (int32_t k = 1; (1 <= (e + 1)) ? k < (e + 1) : k > (e + 1); k += (1 <= (e + 1)) ? 1 : -1) {
int64_t mult = (FLOW_CHECKED_DIV((e), (k)) + 1);
int64_t m = 1;
int32_t __flow_step_6 = 1;
for (int32_t i = 0; (0 <= c) ? i < c : i > c; i += (0 <= c) ? 1 : -1) {
m = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(m)) * ((__int128)(mult)))), (((__int128)(MOD))))));
}
D[k] = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(D[k])) * ((__int128)(m)))), (((__int128)(MOD))))));
}
}
int64_t total_roundness_factorial_i64(int64_t n) {
if (n <= 1) {
return 0;
}
int64_t emax = v_p_factorial_i64_i64(n, 2);
int64_t* D = (int64_t*)(calloc((emax + 1), 8));
int32_t __flow_step_7 = 1;
for (int32_t i = 0; (0 <= (emax + 1)) ? i < (emax + 1) : i > (emax + 1); i += (0 <= (emax + 1)) ? 1 : -1) {
D[i] = 1;
}
int64_t pc = 0;
int64_t* primes = (int64_t*)(primes_upto_i64_ptr_i64(n, (&(pc))));
int64_t sq = ((int64_t)(sqrt(((double)(n)))));
while (((sq + 1) * (sq + 1)) <= n) {
sq = (sq + 1);
}
while ((sq * sq) > n) {
sq = (sq - 1);
}
int64_t split = 0;
while ((split < pc && primes[split] <= sq)) {
split = (split + 1);
}
int32_t __flow_step_8 = 1;
for (int32_t i = 0; (0 <= split) ? i < split : i > split; i += (0 <= split) ? 1 : -1) {
int64_t e = v_p_factorial_i64_i64(n, primes[i]);
apply_exp_ptr_i64_i64_i64(D, e, 1);
}
int64_t* counts = (int64_t*)(calloc((sq + 1), 8));
int32_t __flow_step_9 = 1;
for (int32_t i = split; (split <= pc) ? i < pc : i > pc; i += (split <= pc) ? 1 : -1) {
counts[FLOW_CHECKED_DIV((n), (primes[i]))] = (counts[FLOW_CHECKED_DIV((n), (primes[i]))] + 1);
}
int32_t __flow_step_10 = 1;
for (int32_t e = 1; (1 <= (sq + 1)) ? e < (sq + 1) : e > (sq + 1); e += (1 <= (sq + 1)) ? 1 : -1) {
if (counts[e] != 0) {
apply_exp_ptr_i64_i64_i64(D, e, counts[e]);
}
}
int64_t total = 0;
int32_t __flow_step_11 = 1;
for (int32_t k = 1; (1 <= (emax + 1)) ? k < (emax + 1) : k > (emax + 1); k += (1 <= (emax + 1)) ? 1 : -1) {
total = FLOW_CHECKED_MOD(((total + D[k])), (MOD));
}
free(D);
free(primes);
free(counts);
return FLOW_CHECKED_MOD((((total - FLOW_CHECKED_MOD((emax), (MOD))) + MOD)), (MOD));
}
int32_t main(void) {
printf("%lld\n", total_roundness_factorial_i64(10000000));
return 0;
}