# Project Euler 841
# Regular Star Polygons — sum A(F_{n+1}, F_{n-1}) for n=3..34.
extern {
function cos(x: f64) -> f64
function sin(x: f64) -> f64
}
const PI: f64 = 3.14159265358979323846
function A(p: i64, q: i64) -> f64 {
let s: f64 = PI / (p as f64)
let p_sin_s: f64 = (p as f64) * sin(s)
let mut sign_odd: f64 = -1.0
if (q & 1) != 0 { sign_odd = 1.0 }
let mut cos_prev: f64 = 1.0
let mut C: f64 = 0.0
let mut c: f64 = 0.0
let mut k: i64 = 1
while k <= q {
let cos_k: f64 = cos((k as f64) * s)
let mut sign: f64 = -sign_odd
if (k & 1) != 0 { sign = sign_odd }
let term: f64 = sign / (cos_k * cos_prev)
let y: f64 = term - c
let t: f64 = C + y
c = (t - C) - y
C = t
cos_prev = cos_k
k = k + 1
}
return p_sin_s * C
}
function main() -> i32 {
let mut F: [i64; 40]
F[0] = 0
F[1] = 1
let mut i: i64 = 2
while i <= 35 {
F[i] = F[i - 1] + F[i - 2]
i = i + 1
}
let mut total: f64 = 0.0
let mut c: f64 = 0.0
let mut n: i64 = 3
while n <= 34 {
let a: f64 = A(F[n + 1], F[n - 1])
let y: f64 = a - c
let t: f64 = total + y
c = (t - total) - y
total = t
n = n + 1
}
printf("%.10f\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; }
double A_i64_i64(int64_t p, int64_t q);
int32_t main(void);
static const double PI = 3.14159265358979323846;
double A_i64_i64(int64_t p, int64_t q) {
double s = (PI / ((double)(p)));
double p_sin_s = (((double)(p)) * sin(s));
double sign_odd = (-1.0);
if ((q & 1) != 0) {
sign_odd = 1.0;
}
double cos_prev = 1.0;
double C = 0.0;
double c = 0.0;
int64_t k = 1;
while (k <= q) {
double cos_k = cos((((double)(k)) * s));
double sign = (-sign_odd);
if ((k & 1) != 0) {
sign = sign_odd;
}
double term = (sign / (cos_k * cos_prev));
double y = (term - c);
double t = (C + y);
c = ((t - C) - y);
C = t;
cos_prev = cos_k;
k = (k + 1);
}
return (p_sin_s * C);
}
int32_t main(void) {
int64_t F[40];
F[0] = 0;
F[1] = 1;
int64_t i = 2;
while (i <= 35) {
F[i] = ((((unsigned)((i - 1)) < 40) ? F[(i - 1)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((i - 1)), 40), flow_fault_handler("array index out of bounds"), F[0])) + (((unsigned)((i - 2)) < 40) ? F[(i - 2)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((i - 2)), 40), flow_fault_handler("array index out of bounds"), F[0])));
i = (i + 1);
}
double total = 0.0;
double c = 0.0;
int64_t n = 3;
while (n <= 34) {
double a = A_i64_i64((((unsigned)((n + 1)) < 40) ? F[(n + 1)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((n + 1)), 40), flow_fault_handler("array index out of bounds"), F[0])), (((unsigned)((n - 1)) < 40) ? F[(n - 1)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((n - 1)), 40), flow_fault_handler("array index out of bounds"), F[0])));
double y = (a - c);
double t = (total + y);
c = ((t - total) - y);
total = t;
n = (n + 1);
}
printf("%.10f\n", total);
return 0;
}