# Project Euler 111
# Sum of S(10, d) for d=0..9 — primes with maximal repeated digit d.
function mulmod(a0: i64, b0: i64, mod: i64) -> i64 {
let mut a: i64 = a0 % mod
let mut b: i64 = b0 % mod
if a < 0 { a = a + mod }
if b < 0 { b = b + 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 }
let bases: array<i64, 7>
bases[0] = 2
bases[1] = 325
bases[2] = 9375
bases[3] = 28178
bases[4] = 450775
bases[5] = 9780504
bases[6] = 1795265022
let mut d: i64 = n - 1
let mut s: i32 = 0
while d % 2 == 0 {
d = d / 2
s = s + 1
}
let mut bi: i32 = 0
while bi < 7 {
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 composite: bool = true
while r < s {
x = mulmod(x, x, n)
if x == n - 1 {
composite = false
break
}
r = r + 1
}
if composite { return false }
}
}
bi = bi + 1
}
return true
}
function popcount(x0: i32) -> i32 {
let mut x: i32 = x0
let mut c: i32 = 0
while x > 0 {
c = c + (x & 1)
x = x / 2
}
return c
}
function sum_for_digit(d: i32, N: i32) -> i64 {
let mut M: i32 = N
while M >= 0 {
let freec: i32 = N - M
let mut total: i64 = 0
let mut found: bool = false
let lim: i32 = 1 << N
let mut maxv: i32 = 1
let mut i: i32 = 0
while i < freec {
maxv = maxv * 10
i = i + 1
}
let mut mask: i32 = 0
while mask < lim {
if popcount(mask) == freec {
let mut v: i32 = 0
while v < maxv {
let mut num: i64 = 0
let mut vv: i32 = v
let mut ok: bool = true
let mut pos: i32 = 0
while pos < N {
let mut digit: i32 = d
if ((mask >> pos) & 1) == 1 {
digit = vv % 10
vv = vv / 10
if digit == d {
ok = false
break
}
}
if pos == 0 && digit == 0 {
ok = false
break
}
num = num * 10 + (digit as i64)
pos = pos + 1
}
if ok && is_prime(num) {
total = total + num
found = true
}
v = v + 1
}
}
mask = mask + 1
}
if found {
return total
}
M = M - 1
}
return 0
}
function main() -> i32 {
let N: i32 = 10
let mut ans: i64 = 0
let mut d: i32 = 0
while d <= 9 {
ans = ans + sum_for_digit(d, N)
d = d + 1
}
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 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);
int32_t popcount_i32(int32_t x0);
int64_t sum_for_digit_i32_i32(int32_t d, 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));
if (a < 0) {
a = (a + mod);
}
if (b < 0) {
b = (b + 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;
}
int64_t bases[7];
bases[0] = 2;
bases[1] = 325;
bases[2] = 9375;
bases[3] = 28178;
bases[4] = 450775;
bases[5] = 9780504;
bases[6] = 1795265022;
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 bi = 0;
while (bi < 7) {
int64_t a = FLOW_CHECKED_MOD(((((unsigned)(bi) < 7) ? bases[bi] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(bi), 7), 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 composite = 1;
while (r < s) {
x = mulmod_i64_i64_i64(x, x, n);
if (x == (n - 1)) {
composite = 0;
break;
}
r = (r + 1);
}
if (composite) {
return 0;
}
}
}
bi = (bi + 1);
}
return 1;
}
int32_t popcount_i32(int32_t x0) {
int32_t x = x0;
int32_t c = 0;
while (x > 0) {
c = (c + (x & 1));
x = FLOW_CHECKED_DIV((x), (2));
}
return c;
}
int64_t sum_for_digit_i32_i32(int32_t d, int32_t N) {
int32_t M = N;
while (M >= 0) {
int32_t freec = (N - M);
int64_t total = 0;
bool found = 0;
int32_t lim = FLOW_CHECKED_SHL((1), (N));
int32_t maxv = 1;
int32_t i = 0;
while (i < freec) {
maxv = (maxv * 10);
i = (i + 1);
}
int32_t mask = 0;
while (mask < lim) {
if (popcount_i32(mask) == freec) {
int32_t v = 0;
while (v < maxv) {
int64_t num = 0;
int32_t vv = v;
bool ok = 1;
int32_t pos = 0;
while (pos < N) {
int32_t digit = d;
if ((FLOW_CHECKED_SHR((mask), (pos)) & 1) == 1) {
digit = FLOW_CHECKED_MOD((vv), (10));
vv = FLOW_CHECKED_DIV((vv), (10));
if (digit == d) {
ok = 0;
break;
}
}
if ((pos == 0 && digit == 0)) {
ok = 0;
break;
}
num = ((num * 10) + ((int64_t)(digit)));
pos = (pos + 1);
}
if ((ok && is_prime_i64(num))) {
total = (total + num);
found = 1;
}
v = (v + 1);
}
}
mask = (mask + 1);
}
if (found) {
return total;
}
M = (M - 1);
}
return 0;
}
int32_t main(void) {
int32_t N = 10;
int64_t ans = 0;
int32_t d = 0;
while (d <= 9) {
ans = (ans + sum_for_digit_i32_i32(d, N));
d = (d + 1);
}
printf("%lld\n", ans);
return 0;
}