Problem 761
Runner and spider geometry for n=6? Problem asks specific n — check answer format 5.05505046
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | ? |
| Space complexity | O(1) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 761
# Runner and spider geometry for n=6? Problem asks specific n — check answer format 5.05505046
extern {
function acos(x: f64) -> f64
function cos(x: f64) -> f64
function sin(x: f64) -> f64
function tan(x: f64) -> f64
}
const PI: f64 = 3.14159265358979323846
function compute(n: i64) -> f64 {
let theta: f64 = PI / (n as f64)
let tangent: f64 = tan(theta)
let mut branch: i64 = 0
let mut k: i64 = 0
while k <= n {
let value: f64 = sin((k as f64) * theta) - ((k + n) as f64) * tangent * cos((k as f64) * theta)
if value >= 0.0 {
branch = k - 1
break
}
k = k + 1
}
let mut argument: f64 = 2.0 * sin((branch as f64) * theta) / (((branch + n) as f64) * tangent) - cos((branch as f64) * theta)
if argument > 1.0 { argument = 1.0 }
if argument < -1.0 { argument = -1.0 }
let alpha: f64 = ((branch as f64) * theta + acos(argument)) / 2.0
return 1.0 / cos(alpha)
}
function main() -> i32 {
printf("%.8f\n", compute(6))
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 acos(double x);
double compute_i64(int64_t n);
int32_t main(void);
static const double PI = 3.14159265358979323846;
double compute_i64(int64_t n) {
double theta = (PI / ((double)(n)));
double tangent = tan(theta);
int64_t branch = 0;
int64_t k = 0;
while (k <= n) {
double value = (sin((((double)(k)) * theta)) - ((((double)((k + n))) * tangent) * cos((((double)(k)) * theta))));
if (value >= 0.0) {
branch = (k - 1);
break;
}
k = (k + 1);
}
double argument = (((2.0 * sin((((double)(branch)) * theta))) / (((double)((branch + n))) * tangent)) - cos((((double)(branch)) * theta)));
if (argument > 1.0) {
argument = 1.0;
}
if (argument < (-1.0)) {
argument = (-1.0);
}
double alpha = (((((double)(branch)) * theta) + acos(argument)) / 2.0);
return (1.0 / cos(alpha));
}
int32_t main(void) {
printf("%.8f\n", compute_i64(6));
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.8f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @acos(f64) -> f64
func.func private @cos(f64) -> f64
func.func private @sin(f64) -> f64
func.func private @tan(f64) -> f64
// Constant: PI
llvm.mlir.global internal constant @PI(3.14159265358979323846 : f64) : f64
func.func @compute(%arg0: 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 = math.tan %3 : f64
%5 = arith.constant 0 : i32
%6 = arith.extsi %5 : i32 to i64
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
llvm.store %6, %8 : i64, !llvm.ptr
%9 = arith.constant 0 : i32
%10 = arith.extsi %9 : i32 to i64
%11 = llvm.mlir.constant(1 : i64) : i64
%12 = llvm.alloca %11 x i64 : (i64) -> !llvm.ptr
llvm.store %10, %12 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%13 = llvm.load %12 : !llvm.ptr -> i64
%14 = arith.cmpi sle, %13, %arg0 : i64
cf.cond_br %14, ^bb1, ^bb2
^bb1:
%15 = llvm.load %12 : !llvm.ptr -> i64
%16 = arith.sitofp %15 : i64 to f64
%17 = arith.mulf %16, %3 : f64
%18 = math.sin %17 : f64
%19 = llvm.load %12 : !llvm.ptr -> i64
%20 = arith.addi %19, %arg0 : i64
%21 = arith.sitofp %20 : i64 to f64
%22 = arith.mulf %21, %4 : f64
%23 = llvm.load %12 : !llvm.ptr -> i64
%24 = arith.sitofp %23 : i64 to f64
%25 = arith.mulf %24, %3 : f64
%26 = math.cos %25 : f64
%27 = arith.mulf %22, %26 : f64
%28 = arith.subf %18, %27 : f64
%29 = arith.constant 0.0 : f32
%31 = arith.extf %29 : f32 to f64
%30 = arith.cmpf oge, %28, %31 : f64
cf.cond_br %30, ^bb3, ^bb4
^bb3:
%32 = llvm.load %12 : !llvm.ptr -> i64
%33 = arith.constant 1 : i32
%35 = arith.extsi %33 : i32 to i64
%34 = arith.subi %32, %35 : i64
llvm.store %34, %8 : i64, !llvm.ptr
cf.br ^bb2
^bb4:
cf.br ^bb5
^bb5:
%36 = llvm.load %12 : !llvm.ptr -> i64
%37 = arith.constant 1 : i32
%39 = arith.extsi %37 : i32 to i64
%38 = arith.addi %36, %39 : i64
llvm.store %38, %12 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%40 = arith.constant 2.0 : f32
%41 = llvm.load %8 : !llvm.ptr -> i64
%42 = arith.sitofp %41 : i64 to f64
%43 = arith.mulf %42, %3 : f64
%44 = math.sin %43 : f64
%46 = arith.extf %40 : f32 to f64
%45 = arith.mulf %46, %44 : f64
%47 = llvm.load %8 : !llvm.ptr -> i64
%48 = arith.addi %47, %arg0 : i64
%49 = arith.sitofp %48 : i64 to f64
%50 = arith.mulf %49, %4 : f64
%51 = arith.divf %45, %50 : f64
%52 = llvm.load %8 : !llvm.ptr -> i64
%53 = arith.sitofp %52 : i64 to f64
%54 = arith.mulf %53, %3 : f64
%55 = math.cos %54 : f64
%56 = arith.subf %51, %55 : f64
%57 = llvm.mlir.constant(1 : i64) : i64
%58 = llvm.alloca %57 x f64 : (i64) -> !llvm.ptr
llvm.store %56, %58 : f64, !llvm.ptr
%59 = llvm.load %58 : !llvm.ptr -> f64
%60 = arith.constant 1.0 : f32
%62 = arith.extf %60 : f32 to f64
%61 = arith.cmpf ogt, %59, %62 : f64
cf.cond_br %61, ^bb6, ^bb7
^bb6:
%63 = arith.constant 1.0 : f32
%64 = arith.extf %63 : f32 to f64
llvm.store %64, %58 : f64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%65 = llvm.load %58 : !llvm.ptr -> f64
%66 = arith.constant 1.0 : f32
%67 = arith.negf %66 : f32
%69 = arith.extf %67 : f32 to f64
%68 = arith.cmpf olt, %65, %69 : f64
cf.cond_br %68, ^bb9, ^bb10
^bb9:
%70 = arith.constant 1.0 : f32
%71 = arith.negf %70 : f32
%72 = arith.extf %71 : f32 to f64
llvm.store %72, %58 : f64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%73 = llvm.load %8 : !llvm.ptr -> i64
%74 = arith.sitofp %73 : i64 to f64
%75 = arith.mulf %74, %3 : f64
%77 = llvm.load %58 : !llvm.ptr -> f64
%76 = func.call @acos(%77) : (f64) -> f64
%78 = arith.addf %75, %76 : f64
%79 = arith.constant 2.0 : f32
%81 = arith.extf %79 : f32 to f64
%80 = arith.divf %78, %81 : f64
%82 = arith.constant 1.0 : f32
%83 = math.cos %80 : f64
%85 = arith.extf %82 : f32 to f64
%84 = arith.divf %85, %83 : f64
func.return %84 : f64
}
func.func @main() -> i32 {
%86 = llvm.mlir.addressof @str_0 : !llvm.ptr
%88 = arith.constant 6 : i32
%89 = arith.extsi %88 : i32 to i64
%87 = func.call @compute(%89) : (i64) -> f64
%90 = llvm.call @printf(%86, %87) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%91 = arith.constant 0 : i32
func.return %91 : i32
}
}