Fermat-like Equations: count (a,b,c,e,f) with 0 < a < b, e >= 2, f >= 3 and a^e + b^e = c^f <= 10^18. Split by e: e = 2: for every perfect power m = c^f, count a < b with a^2 + b^2 = m from the factorisation of c (r2 divisor formula; square / half-square corrections read off the exponents, no big sqrt). e = 3: f = 3 is impossible (Fermat); for f >= 4 use a^3 + b^3 = (a+b)(a^2-ab+b^2): a+b = d runs over divisors of c^f with m <= d^3 <= 4m, then ab and the discriminant decide. e >= 4: few enough pairs (a, b) to enumerate directly; each sum is looked up in a hash table mapping c^f -> number of (c, f) pairs.
# Project Euler 678
# Fermat-like Equations: count (a,b,c,e,f) with 0 < a < b, e >= 2, f >= 3
# and a^e + b^e = c^f <= 10^18.
#
# Split by e:
# e = 2: for every perfect power m = c^f, count a < b with a^2 + b^2 = m
# from the factorisation of c (r2 divisor formula; square /
# half-square corrections read off the exponents, no big sqrt).
# e = 3: f = 3 is impossible (Fermat); for f >= 4 use
# a^3 + b^3 = (a+b)(a^2-ab+b^2): a+b = d runs over divisors of
# c^f with m <= d^3 <= 4m, then ab and the discriminant decide.
# e >= 4: few enough pairs (a, b) to enumerate directly; each sum is
# looked up in a hash table mapping c^f -> number of (c, f) pairs.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const N: i64 = 1000000000000000000
const HB: i64 = 4194304 # hash slots (2^22), ~1.03M keys
const HMASK: i64 = 4194303
function isqrt64(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
}
# a^e, or N+1 if it exceeds N (overflow-safe)
function ipow_capped(a: i64, e: i64) -> i64 {
let mut cur: i64 = 1
let mut i: i64 = 0
while i < e {
if cur > N / a {
return N + 1
}
cur = cur * a
i = i + 1
}
return cur
}
function hash_slot(keys: ptr<i64>, s: i64) -> i64 {
let mut h: i64 = s % HB
while keys[h] != 0 && keys[h] != s {
h = (h + 1) & HMASK
}
return h
}
function main() -> i32 {
# smallest-prime-factor sieve up to 10^6 (c <= N^(1/3))
let CM: i64 = 1000000
let spf: ptr<i32> = calloc(CM + 1, 4)
let mut i: i64 = 2
while i <= CM {
if spf[i] == 0 {
let mut j: i64 = i
while j <= CM {
if spf[j] == 0 {
spf[j] = i as i32
}
j = j + i
}
}
i = i + 1
}
let hkey: ptr<i64> = calloc(HB, 8)
let hcnt: ptr<i64> = calloc(HB, 8)
let pf: ptr<i64> = calloc(64, 8) # prime factors of c
let px: ptr<i64> = calloc(64, 8) # exponents
let divs: ptr<i64> = calloc(70000, 8)
let mut total: i64 = 0
# ---- enumerate (c, f) pairs: e=2 formula, e=3 divisors, hash fill ----
let mut f: i64 = 3
while ipow_capped(2, f) <= N {
let mut c: i64 = 2
let mut m: i64 = ipow_capped(c, f)
while m <= N {
# register in hash for the e >= 4 loop
let hs: i64 = hash_slot(hkey, m)
hkey[hs] = m
hcnt[hs] = hcnt[hs] + 1
# factor c
let mut np: i64 = 0
let mut cc: i64 = c
while cc > 1 {
let p: i64 = spf[cc] as i64
let mut x: i64 = 0
while cc % p == 0 {
cc = cc / p
x = x + 1
}
pf[np] = p
px[np] = x
np = np + 1
}
# e = 2: B = prod (x*f + 1) over p = 1 mod 4; need q = 3 mod 4
# exponents even; square / half-square from exponents alone
let mut B: i64 = 1
let mut ok: i64 = 1
let mut allev: i64 = 1 # all exponents of m even?
let mut oddev: i64 = 1 # all odd-prime exponents even?
let mut s2: i64 = 0 # exponent of 2 in m
i = 0
while i < np {
let xf: i64 = px[i] * f
if pf[i] == 2 {
s2 = xf
} else {
if xf % 2 == 1 {
oddev = 0
}
if pf[i] % 4 == 1 {
B = B * (xf + 1)
} else {
if xf % 2 == 1 {
ok = 0
}
}
}
if xf % 2 == 1 {
allev = 0
}
i = i + 1
}
if ok == 1 {
let mut sq: i64 = 0
if allev == 1 {
sq = 1
}
let mut hsq: i64 = 0
if s2 >= 1 && (s2 - 1) % 2 == 0 && oddev == 1 {
hsq = 1
}
total = total + (B - sq - hsq) / 2
}
# e = 3, f >= 4: d = a+b over divisors of m, m <= d^3 <= 4m
if f >= 4 {
divs[0] = 1
let mut nd: i64 = 1
i = 0
while i < np {
let xf: i64 = px[i] * f
let mut nnd: i64 = nd
let mut pe: i64 = 1
let mut t: i64 = 1
while t <= xf {
# cap: only extend divisors while pe*p stays <= dmax bound
pe = pe * pf[i]
let mut u: i64 = 0
while u < nd {
let dv: i64 = divs[u]
# keep divisors up to 2e6 (d^3 <= 4m <= 4e18)
if dv <= 2000000 / pe {
divs[nnd] = dv * pe
nnd = nnd + 1
}
u = u + 1
}
t = t + 1
}
nd = nnd
i = i + 1
}
let mut u: i64 = 0
while u < nd {
let d: i64 = divs[u]
if m % d == 0 {
let d3: i64 = d * d * d
if d3 >= m && d3 <= 4 * m {
let q: i64 = m / d
let t2: i64 = d * d - q
if t2 > 0 && t2 % 3 == 0 {
let ab: i64 = t2 / 3
let disc: i64 = d * d - 4 * ab
if disc > 0 {
let rr: i64 = isqrt64(disc)
if rr * rr == disc && (d - rr) % 2 == 0 {
let aa: i64 = (d - rr) / 2
let bbv: i64 = (d + rr) / 2
if aa >= 1 && aa < bbv {
if aa * aa * aa + bbv * bbv * bbv == m {
total = total + 1
}
}
}
}
}
}
}
u = u + 1
}
}
c = c + 1
m = ipow_capped(c, f)
}
f = f + 1
}
# ---- e >= 4: enumerate pairs, look sums up in the hash ----
let P: ptr<i64> = calloc(40000, 8)
let mut e: i64 = 4
while ipow_capped(2, e) < N {
let mut amax: i64 = 0
let mut a: i64 = 1
let mut pa: i64 = ipow_capped(a, e)
while pa <= N {
P[a] = pa
amax = a
a = a + 1
pa = ipow_capped(a, e)
}
a = 1
while a < amax {
let ae: i64 = P[a]
if ae + P[a + 1] > N {
a = amax
} else {
let mut b: i64 = a + 1
while b <= amax && ae + P[b] <= N {
let s: i64 = ae + P[b]
let hs2: i64 = hash_slot(hkey, s)
if hkey[hs2] == s {
total = total + hcnt[hs2]
}
b = b + 1
}
a = a + 1
}
}
e = e + 1
}
printf("%lld\n", total)
free(spf)
free(hkey)
free(hcnt)
free(pf)
free(px)
free(divs)
free(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; }
int64_t isqrt64_i64(int64_t n);
int64_t ipow_capped_i64_i64(int64_t a, int64_t e);
int64_t hash_slot_ptr_i64_i64(int64_t* keys, int64_t s);
int32_t main(void);
static const int64_t N = 1000000000000000000;
static const int64_t HB = 4194304;
static const int64_t HMASK = 4194303;
int64_t isqrt64_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_capped_i64_i64(int64_t a, int64_t e) {
int64_t cur = 1;
int64_t i = 0;
while (i < e) {
if (cur > FLOW_CHECKED_DIV((N), (a))) {
return (N + 1);
}
cur = (cur * a);
i = (i + 1);
}
return cur;
}
int64_t hash_slot_ptr_i64_i64(int64_t* keys, int64_t s) {
int64_t h = FLOW_CHECKED_MOD((s), (HB));
while ((keys[h] != 0 && keys[h] != s)) {
h = ((h + 1) & HMASK);
}
return h;
}
int32_t main(void) {
int64_t CM = 1000000;
int32_t* spf = (int32_t*)(calloc((CM + 1), 4));
int64_t i = 2;
while (i <= CM) {
if (spf[i] == 0) {
int64_t j = i;
while (j <= CM) {
if (spf[j] == 0) {
spf[j] = ((int32_t)(i));
}
j = (j + i);
}
}
i = (i + 1);
}
int64_t* hkey = (int64_t*)(calloc(HB, 8));
int64_t* hcnt = (int64_t*)(calloc(HB, 8));
int64_t* pf = (int64_t*)(calloc(64, 8));
int64_t* px = (int64_t*)(calloc(64, 8));
int64_t* divs = (int64_t*)(calloc(70000, 8));
int64_t total = 0;
int64_t f = 3;
while (ipow_capped_i64_i64(2, f) <= N) {
int64_t c = 2;
int64_t m = ipow_capped_i64_i64(c, f);
while (m <= N) {
int64_t hs = hash_slot_ptr_i64_i64(hkey, m);
hkey[hs] = m;
hcnt[hs] = (hcnt[hs] + 1);
int64_t np = 0;
int64_t cc = c;
while (cc > 1) {
int64_t p = ((int64_t)(spf[cc]));
int64_t x = 0;
while (FLOW_CHECKED_MOD((cc), (p)) == 0) {
cc = FLOW_CHECKED_DIV((cc), (p));
x = (x + 1);
}
pf[np] = p;
px[np] = x;
np = (np + 1);
}
int64_t B = 1;
int64_t ok = 1;
int64_t allev = 1;
int64_t oddev = 1;
int64_t s2 = 0;
i = 0;
while (i < np) {
int64_t xf = (px[i] * f);
if (pf[i] == 2) {
s2 = xf;
} else {
if (FLOW_CHECKED_MOD((xf), (2)) == 1) {
oddev = 0;
}
if (FLOW_CHECKED_MOD((pf[i]), (4)) == 1) {
B = (B * (xf + 1));
} else {
if (FLOW_CHECKED_MOD((xf), (2)) == 1) {
ok = 0;
}
}
}
if (FLOW_CHECKED_MOD((xf), (2)) == 1) {
allev = 0;
}
i = (i + 1);
}
if (ok == 1) {
int64_t sq = 0;
if (allev == 1) {
sq = 1;
}
int64_t hsq = 0;
if (((s2 >= 1 && FLOW_CHECKED_MOD(((s2 - 1)), (2)) == 0) && oddev == 1)) {
hsq = 1;
}
total = (total + FLOW_CHECKED_DIV((((B - sq) - hsq)), (2)));
}
if (f >= 4) {
divs[0] = 1;
int64_t nd = 1;
i = 0;
while (i < np) {
int64_t xf = (px[i] * f);
int64_t nnd = nd;
int64_t pe = 1;
int64_t t = 1;
while (t <= xf) {
pe = (pe * pf[i]);
int64_t u = 0;
while (u < nd) {
int64_t dv = divs[u];
if (dv <= FLOW_CHECKED_DIV((2000000), (pe))) {
divs[nnd] = (dv * pe);
nnd = (nnd + 1);
}
u = (u + 1);
}
t = (t + 1);
}
nd = nnd;
i = (i + 1);
}
int64_t u = 0;
while (u < nd) {
int64_t d = divs[u];
if (FLOW_CHECKED_MOD((m), (d)) == 0) {
int64_t d3 = ((d * d) * d);
if ((d3 >= m && d3 <= (4 * m))) {
int64_t q = FLOW_CHECKED_DIV((m), (d));
int64_t t2 = ((d * d) - q);
if ((t2 > 0 && FLOW_CHECKED_MOD((t2), (3)) == 0)) {
int64_t ab = FLOW_CHECKED_DIV((t2), (3));
int64_t disc = ((d * d) - (4 * ab));
if (disc > 0) {
int64_t rr = isqrt64_i64(disc);
if (((rr * rr) == disc && FLOW_CHECKED_MOD(((d - rr)), (2)) == 0)) {
int64_t aa = FLOW_CHECKED_DIV(((d - rr)), (2));
int64_t bbv = FLOW_CHECKED_DIV(((d + rr)), (2));
if ((aa >= 1 && aa < bbv)) {
if ((((aa * aa) * aa) + ((bbv * bbv) * bbv)) == m) {
total = (total + 1);
}
}
}
}
}
}
}
u = (u + 1);
}
}
c = (c + 1);
m = ipow_capped_i64_i64(c, f);
}
f = (f + 1);
}
int64_t* P = (int64_t*)(calloc(40000, 8));
int64_t e = 4;
while (ipow_capped_i64_i64(2, e) < N) {
int64_t amax = 0;
int64_t a = 1;
int64_t pa = ipow_capped_i64_i64(a, e);
while (pa <= N) {
P[a] = pa;
amax = a;
a = (a + 1);
pa = ipow_capped_i64_i64(a, e);
}
a = 1;
while (a < amax) {
int64_t ae = P[a];
if ((ae + P[(a + 1)]) > N) {
a = amax;
} else {
int64_t b = (a + 1);
while ((b <= amax && (ae + P[b]) <= N)) {
int64_t s = (ae + P[b]);
int64_t hs2 = hash_slot_ptr_i64_i64(hkey, s);
if (hkey[hs2] == s) {
total = (total + hcnt[hs2]);
}
b = (b + 1);
}
a = (a + 1);
}
}
e = (e + 1);
}
printf("%lld\n", total);
free(spf);
free(hkey);
free(hcnt);
free(pf);
free(px);
free(divs);
free(P);
return 0;
}