# Project Euler 166
# 4x4 grids of digits 0-9 with equal row/column/diagonal sums.
function main() -> i32 {
let mut ans: i64 = 0
let mut b: i32 = 0
while b <= 9 {
let mut c: i32 = 0
while c <= 9 {
let mut d: i32 = 0
while d <= 9 {
let mut e: i32 = 0
while e <= 9 {
let mut ii: i32 = 0
while ii <= 9 {
let m: i32 = b + c + d - e - ii
if m >= 0 && m <= 9 {
let mut k: i32 = 0
while k <= 9 {
let f: i32 = b + c + d * 2 - e - ii - k
if f >= 0 && f <= 9 {
let mut a: i32 = 0
while a <= 9 {
let mut g: i32 = 0
while g <= 9 {
let o: i32 = a + b + d - g - k
if o >= 0 && o <= 9 {
let j: i32 = a + b + c - g - m
if j >= 0 && j <= 9 {
let l: i32 = a + b + c + d - ii - j - k
if l >= 0 && l <= 9 {
let h: i32 = a + b + c + d - e - f - g
if h >= 0 && h <= 9 {
let n: i32 = a + c + d - f - j
if n >= 0 && n <= 9 {
let p: i32 = a + b + c - h - l
if p >= 0 && p <= 9 {
ans = ans + 1
}
}
}
}
}
}
g = g + 1
}
a = a + 1
}
}
k = k + 1
}
}
ii = ii + 1
}
e = e + 1
}
d = d + 1
}
c = c + 1
}
b = b + 1
}
printf("%lld\n", ans)
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 main(void);
int32_t main(void) {
int64_t ans = 0;
int32_t b = 0;
while (b <= 9) {
int32_t c = 0;
while (c <= 9) {
int32_t d = 0;
while (d <= 9) {
int32_t e = 0;
while (e <= 9) {
int32_t ii = 0;
while (ii <= 9) {
int32_t m = ((((b + c) + d) - e) - ii);
if ((m >= 0 && m <= 9)) {
int32_t k = 0;
while (k <= 9) {
int32_t f = (((((b + c) + (d * 2)) - e) - ii) - k);
if ((f >= 0 && f <= 9)) {
int32_t a = 0;
while (a <= 9) {
int32_t g = 0;
while (g <= 9) {
int32_t o = ((((a + b) + d) - g) - k);
if ((o >= 0 && o <= 9)) {
int32_t j = ((((a + b) + c) - g) - m);
if ((j >= 0 && j <= 9)) {
int32_t l = ((((((a + b) + c) + d) - ii) - j) - k);
if ((l >= 0 && l <= 9)) {
int32_t h = ((((((a + b) + c) + d) - e) - f) - g);
if ((h >= 0 && h <= 9)) {
int32_t n = ((((a + c) + d) - f) - j);
if ((n >= 0 && n <= 9)) {
int32_t p = ((((a + b) + c) - h) - l);
if ((p >= 0 && p <= 9)) {
ans = (ans + 1);
}
}
}
}
}
}
g = (g + 1);
}
a = (a + 1);
}
}
k = (k + 1);
}
}
ii = (ii + 1);
}
e = (e + 1);
}
d = (d + 1);
}
c = (c + 1);
}
b = (b + 1);
}
printf("%lld\n", ans);
return 0;
}