# Project Euler 900
# DistribuNim II — S(N) via order-5 linear recurrence mod 900497239.
const MOD: i64 = 900497239
function next_pow2_gt(x: i64) -> i64 {
let mut p: i64 = 1
while p <= x {
p = p << 1
}
return p
}
function t(n: i64) -> i64 {
let p: i64 = next_pow2_gt(n)
let mut v: i64 = (-(n * n) - (n & 1)) % p
if v < 0 { v = v + p }
return v
}
function S_mod(N: i64) -> i64 {
# Seed with exact S(1)..S(5) from closed form (sum to 2^5=32).
let mut S: [i64; 6]
S[0] = 0
let mut total: i64 = 0
let mut n: i64 = 1
while n <= 32 {
total = total + t(n)
if n == 2 { S[1] = total }
if n == 4 { S[2] = total }
if n == 8 { S[3] = total }
if n == 16 { S[4] = total }
if n == 32 { S[5] = total }
n = n + 1
}
if N <= 5 {
return S[N] % MOD
}
let mut p0: i64 = S[5] % MOD
let mut p1: i64 = S[4] % MOD
let mut p2: i64 = S[3] % MOD
let mut p3: i64 = S[2] % MOD
let mut p4: i64 = S[1] % MOD
let mut k: i64 = 6
while k <= N {
let mut neu: i64 = (7 * p0 - 6 * p1 - 48 * p2 + 112 * p3 - 64 * p4) % MOD
if neu < 0 { neu = neu + MOD }
p4 = p3
p3 = p2
p2 = p1
p1 = p0
p0 = neu
k = k + 1
}
return p0
}
function main() -> i32 {
printf("%lld\n", S_mod(10000))
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 next_pow2_gt_i64(int64_t x);
int64_t t_i64(int64_t n);
int64_t S_mod_i64(int64_t N);
int32_t main(void);
static const int64_t MOD = 900497239;
int64_t next_pow2_gt_i64(int64_t x) {
int64_t p = 1;
while (p <= x) {
p = FLOW_CHECKED_SHL((p), (1));
}
return p;
}
int64_t t_i64(int64_t n) {
int64_t p = next_pow2_gt_i64(n);
int64_t v = FLOW_CHECKED_MOD((((-(n * n)) - (n & 1))), (p));
if (v < 0) {
v = (v + p);
}
return v;
}
int64_t S_mod_i64(int64_t N) {
int64_t S[6];
S[0] = 0;
int64_t total = 0;
int64_t n = 1;
while (n <= 32) {
total = (total + t_i64(n));
if (n == 2) {
S[1] = total;
}
if (n == 4) {
S[2] = total;
}
if (n == 8) {
S[3] = total;
}
if (n == 16) {
S[4] = total;
}
if (n == 32) {
S[5] = total;
}
n = (n + 1);
}
if (N <= 5) {
return FLOW_CHECKED_MOD(((((unsigned)(N) < 6) ? S[N] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(N), 6), flow_fault_handler("array index out of bounds"), S[0]))), (MOD));
}
int64_t p0 = FLOW_CHECKED_MOD(((((unsigned)(5) < 6) ? S[5] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(5), 6), flow_fault_handler("array index out of bounds"), S[0]))), (MOD));
int64_t p1 = FLOW_CHECKED_MOD(((((unsigned)(4) < 6) ? S[4] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(4), 6), flow_fault_handler("array index out of bounds"), S[0]))), (MOD));
int64_t p2 = FLOW_CHECKED_MOD(((((unsigned)(3) < 6) ? S[3] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(3), 6), flow_fault_handler("array index out of bounds"), S[0]))), (MOD));
int64_t p3 = FLOW_CHECKED_MOD(((((unsigned)(2) < 6) ? S[2] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(2), 6), flow_fault_handler("array index out of bounds"), S[0]))), (MOD));
int64_t p4 = FLOW_CHECKED_MOD(((((unsigned)(1) < 6) ? S[1] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(1), 6), flow_fault_handler("array index out of bounds"), S[0]))), (MOD));
int64_t k = 6;
while (k <= N) {
int64_t neu = FLOW_CHECKED_MOD(((((((7 * p0) - (6 * p1)) - (48 * p2)) + (112 * p3)) - (64 * p4))), (MOD));
if (neu < 0) {
neu = (neu + MOD);
}
p4 = p3;
p3 = p2;
p2 = p1;
p1 = p0;
p0 = neu;
k = (k + 1);
}
return p0;
}
int32_t main(void) {
printf("%lld\n", S_mod_i64(10000));
return 0;
}