# Project Euler 905
# Now I Know — epistemic hat game reduced to accelerated Euclidean process.
# Sum F(a^b, b^a, a^b+b^a) for a=1..7, b=1..19.
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 { r = r * b }
b = b * b
e = e / 2
}
return r
}
function k_fast(state0: i64, a0: i64, b0: i64) -> i64 {
let mut state: i64 = state0
let mut a: i64 = a0
let mut b: i64 = b0
let mut cost: i64 = 0
while a != b {
if state == 2 {
if a > b {
let p: i64 = (a - 1) / (2 * b)
if p > 0 {
cost = cost + p
a = a - 2 * b * p
} else {
cost = cost + 1
state = 0
let na: i64 = b
let nb: i64 = a - b
a = na
b = nb
}
} else {
let p2: i64 = (b - 1) / (2 * a)
if p2 > 0 {
cost = cost + p2
b = b - 2 * a * p2
} else {
cost = cost + 1
state = 1
b = b - a
}
}
} else {
if state == 0 {
if a > b {
let p3: i64 = (a - 1) / (2 * b)
if p3 > 0 {
cost = cost + p3
a = a - 2 * b * p3
} else {
cost = cost + 1
state = 1
a = a - b
}
} else {
state = 2
let na2: i64 = b - a
let nb2: i64 = a
a = na2
b = nb2
}
} else {
if a > b {
state = 0
a = a - b
} else {
state = 2
b = b - a
}
}
}
}
if state == 2 {
return cost + 1
}
return cost
}
function F(A: i64, B: i64, C: i64) -> i64 {
if A == B + C {
return 3 * k_fast(0, B, C) + 1
}
if B == A + C {
return 3 * k_fast(1, A, C) + 2
}
if C == A + B {
return 3 * k_fast(2, A, B)
}
return 0
}
function main() -> i32 {
let mut total: i64 = 0
let mut a: i64 = 1
while a <= 7 {
let mut b: i64 = 1
while b <= 19 {
let A: i64 = ipow(a, b)
let B: i64 = ipow(b, a)
total = total + F(A, B, A + B)
b = b + 1
}
a = a + 1
}
printf("%lld\n", total)
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 ipow_i64_i64(int64_t base, int64_t exp);
int64_t k_fast_i64_i64_i64(int64_t state0, int64_t a0, int64_t b0);
int64_t F_i64_i64_i64(int64_t A, int64_t B, int64_t C);
int32_t main(void);
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) {
r = (r * b);
}
b = (b * b);
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t k_fast_i64_i64_i64(int64_t state0, int64_t a0, int64_t b0) {
int64_t state = state0;
int64_t a = a0;
int64_t b = b0;
int64_t cost = 0;
while (a != b) {
if (state == 2) {
if (a > b) {
int64_t p = FLOW_CHECKED_DIV(((a - 1)), ((2 * b)));
if (p > 0) {
cost = (cost + p);
a = (a - ((2 * b) * p));
} else {
cost = (cost + 1);
state = 0;
int64_t na = b;
int64_t nb = (a - b);
a = na;
b = nb;
}
} else {
int64_t p2 = FLOW_CHECKED_DIV(((b - 1)), ((2 * a)));
if (p2 > 0) {
cost = (cost + p2);
b = (b - ((2 * a) * p2));
} else {
cost = (cost + 1);
state = 1;
b = (b - a);
}
}
} else {
if (state == 0) {
if (a > b) {
int64_t p3 = FLOW_CHECKED_DIV(((a - 1)), ((2 * b)));
if (p3 > 0) {
cost = (cost + p3);
a = (a - ((2 * b) * p3));
} else {
cost = (cost + 1);
state = 1;
a = (a - b);
}
} else {
state = 2;
int64_t na2 = (b - a);
int64_t nb2 = a;
a = na2;
b = nb2;
}
} else {
if (a > b) {
state = 0;
a = (a - b);
} else {
state = 2;
b = (b - a);
}
}
}
}
if (state == 2) {
return (cost + 1);
}
return cost;
}
int64_t F_i64_i64_i64(int64_t A, int64_t B, int64_t C) {
if (A == (B + C)) {
return ((3 * k_fast_i64_i64_i64(0, B, C)) + 1);
}
if (B == (A + C)) {
return ((3 * k_fast_i64_i64_i64(1, A, C)) + 2);
}
if (C == (A + B)) {
return (3 * k_fast_i64_i64_i64(2, A, B));
}
return 0;
}
int32_t main(void) {
int64_t total = 0;
int64_t a = 1;
while (a <= 7) {
int64_t b = 1;
while (b <= 19) {
int64_t A = ipow_i64_i64(a, b);
int64_t B = ipow_i64_i64(b, a);
total = (total + F_i64_i64_i64(A, B, (A + B)));
b = (b + 1);
}
a = (a + 1);
}
printf("%lld\n", total);
return 0;
}