Count integers x ≤ 9*10^18 writable as a²*b³ with a,b > 1. F(n) = P - X1 - X2 - X3 + 1, where P = powerful count, X1 = squares of cubefree, X2 = cubes of squarefree, X3 = prime sixth powers.
# Project Euler 634
# Count integers x ≤ 9*10^18 writable as a²*b³ with a,b > 1.
# F(n) = P - X1 - X2 - X3 + 1, where P = powerful count, X1 = squares of cubefree,
# X2 = cubes of squarefree, X3 = prime sixth powers.
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
}
function iroot3(n: i64) -> i64 {
let mut lo: i64 = 0
let mut hi: i64 = 2
while true {
let mut p: i128 = 1 as i128
for _ in 0..3 { p = p * (hi as i128) }
if p > (n as i128) { break }
hi = hi * 2
}
while lo + 1 < hi {
let mid: i64 = (lo + hi) / 2
let mut p: i128 = 1 as i128
for _ in 0..3 { p = p * (mid as i128) }
if p <= (n as i128) { lo = mid } else { hi = mid }
}
return lo
}
function iroot6(n: i64) -> i64 {
let mut lo: i64 = 0
let mut hi: i64 = 2
while true {
let mut p: i128 = 1 as i128
for _ in 0..6 { p = p * (hi as i128) }
if p > (n as i128) { break }
hi = hi * 2
}
while lo + 1 < hi {
let mid: i64 = (lo + hi) / 2
let m2: i128 = (mid as i128) * (mid as i128)
let m3: i128 = m2 * m2 * m2
if m3 <= (n as i128) { lo = mid } else { hi = mid }
}
return lo
}
# Count squarefree numbers ≤ m using Möbius
function squarefree_count(m: i64, mu: ptr<i8>) -> i64 {
let mut s: i64 = 0
let mut i: i64 = 1
while i * i <= m {
let mu_i: i64 = mu[i] as i64
if mu_i != 0 {
s = s + mu_i * (m / (i * i))
}
i = i + 1
}
return s
}
# Count cubefree numbers ≤ m: numbers not divisible by p³ for any prime p
function cubefree_count(m: i64, mu: ptr<i8>) -> i64 {
# Cubefree count = sum_{d: d^3 | } mu(d) * floor(m / d^3)
# where mu is Möbius. We need mu up to m^(1/3).
let mut s: i64 = 0
let mut d: i64 = 1
while d * d * d <= m {
let mu_d: i64 = mu[d] as i64
if mu_d != 0 {
s = s + mu_d * (m / (d * d * d))
}
d = d + 1
}
return s
}
function main() -> i32 {
let N: i64 = 9000000000000000000 # 9 * 10^18
# Compute Möbius function up to cbrt(isqrt(N)) (only need for cubefree_count)
# cubefree_count(R, mu) iterates d while d^3 <= R, so needs mu up to cbrt(R).
# R = isqrt(N) ≈ 3*10^9, cbrt(R) ≈ 1442. Use iroot6(N)+10 for safety margin.
let L: i64 = iroot6(N) + 10
let mu: ptr<i8> = calloc(L + 1, 1)
let is_comp: ptr<i8> = calloc(L + 1, 1)
let primes: ptr<i32> = calloc(L / 5 + 10, 4)
if mu == null || is_comp == null || primes == null { return 1 }
mu[1] = 1
let mut pc: i64 = 0
for i in 2..(L + 1) {
if is_comp[i] == 0 {
primes[pc] = i as i32
pc = pc + 1
mu[i] = -1
}
let mut j: i64 = 0
while j < pc {
let p: i64 = primes[j] as i64
let ip: i64 = i * p
if ip > L { break }
is_comp[ip] = 1
if i % p == 0 { mu[ip] = 0; break }
mu[ip] = (0 - (mu[i] as i64)) as i8
j = j + 1
}
}
# P = powerful count = sum_{squarefree b ≤ N^(1/3)} floor(sqrt(N / b³))
let m: i64 = iroot3(N)
# Sieve squarefree up to m
let sqfree: ptr<i8> = calloc(m + 1, 1)
if sqfree == null { return 1 }
for k in 0..(m + 1) { sqfree[k] = 1 }
sqfree[0] = 0
let mut p: i64 = 2
while p * p <= m {
if sqfree[p] == 1 {
let sq: i64 = p * p
let mut j2: i64 = sq
while j2 <= m { sqfree[j2] = 0; j2 = j2 + sq }
}
p = p + 1
}
let mut P: i64 = 0
let mut b: i64 = 1
while b <= m {
if sqfree[b] == 1 {
let b3: i64 = b * b * b
P = P + isqrt(N / b3)
}
b = b + 1
}
# X1 = count of cubefree numbers ≤ isqrt(N) (squares of cubefree)
let R: i64 = isqrt(N)
let X1: i64 = cubefree_count(R, mu)
# X2 = count of squarefree numbers ≤ N^(1/3) (cubes of squarefree)
let X2: i64 = 0
for b in 1..(m + 1) {
if sqfree[b] == 1 { X2 = X2 + 1 }
}
# X3 = count of primes p with p^6 ≤ N
let r6: i64 = iroot6(N)
# Count primes up to r6
let sieve6: ptr<i8> = calloc(r6 + 1, 1)
if sieve6 == null { return 1 }
sieve6[0] = 1
sieve6[1] = 1
p = 2
while p * p <= r6 {
if sieve6[p] == 0 {
let mut j3: i64 = p * p
while j3 <= r6 { sieve6[j3] = 1; j3 = j3 + p }
}
p = p + 1
}
let mut X3: i64 = 0
for p in 2..(r6 + 1) {
if sieve6[p] == 0 { X3 = X3 + 1 }
}
let ans: i64 = P - X1 - X2 - X3 + 1
printf("%lld\n", ans)
free(sieve6)
free(sqfree)
free(primes)
free(is_comp)
free(mu)
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);
int64_t iroot3_i64(int64_t n);
int64_t iroot6_i64(int64_t n);
int64_t squarefree_count_i64_ptr_i8(int64_t m, int8_t* mu);
int64_t cubefree_count_i64_ptr_i8(int64_t m, int8_t* mu);
int32_t main(void);
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;
}
int64_t iroot3_i64(int64_t n) {
int64_t lo = 0;
int64_t hi = 2;
while (1) {
__int128 p = ((__int128)(1));
int32_t __flow_step_1 = 1;
for (int32_t _ = 0; (0 <= 3) ? _ < 3 : _ > 3; _ += (0 <= 3) ? 1 : -1) {
p = (p * ((__int128)(hi)));
}
if (p > ((__int128)(n))) {
break;
}
hi = (hi * 2);
}
while ((lo + 1) < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
__int128 p = ((__int128)(1));
int32_t __flow_step_2 = 1;
for (int32_t _ = 0; (0 <= 3) ? _ < 3 : _ > 3; _ += (0 <= 3) ? 1 : -1) {
p = (p * ((__int128)(mid)));
}
if (p <= ((__int128)(n))) {
lo = mid;
} else {
hi = mid;
}
}
return lo;
}
int64_t iroot6_i64(int64_t n) {
int64_t lo = 0;
int64_t hi = 2;
while (1) {
__int128 p = ((__int128)(1));
int32_t __flow_step_3 = 1;
for (int32_t _ = 0; (0 <= 6) ? _ < 6 : _ > 6; _ += (0 <= 6) ? 1 : -1) {
p = (p * ((__int128)(hi)));
}
if (p > ((__int128)(n))) {
break;
}
hi = (hi * 2);
}
while ((lo + 1) < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
__int128 m2 = (((__int128)(mid)) * ((__int128)(mid)));
__int128 m3 = ((m2 * m2) * m2);
if (m3 <= ((__int128)(n))) {
lo = mid;
} else {
hi = mid;
}
}
return lo;
}
int64_t squarefree_count_i64_ptr_i8(int64_t m, int8_t* mu) {
int64_t s = 0;
int64_t i = 1;
while ((i * i) <= m) {
int64_t mu_i = ((int64_t)(mu[i]));
if (mu_i != 0) {
s = (s + (mu_i * FLOW_CHECKED_DIV((m), ((i * i)))));
}
i = (i + 1);
}
return s;
}
int64_t cubefree_count_i64_ptr_i8(int64_t m, int8_t* mu) {
int64_t s = 0;
int64_t d = 1;
while (((d * d) * d) <= m) {
int64_t mu_d = ((int64_t)(mu[d]));
if (mu_d != 0) {
s = (s + (mu_d * FLOW_CHECKED_DIV((m), (((d * d) * d)))));
}
d = (d + 1);
}
return s;
}
int32_t main(void) {
int64_t N = 9000000000000000000;
int64_t L = (iroot6_i64(N) + 10);
int8_t* mu = (int8_t*)(calloc((L + 1), 1));
int8_t* is_comp = (int8_t*)(calloc((L + 1), 1));
int32_t* primes = (int32_t*)(calloc((FLOW_CHECKED_DIV((L), (5)) + 10), 4));
if (((mu == NULL || is_comp == NULL) || primes == NULL)) {
return 1;
}
mu[1] = 1;
int64_t pc = 0;
int32_t __flow_step_4 = 1;
for (int32_t i = 2; (2 <= (L + 1)) ? i < (L + 1) : i > (L + 1); i += (2 <= (L + 1)) ? 1 : -1) {
if (is_comp[i] == 0) {
primes[pc] = ((int32_t)(i));
pc = (pc + 1);
mu[i] = (-1);
}
int64_t j = 0;
while (j < pc) {
int64_t p = ((int64_t)(primes[j]));
int64_t ip = (i * p);
if (ip > L) {
break;
}
is_comp[ip] = 1;
if (FLOW_CHECKED_MOD((i), (p)) == 0) {
mu[ip] = 0;
break;
}
mu[ip] = ((int8_t)((0 - ((int64_t)(mu[i])))));
j = (j + 1);
}
}
int64_t m = iroot3_i64(N);
int8_t* sqfree = (int8_t*)(calloc((m + 1), 1));
if (sqfree == NULL) {
return 1;
}
int32_t __flow_step_5 = 1;
for (int32_t k = 0; (0 <= (m + 1)) ? k < (m + 1) : k > (m + 1); k += (0 <= (m + 1)) ? 1 : -1) {
sqfree[k] = 1;
}
sqfree[0] = 0;
int64_t p = 2;
while ((p * p) <= m) {
if (sqfree[p] == 1) {
int64_t sq = (p * p);
int64_t j2 = sq;
while (j2 <= m) {
sqfree[j2] = 0;
j2 = (j2 + sq);
}
}
p = (p + 1);
}
int64_t P = 0;
int64_t b = 1;
while (b <= m) {
if (sqfree[b] == 1) {
int64_t b3 = ((b * b) * b);
P = (P + isqrt_i64(FLOW_CHECKED_DIV((N), (b3))));
}
b = (b + 1);
}
int64_t R = isqrt_i64(N);
int64_t X1 = cubefree_count_i64_ptr_i8(R, mu);
int64_t X2 = 0;
int32_t __flow_step_6 = 1;
for (int32_t b = 1; (1 <= (m + 1)) ? b < (m + 1) : b > (m + 1); b += (1 <= (m + 1)) ? 1 : -1) {
if (sqfree[b] == 1) {
X2 = (X2 + 1);
}
}
int64_t r6 = iroot6_i64(N);
int8_t* sieve6 = (int8_t*)(calloc((r6 + 1), 1));
if (sieve6 == NULL) {
return 1;
}
sieve6[0] = 1;
sieve6[1] = 1;
p = 2;
while ((p * p) <= r6) {
if (sieve6[p] == 0) {
int64_t j3 = (p * p);
while (j3 <= r6) {
sieve6[j3] = 1;
j3 = (j3 + p);
}
}
p = (p + 1);
}
int64_t X3 = 0;
int32_t __flow_step_7 = 1;
for (int32_t p = 2; (2 <= (r6 + 1)) ? p < (r6 + 1) : p > (r6 + 1); p += (2 <= (r6 + 1)) ? 1 : -1) {
if (sieve6[p] == 0) {
X3 = (X3 + 1);
}
}
int64_t ans = ((((P - X1) - X2) - X3) + 1);
printf("%lld\n", ans);
free(sieve6);
free(sqfree);
free(primes);
free(is_comp);
free(mu);
return 0;
}