# Project Euler 616
# Creative numbers <= 1e12: perfect powers minus p^q minus 16.
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 ipow(base: i64, exp: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base
let mut e: i64 = exp
while e > 0 {
if (e & 1) == 1 {
if r > 1000000000000 / b { return -1 }
r = r * b
}
e = e / 2
if e > 0 {
if b > 1000000000000 / b { return -1 }
b = b * b
}
}
return r
}
function iroot(limit: i64, exp: i64) -> i64 {
# largest a with a^exp <= limit
let mut lo: i64 = 1
let mut hi: i64 = limit
if exp >= 2 {
hi = isqrt(limit) + 2
if hi > limit { hi = limit }
}
while lo < hi {
let mid: i64 = (lo + hi + 1) / 2
let p: i64 = ipow(mid, exp)
if p < 0 || p > limit { hi = mid - 1 } else { lo = mid }
}
return lo
}
function heapify(a: ptr<i64>, n: i64, i0: i64) -> void {
let mut i: i64 = i0
while true {
let mut largest: i64 = i
let l: i64 = 2 * i + 1
let r: i64 = 2 * i + 2
if l < n && a[l] > a[largest] { largest = l }
if r < n && a[r] > a[largest] { largest = r }
if largest == i { break }
let t: i64 = a[i]
a[i] = a[largest]
a[largest] = t
i = largest
}
}
function heapsort(a: ptr<i64>, n: i64) -> void {
let mut i: i64 = n / 2 - 1
while i >= 0 {
heapify(a, n, i)
i = i - 1
}
i = n - 1
while i > 0 {
let t: i64 = a[0]
a[0] = a[i]
a[i] = t
heapify(a, i, 0)
i = i - 1
}
}
function main() -> i32 {
let LIMIT: i64 = 1000000000000
# Collect perfect powers
let arr: ptr<i64> = calloc(2000000, 8)
if arr == null { return 1 }
let mut n: i64 = 0
let mut exp: i64 = 2
while exp <= 40 {
let base: i64 = iroot(LIMIT, exp)
let mut a: i64 = 2
while a <= base {
let p: i64 = ipow(a, exp)
if p > 0 && p <= LIMIT {
arr[n] = p
n = n + 1
}
a = a + 1
}
exp = exp + 1
}
heapsort(arr, n)
let mut sum_pp: i64 = 0
let mut i: i64 = 0
while i < n {
if i == 0 || arr[i] != arr[i - 1] {
sum_pp = sum_pp + arr[i]
}
i = i + 1
}
# primes <= 1e6
let M: i64 = 1000000
let sieve: ptr<i8> = calloc(M + 1, 1)
let primes: ptr<i32> = calloc(M / 5 + 10, 4)
if sieve == null || primes == null { return 1 }
i = 2
while i <= M {
sieve[i] = 1
i = i + 1
}
let mut p: i64 = 2
while p * p <= M {
if sieve[p] != 0 {
let mut q: i64 = p * p
while q <= M {
sieve[q] = 0
q = q + p
}
}
p = p + 1
}
let mut pn: i64 = 0
i = 2
while i <= M {
if sieve[i] != 0 {
primes[pn] = i as i32
pn = pn + 1
}
i = i + 1
}
let exps: ptr<i64> = calloc(12, 8)
exps[0] = 2; exps[1] = 3; exps[2] = 5; exps[3] = 7
exps[4] = 11; exps[5] = 13; exps[6] = 17; exps[7] = 19
exps[8] = 23; exps[9] = 29; exps[10] = 31; exps[11] = 37
let pp: ptr<i64> = calloc(200000, 8)
let mut m: i64 = 0
let mut ei: i64 = 0
while ei < 12 {
let qe: i64 = exps[ei]
let maxb: i64 = iroot(LIMIT, qe)
let mut j: i64 = 0
while j < pn {
let pr: i64 = primes[j] as i64
if pr > maxb { break }
let val: i64 = ipow(pr, qe)
if val > 0 && val <= LIMIT {
pp[m] = val
m = m + 1
}
j = j + 1
}
ei = ei + 1
}
heapsort(pp, m)
let mut sum_pq: i64 = 0
i = 0
while i < m {
if i == 0 || pp[i] != pp[i - 1] {
sum_pq = sum_pq + pp[i]
}
i = i + 1
}
let ans: i64 = sum_pp - sum_pq - 16
printf("%lld\n", ans)
free(arr)
free(sieve)
free(primes)
free(exps)
free(pp)
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 ipow_i64_i64(int64_t base, int64_t exp);
int64_t iroot_i64_i64(int64_t limit, int64_t exp);
void heapify_ptr_i64_i64_i64(int64_t* a, int64_t n, int64_t i0);
void heapsort_ptr_i64_i64(int64_t* a, int64_t n);
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 ipow_i64_i64(int64_t base, int64_t exp) {
int64_t r = 1;
int64_t b = base;
int64_t e = exp;
while (e > 0) {
if ((e & 1) == 1) {
if (r > FLOW_CHECKED_DIV((1000000000000), (b))) {
return (-1);
}
r = (r * b);
}
e = FLOW_CHECKED_DIV((e), (2));
if (e > 0) {
if (b > FLOW_CHECKED_DIV((1000000000000), (b))) {
return (-1);
}
b = (b * b);
}
}
return r;
}
int64_t iroot_i64_i64(int64_t limit, int64_t exp) {
int64_t lo = 1;
int64_t hi = limit;
if (exp >= 2) {
hi = (isqrt_i64(limit) + 2);
if (hi > limit) {
hi = limit;
}
}
while (lo < hi) {
int64_t mid = FLOW_CHECKED_DIV((((lo + hi) + 1)), (2));
int64_t p = ipow_i64_i64(mid, exp);
if ((p < 0 || p > limit)) {
hi = (mid - 1);
} else {
lo = mid;
}
}
return lo;
}
void heapify_ptr_i64_i64_i64(int64_t* a, int64_t n, int64_t i0) {
int64_t i = i0;
while (1) {
int64_t largest = i;
int64_t l = ((2 * i) + 1);
int64_t r = ((2 * i) + 2);
if ((l < n && a[l] > a[largest])) {
largest = l;
}
if ((r < n && a[r] > a[largest])) {
largest = r;
}
if (largest == i) {
break;
}
int64_t t = a[i];
a[i] = a[largest];
a[largest] = t;
i = largest;
}
}
void heapsort_ptr_i64_i64(int64_t* a, int64_t n) {
int64_t i = (FLOW_CHECKED_DIV((n), (2)) - 1);
while (i >= 0) {
heapify_ptr_i64_i64_i64(a, n, i);
i = (i - 1);
}
i = (n - 1);
while (i > 0) {
int64_t t = a[0];
a[0] = a[i];
a[i] = t;
heapify_ptr_i64_i64_i64(a, i, 0);
i = (i - 1);
}
}
int32_t main(void) {
int64_t LIMIT = 1000000000000;
int64_t* arr = (int64_t*)(calloc(2000000, 8));
if (arr == NULL) {
return 1;
}
int64_t n = 0;
int64_t exp = 2;
while (exp <= 40) {
int64_t base = iroot_i64_i64(LIMIT, exp);
int64_t a = 2;
while (a <= base) {
int64_t p = ipow_i64_i64(a, exp);
if ((p > 0 && p <= LIMIT)) {
arr[n] = p;
n = (n + 1);
}
a = (a + 1);
}
exp = (exp + 1);
}
heapsort_ptr_i64_i64(arr, n);
int64_t sum_pp = 0;
int64_t i = 0;
while (i < n) {
if ((i == 0 || arr[i] != arr[(i - 1)])) {
sum_pp = (sum_pp + arr[i]);
}
i = (i + 1);
}
int64_t M = 1000000;
int8_t* sieve = (int8_t*)(calloc((M + 1), 1));
int32_t* primes = (int32_t*)(calloc((FLOW_CHECKED_DIV((M), (5)) + 10), 4));
if ((sieve == NULL || primes == NULL)) {
return 1;
}
i = 2;
while (i <= M) {
sieve[i] = 1;
i = (i + 1);
}
int64_t p = 2;
while ((p * p) <= M) {
if (sieve[p] != 0) {
int64_t q = (p * p);
while (q <= M) {
sieve[q] = 0;
q = (q + p);
}
}
p = (p + 1);
}
int64_t pn = 0;
i = 2;
while (i <= M) {
if (sieve[i] != 0) {
primes[pn] = ((int32_t)(i));
pn = (pn + 1);
}
i = (i + 1);
}
int64_t* exps = (int64_t*)(calloc(12, 8));
exps[0] = 2;
exps[1] = 3;
exps[2] = 5;
exps[3] = 7;
exps[4] = 11;
exps[5] = 13;
exps[6] = 17;
exps[7] = 19;
exps[8] = 23;
exps[9] = 29;
exps[10] = 31;
exps[11] = 37;
int64_t* pp = (int64_t*)(calloc(200000, 8));
int64_t m = 0;
int64_t ei = 0;
while (ei < 12) {
int64_t qe = exps[ei];
int64_t maxb = iroot_i64_i64(LIMIT, qe);
int64_t j = 0;
while (j < pn) {
int64_t pr = ((int64_t)(primes[j]));
if (pr > maxb) {
break;
}
int64_t val = ipow_i64_i64(pr, qe);
if ((val > 0 && val <= LIMIT)) {
pp[m] = val;
m = (m + 1);
}
j = (j + 1);
}
ei = (ei + 1);
}
heapsort_ptr_i64_i64(pp, m);
int64_t sum_pq = 0;
i = 0;
while (i < m) {
if ((i == 0 || pp[i] != pp[(i - 1)])) {
sum_pq = (sum_pq + pp[i]);
}
i = (i + 1);
}
int64_t ans = ((sum_pp - sum_pq) - 16);
printf("%lld\n", ans);
free(arr);
free(sieve);
free(primes);
free(exps);
free(pp);
return 0;
}