Mirror Power Sequence: D(10^18) = sum C(n) for n=2..10^18. Valid n = t^(e^a) + t^(e^b) with t>1 primitive (not an e-th power), 0≤a<b. For b=1: count primitive t with t+t^e ≤ N via integer roots. For b≥2: enumerate t (search space is tiny since t^(e^b) grows fast).
# Project Euler 617
# Mirror Power Sequence: D(10^18) = sum C(n) for n=2..10^18.
# Valid n = t^(e^a) + t^(e^b) with t>1 primitive (not an e-th power), 0≤a<b.
# For b=1: count primitive t with t+t^e ≤ N via integer roots.
# For b≥2: enumerate t (search space is tiny since t^(e^b) grows fast).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
# min(base^exp, cap+1) with early stop
function pow_capped(base0: i64, exp0: i64, cap: i64) -> i64 {
let mut result: i128 = 1 as i128
let mut a: i128 = base0 as i128
let mut e: i64 = exp0
let cap128: i128 = cap as i128
while e > 0 {
if e % 2 == 1 {
result = result * a
if result > cap128 { return cap + 1 }
}
e = e / 2
if e > 0 {
a = a * a
if a > cap128 { a = cap128 + 1 }
}
}
return result as i64
}
# floor(n^(1/k)) via binary search
function int_nth_root(n: i64, k: i64) -> i64 {
if k <= 1 { return n }
if n < 2 { return n }
let mut lo: i64 = 1
let mut hi: i64 = 2
while pow_capped(hi, k, n) <= n { hi = hi * 2 }
while lo + 1 < hi {
let mid: i64 = (lo + hi) / 2
if pow_capped(mid, k, n) <= n { lo = mid }
if pow_capped(mid, k, n) > n { hi = mid }
}
return lo
}
# Largest t with t + t^e ≤ N
function max_t_for_b1(N: i64, e: i64) -> i64 {
if N < 4 { return 1 }
let mut hi: i64 = int_nth_root(N, e) + 1
let mut lo: i64 = 1
while lo + 1 < hi {
let mid: i64 = (lo + hi) / 2
if mid + pow_capped(mid, e, N) <= N { lo = mid }
if mid + pow_capped(mid, e, N) > N { hi = mid }
}
return lo
}
function ipow(base0: i64, exp0: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base0
let mut e: i64 = exp0
while e > 0 {
if e % 2 == 1 { r = r * b }
b = b * b
e = e / 2
}
return r
}
function count_for_exponent(N: i64, e: i64) -> i64 {
let mut total: i64 = 0
# b=1: n = t + t^e, count primitive t (not exact e-th powers)
let t_max: i64 = max_t_for_b1(N, e)
total = total + t_max - int_nth_root(t_max, e)
if N <= 4 { return total }
# b≥2: enumerate. Find max b such that 2^(e^b) ≤ N-2
# max_exp = floor(log2(N-2))
let mut max_exp: i64 = 0
let mut tmp: i64 = N - 2
while tmp > 1 { max_exp = max_exp + 1; tmp = tmp / 2 }
# Collect valid b values
let MAXB: i64 = 64
let bs: ptr<i64> = calloc(MAXB, 8)
if bs == null { return -1 }
let mut nbs: i64 = 0
let mut b: i64 = 2
while nbs < MAXB {
let exp: i64 = ipow(e, b)
if exp > max_exp { break }
if 2 + ((1 as i64) << exp) > N { break }
bs[nbs] = b
nbs = nbs + 1
b = b + 1
}
if nbs == 0 { free(bs); return total }
# Precompute non-primitive set (exact e-th powers up to max t)
let mut max_t_limit: i64 = 0
for bi in 0..nbs {
let exp: i64 = ipow(e, bs[bi])
let tl: i64 = int_nth_root(N - 2, exp)
if tl > max_t_limit { max_t_limit = tl }
}
let u_max: i64 = int_nth_root(max_t_limit, e)
# Mark e-th powers in a set (use a hash or array)
let non_prim: ptr<i8> = calloc(max_t_limit + 1, 1)
if non_prim == null { free(bs); return -1 }
for u in 2..(u_max + 1) {
let pk: i64 = ipow(u, e)
if pk <= max_t_limit { non_prim[pk] = 1 }
}
# For each b, enumerate primitive t
for bi in 0..nbs {
let bval: i64 = bs[bi]
let exp: i64 = ipow(e, bval)
let t_limit: i64 = int_nth_root(N - 2, exp)
for t in 2..(t_limit + 1) {
if non_prim[t] == 0 {
# Build power tower p[0..b], p[k] = t^(e^k)
# Count valid a in [0..b-1] with p[a] + p[b] ≤ N
let top: i64 = pow_capped(t, exp, N)
let mut pa: i64 = 1 # p[0] = t^1 = t
let mut ek: i64 = 1 # e^0 = 1
for a in 0..bval {
if pa + top <= N {
total = total + bval
}
ek = ek * e
pa = pow_capped(t, ek, N)
}
}
}
}
free(non_prim)
free(bs)
return total
}
function main() -> i32 {
let N: i64 = 1000000000000000000
let mut total: i64 = 0
let mut e: i64 = 2
while 2 + ((1 as i64) << e) <= N {
total = total + count_for_exponent(N, e)
e = e + 1
}
printf("%lld\n", total)
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 pow_capped_i64_i64_i64(int64_t base0, int64_t exp0, int64_t cap);
int64_t int_nth_root_i64_i64(int64_t n, int64_t k);
int64_t max_t_for_b1_i64_i64(int64_t N, int64_t e);
int64_t ipow_i64_i64(int64_t base0, int64_t exp0);
int64_t count_for_exponent_i64_i64(int64_t N, int64_t e);
int32_t main(void);
int64_t pow_capped_i64_i64_i64(int64_t base0, int64_t exp0, int64_t cap) {
__int128 result = ((__int128)(1));
__int128 a = ((__int128)(base0));
int64_t e = exp0;
__int128 cap128 = ((__int128)(cap));
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
result = (result * a);
if (result > cap128) {
return (cap + 1);
}
}
e = FLOW_CHECKED_DIV((e), (2));
if (e > 0) {
a = (a * a);
if (a > cap128) {
a = (cap128 + 1);
}
}
}
return ((int64_t)(result));
}
int64_t int_nth_root_i64_i64(int64_t n, int64_t k) {
if (k <= 1) {
return n;
}
if (n < 2) {
return n;
}
int64_t lo = 1;
int64_t hi = 2;
while (pow_capped_i64_i64_i64(hi, k, n) <= n) {
hi = (hi * 2);
}
while ((lo + 1) < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (pow_capped_i64_i64_i64(mid, k, n) <= n) {
lo = mid;
}
if (pow_capped_i64_i64_i64(mid, k, n) > n) {
hi = mid;
}
}
return lo;
}
int64_t max_t_for_b1_i64_i64(int64_t N, int64_t e) {
if (N < 4) {
return 1;
}
int64_t hi = (int_nth_root_i64_i64(N, e) + 1);
int64_t lo = 1;
while ((lo + 1) < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if ((mid + pow_capped_i64_i64_i64(mid, e, N)) <= N) {
lo = mid;
}
if ((mid + pow_capped_i64_i64_i64(mid, e, N)) > N) {
hi = mid;
}
}
return lo;
}
int64_t ipow_i64_i64(int64_t base0, int64_t exp0) {
int64_t r = 1;
int64_t b = base0;
int64_t e = exp0;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = (r * b);
}
b = (b * b);
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t count_for_exponent_i64_i64(int64_t N, int64_t e) {
int64_t total = 0;
int64_t t_max = max_t_for_b1_i64_i64(N, e);
total = ((total + t_max) - int_nth_root_i64_i64(t_max, e));
if (N <= 4) {
return total;
}
int64_t max_exp = 0;
int64_t tmp = (N - 2);
while (tmp > 1) {
max_exp = (max_exp + 1);
tmp = FLOW_CHECKED_DIV((tmp), (2));
}
int64_t MAXB = 64;
int64_t* bs = (int64_t*)(calloc(MAXB, 8));
if (bs == NULL) {
return (-1);
}
int64_t nbs = 0;
int64_t b = 2;
while (nbs < MAXB) {
int64_t exp = ipow_i64_i64(e, b);
if (exp > max_exp) {
break;
}
if ((2 + FLOW_CHECKED_SHL((((int64_t)(1))), (exp))) > N) {
break;
}
bs[nbs] = b;
nbs = (nbs + 1);
b = (b + 1);
}
if (nbs == 0) {
free(bs);
return total;
}
int64_t max_t_limit = 0;
int32_t __flow_step_1 = 1;
for (int32_t bi = 0; (0 <= nbs) ? bi < nbs : bi > nbs; bi += (0 <= nbs) ? 1 : -1) {
int64_t exp = ipow_i64_i64(e, bs[bi]);
int64_t tl = int_nth_root_i64_i64((N - 2), exp);
if (tl > max_t_limit) {
max_t_limit = tl;
}
}
int64_t u_max = int_nth_root_i64_i64(max_t_limit, e);
int8_t* non_prim = (int8_t*)(calloc((max_t_limit + 1), 1));
if (non_prim == NULL) {
free(bs);
return (-1);
}
int32_t __flow_step_2 = 1;
for (int32_t u = 2; (2 <= (u_max + 1)) ? u < (u_max + 1) : u > (u_max + 1); u += (2 <= (u_max + 1)) ? 1 : -1) {
int64_t pk = ipow_i64_i64(u, e);
if (pk <= max_t_limit) {
non_prim[pk] = 1;
}
}
int32_t __flow_step_3 = 1;
for (int32_t bi = 0; (0 <= nbs) ? bi < nbs : bi > nbs; bi += (0 <= nbs) ? 1 : -1) {
int64_t bval = bs[bi];
int64_t exp = ipow_i64_i64(e, bval);
int64_t t_limit = int_nth_root_i64_i64((N - 2), exp);
int32_t __flow_step_4 = 1;
for (int32_t t = 2; (2 <= (t_limit + 1)) ? t < (t_limit + 1) : t > (t_limit + 1); t += (2 <= (t_limit + 1)) ? 1 : -1) {
if (non_prim[t] == 0) {
int64_t top = pow_capped_i64_i64_i64(t, exp, N);
int64_t pa = 1;
int64_t ek = 1;
int32_t __flow_step_5 = 1;
for (int32_t a = 0; (0 <= bval) ? a < bval : a > bval; a += (0 <= bval) ? 1 : -1) {
if ((pa + top) <= N) {
total = (total + bval);
}
ek = (ek * e);
pa = pow_capped_i64_i64_i64(t, ek, N);
}
}
}
}
free(non_prim);
free(bs);
return total;
}
int32_t main(void) {
int64_t N = 1000000000000000000;
int64_t total = 0;
int64_t e = 2;
while ((2 + FLOW_CHECKED_SHL((((int64_t)(1))), (e))) <= N) {
total = (total + count_for_exponent_i64_i64(N, e));
e = (e + 1);
}
printf("%lld\n", total);
return 0;
}