# Project Euler 578
# Integers with Decreasing Prime Powers — C(10^13) via Möbius squarefree tails.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
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
}
const SIEVE_LIMIT: i64 = 31622781
const MCAP: i64 = 16000057
let mut PRIMES: ptr<i64> = null
let mut NPR: i64 = 0
let mut PREF: ptr<i32> = null
let mut QK: ptr<i64> = null
let mut QV: ptr<i64> = null
let mut QU: ptr<i8> = null
let mut FK: ptr<i64> = null
let mut FV: ptr<i64> = null
let mut FU: ptr<i8> = null
let mut CK: ptr<i64> = null
let mut CV: ptr<i64> = null
let mut CU: ptr<i8> = null
function build_sieve() -> void {
let n: i64 = SIEVE_LIMIT
let lp: ptr<i32> = calloc(n + 1, 4)
let mu: ptr<i8> = calloc(n + 1, 1)
PRIMES = calloc(n / 5, 8)
NPR = 0
mu[1] = 1
let mut i: i64 = 2
while i <= n {
if lp[i] == 0 {
lp[i] = i as i32
PRIMES[NPR] = i
NPR = NPR + 1
mu[i] = 0 - 1
}
let mut j: i64 = 0
while j < NPR {
let p: i64 = PRIMES[j]
let ip: i64 = i * p
if ip > n { break }
lp[ip] = p as i32
if p == (lp[i] as i64) {
mu[ip] = 0
break
}
mu[ip] = 0 - mu[i]
j = j + 1
}
i = i + 1
}
PREF = calloc(n + 1, 4)
let mut s: i32 = 0
i = 1
while i <= n {
s = s + (mu[i] as i32)
PREF[i] = s
i = i + 1
}
free(lp); free(mu)
QK = calloc(MCAP, 8); QV = calloc(MCAP, 8); QU = calloc(MCAP, 1)
FK = calloc(MCAP, 8); FV = calloc(MCAP, 8); FU = calloc(MCAP, 1)
CK = calloc(MCAP, 8); CV = calloc(MCAP, 8); CU = calloc(MCAP, 1)
}
function memo_get(keys: ptr<i64>, vals: ptr<i64>, used: ptr<i8>, key: i64, out: ptr<i64>) -> i64 {
let mut h: i64 = key % MCAP
if h < 0 { h = -h }
let mut p: i64 = 0
while p < 10000 {
if used[h] == 0 { return 0 }
if keys[h] == key {
out[0] = vals[h]
return 1
}
h = h + 1
if h >= MCAP { h = 0 }
p = p + 1
}
return 0
}
function memo_put(keys: ptr<i64>, vals: ptr<i64>, used: ptr<i8>, key: i64, val: i64) -> void {
let mut h: i64 = key % MCAP
if h < 0 { h = -h }
let mut p: i64 = 0
while p < 10000 {
if used[h] == 0 {
used[h] = 1
keys[h] = key
vals[h] = val
return
}
if keys[h] == key {
vals[h] = val
return
}
h = h + 1
if h >= MCAP { h = 0 }
p = p + 1
}
}
function squarefree_upto(x: i64) -> i64 {
if x <= 0 { return 0 }
let out: ptr<i64> = calloc(1, 8)
if memo_get(QK, QV, QU, x, out) != 0 {
let v: i64 = out[0]
free(out)
return v
}
let r: i64 = isqrt(x)
let mut res: i64 = 0
let mut i: i64 = 1
while i <= r {
let t: i64 = x / (i * i)
let j: i64 = isqrt(x / t)
res = res + t * ((PREF[j] - PREF[i - 1]) as i64)
i = j + 1
}
memo_put(QK, QV, QU, x, res)
free(out)
return res
}
function pack2(a: i64, b: i64) -> i64 {
return (a << 16) ^ b
}
function squarefree_min(x: i64, start_idx: i64) -> i64 {
if x <= 0 { return 0 }
if x == 1 { return 1 }
if start_idx == 0 { return squarefree_upto(x) }
if start_idx < NPR && PRIMES[start_idx] > x { return 1 }
let key: i64 = pack2(x, start_idx)
let out: ptr<i64> = calloc(1, 8)
if memo_get(FK, FV, FU, key, out) != 0 {
let v: i64 = out[0]
free(out)
return v
}
let mut total: i64 = squarefree_upto(x)
let mut i: i64 = 0
while i < start_idx {
let p: i64 = PRIMES[i]
if p > x { break }
total = total - squarefree_min(x / p, i + 1)
i = i + 1
}
memo_put(FK, FV, FU, key, total)
free(out)
return total
}
function pack3(a: i64, b: i64, c: i64) -> i64 {
return ((a * 1315423911) ^ (b * 2654435761) ^ (c * 97531))
}
function count_dpowers(limit: i64, start_idx: i64, max_exp: i64) -> i64 {
if limit <= 0 { return 0 }
if limit == 1 { return 1 }
if max_exp <= 1 { return squarefree_min(limit, start_idx) }
let key: i64 = pack3(limit, start_idx, max_exp)
let out: ptr<i64> = calloc(1, 8)
if memo_get(CK, CV, CU, key, out) != 0 {
let v: i64 = out[0]
free(out)
return v
}
let mut res: i64 = squarefree_min(limit, start_idx)
let mut i: i64 = start_idx
while i < NPR {
let p: i64 = PRIMES[i]
let p2: i64 = p * p
if p2 > limit { break }
let mut pe: i64 = p2
let mut e: i64 = 2
while e <= max_exp && pe <= limit {
res = res + count_dpowers(limit / pe, i + 1, e)
e = e + 1
if pe > limit / p { break }
pe = pe * p
}
i = i + 1
}
memo_put(CK, CV, CU, key, res)
free(out)
return res
}
function max_exp(n: i64) -> i64 {
let mut e: i64 = 0
let mut v: i64 = 1
while v * 2 <= n {
v = v * 2
e = e + 1
}
return e
}
function main() -> i32 {
build_sieve()
let n: i64 = 10000000000000
printf("%lld\n", count_dpowers(n, 0, max_exp(n)))
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 isqrt_i64(int64_t n);
void build_sieve(void);
int64_t memo_get_ptr_i64_ptr_i64_ptr_i8_i64_ptr_i64(int64_t* keys, int64_t* vals, int8_t* used, int64_t key, int64_t* out);
void memo_put_ptr_i64_ptr_i64_ptr_i8_i64_i64(int64_t* keys, int64_t* vals, int8_t* used, int64_t key, int64_t val);
int64_t squarefree_upto_i64(int64_t x);
int64_t pack2_i64_i64(int64_t a, int64_t b);
int64_t squarefree_min_i64_i64(int64_t x, int64_t start_idx);
int64_t pack3_i64_i64_i64(int64_t a, int64_t b, int64_t c);
int64_t count_dpowers_i64_i64_i64(int64_t limit, int64_t start_idx, int64_t max_exp);
int64_t max_exp_i64(int64_t n);
int32_t main(void);
static const int64_t SIEVE_LIMIT = 31622781;
static const int64_t MCAP = 16000057;
/* Module statics */
static int64_t* PRIMES = NULL;
static int64_t NPR = 0;
static int32_t* PREF = NULL;
static int64_t* QK = NULL;
static int64_t* QV = NULL;
static int8_t* QU = NULL;
static int64_t* FK = NULL;
static int64_t* FV = NULL;
static int8_t* FU = NULL;
static int64_t* CK = NULL;
static int64_t* CV = NULL;
static int8_t* CU = NULL;
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;
}
void build_sieve(void) {
int64_t n = SIEVE_LIMIT;
int32_t* lp = (int32_t*)(calloc((n + 1), 4));
int8_t* mu = (int8_t*)(calloc((n + 1), 1));
PRIMES = calloc(FLOW_CHECKED_DIV((n), (5)), 8);
NPR = 0;
mu[1] = 1;
int64_t i = 2;
while (i <= n) {
if (lp[i] == 0) {
lp[i] = ((int32_t)(i));
PRIMES[NPR] = i;
NPR = (NPR + 1);
mu[i] = (0 - 1);
}
int64_t j = 0;
while (j < NPR) {
int64_t p = PRIMES[j];
int64_t ip = (i * p);
if (ip > n) {
break;
}
lp[ip] = ((int32_t)(p));
if (p == ((int64_t)(lp[i]))) {
mu[ip] = 0;
break;
}
mu[ip] = (0 - mu[i]);
j = (j + 1);
}
i = (i + 1);
}
PREF = calloc((n + 1), 4);
int32_t s = 0;
i = 1;
while (i <= n) {
s = (s + ((int32_t)(mu[i])));
PREF[i] = s;
i = (i + 1);
}
free(lp);
free(mu);
QK = calloc(MCAP, 8);
QV = calloc(MCAP, 8);
QU = calloc(MCAP, 1);
FK = calloc(MCAP, 8);
FV = calloc(MCAP, 8);
FU = calloc(MCAP, 1);
CK = calloc(MCAP, 8);
CV = calloc(MCAP, 8);
CU = calloc(MCAP, 1);
}
int64_t memo_get_ptr_i64_ptr_i64_ptr_i8_i64_ptr_i64(int64_t* keys, int64_t* vals, int8_t* used, int64_t key, int64_t* out) {
int64_t h = FLOW_CHECKED_MOD((key), (MCAP));
if (h < 0) {
h = (-h);
}
int64_t p = 0;
while (p < 10000) {
if (used[h] == 0) {
return 0;
}
if (keys[h] == key) {
out[0] = vals[h];
return 1;
}
h = (h + 1);
if (h >= MCAP) {
h = 0;
}
p = (p + 1);
}
return 0;
}
void memo_put_ptr_i64_ptr_i64_ptr_i8_i64_i64(int64_t* keys, int64_t* vals, int8_t* used, int64_t key, int64_t val) {
int64_t h = FLOW_CHECKED_MOD((key), (MCAP));
if (h < 0) {
h = (-h);
}
int64_t p = 0;
while (p < 10000) {
if (used[h] == 0) {
used[h] = 1;
keys[h] = key;
vals[h] = val;
return;
}
if (keys[h] == key) {
vals[h] = val;
return;
}
h = (h + 1);
if (h >= MCAP) {
h = 0;
}
p = (p + 1);
}
}
int64_t squarefree_upto_i64(int64_t x) {
if (x <= 0) {
return 0;
}
int64_t* out = (int64_t*)(calloc(1, 8));
if (memo_get_ptr_i64_ptr_i64_ptr_i8_i64_ptr_i64(QK, QV, QU, x, out) != 0) {
int64_t v = out[0];
free(out);
return v;
}
int64_t r = isqrt_i64(x);
int64_t res = 0;
int64_t i = 1;
while (i <= r) {
int64_t t = FLOW_CHECKED_DIV((x), ((i * i)));
int64_t j = isqrt_i64(FLOW_CHECKED_DIV((x), (t)));
res = (res + (t * ((int64_t)((PREF[j] - PREF[(i - 1)])))));
i = (j + 1);
}
memo_put_ptr_i64_ptr_i64_ptr_i8_i64_i64(QK, QV, QU, x, res);
free(out);
return res;
}
int64_t pack2_i64_i64(int64_t a, int64_t b) {
return (FLOW_CHECKED_SHL((a), (16)) ^ b);
}
int64_t squarefree_min_i64_i64(int64_t x, int64_t start_idx) {
if (x <= 0) {
return 0;
}
if (x == 1) {
return 1;
}
if (start_idx == 0) {
return squarefree_upto_i64(x);
}
if ((start_idx < NPR && PRIMES[start_idx] > x)) {
return 1;
}
int64_t key = pack2_i64_i64(x, start_idx);
int64_t* out = (int64_t*)(calloc(1, 8));
if (memo_get_ptr_i64_ptr_i64_ptr_i8_i64_ptr_i64(FK, FV, FU, key, out) != 0) {
int64_t v = out[0];
free(out);
return v;
}
int64_t total = squarefree_upto_i64(x);
int64_t i = 0;
while (i < start_idx) {
int64_t p = PRIMES[i];
if (p > x) {
break;
}
total = (total - squarefree_min_i64_i64(FLOW_CHECKED_DIV((x), (p)), (i + 1)));
i = (i + 1);
}
memo_put_ptr_i64_ptr_i64_ptr_i8_i64_i64(FK, FV, FU, key, total);
free(out);
return total;
}
int64_t pack3_i64_i64_i64(int64_t a, int64_t b, int64_t c) {
return (((a * 1315423911) ^ (b * 2654435761)) ^ (c * 97531));
}
int64_t count_dpowers_i64_i64_i64(int64_t limit, int64_t start_idx, int64_t max_exp) {
if (limit <= 0) {
return 0;
}
if (limit == 1) {
return 1;
}
if (max_exp <= 1) {
return squarefree_min_i64_i64(limit, start_idx);
}
int64_t key = pack3_i64_i64_i64(limit, start_idx, max_exp);
int64_t* out = (int64_t*)(calloc(1, 8));
if (memo_get_ptr_i64_ptr_i64_ptr_i8_i64_ptr_i64(CK, CV, CU, key, out) != 0) {
int64_t v = out[0];
free(out);
return v;
}
int64_t res = squarefree_min_i64_i64(limit, start_idx);
int64_t i = start_idx;
while (i < NPR) {
int64_t p = PRIMES[i];
int64_t p2 = (p * p);
if (p2 > limit) {
break;
}
int64_t pe = p2;
int64_t e = 2;
while ((e <= max_exp && pe <= limit)) {
res = (res + count_dpowers_i64_i64_i64(FLOW_CHECKED_DIV((limit), (pe)), (i + 1), e));
e = (e + 1);
if (pe > FLOW_CHECKED_DIV((limit), (p))) {
break;
}
pe = (pe * p);
}
i = (i + 1);
}
memo_put_ptr_i64_ptr_i64_ptr_i8_i64_i64(CK, CV, CU, key, res);
free(out);
return res;
}
int64_t max_exp_i64(int64_t n) {
int64_t e = 0;
int64_t v = 1;
while ((v * 2) <= n) {
v = (v * 2);
e = (e + 1);
}
return e;
}
int32_t main(void) {
build_sieve();
int64_t n = 10000000000000;
printf("%lld\n", count_dpowers_i64_i64_i64(n, 0, max_exp_i64(n)));
return 0;
}