# Project Euler 060
# Lowest sum for a set of five primes with pairwise prime concatenations.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function mul_mod(a0: i64, b0: i64, m: i64) -> i64 {
let mut a: i64 = a0 % m
let mut b: i64 = b0
let mut r: i64 = 0
if a < 0 { a = a + m }
while b > 0 {
if b % 2 == 1 {
r = r + a
if r >= m { r = r - m }
}
a = a + a
if a >= m { a = a - m }
b = b / 2
}
return r
}
function pow_mod(a0: i64, e0: i64, m: i64) -> i64 {
let mut a: i64 = a0 % m
let mut e: i64 = e0
let mut r: i64 = 1
while e > 0 {
if e % 2 == 1 {
r = mul_mod(r, a, m)
}
a = mul_mod(a, a, m)
e = e / 2
}
return r
}
function mr_check(n: i64, a: i64) -> bool {
# write n-1 = d * 2^s
let mut d: i64 = n - 1
let mut s: i32 = 0
while d % 2 == 0 {
d = d / 2
s = s + 1
}
let mut x: i64 = pow_mod(a, d, n)
if x == 1 || x == n - 1 { return true }
let mut r: i32 = 1
while r < s {
x = mul_mod(x, x, n)
if x == n - 1 { return true }
r = r + 1
}
return false
}
function is_prime(n: i64) -> bool {
if n < 2 { return false }
if n < 4 { return true }
if n % 2 == 0 || n % 3 == 0 { return false }
if n < 1000000 {
let mut i: i64 = 5
while i * i <= n {
if n % i == 0 || n % (i + 2) == 0 { return false }
i = i + 6
}
return true
}
# deterministic MR for n < 3e9+ suffices with these witnesses
if !mr_check(n, 2) { return false }
if !mr_check(n, 3) { return false }
if !mr_check(n, 5) { return false }
if !mr_check(n, 7) { return false }
if !mr_check(n, 11) { return false }
if !mr_check(n, 13) { return false }
if !mr_check(n, 23) { return false }
return true
}
function concat(a: i64, b: i64) -> i64 {
let mut p: i64 = 1
let mut x: i64 = b
while x > 0 {
p = p * 10
x = x / 10
}
return a * p + b
}
function pair_ok(a: i64, b: i64) -> bool {
return is_prime(concat(a, b)) && is_prime(concat(b, a))
}
function dfs(depth: i32, start: i32, pc: i32, primes: ptr<i64>, ok: ptr<i8>, choose: ptr<i32>, best: ptr<i64>) -> void {
if depth == 5 {
let mut s: i64 = 0
let mut i: i32 = 0
while i < 5 {
s = s + primes[choose[i]]
i = i + 1
}
if best[0] == 0 || s < best[0] {
best[0] = s
}
return
}
let mut i: i32 = start
while i < pc {
# prune if partial sum already worse
let mut ok_all: bool = true
let mut j: i32 = 0
while j < depth {
if ok[choose[j] * pc + i] == 0 {
ok_all = false
break
}
j = j + 1
}
if ok_all {
choose[depth] = i
# if we have a best, prune by lower bound (remaining min primes)
let mut partial: i64 = 0
j = 0
while j <= depth {
partial = partial + primes[choose[j]]
j = j + 1
}
if best[0] == 0 || partial < best[0] {
dfs(depth + 1, i + 1, pc, primes, ok, choose, best)
}
}
i = i + 1
}
}
function main() -> i32 {
let limit: i64 = 9000
let sieve: ptr<i8> = calloc(limit, 1)
let primes: ptr<i64> = calloc(2000, 8)
if sieve == null || primes == null { return 1 }
sieve[0] = 1
sieve[1] = 1
let mut p: i64 = 2
while p * p < limit {
if sieve[p] == 0 {
let mut m: i64 = p * p
while m < limit {
sieve[m] = 1
m = m + p
}
}
p = p + 1
}
let mut pc: i32 = 0
p = 3
while p < limit {
if sieve[p] == 0 {
primes[pc] = p
pc = pc + 1
}
p = p + 2
}
let ok: ptr<i8> = calloc((pc * pc) as i64, 1)
if ok == null { return 1 }
let mut i: i32 = 0
while i < pc {
let mut j: i32 = i + 1
while j < pc {
if pair_ok(primes[i], primes[j]) {
ok[i * pc + j] = 1
ok[j * pc + i] = 1
}
j = j + 1
}
i = i + 1
}
let choose: ptr<i32> = calloc(5, 4)
let best: ptr<i64> = calloc(1, 8)
if choose == null || best == null { return 1 }
best[0] = 0
dfs(0, 0, pc, primes, ok, choose, best)
printf("%lld\n", best[0])
free(best)
free(choose)
free(ok)
free(primes)
free(sieve)
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_mod_i64_i64_i64(int64_t a0, int64_t b0, int64_t m);
int64_t pow_mod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m);
bool mr_check_i64_i64(int64_t n, int64_t a);
bool is_prime_i64(int64_t n);
int64_t concat_i64_i64(int64_t a, int64_t b);
bool pair_ok_i64_i64(int64_t a, int64_t b);
void dfs_i32_i32_i32_ptr_i64_ptr_i8_ptr_i32_ptr_i64(int32_t depth, int32_t start, int32_t pc, int64_t* primes, int8_t* ok, int32_t* choose, int64_t* best);
int32_t main(void);
int64_t mul_mod_i64_i64_i64(int64_t a0, int64_t b0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD((a0), (m));
int64_t b = b0;
int64_t r = 0;
if (a < 0) {
a = (a + m);
}
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
r = (r + a);
if (r >= m) {
r = (r - m);
}
}
a = (a + a);
if (a >= m) {
a = (a - m);
}
b = FLOW_CHECKED_DIV((b), (2));
}
return r;
}
int64_t pow_mod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD((a0), (m));
int64_t e = e0;
int64_t r = 1;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = mul_mod_i64_i64_i64(r, a, m);
}
a = mul_mod_i64_i64_i64(a, a, m);
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
bool mr_check_i64_i64(int64_t n, int64_t a) {
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);
}
int64_t x = pow_mod_i64_i64_i64(a, d, n);
if ((x == 1 || x == (n - 1))) {
return 1;
}
int32_t r = 1;
while (r < s) {
x = mul_mod_i64_i64_i64(x, x, n);
if (x == (n - 1)) {
return 1;
}
r = (r + 1);
}
return 0;
}
bool is_prime_i64(int64_t n) {
if (n < 2) {
return 0;
}
if (n < 4) {
return 1;
}
if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
return 0;
}
if (n < 1000000) {
int64_t i = 5;
while ((i * i) <= n) {
if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
return 0;
}
i = (i + 6);
}
return 1;
}
if ((!(mr_check_i64_i64(n, 2)))) {
return 0;
}
if ((!(mr_check_i64_i64(n, 3)))) {
return 0;
}
if ((!(mr_check_i64_i64(n, 5)))) {
return 0;
}
if ((!(mr_check_i64_i64(n, 7)))) {
return 0;
}
if ((!(mr_check_i64_i64(n, 11)))) {
return 0;
}
if ((!(mr_check_i64_i64(n, 13)))) {
return 0;
}
if ((!(mr_check_i64_i64(n, 23)))) {
return 0;
}
return 1;
}
int64_t concat_i64_i64(int64_t a, int64_t b) {
int64_t p = 1;
int64_t x = b;
while (x > 0) {
p = (p * 10);
x = FLOW_CHECKED_DIV((x), (10));
}
return ((a * p) + b);
}
bool pair_ok_i64_i64(int64_t a, int64_t b) {
return (is_prime_i64(concat_i64_i64(a, b)) && is_prime_i64(concat_i64_i64(b, a)));
}
void dfs_i32_i32_i32_ptr_i64_ptr_i8_ptr_i32_ptr_i64(int32_t depth, int32_t start, int32_t pc, int64_t* primes, int8_t* ok, int32_t* choose, int64_t* best) {
if (depth == 5) {
int64_t s = 0;
int32_t i = 0;
while (i < 5) {
s = (s + primes[choose[i]]);
i = (i + 1);
}
if ((best[0] == 0 || s < best[0])) {
best[0] = s;
}
return;
}
int32_t i = start;
while (i < pc) {
bool ok_all = 1;
int32_t j = 0;
while (j < depth) {
if (ok[((choose[j] * pc) + i)] == 0) {
ok_all = 0;
break;
}
j = (j + 1);
}
if (ok_all) {
choose[depth] = i;
int64_t partial = 0;
j = 0;
while (j <= depth) {
partial = (partial + primes[choose[j]]);
j = (j + 1);
}
if ((best[0] == 0 || partial < best[0])) {
dfs_i32_i32_i32_ptr_i64_ptr_i8_ptr_i32_ptr_i64((depth + 1), (i + 1), pc, primes, ok, choose, best);
}
}
i = (i + 1);
}
}
int32_t main(void) {
int64_t limit = 9000;
int8_t* sieve = (int8_t*)(calloc(limit, 1));
int64_t* primes = (int64_t*)(calloc(2000, 8));
if ((sieve == NULL || primes == NULL)) {
return 1;
}
sieve[0] = 1;
sieve[1] = 1;
int64_t p = 2;
while ((p * p) < limit) {
if (sieve[p] == 0) {
int64_t m = (p * p);
while (m < limit) {
sieve[m] = 1;
m = (m + p);
}
}
p = (p + 1);
}
int32_t pc = 0;
p = 3;
while (p < limit) {
if (sieve[p] == 0) {
primes[pc] = p;
pc = (pc + 1);
}
p = (p + 2);
}
int8_t* ok = (int8_t*)(calloc(((int64_t)((pc * pc))), 1));
if (ok == NULL) {
return 1;
}
int32_t i = 0;
while (i < pc) {
int32_t j = (i + 1);
while (j < pc) {
if (pair_ok_i64_i64(primes[i], primes[j])) {
ok[((i * pc) + j)] = 1;
ok[((j * pc) + i)] = 1;
}
j = (j + 1);
}
i = (i + 1);
}
int32_t* choose = (int32_t*)(calloc(5, 4));
int64_t* best = (int64_t*)(calloc(1, 8));
if ((choose == NULL || best == NULL)) {
return 1;
}
best[0] = 0;
dfs_i32_i32_i32_ptr_i64_ptr_i8_ptr_i32_ptr_i64(0, 0, pc, primes, ok, choose, best);
printf("%lld\n", best[0]);
free(best);
free(choose);
free(ok);
free(primes);
free(sieve);
return 0;
}