# Project Euler 773
# Riffle Shuffles: F(97) mod 1_000_000_007.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1000000007
function modpow(b: i64, e: i64) -> i64 {
let mut r: i64 = 1
let mut bb: i64 = b % MOD
let mut ee: i64 = e
while ee > 0 {
if ee % 2 == 1 {
r = r * bb % MOD
}
bb = bb * bb % MOD
ee = ee / 2
}
return r
}
function is_prime_flow(n: i32) -> bool {
if n < 2 { return false }
let mut i: i32 = 2
while i * i <= n {
if n % i == 0 { return false }
i = i + 1
}
return true
}
function main() -> i32 {
let k: i32 = 97
let primes: ptr<i64> = calloc(k as i64, 8)
let mut cnt: i32 = 0
let mut x: i32 = 7
while cnt < k {
if x % 10 == 7 && is_prime_flow(x) {
primes[cnt] = x as i64
cnt = cnt + 1
}
x = x + 2
}
let mut M_mod: i64 = 1
let mut phi_mod: i64 = 1
let mut i: i32 = 0
while i < k {
M_mod = M_mod * primes[i] % MOD
phi_mod = phi_mod * (primes[i] - 1) % MOD
i = i + 1
}
let q_table: array<i64, 4> = [7, 1, 3, 9]
let mut A: i64 = 0
let mut c: i64 = 1
let mut s: i32 = 0
while s <= k {
let term: i64 = c * q_table[s % 4] % MOD
if s % 2 == 1 {
A = (A - term) % MOD
} else {
A = (A + term) % MOD
}
if s < k {
c = c * ((k - s) as i64) % MOD
c = c * modpow((s + 1) as i64, MOD - 2) % MOD
}
s = s + 1
}
if A < 0 {
A = A + MOD
}
let result: i64 = M_mod * ((A + 5 * phi_mod) % MOD) % MOD
printf("%lld\n", result)
free(primes)
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(int64_t b, int64_t e);
bool is_prime_flow_i32(int32_t n);
int32_t main(void);
static const int64_t MOD = 1000000007;
int64_t modpow_i64_i64(int64_t b, int64_t e) {
int64_t r = 1;
int64_t bb = FLOW_CHECKED_MOD((b), (MOD));
int64_t ee = e;
while (ee > 0) {
if (FLOW_CHECKED_MOD((ee), (2)) == 1) {
r = FLOW_CHECKED_MOD(((r * bb)), (MOD));
}
bb = FLOW_CHECKED_MOD(((bb * bb)), (MOD));
ee = FLOW_CHECKED_DIV((ee), (2));
}
return r;
}
bool is_prime_flow_i32(int32_t n) {
if (n < 2) {
return 0;
}
int32_t i = 2;
while ((i * i) <= n) {
if (FLOW_CHECKED_MOD((n), (i)) == 0) {
return 0;
}
i = (i + 1);
}
return 1;
}
int32_t main(void) {
int32_t k = 97;
int64_t* primes = (int64_t*)(calloc(((int64_t)(k)), 8));
int32_t cnt = 0;
int32_t x = 7;
while (cnt < k) {
if ((FLOW_CHECKED_MOD((x), (10)) == 7 && is_prime_flow_i32(x))) {
primes[cnt] = ((int64_t)(x));
cnt = (cnt + 1);
}
x = (x + 2);
}
int64_t M_mod = 1;
int64_t phi_mod = 1;
int32_t i = 0;
while (i < k) {
M_mod = FLOW_CHECKED_MOD(((M_mod * primes[i])), (MOD));
phi_mod = FLOW_CHECKED_MOD(((phi_mod * (primes[i] - 1))), (MOD));
i = (i + 1);
}
int64_t q_table[4] = { 7, 1, 3, 9 };
int64_t A = 0;
int64_t c = 1;
int32_t s = 0;
while (s <= k) {
int64_t term = FLOW_CHECKED_MOD(((c * (((unsigned)(FLOW_CHECKED_MOD((s), (4))) < 4) ? q_table[FLOW_CHECKED_MOD((s), (4))] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(FLOW_CHECKED_MOD((s), (4))), 4), flow_fault_handler("array index out of bounds"), q_table[0])))), (MOD));
if (FLOW_CHECKED_MOD((s), (2)) == 1) {
A = FLOW_CHECKED_MOD(((A - term)), (MOD));
} else {
A = FLOW_CHECKED_MOD(((A + term)), (MOD));
}
if (s < k) {
c = FLOW_CHECKED_MOD(((c * ((int64_t)((k - s))))), (MOD));
c = FLOW_CHECKED_MOD(((c * modpow_i64_i64(((int64_t)((s + 1))), (MOD - 2)))), (MOD));
}
s = (s + 1);
}
if (A < 0) {
A = (A + MOD);
}
int64_t result = FLOW_CHECKED_MOD(((M_mod * FLOW_CHECKED_MOD(((A + (5 * phi_mod))), (MOD)))), (MOD));
printf("%lld\n", result);
free(primes);
return 0;
}