# Project Euler 266
# Pseudo-Fortunate? No - sqrt of product of primes <=190, last 16 digits of floor(sqrt(P)).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function log(x: f64) -> f64
function exp(x: f64) -> f64
}
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
if result >= mod { result = result - mod }
}
a = a + a
if a >= mod { a = a - mod }
b = b / 2
}
return result
}
function main() -> i32 {
# primes <= 190
let primes: ptr<i64> = calloc(50, 8)
let mut pc: i64 = 0
let mut n: i64 = 2
while n <= 190 {
let mut ok: bool = true
let mut d: i64 = 2
while d * d <= n {
if n % d == 0 { ok = false; break }
d = d + 1
}
if ok {
primes[pc] = n
pc = pc + 1
}
n = n + 1
}
let logs: ptr<f64> = calloc(pc, 8)
let mut log_product: f64 = 0.0
let mut i: i64 = 0
while i < pc {
logs[i] = log(primes[i] as f64)
log_product = log_product + logs[i]
i = i + 1
}
let log_root: f64 = log_product * 0.5
let half: i64 = pc / 2
let one: i64 = 1
let right_n: i64 = one << half
let right_log: ptr<f64> = calloc(right_n, 8)
let right_mask: ptr<i64> = calloc(right_n, 8)
let mut rc: i64 = 0
let mut bitmask: i64 = 0
while bitmask < right_n {
let mut log_right: f64 = 0.0
let mut pos: i64 = 0
while pos < half {
if (bitmask & (one << pos)) != 0 {
log_right = log_right + logs[pos + half]
}
pos = pos + 1
}
if log_right <= log_root {
right_log[rc] = log_right
right_mask[rc] = bitmask
rc = rc + 1
}
bitmask = bitmask + 1
}
# sort right by log (shell sort)
let mut gap: i64 = rc / 2
while gap > 0 {
i = gap
while i < rc {
let mut j: i64 = i
let key_l: f64 = right_log[i]
let key_m: i64 = right_mask[i]
while j >= gap && right_log[j - gap] > key_l {
right_log[j] = right_log[j - gap]
right_mask[j] = right_mask[j - gap]
j = j - gap
}
right_log[j] = key_l
right_mask[j] = key_m
i = i + 1
}
gap = gap / 2
}
let mut best: f64 = 0.0
let mut left_mask: i64 = 0
let mut right_best: i64 = 0
bitmask = 0
while bitmask < right_n {
let mut log_left: f64 = 0.0
let mut pos: i64 = 0
while pos < half {
if (bitmask & (one << pos)) != 0 {
log_left = log_left + logs[pos]
}
pos = pos + 1
}
let missing: f64 = log_root - log_left
# upper_bound binary search
let mut lo: i64 = 0
let mut hi: i64 = rc
while lo < hi {
let mid: i64 = (lo + hi) / 2
if right_log[mid] <= missing {
lo = mid + 1
} else {
hi = mid
}
}
if lo > 0 {
let idx: i64 = lo - 1
let cand: f64 = log_left + right_log[idx]
if cand > best {
best = cand
left_mask = bitmask
right_best = right_mask[idx]
}
}
bitmask = bitmask + 1
}
let mod: i64 = 10000000000000000
let mut result: i64 = 1
i = 0
while i < half {
if (left_mask & 1) != 0 {
result = mulmod(result, primes[i], mod)
}
left_mask = left_mask / 2
i = i + 1
}
i = half
while i < pc {
if (right_best & 1) != 0 {
result = mulmod(result, primes[i], mod)
}
right_best = right_best / 2
i = i + 1
}
printf("%lld\n", result)
free(primes); free(logs); free(right_log); free(right_mask)
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);
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 = (result + a);
if (result >= mod) {
result = (result - mod);
}
}
a = (a + a);
if (a >= mod) {
a = (a - mod);
}
b = FLOW_CHECKED_DIV((b), (2));
}
return result;
}
int32_t main(void) {
int64_t* primes = (int64_t*)(calloc(50, 8));
int64_t pc = 0;
int64_t n = 2;
while (n <= 190) {
bool ok = 1;
int64_t d = 2;
while ((d * d) <= n) {
if (FLOW_CHECKED_MOD((n), (d)) == 0) {
ok = 0;
break;
}
d = (d + 1);
}
if (ok) {
primes[pc] = n;
pc = (pc + 1);
}
n = (n + 1);
}
double* logs = (double*)(calloc(pc, 8));
double log_product = 0.0;
int64_t i = 0;
while (i < pc) {
logs[i] = log(((double)(primes[i])));
log_product = (log_product + logs[i]);
i = (i + 1);
}
double log_root = (log_product * 0.5);
int64_t half = FLOW_CHECKED_DIV((pc), (2));
int64_t one = 1;
int64_t right_n = FLOW_CHECKED_SHL((one), (half));
double* right_log = (double*)(calloc(right_n, 8));
int64_t* right_mask = (int64_t*)(calloc(right_n, 8));
int64_t rc = 0;
int64_t bitmask = 0;
while (bitmask < right_n) {
double log_right = 0.0;
int64_t pos = 0;
while (pos < half) {
if ((bitmask & FLOW_CHECKED_SHL((one), (pos))) != 0) {
log_right = (log_right + logs[(pos + half)]);
}
pos = (pos + 1);
}
if (log_right <= log_root) {
right_log[rc] = log_right;
right_mask[rc] = bitmask;
rc = (rc + 1);
}
bitmask = (bitmask + 1);
}
int64_t gap = FLOW_CHECKED_DIV((rc), (2));
while (gap > 0) {
i = gap;
while (i < rc) {
int64_t j = i;
double key_l = right_log[i];
int64_t key_m = right_mask[i];
while ((j >= gap && right_log[(j - gap)] > key_l)) {
right_log[j] = right_log[(j - gap)];
right_mask[j] = right_mask[(j - gap)];
j = (j - gap);
}
right_log[j] = key_l;
right_mask[j] = key_m;
i = (i + 1);
}
gap = FLOW_CHECKED_DIV((gap), (2));
}
double best = 0.0;
int64_t left_mask = 0;
int64_t right_best = 0;
bitmask = 0;
while (bitmask < right_n) {
double log_left = 0.0;
int64_t pos = 0;
while (pos < half) {
if ((bitmask & FLOW_CHECKED_SHL((one), (pos))) != 0) {
log_left = (log_left + logs[pos]);
}
pos = (pos + 1);
}
double missing = (log_root - log_left);
int64_t lo = 0;
int64_t hi = rc;
while (lo < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (right_log[mid] <= missing) {
lo = (mid + 1);
} else {
hi = mid;
}
}
if (lo > 0) {
int64_t idx = (lo - 1);
double cand = (log_left + right_log[idx]);
if (cand > best) {
best = cand;
left_mask = bitmask;
right_best = right_mask[idx];
}
}
bitmask = (bitmask + 1);
}
int64_t mod = 10000000000000000;
int64_t result = 1;
i = 0;
while (i < half) {
if ((left_mask & 1) != 0) {
result = mulmod_i64_i64_i64(result, primes[i], mod);
}
left_mask = FLOW_CHECKED_DIV((left_mask), (2));
i = (i + 1);
}
i = half;
while (i < pc) {
if ((right_best & 1) != 0) {
result = mulmod_i64_i64_i64(result, primes[i], mod);
}
right_best = FLOW_CHECKED_DIV((right_best), (2));
i = (i + 1);
}
printf("%lld\n", result);
free(primes);
free(logs);
free(right_log);
free(right_mask);
return 0;
}