# Project Euler 592
# Factorial Trailing Digits II
# f(N) = odd_part(N!) * 2^{v2(N!) mod 4} mod 2^48 for N=20!, printed as 12 hex digits.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const M: i64 = 33554432
const MOD: i64 = 281474976710656
const NVAL: i64 = 2432902008176640000
const HALF: i64 = 16777216
let mut FAC: ptr<i64> = null
let mut TAB: ptr<i64> = null
function mul(A: i64, B: i64) -> i64 {
let A1: i64 = (A >> 24) & 16777215
let A2: i64 = A & 16777215
let B1: i64 = (B >> 24) & 16777215
let B2: i64 = B & 16777215
let mid: i64 = ((A1 * B2 + A2 * B1) & 16777215) << 24
return (mid + A2 * B2) & (MOD - 1)
}
function partial(a: i64, b: i64) -> i64 {
let ret: i64 = FAC[a] + mul(b, TAB[a])
return ret % MOD
}
function factorial_oddprod(n0: i64) -> i64 {
let mut n: i64 = n0 % MOD
let mut ret: i64 = 1
let mut b: i64 = 0
while b < n {
let mut a: i64 = M
if n - b < a { a = n - b }
ret = mul(ret, partial((a - 1) >> 1, b))
b = b + a
}
return ret
}
function print_hex12(v0: i64) -> void {
let hex: ptr<i8> = calloc(13, 1)
if hex == null { return }
let mut v: i64 = v0
let mut i: i64 = 11
while i >= 0 {
let d: i64 = v & 15
if d < 10 {
hex[i] = (48 + d) as i8
} else {
hex[i] = (55 + d) as i8
}
v = v >> 4
i = i - 1
}
hex[12] = 0
printf("%s\n", hex)
free(hex)
}
function main() -> i32 {
FAC = calloc(HALF, 8)
TAB = calloc(HALF, 8)
if FAC == null || TAB == null { return 1 }
FAC[0] = 1
TAB[0] = 1
let mut i: i64 = 1
while i < HALF {
let p: i64 = 2 * i + 1
FAC[i] = mul(FAC[i - 1], p)
TAB[i] = (mul(TAB[i - 1], p) + FAC[i - 1]) % MOD
i = i + 1
}
let mut ans: i64 = 1
let mut expo: i64 = 0
let mut n: i64 = NVAL
while n > 1 {
n = n >> 1
expo = expo + n
}
expo = expo % 4
i = 0
while i < expo {
ans = ans * 2
i = i + 1
}
n = NVAL
while n > 0 {
ans = mul(ans, factorial_oddprod(n))
n = n >> 1
}
print_hex12(ans)
free(FAC)
free(TAB)
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 mul_i64_i64(int64_t A, int64_t B);
int64_t partial_i64_i64(int64_t a, int64_t b);
int64_t factorial_oddprod_i64(int64_t n0);
void print_hex12_i64(int64_t v0);
int32_t main(void);
static const int64_t M = 33554432;
static const int64_t MOD = 281474976710656;
static const int64_t NVAL = 2432902008176640000;
static const int64_t HALF = 16777216;
/* Module statics */
static int64_t* FAC = NULL;
static int64_t* TAB = NULL;
int64_t mul_i64_i64(int64_t A, int64_t B) {
int64_t A1 = (FLOW_CHECKED_SHR((A), (24)) & 16777215);
int64_t A2 = (A & 16777215);
int64_t B1 = (FLOW_CHECKED_SHR((B), (24)) & 16777215);
int64_t B2 = (B & 16777215);
int64_t mid = FLOW_CHECKED_SHL(((((A1 * B2) + (A2 * B1)) & 16777215)), (24));
return ((mid + (A2 * B2)) & (MOD - 1));
}
int64_t partial_i64_i64(int64_t a, int64_t b) {
int64_t ret = (FAC[a] + mul_i64_i64(b, TAB[a]));
return FLOW_CHECKED_MOD((ret), (MOD));
}
int64_t factorial_oddprod_i64(int64_t n0) {
int64_t n = FLOW_CHECKED_MOD((n0), (MOD));
int64_t ret = 1;
int64_t b = 0;
while (b < n) {
int64_t a = M;
if ((n - b) < a) {
a = (n - b);
}
ret = mul_i64_i64(ret, partial_i64_i64(FLOW_CHECKED_SHR(((a - 1)), (1)), b));
b = (b + a);
}
return ret;
}
void print_hex12_i64(int64_t v0) {
int8_t* hex = (int8_t*)(calloc(13, 1));
if (hex == NULL) {
return;
}
int64_t v = v0;
int64_t i = 11;
while (i >= 0) {
int64_t d = (v & 15);
if (d < 10) {
hex[i] = ((int8_t)((48 + d)));
} else {
hex[i] = ((int8_t)((55 + d)));
}
v = FLOW_CHECKED_SHR((v), (4));
i = (i - 1);
}
hex[12] = 0;
printf("%s\n", hex);
free(hex);
}
int32_t main(void) {
FAC = calloc(HALF, 8);
TAB = calloc(HALF, 8);
if ((FAC == NULL || TAB == NULL)) {
return 1;
}
FAC[0] = 1;
TAB[0] = 1;
int64_t i = 1;
while (i < HALF) {
int64_t p = ((2 * i) + 1);
FAC[i] = mul_i64_i64(FAC[(i - 1)], p);
TAB[i] = FLOW_CHECKED_MOD(((mul_i64_i64(TAB[(i - 1)], p) + FAC[(i - 1)])), (MOD));
i = (i + 1);
}
int64_t ans = 1;
int64_t expo = 0;
int64_t n = NVAL;
while (n > 1) {
n = FLOW_CHECKED_SHR((n), (1));
expo = (expo + n);
}
expo = FLOW_CHECKED_MOD((expo), (4));
i = 0;
while (i < expo) {
ans = (ans * 2);
i = (i + 1);
}
n = NVAL;
while (n > 0) {
ans = mul_i64_i64(ans, factorial_oddprod_i64(n));
n = FLOW_CHECKED_SHR((n), (1));
}
print_hex12_i64(ans);
free(FAC);
free(TAB);
return 0;
}