# Project Euler 118
# How many sets of primes using digits 1-9 exactly once?
function mulmod(a0: i64, b0: i64, mod: i64) -> i64 {
let mut a: i64 = a0 % mod
let mut b: i64 = b0 % mod
let mut result: i64 = 0
while b > 0 {
if b % 2 == 1 { result = (result + a) % mod }
a = (a * 2) % mod
b = b / 2
}
return result
}
function modpow(base: i64, exp: i64, mod: i64) -> i64 {
let mut result: i64 = 1
let mut b: i64 = base % mod
let mut e: i64 = exp
while e > 0 {
if e % 2 == 1 { result = mulmod(result, b, mod) }
b = mulmod(b, b, mod)
e = e / 2
}
return result
}
function is_prime(n: i64) -> bool {
if n < 2 { return false }
if n % 2 == 0 { return n == 2 }
if n % 3 == 0 { return n == 3 }
if n % 5 == 0 { return n == 5 }
let bases: array<i64, 5>
bases[0] = 2
bases[1] = 3
bases[2] = 5
bases[3] = 7
bases[4] = 11
let mut d: i64 = n - 1
let mut s: i32 = 0
while d % 2 == 0 {
d = d / 2
s = s + 1
}
for bi in 0..5 {
let a: i64 = bases[bi] % n
if a != 0 {
let mut x: i64 = modpow(a, d, n)
if x != 1 && x != n - 1 {
let mut r: i32 = 1
let mut bad: bool = true
while r < s {
x = mulmod(x, x, n)
if x == n - 1 { bad = false; break }
r = r + 1
}
if bad { return false }
}
}
}
return true
}
# Count splits of digit array into nondecreasing prime sequence.
function count_splits(digs: ptr<i32>, start: i32, last: i64) -> i64 {
if start == 9 { return 1 }
let mut total: i64 = 0
let mut num: i64 = 0
for j in start..9 {
num = num * 10 + (digs[j] as i64)
if num >= last && is_prime(num) {
total = total + count_splits(digs, j + 1, num)
}
}
return total
}
function next_perm(a: ptr<i32>, n: i32) -> bool {
# standard next permutation
let mut i: i32 = n - 2
while i >= 0 && a[i] >= a[i + 1] {
i = i - 1
}
if i < 0 { return false }
let mut j: i32 = n - 1
while a[j] <= a[i] {
j = j - 1
}
let t: i32 = a[i]
a[i] = a[j]
a[j] = t
# reverse i+1..n-1
let mut L: i32 = i + 1
let mut R: i32 = n - 1
while L < R {
let u: i32 = a[L]
a[L] = a[R]
a[R] = u
L = L + 1
R = R - 1
}
return true
}
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let digs: ptr<i32> = calloc(9, 4)
if digs == null { return 1 }
for i in 0..9 {
digs[i] = i + 1
}
let mut total: i64 = 0
while true {
total = total + count_splits(digs, 0, 0)
if !next_perm(digs, 9) { break }
}
# Each set is counted |set|! / automorphisms? NO —
# We form ordered concatenations via permutations, and splits require
# nondecreasing primes, so each set is counted once per ordering of digits
# consistent with concatenating the primes in sorted order.
# That is exactly once per set (digits follow sorted primes concatenated).
# Wait: for a set {p1<p2<...<pk}, only ONE permutation of digits matches
# the concatenation p1|p2|...|pk. So each set counted once. Good.
printf("%lld\n", total)
free(digs)
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 mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t count_splits_ptr_i32_i32_i64(int32_t* digs, int32_t start, int64_t last);
bool next_perm_ptr_i32_i32(int32_t* a, int32_t n);
int32_t main(void);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
int64_t result = 0;
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
result = FLOW_CHECKED_MOD(((result + a)), (mod));
}
a = FLOW_CHECKED_MOD(((a * 2)), (mod));
b = FLOW_CHECKED_DIV((b), (2));
}
return result;
}
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
result = mulmod_i64_i64_i64(result, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return result;
}
bool is_prime_i64(int64_t n) {
if (n < 2) {
return 0;
}
if (FLOW_CHECKED_MOD((n), (2)) == 0) {
return n == 2;
}
if (FLOW_CHECKED_MOD((n), (3)) == 0) {
return n == 3;
}
if (FLOW_CHECKED_MOD((n), (5)) == 0) {
return n == 5;
}
int64_t bases[5];
bases[0] = 2;
bases[1] = 3;
bases[2] = 5;
bases[3] = 7;
bases[4] = 11;
int64_t d = (n - 1);
int32_t s = 0;
while (FLOW_CHECKED_MOD((d), (2)) == 0) {
d = FLOW_CHECKED_DIV((d), (2));
s = (s + 1);
}
int32_t __flow_step_1 = 1;
for (int32_t bi = 0; (0 <= 5) ? bi < 5 : bi > 5; bi += (0 <= 5) ? 1 : -1) {
int64_t a = FLOW_CHECKED_MOD(((((unsigned)(bi) < 5) ? bases[bi] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(bi), 5), flow_fault_handler("array index out of bounds"), bases[0]))), (n));
if (a != 0) {
int64_t x = modpow_i64_i64_i64(a, d, n);
if ((x != 1 && x != (n - 1))) {
int32_t r = 1;
bool bad = 1;
while (r < s) {
x = mulmod_i64_i64_i64(x, x, n);
if (x == (n - 1)) {
bad = 0;
break;
}
r = (r + 1);
}
if (bad) {
return 0;
}
}
}
}
return 1;
}
int64_t count_splits_ptr_i32_i32_i64(int32_t* digs, int32_t start, int64_t last) {
if (start == 9) {
return 1;
}
int64_t total = 0;
int64_t num = 0;
int32_t __flow_step_2 = 1;
for (int32_t j = start; (start <= 9) ? j < 9 : j > 9; j += (start <= 9) ? 1 : -1) {
num = ((num * 10) + ((int64_t)(digs[j])));
if ((num >= last && is_prime_i64(num))) {
total = (total + count_splits_ptr_i32_i32_i64(digs, (j + 1), num));
}
}
return total;
}
bool next_perm_ptr_i32_i32(int32_t* a, int32_t n) {
int32_t i = (n - 2);
while ((i >= 0 && a[i] >= a[(i + 1)])) {
i = (i - 1);
}
if (i < 0) {
return 0;
}
int32_t j = (n - 1);
while (a[j] <= a[i]) {
j = (j - 1);
}
int32_t t = a[i];
a[i] = a[j];
a[j] = t;
int32_t L = (i + 1);
int32_t R = (n - 1);
while (L < R) {
int32_t u = a[L];
a[L] = a[R];
a[R] = u;
L = (L + 1);
R = (R - 1);
}
return 1;
}
int32_t main(void) {
int32_t* digs = (int32_t*)(calloc(9, 4));
if (digs == NULL) {
return 1;
}
int32_t __flow_step_3 = 1;
for (int32_t i = 0; (0 <= 9) ? i < 9 : i > 9; i += (0 <= 9) ? 1 : -1) {
digs[i] = (i + 1);
}
int64_t total = 0;
while (1) {
total = (total + count_splits_ptr_i32_i32_i64(digs, 0, 0));
if ((!(next_perm_ptr_i32_i32(digs, 9)))) {
break;
}
}
printf("%lld\n", total);
free(digs);
return 0;
}