Problem 965

Expected Minimal Fractional Value — Farey adjacent sum F(10^4).

Answer0.0003452201133
Output0.0003452201133
StatusPASS
Native helperno
Runtime150 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n * s^2)
Space complexityO(1)O(s^2)
ApproachFlow solutionMarkov chain or DP over states
VerdictUnknown

Flow source

# Project Euler 965
# Expected Minimal Fractional Value — Farey adjacent sum F(10^4).

function farey_integral_expected_min(N: i64) -> f64 {
    let mut a: i64 = 0
    let mut b: i64 = 1
    let mut c: i64 = 1
    let mut d: i64 = N
    let mut total: f64 = 0.0
    let mut comp: f64 = 0.0
    let mut partial: f64 = 0.0
    let mut cnt: i64 = 0
    while !(a == 1 && b == 1) {
        partial = partial + 0.5 / ((b * d * d) as f64)
        cnt = cnt + 1
        if (cnt & 4095) == 0 {
            let y: f64 = partial - comp
            let t: f64 = total + y
            comp = (t - total) - y
            total = t
            partial = 0.0
        }
        let k: i64 = (N + b) / d
        let a2: i64 = c
        let b2: i64 = d
        let c2: i64 = k * c - a
        let d2: i64 = k * d - b
        a = a2
        b = b2
        c = c2
        d = d2
    }
    if partial != 0.0 {
        let y2: f64 = partial - comp
        let t2: f64 = total + y2
        total = t2
    }
    return total
}

function main() -> i32 {
    printf("%.13f\n", farey_integral_expected_min(10000))
    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 farey_integral_expected_min_i64(int64_t N);
int32_t main(void);

double farey_integral_expected_min_i64(int64_t N) {
    int64_t a = 0;
    int64_t b = 1;
    int64_t c = 1;
    int64_t d = N;
    double total = 0.0;
    double comp = 0.0;
    double partial = 0.0;
    int64_t cnt = 0;
    while ((!((a == 1 && b == 1)))) {
        partial = (partial + (0.5 / ((double)(((b * d) * d)))));
        cnt = (cnt + 1);
        if ((cnt & 4095) == 0) {
            double y = (partial - comp);
            double t = (total + y);
            comp = ((t - total) - y);
            total = t;
            partial = 0.0;
        }
        int64_t k = FLOW_CHECKED_DIV(((N + b)), (d));
        int64_t a2 = c;
        int64_t b2 = d;
        int64_t c2 = ((k * c) - a);
        int64_t d2 = ((k * d) - b);
        a = a2;
        b = b2;
        c = c2;
        d = d2;
    }
    if (partial != 0.0) {
        double y2 = (partial - comp);
        double t2 = (total + y2);
        total = t2;
    }
    return total;
}

int32_t main(void) {
    printf("%.13f\n", farey_integral_expected_min_i64(10000));
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.13f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
  func.func @farey_integral_expected_min(%arg0: i64) -> f64 {
    %0 = arith.constant 0 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i64, !llvm.ptr
    %4 = arith.constant 1 : i32
    %5 = arith.extsi %4 : i32 to i64
    %6 = llvm.mlir.constant(1 : i64) : i64
    %7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
    llvm.store %5, %7 : i64, !llvm.ptr
    %8 = arith.constant 1 : i32
    %9 = arith.extsi %8 : i32 to i64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i64, !llvm.ptr
    %12 = llvm.mlir.constant(1 : i64) : i64
    %13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %13 : i64, !llvm.ptr
    %14 = arith.constant 0.0 : f32
    %15 = arith.extf %14 : f32 to f64
    %16 = llvm.mlir.constant(1 : i64) : i64
    %17 = llvm.alloca %16 x f64 : (i64) -> !llvm.ptr
    llvm.store %15, %17 : f64, !llvm.ptr
    %18 = arith.constant 0.0 : f32
    %19 = arith.extf %18 : f32 to f64
    %20 = llvm.mlir.constant(1 : i64) : i64
    %21 = llvm.alloca %20 x f64 : (i64) -> !llvm.ptr
    llvm.store %19, %21 : f64, !llvm.ptr
    %22 = arith.constant 0.0 : f32
    %23 = arith.extf %22 : f32 to f64
    %24 = llvm.mlir.constant(1 : i64) : i64
    %25 = llvm.alloca %24 x f64 : (i64) -> !llvm.ptr
    llvm.store %23, %25 : f64, !llvm.ptr
    %26 = arith.constant 0 : i32
    %27 = arith.extsi %26 : i32 to i64
    %28 = llvm.mlir.constant(1 : i64) : i64
    %29 = llvm.alloca %28 x i64 : (i64) -> !llvm.ptr
    llvm.store %27, %29 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %30 = llvm.load %3 : !llvm.ptr -> i64
    %31 = arith.constant 1 : i32
    %33 = arith.extsi %31 : i32 to i64
    %32 = arith.cmpi eq, %30, %33 : i64
    %34 = scf.if %32 -> (i1) {
      %35 = llvm.load %7 : !llvm.ptr -> i64
      %36 = arith.constant 1 : i32
      %38 = arith.extsi %36 : i32 to i64
      %37 = arith.cmpi eq, %35, %38 : i64
      scf.yield %37 : i1
    } else {
      %39 = arith.constant false
      scf.yield %39 : i1
    }
    %41 = arith.constant 1 : i1
    %40 = arith.xori %34, %41 : i1
    cf.cond_br %40, ^bb1, ^bb2
    ^bb1:
      %43 = llvm.load %25 : !llvm.ptr -> f64
      %44 = arith.constant 0.5 : f32
      %45 = llvm.load %7 : !llvm.ptr -> i64
      %46 = llvm.load %13 : !llvm.ptr -> i64
      %47 = arith.muli %45, %46 : i64
      %48 = llvm.load %13 : !llvm.ptr -> i64
      %49 = arith.muli %47, %48 : i64
      %50 = arith.sitofp %49 : i64 to f64
      %52 = arith.extf %44 : f32 to f64
      %51 = arith.divf %52, %50 : f64
      %53 = arith.addf %43, %51 : f64
      llvm.store %53, %25 : f64, !llvm.ptr
      %54 = llvm.load %29 : !llvm.ptr -> i64
      %55 = arith.constant 1 : i32
      %57 = arith.extsi %55 : i32 to i64
      %56 = arith.addi %54, %57 : i64
      llvm.store %56, %29 : i64, !llvm.ptr
      %58 = llvm.load %29 : !llvm.ptr -> i64
      %59 = arith.constant 4095 : i32
      %61 = arith.extsi %59 : i32 to i64
      %60 = arith.andi %58, %61 : i64
      %62 = arith.constant 0 : i32
      %64 = arith.extsi %62 : i32 to i64
      %63 = arith.cmpi eq, %60, %64 : i64
      cf.cond_br %63, ^bb3, ^bb4
      ^bb3:
        %65 = llvm.load %25 : !llvm.ptr -> f64
        %66 = llvm.load %21 : !llvm.ptr -> f64
        %67 = arith.subf %65, %66 : f64
        %68 = llvm.load %17 : !llvm.ptr -> f64
        %69 = arith.addf %68, %67 : f64
        %70 = llvm.load %17 : !llvm.ptr -> f64
        %71 = arith.subf %69, %70 : f64
        %72 = arith.subf %71, %67 : f64
        llvm.store %72, %21 : f64, !llvm.ptr
        llvm.store %69, %17 : f64, !llvm.ptr
        %73 = arith.constant 0.0 : f32
        %74 = arith.extf %73 : f32 to f64
        llvm.store %74, %25 : f64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %75 = llvm.load %7 : !llvm.ptr -> i64
      %76 = arith.addi %arg0, %75 : i64
      %77 = llvm.load %13 : !llvm.ptr -> i64
      %78 = arith.divsi %76, %77 : i64
      %79 = llvm.load %11 : !llvm.ptr -> i64
      %80 = llvm.load %13 : !llvm.ptr -> i64
      %81 = llvm.load %11 : !llvm.ptr -> i64
      %82 = arith.muli %78, %81 : i64
      %83 = llvm.load %3 : !llvm.ptr -> i64
      %84 = arith.subi %82, %83 : i64
      %85 = llvm.load %13 : !llvm.ptr -> i64
      %86 = arith.muli %78, %85 : i64
      %87 = llvm.load %7 : !llvm.ptr -> i64
      %88 = arith.subi %86, %87 : i64
      llvm.store %79, %3 : i64, !llvm.ptr
      llvm.store %80, %7 : i64, !llvm.ptr
      llvm.store %84, %11 : i64, !llvm.ptr
      llvm.store %88, %13 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %89 = llvm.load %25 : !llvm.ptr -> f64
    %90 = arith.constant 0.0 : f32
    %92 = arith.extf %90 : f32 to f64
    %91 = arith.cmpf one, %89, %92 : f64
    cf.cond_br %91, ^bb6, ^bb7
    ^bb6:
      %93 = llvm.load %25 : !llvm.ptr -> f64
      %94 = llvm.load %21 : !llvm.ptr -> f64
      %95 = arith.subf %93, %94 : f64
      %96 = llvm.load %17 : !llvm.ptr -> f64
      %97 = arith.addf %96, %95 : f64
      llvm.store %97, %17 : f64, !llvm.ptr
      cf.br ^bb8
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %98 = llvm.load %17 : !llvm.ptr -> f64
    func.return %98 : f64
  }
  func.func @main() -> i32 {
    %99 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %101 = arith.constant 10000 : i32
    %102 = arith.extsi %101 : i32 to i64
    %100 = func.call @farey_integral_expected_min(%102) : (i64) -> f64
    %103 = llvm.call @printf(%99, %100) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %104 = arith.constant 0 : i32
    func.return %104 : i32
  }
}