# Project Euler 748
# Upside Down Diophantine Equation: last 9 digits of S(10^16).
# Pure Flow port of the Gaussian integer parametrization.
function gcd_ll(a: i64, b: i64) -> i64 {
let mut x: i64 = a
if x < 0 { x = 0 - x }
let mut y: i64 = b
if y < 0 { y = 0 - y }
while y != 0 {
let t: i64 = x % y
x = y
y = t
}
return x
}
function isqrt128(n: i128) -> i128 {
if n == 0 { return 0 }
let mut x: i128 = n
let mut y: i128 = (x + 1) / 2
while y < x {
x = y
y = (x + n / x) / 2
}
return x
}
function fourth_root_floor128(n: i128) -> i128 {
if n == 0 { return 0 }
let mut x: i128 = n |> isqrt128 |> isqrt128
let mut xp1: i128 = x + 1
while xp1 * xp1 * xp1 * xp1 <= n {
x = xp1
xp1 = x + 1
}
while x * x * x * x > n {
x = x - 1
}
return x
}
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
}
function main() -> i32 {
let N: i64 = 10000000000000000
let modv: i64 = 1000000000
let Nsq: i128 = (N as i128) * (N as i128)
let val: i128 = 2 * Nsq / 13
let r_max: i64 = (fourth_root_floor128(val)) as i64
let m_max: i64 = isqrt64(r_max)
let mut total: i64 = 0
let THRESH: i64 = 1000000000000000000
let mut m: i64 = 1
while m <= m_max {
let mm: i64 = m * m
let n_max: i64 = isqrt64(r_max - mm)
let n_start: i64 = 0
if (m & 1) == 0 {
n_start = 1
}
let mut n: i64 = n_start
while n <= n_max {
if gcd_ll(m, n) != 1 {
n = n + 2
continue
}
let nn: i64 = n * n
let r: i64 = mm + nn
let u: i64 = mm - nn
let v: i64 = 2 * m * n
let mut a: i64 = 3 * u - 2 * v
if a < 0 { a = 0 - a }
let mut b: i64 = 3 * v + 2 * u
if b < 0 { b = 0 - b }
let mut p: i64 = a
let mut q: i64 = b
if a < b {
p = b
q = a
}
if p % 13 == 0 && q % 13 == 0 {
n = n + 2
continue
}
let x: i64 = q * r
let y: i64 = p * r
if x > N || y > N {
n = n + 2
continue
}
let z: i64 = p * q
if z > N {
n = n + 2
continue
}
total = total + x + y + z
if total >= THRESH {
total = total % modv
}
n = n + 2
}
m = m + 1
}
printf("%lld\n", total % modv)
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 gcd_ll_i64_i64(int64_t a, int64_t b);
__int128 isqrt128_i128(__int128 n);
__int128 fourth_root_floor128_i128(__int128 n);
int64_t isqrt64_i64(int64_t n);
int32_t main(void);
int64_t gcd_ll_i64_i64(int64_t a, int64_t b) {
int64_t x = a;
if (x < 0) {
x = (0 - x);
}
int64_t y = b;
if (y < 0) {
y = (0 - y);
}
while (y != 0) {
int64_t t = FLOW_CHECKED_MOD((x), (y));
x = y;
y = t;
}
return x;
}
__int128 isqrt128_i128(__int128 n) {
if (n == 0) {
return 0;
}
__int128 x = n;
__int128 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;
}
__int128 fourth_root_floor128_i128(__int128 n) {
if (n == 0) {
return 0;
}
__int128 x = isqrt128_i128(isqrt128_i128(n));
__int128 xp1 = (x + 1);
while ((((xp1 * xp1) * xp1) * xp1) <= n) {
x = xp1;
xp1 = (x + 1);
}
while ((((x * x) * x) * x) > n) {
x = (x - 1);
}
return x;
}
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;
}
int32_t main(void) {
int64_t N = 10000000000000000;
int64_t modv = 1000000000;
__int128 Nsq = (((__int128)(N)) * ((__int128)(N)));
__int128 val = FLOW_CHECKED_DIV(((2 * Nsq)), (13));
int64_t r_max = ((int64_t)(fourth_root_floor128_i128(val)));
int64_t m_max = isqrt64_i64(r_max);
int64_t total = 0;
int64_t THRESH = 1000000000000000000;
int64_t m = 1;
while (m <= m_max) {
int64_t mm = (m * m);
int64_t n_max = isqrt64_i64((r_max - mm));
int64_t n_start = 0;
if ((m & 1) == 0) {
n_start = 1;
}
int64_t n = n_start;
while (n <= n_max) {
if (gcd_ll_i64_i64(m, n) != 1) {
n = (n + 2);
continue;
}
int64_t nn = (n * n);
int64_t r = (mm + nn);
int64_t u = (mm - nn);
int64_t v = ((2 * m) * n);
int64_t a = ((3 * u) - (2 * v));
if (a < 0) {
a = (0 - a);
}
int64_t b = ((3 * v) + (2 * u));
if (b < 0) {
b = (0 - b);
}
int64_t p = a;
int64_t q = b;
if (a < b) {
p = b;
q = a;
}
if ((FLOW_CHECKED_MOD((p), (13)) == 0 && FLOW_CHECKED_MOD((q), (13)) == 0)) {
n = (n + 2);
continue;
}
int64_t x = (q * r);
int64_t y = (p * r);
if ((x > N || y > N)) {
n = (n + 2);
continue;
}
int64_t z = (p * q);
if (z > N) {
n = (n + 2);
continue;
}
total = (((total + x) + y) + z);
if (total >= THRESH) {
total = FLOW_CHECKED_MOD((total), (modv));
}
n = (n + 2);
}
m = (m + 1);
}
printf("%lld\n", FLOW_CHECKED_MOD((total), (modv)));
return 0;
}