# Project Euler 304
# Sum F(p) mod 1234567891011 for first 100000 primes p > 10^14.
function mulmod(a0: i64, b0: i64, mod: i64) -> i64 {
let mut a: i64 = a0 % mod
if a < 0 { a = a + mod }
let mut b: i64 = b0 % mod
if b < 0 { b = b + mod }
let mut r: i64 = 0
while b > 0 {
if (b & 1) == 1 {
r = r + a
if r >= mod { r = r - mod }
}
a = a * 2
if a >= mod { a = a - mod }
b = b >> 1
}
return r
}
function modpow(base: i64, exp: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base % mod
let mut e: i64 = exp
while e > 0 {
if (e & 1) == 1 { r = mulmod(r, b, mod) }
b = mulmod(b, b, mod)
e = e >> 1
}
return r
}
function is_prime(p: i64) -> bool {
if p < 2 { return false }
if p % 2 == 0 { return p == 2 }
if p % 3 == 0 { return p == 3 }
if p % 5 == 0 { return p == 5 }
if p % 7 == 0 { return p == 7 }
if p % 11 == 0 { return p == 11 }
if p % 13 == 0 { return p == 13 }
if p % 17 == 0 { return p == 17 }
let mut d: i64 = p - 1
let mut shift: i32 = 0
while (d & 1) == 0 {
d = d >> 1
shift = shift + 1
}
# bases for 64-bit
# fixed arrays may fail — use sequential checks
let mut bi: i32 = 0
while bi < 7 {
let base: i64 = 0
match bi {
0 => { base = 2 }
1 => { base = 325 }
2 => { base = 9375 }
3 => { base = 28178 }
4 => { base = 450775 }
5 => { base = 9780504 }
_ => { base = 1795265022 }
}
let a: i64 = base % p
if a == 0 {
bi = bi + 1
continue
}
let mut x: i64 = modpow(a, d, p)
if x == 1 || x == p - 1 {
bi = bi + 1
continue
}
let mut ok: bool = false
let mut r: i32 = 0
while r < shift - 1 {
x = mulmod(x, x, p)
if x == p - 1 {
ok = true
break
}
if x == 1 { return false }
r = r + 1
}
if !ok { return false }
bi = bi + 1
}
return true
}
function fibonacci(n0: i64, mod: i64) -> i64 {
if n0 == 0 { return 0 }
# find top bit
let mut bit: i64 = 1
while bit <= n0 / 2 {
bit = bit << 1
}
let mut a: i64 = 0
let mut b: i64 = 1
while bit > 0 {
let next_a: i64 = mulmod(a, (2 * b - a + mod) % mod, mod)
b = (mulmod(a, a, mod) + mulmod(b, b, mod)) % mod
a = next_a
if (n0 & bit) != 0 {
let na: i64 = b
b = (a + b) % mod
a = na
}
bit = bit >> 1
}
return a
}
function main() -> i32 {
let mod: i64 = 1234567891011
let mut n: i64 = 100000000000000
let mut last: i64 = fibonacci(n - 1, mod)
let mut current: i64 = fibonacci(n, mod)
let mut total: i64 = 0
let mut count: i64 = 0
while count < 100000 {
n = n + 1
let next_v: i64 = (last + current) % mod
last = current
current = next_v
if is_prime(n) {
total = (total + current) % mod
count = count + 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 mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t p);
int64_t fibonacci_i64_i64(int64_t n0, int64_t mod);
int32_t main(void);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
if (a < 0) {
a = (a + mod);
}
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
if (b < 0) {
b = (b + mod);
}
int64_t r = 0;
while (b > 0) {
if ((b & 1) == 1) {
r = (r + a);
if (r >= mod) {
r = (r - mod);
}
}
a = (a * 2);
if (a >= mod) {
a = (a - mod);
}
b = FLOW_CHECKED_SHR((b), (1));
}
return r;
}
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if ((e & 1) == 1) {
r = mulmod_i64_i64_i64(r, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_SHR((e), (1));
}
return r;
}
bool is_prime_i64(int64_t p) {
if (p < 2) {
return 0;
}
if (FLOW_CHECKED_MOD((p), (2)) == 0) {
return p == 2;
}
if (FLOW_CHECKED_MOD((p), (3)) == 0) {
return p == 3;
}
if (FLOW_CHECKED_MOD((p), (5)) == 0) {
return p == 5;
}
if (FLOW_CHECKED_MOD((p), (7)) == 0) {
return p == 7;
}
if (FLOW_CHECKED_MOD((p), (11)) == 0) {
return p == 11;
}
if (FLOW_CHECKED_MOD((p), (13)) == 0) {
return p == 13;
}
if (FLOW_CHECKED_MOD((p), (17)) == 0) {
return p == 17;
}
int64_t d = (p - 1);
int32_t shift = 0;
while ((d & 1) == 0) {
d = FLOW_CHECKED_SHR((d), (1));
shift = (shift + 1);
}
int32_t bi = 0;
while (bi < 7) {
int64_t base = 0;
{ // match block
if ((bi) == 0) {
base = 2;
} else if ((bi) == 1) {
base = 325;
} else if ((bi) == 2) {
base = 9375;
} else if ((bi) == 3) {
base = 28178;
} else if ((bi) == 4) {
base = 450775;
} else if ((bi) == 5) {
base = 9780504;
} else { // exhaustive
base = 1795265022;
}
} // end match
int64_t a = FLOW_CHECKED_MOD((base), (p));
if (a == 0) {
bi = (bi + 1);
continue;
}
int64_t x = modpow_i64_i64_i64(a, d, p);
if ((x == 1 || x == (p - 1))) {
bi = (bi + 1);
continue;
}
bool ok = 0;
int32_t r = 0;
while (r < (shift - 1)) {
x = mulmod_i64_i64_i64(x, x, p);
if (x == (p - 1)) {
ok = 1;
break;
}
if (x == 1) {
return 0;
}
r = (r + 1);
}
if ((!(ok))) {
return 0;
}
bi = (bi + 1);
}
return 1;
}
int64_t fibonacci_i64_i64(int64_t n0, int64_t mod) {
if (n0 == 0) {
return 0;
}
int64_t bit = 1;
while (bit <= FLOW_CHECKED_DIV((n0), (2))) {
bit = FLOW_CHECKED_SHL((bit), (1));
}
int64_t a = 0;
int64_t b = 1;
while (bit > 0) {
int64_t next_a = mulmod_i64_i64_i64(a, FLOW_CHECKED_MOD(((((2 * b) - a) + mod)), (mod)), mod);
b = FLOW_CHECKED_MOD(((mulmod_i64_i64_i64(a, a, mod) + mulmod_i64_i64_i64(b, b, mod))), (mod));
a = next_a;
if ((n0 & bit) != 0) {
int64_t na = b;
b = FLOW_CHECKED_MOD(((a + b)), (mod));
a = na;
}
bit = FLOW_CHECKED_SHR((bit), (1));
}
return a;
}
int32_t main(void) {
int64_t mod = 1234567891011;
int64_t n = 100000000000000;
int64_t last = fibonacci_i64_i64((n - 1), mod);
int64_t current = fibonacci_i64_i64(n, mod);
int64_t total = 0;
int64_t count = 0;
while (count < 100000) {
n = (n + 1);
int64_t next_v = FLOW_CHECKED_MOD(((last + current)), (mod));
last = current;
current = next_v;
if (is_prime_i64(n)) {
total = FLOW_CHECKED_MOD(((total + current)), (mod));
count = (count + 1);
}
}
printf("%lld\n", total);
return 0;
}