Problem 285

Expected score sum for k=1..10^5.

Answer157055.80999
Output157055.80999
StatusPASS
Native helperno
Runtime0 ms
Peak memory1152 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 285
# Expected score sum for k=1..10^5.

extern {
    function sqrt(x: f64) -> f64
    function asin(x: f64) -> f64
}

function area_under_circle(k: f64, r: f64) -> f64 {
    let u_max: f64 = sqrt(r * r - 1.0)
    # primitive(u) = 0.5*(u*sqrt(r^2-u^2) + r^2*asin(u/r))
    let p_max: f64 = 0.5 * (u_max * sqrt(r * r - u_max * u_max) + r * r * asin(u_max / r))
    let p_one: f64 = 0.5 * (1.0 * sqrt(r * r - 1.0) + r * r * asin(1.0 / r))
    return (p_max - p_one - (u_max - 1.0)) / (k * k)
}

function main() -> i32 {
    let limit: i64 = 100000
    let mut total: f64 = area_under_circle(1.0, 1.5)
    let mut k: i64 = 2
    while k <= limit {
        let kf: f64 = k as f64
        let up: f64 = area_under_circle(kf, kf + 0.5)
        let lo: f64 = area_under_circle(kf, kf - 0.5)
        total = total + kf * (up - lo)
        k = k + 1
    }
    printf("%.5f\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 asin(double x);
double area_under_circle_f64_f64(double k, double r);
int32_t main(void);



double area_under_circle_f64_f64(double k, double r) {
    double u_max = sqrt(((r * r) - 1.0));
    double p_max = (0.5 * ((u_max * sqrt(((r * r) - (u_max * u_max)))) + ((r * r) * asin((u_max / r)))));
    double p_one = (0.5 * ((1.0 * sqrt(((r * r) - 1.0))) + ((r * r) * asin((1.0 / r)))));
    return (((p_max - p_one) - (u_max - 1.0)) / (k * k));
}

int32_t main(void) {
    int64_t limit = 100000;
    double total = area_under_circle_f64_f64(1.0, 1.5);
    int64_t k = 2;
    while (k <= limit) {
        double kf = ((double)(k));
        double up = area_under_circle_f64_f64(kf, (kf + 0.5));
        double lo = area_under_circle_f64_f64(kf, (kf - 0.5));
        total = (total + (kf * (up - lo)));
        k = (k + 1);
    }
    printf("%.5f\n", total);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.5f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @sqrt(f64) -> f64
  func.func private @asin(f64) -> f64
  func.func @area_under_circle(%arg0: f64, %arg1: f64) -> f64 {
    %0 = arith.mulf %arg1, %arg1 : f64
    %1 = arith.constant 1.0 : f32
    %3 = arith.extf %1 : f32 to f64
    %2 = arith.subf %0, %3 : f64
    %4 = math.sqrt %2 : f64
    %5 = arith.constant 0.5 : f32
    %6 = arith.mulf %arg1, %arg1 : f64
    %7 = arith.mulf %4, %4 : f64
    %8 = arith.subf %6, %7 : f64
    %9 = math.sqrt %8 : f64
    %10 = arith.mulf %4, %9 : f64
    %11 = arith.mulf %arg1, %arg1 : f64
    %13 = arith.divf %4, %arg1 : f64
    %12 = func.call @asin(%13) : (f64) -> f64
    %14 = arith.mulf %11, %12 : f64
    %15 = arith.addf %10, %14 : f64
    %17 = arith.extf %5 : f32 to f64
    %16 = arith.mulf %17, %15 : f64
    %18 = arith.constant 0.5 : f32
    %19 = arith.constant 1.0 : f32
    %20 = arith.mulf %arg1, %arg1 : f64
    %21 = arith.constant 1.0 : f32
    %23 = arith.extf %21 : f32 to f64
    %22 = arith.subf %20, %23 : f64
    %24 = math.sqrt %22 : f64
    %26 = arith.extf %19 : f32 to f64
    %25 = arith.mulf %26, %24 : f64
    %27 = arith.mulf %arg1, %arg1 : f64
    %29 = arith.constant 1.0 : f32
    %31 = arith.extf %29 : f32 to f64
    %30 = arith.divf %31, %arg1 : f64
    %28 = func.call @asin(%30) : (f64) -> f64
    %32 = arith.mulf %27, %28 : f64
    %33 = arith.addf %25, %32 : f64
    %35 = arith.extf %18 : f32 to f64
    %34 = arith.mulf %35, %33 : f64
    %36 = arith.subf %16, %34 : f64
    %37 = arith.constant 1.0 : f32
    %39 = arith.extf %37 : f32 to f64
    %38 = arith.subf %4, %39 : f64
    %40 = arith.subf %36, %38 : f64
    %41 = arith.mulf %arg0, %arg0 : f64
    %42 = arith.divf %40, %41 : f64
    func.return %42 : f64
  }
  func.func @main() -> i32 {
    %43 = arith.constant 100000 : i32
    %44 = arith.extsi %43 : i32 to i64
    %46 = arith.constant 1.0 : f32
    %47 = arith.constant 1.5 : f32
    %48 = arith.extf %46 : f32 to f64
    %49 = arith.extf %47 : f32 to f64
    %45 = func.call @area_under_circle(%48, %49) : (f64, f64) -> f64
    %50 = llvm.mlir.constant(1 : i64) : i64
    %51 = llvm.alloca %50 x f64 : (i64) -> !llvm.ptr
    llvm.store %45, %51 : f64, !llvm.ptr
    %52 = arith.constant 2 : i32
    %53 = arith.extsi %52 : i32 to i64
    %54 = llvm.mlir.constant(1 : i64) : i64
    %55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
    llvm.store %53, %55 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %56 = llvm.load %55 : !llvm.ptr -> i64
    %57 = arith.cmpi sle, %56, %44 : i64
    cf.cond_br %57, ^bb1, ^bb2
    ^bb1:
      %58 = llvm.load %55 : !llvm.ptr -> i64
      %59 = arith.sitofp %58 : i64 to f64
      %61 = arith.constant 0.5 : f32
      %63 = arith.extf %61 : f32 to f64
      %62 = arith.addf %59, %63 : f64
      %60 = func.call @area_under_circle(%59, %62) : (f64, f64) -> f64
      %65 = arith.constant 0.5 : f32
      %67 = arith.extf %65 : f32 to f64
      %66 = arith.subf %59, %67 : f64
      %64 = func.call @area_under_circle(%59, %66) : (f64, f64) -> f64
      %68 = llvm.load %51 : !llvm.ptr -> f64
      %69 = arith.subf %60, %64 : f64
      %70 = arith.mulf %59, %69 : f64
      %71 = arith.addf %68, %70 : f64
      llvm.store %71, %51 : f64, !llvm.ptr
      %72 = llvm.load %55 : !llvm.ptr -> i64
      %73 = arith.constant 1 : i32
      %75 = arith.extsi %73 : i32 to i64
      %74 = arith.addi %72, %75 : i64
      llvm.store %74, %55 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %76 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %77 = llvm.load %51 : !llvm.ptr -> f64
    %78 = llvm.call @printf(%76, %77) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %79 = arith.constant 0 : i32
    func.return %79 : i32
  }
}