# Project Euler 200
# 200th prime-proof sqube containing substring "200".
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function mulmod(a: i64, b: i64, mod: i64) -> i64 {
let r: i128 = ((a as i128) * (b as i128)) % (mod as i128)
return r as i64
}
function modpow(base: i64, exp: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base % mod
let mut e: i64 = exp
while e > 0 {
if e % 2 == 1 { r = mulmod(r, b, mod) }
b = mulmod(b, b, mod)
e = e / 2
}
return r
}
function is_prime(value: i64) -> bool {
if value < 2 { return false }
if value % 2 == 0 { return value == 2 }
if value % 3 == 0 { return value == 3 }
if value % 5 == 0 { return value == 5 }
if value % 7 == 0 { return value == 7 }
if value % 11 == 0 { return value == 11 }
if value % 13 == 0 { return value == 13 }
if value % 17 == 0 { return value == 17 }
if value % 19 == 0 { return value == 19 }
if value % 23 == 0 { return value == 23 }
if value % 29 == 0 { return value == 29 }
let mut d: i64 = value - 1
let mut shifts: i32 = 0
while d % 2 == 0 {
d = d / 2
shifts = shifts + 1
}
let bases: ptr<i64> = calloc(7, 8)
bases[0] = 2; bases[1] = 325; bases[2] = 9375; bases[3] = 28178
bases[4] = 450775; bases[5] = 9780504; bases[6] = 1795265022
let mut bi: i32 = 0
while bi < 7 {
let base: i64 = bases[bi] % value
if base != 0 {
let mut x: i64 = modpow(base, d, value)
if !(x == 1 || x == value - 1) {
let mut ok: bool = false
let mut r: i32 = 0
while r < shifts - 1 {
x = mulmod(x, x, value)
if x == value - 1 {
ok = true
break
}
r = r + 1
}
if !ok {
free(bases)
return false
}
}
}
bi = bi + 1
}
free(bases)
return true
}
function contains_200(value: i64) -> bool {
let mut v: i64 = value
while v >= 200 {
if v % 1000 == 200 { return true }
v = v / 10
}
return false
}
function is_prime_proof(value: i64) -> bool {
let digits: ptr<i32> = calloc(20, 4)
let powers: ptr<i64> = calloc(20, 8)
let mut len: i32 = 0
let mut rem: i64 = value
let mut power: i64 = 1
while rem > 0 {
digits[len] = (rem % 10) as i32
powers[len] = power
rem = rem / 10
power = power * 10
len = len + 1
}
let leading: i32 = len - 1
let mut pos: i32 = 0
while pos < len {
let orig: i32 = digits[pos]
let mut first: i32 = 0
if pos == leading { first = 1 }
let mut digit: i32 = first
while digit <= 9 {
if digit != orig {
if !(pos == 0 && (digit % 2 == 0 || digit == 5)) {
let modified: i64 = value + ((digit - orig) as i64) * powers[pos]
if is_prime(modified) {
free(digits); free(powers)
return false
}
}
}
digit = digit + 1
}
pos = pos + 1
}
free(digits); free(powers)
return true
}
function isqrt(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut x: i64 = n
let mut y: i64 = (x + 1) / 2
while y < x {
x = y
y = (x + n / x) / 2
}
return x
}
function main() -> i32 {
let limit: i64 = 1000000000000
let pmax: i64 = isqrt(limit / 8) + 1
let sieve: ptr<i8> = calloc(pmax + 1, 1)
if sieve == null { return 1 }
let mut i: i64 = 0
while i <= pmax {
sieve[i] = 1
i = i + 1
}
sieve[0] = 0; sieve[1] = 0
let mut p: i64 = 2
while p * p <= pmax {
if sieve[p] == 1 {
let mut m: i64 = p * p
while m <= pmax {
sieve[m] = 0
m = m + p
}
}
p = p + 1
}
let mut pc: i64 = 0
i = 2
while i <= pmax {
if sieve[i] == 1 { pc = pc + 1 }
i = i + 1
}
let primes: ptr<i64> = calloc(pc, 8)
let mut idx: i64 = 0
i = 2
while i <= pmax {
if sieve[i] == 1 {
primes[idx] = i
idx = idx + 1
}
i = i + 1
}
let squbes: ptr<i64> = calloc(5000000, 8)
let mut sc: i64 = 0
let mut ii: i64 = 0
while ii < pc {
let pp: i64 = primes[ii]
let p2: i64 = pp * pp
if p2 > limit / 8 { break }
let mut jj: i64 = 0
while jj < pc {
let qq: i64 = primes[jj]
if qq != pp {
if qq > 1000000 { break }
# q^3 may overflow; check carefully
if qq > 100000 {
# q^3 > 1e15 > limit
# still need smaller q
}
let q2: i64 = qq * qq
if q2 > limit / qq { break }
let q3: i64 = q2 * qq
if q3 > limit / p2 { break }
let value: i64 = p2 * q3
squbes[sc] = value
sc = sc + 1
}
jj = jj + 1
}
ii = ii + 1
}
# shell sort
let mut gap: i64 = sc / 2
while gap > 0 {
let mut i2: i64 = gap
while i2 < sc {
let tmp: i64 = squbes[i2]
let mut j2: i64 = i2
while j2 >= gap && squbes[j2 - gap] > tmp {
squbes[j2] = squbes[j2 - gap]
j2 = j2 - gap
}
squbes[j2] = tmp
i2 = i2 + 1
}
gap = gap / 2
}
let mut count: i64 = 0
let mut ans: i64 = 0
i = 0
while i < sc {
let v: i64 = squbes[i]
if contains_200(v) {
if is_prime_proof(v) {
count = count + 1
if count == 200 {
ans = v
break
}
}
}
i = i + 1
}
printf("%lld\n", ans)
free(sieve); free(primes); free(squbes)
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 a, int64_t b, int64_t mod);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t value);
bool contains_200_i64(int64_t value);
bool is_prime_proof_i64(int64_t value);
int64_t isqrt_i64(int64_t n);
int32_t main(void);
int64_t mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t mod) {
__int128 r = FLOW_CHECKED_MOD(((((__int128)(a)) * ((__int128)(b)))), (((__int128)(mod))));
return ((int64_t)(r));
}
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = mulmod_i64_i64_i64(r, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
bool is_prime_i64(int64_t value) {
if (value < 2) {
return 0;
}
if (FLOW_CHECKED_MOD((value), (2)) == 0) {
return value == 2;
}
if (FLOW_CHECKED_MOD((value), (3)) == 0) {
return value == 3;
}
if (FLOW_CHECKED_MOD((value), (5)) == 0) {
return value == 5;
}
if (FLOW_CHECKED_MOD((value), (7)) == 0) {
return value == 7;
}
if (FLOW_CHECKED_MOD((value), (11)) == 0) {
return value == 11;
}
if (FLOW_CHECKED_MOD((value), (13)) == 0) {
return value == 13;
}
if (FLOW_CHECKED_MOD((value), (17)) == 0) {
return value == 17;
}
if (FLOW_CHECKED_MOD((value), (19)) == 0) {
return value == 19;
}
if (FLOW_CHECKED_MOD((value), (23)) == 0) {
return value == 23;
}
if (FLOW_CHECKED_MOD((value), (29)) == 0) {
return value == 29;
}
int64_t d = (value - 1);
int32_t shifts = 0;
while (FLOW_CHECKED_MOD((d), (2)) == 0) {
d = FLOW_CHECKED_DIV((d), (2));
shifts = (shifts + 1);
}
int64_t* bases = (int64_t*)(calloc(7, 8));
bases[0] = 2;
bases[1] = 325;
bases[2] = 9375;
bases[3] = 28178;
bases[4] = 450775;
bases[5] = 9780504;
bases[6] = 1795265022;
int32_t bi = 0;
while (bi < 7) {
int64_t base = FLOW_CHECKED_MOD((bases[bi]), (value));
if (base != 0) {
int64_t x = modpow_i64_i64_i64(base, d, value);
if ((!((x == 1 || x == (value - 1))))) {
bool ok = 0;
int32_t r = 0;
while (r < (shifts - 1)) {
x = mulmod_i64_i64_i64(x, x, value);
if (x == (value - 1)) {
ok = 1;
break;
}
r = (r + 1);
}
if ((!(ok))) {
free(bases);
return 0;
}
}
}
bi = (bi + 1);
}
free(bases);
return 1;
}
bool contains_200_i64(int64_t value) {
int64_t v = value;
while (v >= 200) {
if (FLOW_CHECKED_MOD((v), (1000)) == 200) {
return 1;
}
v = FLOW_CHECKED_DIV((v), (10));
}
return 0;
}
bool is_prime_proof_i64(int64_t value) {
int32_t* digits = (int32_t*)(calloc(20, 4));
int64_t* powers = (int64_t*)(calloc(20, 8));
int32_t len = 0;
int64_t rem = value;
int64_t power = 1;
while (rem > 0) {
digits[len] = ((int32_t)(FLOW_CHECKED_MOD((rem), (10))));
powers[len] = power;
rem = FLOW_CHECKED_DIV((rem), (10));
power = (power * 10);
len = (len + 1);
}
int32_t leading = (len - 1);
int32_t pos = 0;
while (pos < len) {
int32_t orig = digits[pos];
int32_t first = 0;
if (pos == leading) {
first = 1;
}
int32_t digit = first;
while (digit <= 9) {
if (digit != orig) {
if ((!((pos == 0 && (FLOW_CHECKED_MOD((digit), (2)) == 0 || digit == 5))))) {
int64_t modified = (value + (((int64_t)((digit - orig))) * powers[pos]));
if (is_prime_i64(modified)) {
free(digits);
free(powers);
return 0;
}
}
}
digit = (digit + 1);
}
pos = (pos + 1);
}
free(digits);
free(powers);
return 1;
}
int64_t isqrt_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t x = n;
int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
while (y < x) {
x = y;
y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
}
return x;
}
int32_t main(void) {
int64_t limit = 1000000000000;
int64_t pmax = (isqrt_i64(FLOW_CHECKED_DIV((limit), (8))) + 1);
int8_t* sieve = (int8_t*)(calloc((pmax + 1), 1));
if (sieve == NULL) {
return 1;
}
int64_t i = 0;
while (i <= pmax) {
sieve[i] = 1;
i = (i + 1);
}
sieve[0] = 0;
sieve[1] = 0;
int64_t p = 2;
while ((p * p) <= pmax) {
if (sieve[p] == 1) {
int64_t m = (p * p);
while (m <= pmax) {
sieve[m] = 0;
m = (m + p);
}
}
p = (p + 1);
}
int64_t pc = 0;
i = 2;
while (i <= pmax) {
if (sieve[i] == 1) {
pc = (pc + 1);
}
i = (i + 1);
}
int64_t* primes = (int64_t*)(calloc(pc, 8));
int64_t idx = 0;
i = 2;
while (i <= pmax) {
if (sieve[i] == 1) {
primes[idx] = i;
idx = (idx + 1);
}
i = (i + 1);
}
int64_t* squbes = (int64_t*)(calloc(5000000, 8));
int64_t sc = 0;
int64_t ii = 0;
while (ii < pc) {
int64_t pp = primes[ii];
int64_t p2 = (pp * pp);
if (p2 > FLOW_CHECKED_DIV((limit), (8))) {
break;
}
int64_t jj = 0;
while (jj < pc) {
int64_t qq = primes[jj];
if (qq != pp) {
if (qq > 1000000) {
break;
}
if (qq > 100000) {
}
int64_t q2 = (qq * qq);
if (q2 > FLOW_CHECKED_DIV((limit), (qq))) {
break;
}
int64_t q3 = (q2 * qq);
if (q3 > FLOW_CHECKED_DIV((limit), (p2))) {
break;
}
int64_t value = (p2 * q3);
squbes[sc] = value;
sc = (sc + 1);
}
jj = (jj + 1);
}
ii = (ii + 1);
}
int64_t gap = FLOW_CHECKED_DIV((sc), (2));
while (gap > 0) {
int64_t i2 = gap;
while (i2 < sc) {
int64_t tmp = squbes[i2];
int64_t j2 = i2;
while ((j2 >= gap && squbes[(j2 - gap)] > tmp)) {
squbes[j2] = squbes[(j2 - gap)];
j2 = (j2 - gap);
}
squbes[j2] = tmp;
i2 = (i2 + 1);
}
gap = FLOW_CHECKED_DIV((gap), (2));
}
int64_t count = 0;
int64_t ans = 0;
i = 0;
while (i < sc) {
int64_t v = squbes[i];
if (contains_200_i64(v)) {
if (is_prime_proof_i64(v)) {
count = (count + 1);
if (count == 200) {
ans = v;
break;
}
}
}
i = (i + 1);
}
printf("%lld\n", ans);
free(sieve);
free(primes);
free(squbes);
return 0;
}