Problem 841

Regular Star Polygons — sum A(F_{n+1}, F_{n-1}) for n=3..34.

Answer381.7860132854
Output381.7860132854
StatusPASS
Native helperno
Runtime50 ms
Peak memory1152 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)?
Space complexityO(1)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# 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;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.10f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
  func.func private @cos(f64) -> f64
  func.func private @sin(f64) -> f64
  // Constant: PI
  llvm.mlir.global internal constant @PI(3.14159265358979323846 : f64) : f64
  func.func @A(%arg0: i64, %arg1: i64) -> f64 {
    %0 = llvm.mlir.addressof @PI : !llvm.ptr
    %1 = llvm.load %0 : !llvm.ptr -> f64
    %2 = arith.sitofp %arg0 : i64 to f64
    %3 = arith.divf %1, %2 : f64
    %4 = arith.sitofp %arg0 : i64 to f64
    %5 = math.sin %3 : f64
    %6 = arith.mulf %4, %5 : f64
    %7 = arith.constant 1.0 : f32
    %8 = arith.negf %7 : f32
    %9 = arith.extf %8 : f32 to f64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x f64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : f64, !llvm.ptr
    %12 = arith.constant 1 : i32
    %14 = arith.extsi %12 : i32 to i64
    %13 = arith.andi %arg1, %14 : i64
    %15 = arith.constant 0 : i32
    %17 = arith.extsi %15 : i32 to i64
    %16 = arith.cmpi ne, %13, %17 : i64
    cf.cond_br %16, ^bb0, ^bb1
    ^bb0:
      %18 = arith.constant 1.0 : f32
      %19 = arith.extf %18 : f32 to f64
      llvm.store %19, %11 : f64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %20 = arith.constant 1.0 : f32
    %21 = arith.extf %20 : f32 to f64
    %22 = llvm.mlir.constant(1 : i64) : i64
    %23 = llvm.alloca %22 x f64 : (i64) -> !llvm.ptr
    llvm.store %21, %23 : f64, !llvm.ptr
    %24 = arith.constant 0.0 : f32
    %25 = arith.extf %24 : f32 to f64
    %26 = llvm.mlir.constant(1 : i64) : i64
    %27 = llvm.alloca %26 x f64 : (i64) -> !llvm.ptr
    llvm.store %25, %27 : f64, !llvm.ptr
    %28 = arith.constant 0.0 : f32
    %29 = arith.extf %28 : f32 to f64
    %30 = llvm.mlir.constant(1 : i64) : i64
    %31 = llvm.alloca %30 x f64 : (i64) -> !llvm.ptr
    llvm.store %29, %31 : f64, !llvm.ptr
    %32 = arith.constant 1 : i32
    %33 = arith.extsi %32 : i32 to i64
    %34 = llvm.mlir.constant(1 : i64) : i64
    %35 = llvm.alloca %34 x i64 : (i64) -> !llvm.ptr
    llvm.store %33, %35 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %36 = llvm.load %35 : !llvm.ptr -> i64
    %37 = arith.cmpi sle, %36, %arg1 : i64
    cf.cond_br %37, ^bb4, ^bb5
    ^bb4:
      %38 = llvm.load %35 : !llvm.ptr -> i64
      %39 = arith.sitofp %38 : i64 to f64
      %40 = arith.mulf %39, %3 : f64
      %41 = math.cos %40 : f64
      %42 = llvm.load %11 : !llvm.ptr -> f64
      %43 = arith.negf %42 : f64
      %44 = llvm.mlir.constant(1 : i64) : i64
      %45 = llvm.alloca %44 x f64 : (i64) -> !llvm.ptr
      llvm.store %43, %45 : f64, !llvm.ptr
      %46 = llvm.load %35 : !llvm.ptr -> i64
      %47 = arith.constant 1 : i32
      %49 = arith.extsi %47 : i32 to i64
      %48 = arith.andi %46, %49 : i64
      %50 = arith.constant 0 : i32
      %52 = arith.extsi %50 : i32 to i64
      %51 = arith.cmpi ne, %48, %52 : i64
      cf.cond_br %51, ^bb6, ^bb7
      ^bb6:
        %53 = llvm.load %11 : !llvm.ptr -> f64
        llvm.store %53, %45 : f64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %54 = llvm.load %45 : !llvm.ptr -> f64
      %55 = llvm.load %23 : !llvm.ptr -> f64
      %56 = arith.mulf %41, %55 : f64
      %57 = arith.divf %54, %56 : f64
      %58 = llvm.load %31 : !llvm.ptr -> f64
      %59 = arith.subf %57, %58 : f64
      %60 = llvm.load %27 : !llvm.ptr -> f64
      %61 = arith.addf %60, %59 : f64
      %62 = llvm.load %27 : !llvm.ptr -> f64
      %63 = arith.subf %61, %62 : f64
      %64 = arith.subf %63, %59 : f64
      llvm.store %64, %31 : f64, !llvm.ptr
      llvm.store %61, %27 : f64, !llvm.ptr
      llvm.store %41, %23 : f64, !llvm.ptr
      %65 = llvm.load %35 : !llvm.ptr -> i64
      %66 = arith.constant 1 : i32
      %68 = arith.extsi %66 : i32 to i64
      %67 = arith.addi %65, %68 : i64
      llvm.store %67, %35 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %69 = llvm.load %27 : !llvm.ptr -> f64
    %70 = arith.mulf %6, %69 : f64
    func.return %70 : f64
  }
  func.func @main() -> i32 {
    %71 = memref.alloca() {type = memref<40xi64>} : memref<40xi64>
    %72 = arith.constant 0 : i32
    %73 = arith.constant 0 : i32
    %74 = arith.index_cast %73 : i32 to index
    %75 = arith.extsi %72 : i32 to i64
    memref.store %75, %71[%74] : memref<40xi64>
    %76 = arith.constant 1 : i32
    %77 = arith.constant 1 : i32
    %78 = arith.index_cast %77 : i32 to index
    %79 = arith.extsi %76 : i32 to i64
    memref.store %79, %71[%78] : memref<40xi64>
    %80 = arith.constant 2 : i32
    %81 = arith.extsi %80 : i32 to i64
    %82 = llvm.mlir.constant(1 : i64) : i64
    %83 = llvm.alloca %82 x i64 : (i64) -> !llvm.ptr
    llvm.store %81, %83 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %84 = llvm.load %83 : !llvm.ptr -> i64
    %85 = arith.constant 35 : i32
    %87 = arith.extsi %85 : i32 to i64
    %86 = arith.cmpi sle, %84, %87 : i64
    cf.cond_br %86, ^bb10, ^bb11
    ^bb10:
      %89 = llvm.load %83 : !llvm.ptr -> i64
      %90 = arith.constant 1 : i32
      %92 = arith.extsi %90 : i32 to i64
      %91 = arith.subi %89, %92 : i64
      %93 = arith.index_cast %91 : i32 to index
      %88 = memref.load %71[%93] : memref<40xi64>
      %95 = llvm.load %83 : !llvm.ptr -> i64
      %96 = arith.constant 2 : i32
      %98 = arith.extsi %96 : i32 to i64
      %97 = arith.subi %95, %98 : i64
      %99 = arith.index_cast %97 : i32 to index
      %94 = memref.load %71[%99] : memref<40xi64>
      %100 = arith.addi %88, %94 : i64
      %101 = llvm.load %83 : !llvm.ptr -> i64
      %102 = arith.index_cast %101 : i32 to index
      memref.store %100, %71[%102] : memref<40xi64>
      %103 = llvm.load %83 : !llvm.ptr -> i64
      %104 = arith.constant 1 : i32
      %106 = arith.extsi %104 : i32 to i64
      %105 = arith.addi %103, %106 : i64
      llvm.store %105, %83 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %107 = arith.constant 0.0 : f32
    %108 = arith.extf %107 : f32 to f64
    %109 = llvm.mlir.constant(1 : i64) : i64
    %110 = llvm.alloca %109 x f64 : (i64) -> !llvm.ptr
    llvm.store %108, %110 : f64, !llvm.ptr
    %111 = arith.constant 0.0 : f32
    %112 = arith.extf %111 : f32 to f64
    %113 = llvm.mlir.constant(1 : i64) : i64
    %114 = llvm.alloca %113 x f64 : (i64) -> !llvm.ptr
    llvm.store %112, %114 : f64, !llvm.ptr
    %115 = arith.constant 3 : i32
    %116 = arith.extsi %115 : i32 to i64
    %117 = llvm.mlir.constant(1 : i64) : i64
    %118 = llvm.alloca %117 x i64 : (i64) -> !llvm.ptr
    llvm.store %116, %118 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %119 = llvm.load %118 : !llvm.ptr -> i64
    %120 = arith.constant 34 : i32
    %122 = arith.extsi %120 : i32 to i64
    %121 = arith.cmpi sle, %119, %122 : i64
    cf.cond_br %121, ^bb13, ^bb14
    ^bb13:
      %125 = llvm.load %118 : !llvm.ptr -> i64
      %126 = arith.constant 1 : i32
      %128 = arith.extsi %126 : i32 to i64
      %127 = arith.addi %125, %128 : i64
      %129 = arith.index_cast %127 : i32 to index
      %124 = memref.load %71[%129] : memref<40xi64>
      %131 = llvm.load %118 : !llvm.ptr -> i64
      %132 = arith.constant 1 : i32
      %134 = arith.extsi %132 : i32 to i64
      %133 = arith.subi %131, %134 : i64
      %135 = arith.index_cast %133 : i32 to index
      %130 = memref.load %71[%135] : memref<40xi64>
      %123 = func.call @A(%124, %130) : (i64, i64) -> f64
      %136 = llvm.load %114 : !llvm.ptr -> f64
      %137 = arith.subf %123, %136 : f64
      %138 = llvm.load %110 : !llvm.ptr -> f64
      %139 = arith.addf %138, %137 : f64
      %140 = llvm.load %110 : !llvm.ptr -> f64
      %141 = arith.subf %139, %140 : f64
      %142 = arith.subf %141, %137 : f64
      llvm.store %142, %114 : f64, !llvm.ptr
      llvm.store %139, %110 : f64, !llvm.ptr
      %143 = llvm.load %118 : !llvm.ptr -> i64
      %144 = arith.constant 1 : i32
      %146 = arith.extsi %144 : i32 to i64
      %145 = arith.addi %143, %146 : i64
      llvm.store %145, %118 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %147 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %148 = llvm.load %110 : !llvm.ptr -> f64
    %149 = llvm.call @printf(%147, %148) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %150 = arith.constant 0 : i32
    func.return %150 : i32
  }
}