Problem 978
Random Fibonacci skewness Skew(X_50) to 8 decimals.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(log n) |
| Space complexity | O(1) | O(1) |
| Approach | Flow solution | Matrix exponentiation |
| Verdict | Suboptimal |
Flow source
# Project Euler 978
# Random Fibonacci skewness Skew(X_50) to 8 decimals.
extern {
function sqrt(x: f64) -> f64
function round(x: f64) -> f64
}
function skewness_at(t: i64) -> f64 {
let mut a0: f64 = 0.0
let mut a1: f64 = 1.0
let mut m0: f64 = 0.0
let mut m1: f64 = 1.0
let mut i: i64 = 2
while i <= t {
let na: f64 = a1 + a0
a0 = a1
a1 = na
let nm: f64 = m1 + 3.0 * m0
m0 = m1
m1 = nm
i = i + 1
}
let a_t: f64 = a1
let m_t: f64 = m1
let var: f64 = a_t - 1.0
let central3: f64 = m_t - 3.0 * a_t + 2.0
let sigma: f64 = sqrt(var)
return central3 / (sigma * sigma * sigma)
}
function main() -> i32 {
let sk: f64 = skewness_at(50)
# round to 8 decimals
let scaled: f64 = sk * 100000000.0
let rounded: f64 = round(scaled) / 100000000.0
printf("%.8f\n", rounded)
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 skewness_at_i64(int64_t t);
int32_t main(void);
double skewness_at_i64(int64_t t) {
double a0 = 0.0;
double a1 = 1.0;
double m0 = 0.0;
double m1 = 1.0;
int64_t i = 2;
while (i <= t) {
double na = (a1 + a0);
a0 = a1;
a1 = na;
double nm = (m1 + (3.0 * m0));
m0 = m1;
m1 = nm;
i = (i + 1);
}
double a_t = a1;
double m_t = m1;
double var = (a_t - 1.0);
double central3 = ((m_t - (3.0 * a_t)) + 2.0);
double sigma = sqrt(var);
return (central3 / ((sigma * sigma) * sigma));
}
int32_t main(void) {
double sk = skewness_at_i64(50);
double scaled = (sk * 100000000.0);
double rounded = (round(scaled) / 100000000.0);
printf("%.8f\n", rounded);
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 @sqrt(f64) -> f64
func.func private @round(f64) -> f64
func.func @skewness_at(%arg0: i64) -> f64 {
%0 = arith.constant 0.0 : f32
%1 = arith.extf %0 : f32 to f64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x f64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : f64, !llvm.ptr
%4 = arith.constant 1.0 : f32
%5 = arith.extf %4 : f32 to f64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x f64 : (i64) -> !llvm.ptr
llvm.store %5, %7 : f64, !llvm.ptr
%8 = arith.constant 0.0 : 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.0 : f32
%13 = arith.extf %12 : f32 to f64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x f64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : f64, !llvm.ptr
%16 = arith.constant 2 : i32
%17 = arith.extsi %16 : i32 to i64
%18 = llvm.mlir.constant(1 : i64) : i64
%19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
llvm.store %17, %19 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%20 = llvm.load %19 : !llvm.ptr -> i64
%21 = arith.cmpi sle, %20, %arg0 : i64
cf.cond_br %21, ^bb1, ^bb2
^bb1:
%22 = llvm.load %7 : !llvm.ptr -> f64
%23 = llvm.load %3 : !llvm.ptr -> f64
%24 = arith.addf %22, %23 : f64
%25 = llvm.load %7 : !llvm.ptr -> f64
llvm.store %25, %3 : f64, !llvm.ptr
llvm.store %24, %7 : f64, !llvm.ptr
%26 = llvm.load %15 : !llvm.ptr -> f64
%27 = arith.constant 3.0 : f32
%28 = llvm.load %11 : !llvm.ptr -> f64
%30 = arith.extf %27 : f32 to f64
%29 = arith.mulf %30, %28 : f64
%31 = arith.addf %26, %29 : f64
%32 = llvm.load %15 : !llvm.ptr -> f64
llvm.store %32, %11 : f64, !llvm.ptr
llvm.store %31, %15 : f64, !llvm.ptr
%33 = llvm.load %19 : !llvm.ptr -> i64
%34 = arith.constant 1 : i32
%36 = arith.extsi %34 : i32 to i64
%35 = arith.addi %33, %36 : i64
llvm.store %35, %19 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%37 = llvm.load %7 : !llvm.ptr -> f64
%38 = llvm.load %15 : !llvm.ptr -> f64
%39 = arith.constant 1.0 : f32
%41 = arith.extf %39 : f32 to f64
%40 = arith.subf %37, %41 : f64
%42 = arith.constant 3.0 : f32
%44 = arith.extf %42 : f32 to f64
%43 = arith.mulf %44, %37 : f64
%45 = arith.subf %38, %43 : f64
%46 = arith.constant 2.0 : f32
%48 = arith.extf %46 : f32 to f64
%47 = arith.addf %45, %48 : f64
%49 = math.sqrt %40 : f64
%50 = arith.mulf %49, %49 : f64
%51 = arith.mulf %50, %49 : f64
%52 = arith.divf %47, %51 : f64
func.return %52 : f64
}
func.func @main() -> i32 {
%54 = arith.constant 50 : i32
%55 = arith.extsi %54 : i32 to i64
%53 = func.call @skewness_at(%55) : (i64) -> f64
%56 = arith.constant 100000000.0 : f32
%58 = arith.extf %56 : f32 to f64
%57 = arith.mulf %53, %58 : f64
%59 = func.call @round(%57) : (f64) -> f64
%60 = arith.constant 100000000.0 : f32
%62 = arith.extf %60 : f32 to f64
%61 = arith.divf %59, %62 : f64
%63 = llvm.mlir.addressof @str_0 : !llvm.ptr
%64 = llvm.call @printf(%63, %61) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%65 = arith.constant 0 : i32
func.return %65 : i32
}
}