# Project Euler 457
# Polynomial mod p^2 — sum R(p) for primes p ≤ 10^7.
# Tonelli–Shanks + Hensel lift; i128 mulmod for modpow.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base0 % mod
if b < 0 { b = b + mod }
let mut e: i64 = exp0
while e > 0 {
if (e & 1) == 1 {
r = ((r as i128) * (b as i128) % (mod as i128)) as i64
}
b = ((b as i128) * (b as i128) % (mod as i128)) as i64
e = e / 2
}
return r
}
function modinv(a0: i64, m: i64) -> i64 {
let mut a: i64 = a0 % m
if a < 0 { a = a + m }
let mut b: i64 = m
let mut x0: i64 = 1
let mut x1: i64 = 0
while b != 0 {
let q: i64 = a / b
let t: i64 = a - q * b
a = b
b = t
let tx: i64 = x0 - q * x1
x0 = x1
x1 = tx
}
if x0 < 0 { x0 = x0 + m }
return x0
}
function legendre(a0: i64, p: i64) -> i64 {
let t: i64 = modpow(a0, (p - 1) / 2, p)
if t == p - 1 { return 0 - 1 }
return t
}
function tonelli(a0: i64, p: i64) -> i64 {
let mut a: i64 = a0 % p
if a < 0 { a = a + p }
if legendre(a, p) != 1 { return 0 }
if a == 0 { return 0 }
if p == 2 { return a }
if p % 4 == 3 {
return modpow(a, (p + 1) / 4, p)
}
let mut s: i64 = p - 1
let mut e: i64 = 0
while s % 2 == 0 {
s = s / 2
e = e + 1
}
let mut n: i64 = 2
while legendre(n, p) != 0 - 1 {
n = n + 1
}
let mut x: i64 = modpow(a, (s + 1) / 2, p)
let mut b: i64 = modpow(a, s, p)
let mut g: i64 = modpow(n, s, p)
let mut r: i64 = e
while true {
let mut t: i64 = b
let mut m: i64 = 0
while m < r {
if t == 1 { break }
t = ((t as i128) * (t as i128) % (p as i128)) as i64
m = m + 1
}
if m == 0 { return x }
let mut gs: i64 = g
let mut k: i64 = 0
while k < r - m - 1 {
gs = ((gs as i128) * (gs as i128) % (p as i128)) as i64
k = k + 1
}
g = ((gs as i128) * (gs as i128) % (p as i128)) as i64
x = ((x as i128) * (gs as i128) % (p as i128)) as i64
b = ((b as i128) * (g as i128) % (p as i128)) as i64
r = m
}
return 0
}
function R_p(p: i64) -> i64 {
if p == 3 { return 5 }
let r: i64 = tonelli(13, p)
if r == 0 { return 0 }
let fpr: i64 = (2 * r) % p
if fpr == 0 { return 0 }
let rr13: i64 = r * r - 13
let quot: i64 = rr13 / p
let inv: i64 = modinv(fpr, p)
let mut t: i64 = 0 - ((inv as i128) * (quot as i128) % (p as i128)) as i64
t = t % p
if t < 0 { t = t + p }
let pp: i64 = p * p
let mut n: i64 = (r + t * p) % pp
if n < 0 { n = n + pp }
if n % 2 != 0 {
return (n + 3) / 2
}
return (pp - n + 3) / 2
}
function main() -> i32 {
let LIMIT: i64 = 10000000
let sieve: ptr<i8> = calloc(LIMIT + 1, 1)
if sieve == null { return 1 }
let mut i: i64 = 0
while i <= LIMIT {
sieve[i] = 1
i = i + 1
}
sieve[0] = 0
sieve[1] = 0
i = 2
while i * i <= LIMIT {
if sieve[i] == 1 {
let mut j: i64 = i * i
while j <= LIMIT {
sieve[j] = 0
j = j + i
}
}
i = i + 1
}
let mut total: i64 = 0
let mut p: i64 = 3
while p <= LIMIT {
if sieve[p] == 1 {
if legendre(13, p) == 1 {
total = total + R_p(p)
}
}
p = p + 2
}
free(sieve)
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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t modinv_i64_i64(int64_t a0, int64_t m);
int64_t legendre_i64_i64(int64_t a0, int64_t p);
int64_t tonelli_i64_i64(int64_t a0, int64_t p);
int64_t R_p_i64(int64_t p);
int32_t main(void);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base0), (mod));
if (b < 0) {
b = (b + mod);
}
int64_t e = exp0;
while (e > 0) {
if ((e & 1) == 1) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
}
b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t modinv_i64_i64(int64_t a0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD((a0), (m));
if (a < 0) {
a = (a + m);
}
int64_t b = m;
int64_t x0 = 1;
int64_t x1 = 0;
while (b != 0) {
int64_t q = FLOW_CHECKED_DIV((a), (b));
int64_t t = (a - (q * b));
a = b;
b = t;
int64_t tx = (x0 - (q * x1));
x0 = x1;
x1 = tx;
}
if (x0 < 0) {
x0 = (x0 + m);
}
return x0;
}
int64_t legendre_i64_i64(int64_t a0, int64_t p) {
int64_t t = modpow_i64_i64_i64(a0, FLOW_CHECKED_DIV(((p - 1)), (2)), p);
if (t == (p - 1)) {
return (0 - 1);
}
return t;
}
int64_t tonelli_i64_i64(int64_t a0, int64_t p) {
int64_t a = FLOW_CHECKED_MOD((a0), (p));
if (a < 0) {
a = (a + p);
}
if (legendre_i64_i64(a, p) != 1) {
return 0;
}
if (a == 0) {
return 0;
}
if (p == 2) {
return a;
}
if (FLOW_CHECKED_MOD((p), (4)) == 3) {
return modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((p + 1)), (4)), p);
}
int64_t s = (p - 1);
int64_t e = 0;
while (FLOW_CHECKED_MOD((s), (2)) == 0) {
s = FLOW_CHECKED_DIV((s), (2));
e = (e + 1);
}
int64_t n = 2;
while (legendre_i64_i64(n, p) != (0 - 1)) {
n = (n + 1);
}
int64_t x = modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((s + 1)), (2)), p);
int64_t b = modpow_i64_i64_i64(a, s, p);
int64_t g = modpow_i64_i64_i64(n, s, p);
int64_t r = e;
while (1) {
int64_t t = b;
int64_t m = 0;
while (m < r) {
if (t == 1) {
break;
}
t = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(t)) * ((__int128)(t)))), (((__int128)(p))))));
m = (m + 1);
}
if (m == 0) {
return x;
}
int64_t gs = g;
int64_t k = 0;
while (k < ((r - m) - 1)) {
gs = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(gs)) * ((__int128)(gs)))), (((__int128)(p))))));
k = (k + 1);
}
g = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(gs)) * ((__int128)(gs)))), (((__int128)(p))))));
x = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(x)) * ((__int128)(gs)))), (((__int128)(p))))));
b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(g)))), (((__int128)(p))))));
r = m;
}
return 0;
}
int64_t R_p_i64(int64_t p) {
if (p == 3) {
return 5;
}
int64_t r = tonelli_i64_i64(13, p);
if (r == 0) {
return 0;
}
int64_t fpr = FLOW_CHECKED_MOD(((2 * r)), (p));
if (fpr == 0) {
return 0;
}
int64_t rr13 = ((r * r) - 13);
int64_t quot = FLOW_CHECKED_DIV((rr13), (p));
int64_t inv = modinv_i64_i64(fpr, p);
int64_t t = (0 - ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(inv)) * ((__int128)(quot)))), (((__int128)(p)))))));
t = FLOW_CHECKED_MOD((t), (p));
if (t < 0) {
t = (t + p);
}
int64_t pp = (p * p);
int64_t n = FLOW_CHECKED_MOD(((r + (t * p))), (pp));
if (n < 0) {
n = (n + pp);
}
if (FLOW_CHECKED_MOD((n), (2)) != 0) {
return FLOW_CHECKED_DIV(((n + 3)), (2));
}
return FLOW_CHECKED_DIV((((pp - n) + 3)), (2));
}
int32_t main(void) {
int64_t LIMIT = 10000000;
int8_t* sieve = (int8_t*)(calloc((LIMIT + 1), 1));
if (sieve == NULL) {
return 1;
}
int64_t i = 0;
while (i <= LIMIT) {
sieve[i] = 1;
i = (i + 1);
}
sieve[0] = 0;
sieve[1] = 0;
i = 2;
while ((i * i) <= LIMIT) {
if (sieve[i] == 1) {
int64_t j = (i * i);
while (j <= LIMIT) {
sieve[j] = 0;
j = (j + i);
}
}
i = (i + 1);
}
int64_t total = 0;
int64_t p = 3;
while (p <= LIMIT) {
if (sieve[p] == 1) {
if (legendre_i64_i64(13, p) == 1) {
total = (total + R_p_i64(p));
}
}
p = (p + 2);
}
free(sieve);
printf("%lld\n", total);
return 0;
}