# Project Euler 516
# 5-smooth Totients — S(10^12) mod 2^32.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function is_prime(x0: i64) -> i32 {
let x: i64 = x0
if x % 2 == 0 || x % 3 == 0 || x % 5 == 0 {
if x == 2 || x == 3 || x == 5 { return 1 }
return 0
}
let mut i: i64 = 7
let mut pos: i32 = 1
# wheel deltas
while i * i <= x {
if x % i == 0 { return 0 }
if pos == 0 { i = i + 6 }
elif pos == 1 { i = i + 4 }
elif pos == 2 { i = i + 2 }
elif pos == 3 { i = i + 4 }
elif pos == 4 { i = i + 2 }
elif pos == 5 { i = i + 4 }
elif pos == 6 { i = i + 6 }
else { i = i + 2 }
pos = (pos + 1) & 7
}
if x > 1 { return 1 }
return 0
}
function main() -> i32 {
let limit: i64 = 1000000000000
let hamming: ptr<i64> = calloc(200000, 8)
let primes: ptr<i64> = calloc(200000, 8)
if hamming == null || primes == null { return 1 }
let mut hn: i64 = 0
let mut pn: i64 = 0
let mut two: i64 = 1
while two <= limit {
let mut three: i64 = 1
while two * three <= limit {
let mut five: i64 = 1
while two * three * five <= limit {
let current: i64 = two * three * five
hamming[hn] = current
hn = hn + 1
if current > 5 && is_prime(current + 1) == 1 {
primes[pn] = current + 1
pn = pn + 1
}
if five > limit / 5 { break }
five = five * 5
}
if three > limit / 3 { break }
three = three * 3
}
if two > limit / 2 { break }
two = two * 2
}
# sort hamming and primes (insertion / selection is fine for ~few thousand)
let mut i: i64 = 0
while i < hn {
let mut j: i64 = i + 1
while j < hn {
if hamming[j] < hamming[i] {
let t: i64 = hamming[i]
hamming[i] = hamming[j]
hamming[j] = t
}
j = j + 1
}
i = i + 1
}
i = 0
while i < pn {
let mut j2: i64 = i + 1
while j2 < pn {
if primes[j2] < primes[i] {
let t2: i64 = primes[i]
primes[i] = primes[j2]
primes[j2] = t2
}
j2 = j2 + 1
}
i = i + 1
}
# stack for DFS: (number, largest_prime)
let stack_n: ptr<i64> = calloc(200000, 8)
let stack_p: ptr<i64> = calloc(200000, 8)
if stack_n == null || stack_p == null { return 1 }
let mut sp: i64 = 0
stack_n[0] = 1
stack_p[0] = 1
sp = 1
let mut total: i64 = 0
while sp > 0 {
sp = sp - 1
let number: i64 = stack_n[sp]
let largest_prime: i64 = stack_p[sp]
let mut xi: i64 = 0
while xi < hn {
let x: i64 = hamming[xi]
if x > limit / number { break }
let next_value: i64 = x * number
total = total + next_value
xi = xi + 1
}
# find first prime > largest_prime
let mut lo: i64 = 0
let mut hi: i64 = pn
while lo < hi {
let mid: i64 = (lo + hi) / 2
if primes[mid] <= largest_prime { lo = mid + 1 } else { hi = mid }
}
let mut pi: i64 = lo
while pi < pn {
let prime: i64 = primes[pi]
if prime > limit / number { break }
stack_n[sp] = prime * number
stack_p[sp] = prime
sp = sp + 1
pi = pi + 1
}
}
total = total & 4294967295
printf("%lld\n", total)
free(hamming); free(primes); free(stack_n); free(stack_p)
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; }
int32_t is_prime_i64(int64_t x0);
int32_t main(void);
int32_t is_prime_i64(int64_t x0) {
int64_t x = x0;
if (((FLOW_CHECKED_MOD((x), (2)) == 0 || FLOW_CHECKED_MOD((x), (3)) == 0) || FLOW_CHECKED_MOD((x), (5)) == 0)) {
if (((x == 2 || x == 3) || x == 5)) {
return 1;
}
return 0;
}
int64_t i = 7;
int32_t pos = 1;
while ((i * i) <= x) {
if (FLOW_CHECKED_MOD((x), (i)) == 0) {
return 0;
}
if (pos == 0) {
i = (i + 6);
} else if (pos == 1) {
i = (i + 4);
} else if (pos == 2) {
i = (i + 2);
} else if (pos == 3) {
i = (i + 4);
} else if (pos == 4) {
i = (i + 2);
} else if (pos == 5) {
i = (i + 4);
} else if (pos == 6) {
i = (i + 6);
} else {
i = (i + 2);
}
pos = ((pos + 1) & 7);
}
if (x > 1) {
return 1;
}
return 0;
}
int32_t main(void) {
int64_t limit = 1000000000000;
int64_t* hamming = (int64_t*)(calloc(200000, 8));
int64_t* primes = (int64_t*)(calloc(200000, 8));
if ((hamming == NULL || primes == NULL)) {
return 1;
}
int64_t hn = 0;
int64_t pn = 0;
int64_t two = 1;
while (two <= limit) {
int64_t three = 1;
while ((two * three) <= limit) {
int64_t five = 1;
while (((two * three) * five) <= limit) {
int64_t current = ((two * three) * five);
hamming[hn] = current;
hn = (hn + 1);
if ((current > 5 && is_prime_i64((current + 1)) == 1)) {
primes[pn] = (current + 1);
pn = (pn + 1);
}
if (five > FLOW_CHECKED_DIV((limit), (5))) {
break;
}
five = (five * 5);
}
if (three > FLOW_CHECKED_DIV((limit), (3))) {
break;
}
three = (three * 3);
}
if (two > FLOW_CHECKED_DIV((limit), (2))) {
break;
}
two = (two * 2);
}
int64_t i = 0;
while (i < hn) {
int64_t j = (i + 1);
while (j < hn) {
if (hamming[j] < hamming[i]) {
int64_t t = hamming[i];
hamming[i] = hamming[j];
hamming[j] = t;
}
j = (j + 1);
}
i = (i + 1);
}
i = 0;
while (i < pn) {
int64_t j2 = (i + 1);
while (j2 < pn) {
if (primes[j2] < primes[i]) {
int64_t t2 = primes[i];
primes[i] = primes[j2];
primes[j2] = t2;
}
j2 = (j2 + 1);
}
i = (i + 1);
}
int64_t* stack_n = (int64_t*)(calloc(200000, 8));
int64_t* stack_p = (int64_t*)(calloc(200000, 8));
if ((stack_n == NULL || stack_p == NULL)) {
return 1;
}
int64_t sp = 0;
stack_n[0] = 1;
stack_p[0] = 1;
sp = 1;
int64_t total = 0;
while (sp > 0) {
sp = (sp - 1);
int64_t number = stack_n[sp];
int64_t largest_prime = stack_p[sp];
int64_t xi = 0;
while (xi < hn) {
int64_t x = hamming[xi];
if (x > FLOW_CHECKED_DIV((limit), (number))) {
break;
}
int64_t next_value = (x * number);
total = (total + next_value);
xi = (xi + 1);
}
int64_t lo = 0;
int64_t hi = pn;
while (lo < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (primes[mid] <= largest_prime) {
lo = (mid + 1);
} else {
hi = mid;
}
}
int64_t pi = lo;
while (pi < pn) {
int64_t prime = primes[pi];
if (prime > FLOW_CHECKED_DIV((limit), (number))) {
break;
}
stack_n[sp] = (prime * number);
stack_p[sp] = prime;
sp = (sp + 1);
pi = (pi + 1);
}
}
total = (total & 4294967295);
printf("%lld\n", total);
free(hamming);
free(primes);
free(stack_n);
free(stack_p);
return 0;
}