# Project Euler 172
# 18-digit numbers where no digit occurs more than 3 times.
function multinomial_div(n: i64, parts: ptr<i32>, nparts: i32) -> i64 {
# Compute n! / (p0! p1! ... ) carefully
let mut result: i64 = 1
let mut denom_counts: array<i32, 20> = [
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
]
let mut i: i32 = 0
while i < nparts {
let p: i32 = parts[i]
let mut k: i32 = 2
while k <= p {
denom_counts[k] = denom_counts[k] + 1
k = k + 1
}
i = i + 1
}
# Expand factorials into prime... simpler: multiply 1..n dividing when divisible
let mut next: i64 = 2
while next <= n {
result = result * next
# divide out any pending factors
let mut d: i32 = 2
while d <= 18 {
while denom_counts[d] > 0 && result % (d as i64) == 0 {
result = result / (d as i64)
denom_counts[d] = denom_counts[d] - 1
}
d = d + 1
}
next = next + 1
}
return result
}
function count_ways(freqs: ptr<i32>) -> i64 {
let mut histogram: array<i32, 4> = [0, 0, 0, 0]
let mut i: i32 = 0
while i < 10 {
histogram[freqs[i]] = histogram[freqs[i]] + 1
i = i + 1
}
# 10! / (h0! h1! h2! h3!)
let mut hist_parts: array<i32, 4> = [histogram[0], histogram[1], histogram[2], histogram[3]]
let ways_assign: i64 = multinomial_div(10, hist_parts, 4)
# 18! / (f0! ... f9!)
let ways_perm: i64 = multinomial_div(18, freqs, 10)
return ways_assign * ways_perm
}
function partition(sum: i32, maxv: i32, terms: ptr<i32>, len: i32) -> i64 {
if len == 10 {
if sum == 0 {
return count_ways(terms)
}
return 0
}
let mut result: i64 = 0
let mut lim: i32 = maxv
if sum < lim { lim = sum }
let mut i: i32 = lim
while i >= 0 {
terms[len] = i
result = result + partition(sum - i, i, terms, len + 1)
i = i - 1
}
return result
}
function main() -> i32 {
let mut terms: array<i32, 10> = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
let ans0: i64 = partition(18, 3, terms, 0)
let ans: i64 = ans0 * 9 / 10
printf("%lld\n", ans)
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 multinomial_div_i64_ptr_i32_i32(int64_t n, int32_t* parts, int32_t nparts);
int64_t count_ways_ptr_i32(int32_t* freqs);
int64_t partition_i32_i32_ptr_i32_i32(int32_t sum, int32_t maxv, int32_t* terms, int32_t len);
int32_t main(void);
int64_t multinomial_div_i64_ptr_i32_i32(int64_t n, int32_t* parts, int32_t nparts) {
int64_t result = 1;
int32_t denom_counts[20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int32_t i = 0;
while (i < nparts) {
int32_t p = parts[i];
int32_t k = 2;
while (k <= p) {
denom_counts[k] = ((((unsigned)(k) < 20) ? denom_counts[k] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(k), 20), flow_fault_handler("array index out of bounds"), denom_counts[0])) + 1);
k = (k + 1);
}
i = (i + 1);
}
int64_t next = 2;
while (next <= n) {
result = (result * next);
int32_t d = 2;
while (d <= 18) {
while (((((unsigned)(d) < 20) ? denom_counts[d] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(d), 20), flow_fault_handler("array index out of bounds"), denom_counts[0])) > 0 && FLOW_CHECKED_MOD((result), (((int64_t)(d)))) == 0)) {
result = FLOW_CHECKED_DIV((result), (((int64_t)(d))));
denom_counts[d] = ((((unsigned)(d) < 20) ? denom_counts[d] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(d), 20), flow_fault_handler("array index out of bounds"), denom_counts[0])) - 1);
}
d = (d + 1);
}
next = (next + 1);
}
return result;
}
int64_t count_ways_ptr_i32(int32_t* freqs) {
int32_t histogram[4] = { 0, 0, 0, 0 };
int32_t i = 0;
while (i < 10) {
histogram[freqs[i]] = ((((unsigned)(freqs[i]) < 4) ? histogram[freqs[i]] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(freqs[i]), 4), flow_fault_handler("array index out of bounds"), histogram[0])) + 1);
i = (i + 1);
}
int32_t hist_parts[4] = { (((unsigned)(0) < 4) ? histogram[0] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(0), 4), flow_fault_handler("array index out of bounds"), histogram[0])), (((unsigned)(1) < 4) ? histogram[1] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(1), 4), flow_fault_handler("array index out of bounds"), histogram[0])), (((unsigned)(2) < 4) ? histogram[2] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(2), 4), flow_fault_handler("array index out of bounds"), histogram[0])), (((unsigned)(3) < 4) ? histogram[3] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(3), 4), flow_fault_handler("array index out of bounds"), histogram[0])) };
int64_t ways_assign = multinomial_div_i64_ptr_i32_i32(10, hist_parts, 4);
int64_t ways_perm = multinomial_div_i64_ptr_i32_i32(18, freqs, 10);
return (ways_assign * ways_perm);
}
int64_t partition_i32_i32_ptr_i32_i32(int32_t sum, int32_t maxv, int32_t* terms, int32_t len) {
if (len == 10) {
if (sum == 0) {
return count_ways_ptr_i32(terms);
}
return 0;
}
int64_t result = 0;
int32_t lim = maxv;
if (sum < lim) {
lim = sum;
}
int32_t i = lim;
while (i >= 0) {
terms[len] = i;
result = (result + partition_i32_i32_ptr_i32_i32((sum - i), i, terms, (len + 1)));
i = (i - 1);
}
return result;
}
int32_t main(void) {
int32_t terms[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int64_t ans0 = partition_i32_i32_ptr_i32_i32(18, 3, terms, 0);
int64_t ans = FLOW_CHECKED_DIV(((ans0 * 9)), (10));
printf("%lld\n", ans);
return 0;
}