Problem 222

Sphere packing in pipe of radius 50mm; balls 30..50mm; length in um.

Answer1590933
Output1590933
StatusPASS
Native helperno
Runtime0 ms
Peak memory1088 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n^2)
Space complexityO(1)O(n)
ApproachFlow solutionGreedy packing simulation
VerdictOptimal

Flow source

# Project Euler 222
# Sphere packing in pipe of radius 50mm; balls 30..50mm; length in um.

extern {
    function sqrt(x: f64) -> f64
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function axial(R: f64, a: f64, b: f64) -> f64 {
    return sqrt(4.0 * R * (a + b - R))
}

function main() -> i32 {
    let R: f64 = 50.0
    let order: ptr<f64> = calloc(30, 8)
    if order == null { return 1 }
    let mut n: i32 = 0
    let mut r: i32 = 50
    while r >= 30 {
        if r % 2 == 0 {
            order[n] = r as f64
            n = n + 1
        }
        r = r - 1
    }
    r = 30
    while r <= 50 {
        if r % 2 == 1 {
            order[n] = r as f64
            n = n + 1
        }
        r = r + 1
    }
    let mut total: f64 = order[0] + order[n - 1]
    let mut i: i32 = 0
    while i + 1 < n {
        total = total + axial(R, order[i], order[i + 1])
        i = i + 1
    }
    let um: i64 = (total * 1000.0 + 0.5) as i64
    printf("%lld\n", um)
    free(order)
    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 axial_f64_f64_f64(double R, double a, double b);
int32_t main(void);




double axial_f64_f64_f64(double R, double a, double b) {
    return sqrt(((4.0 * R) * ((a + b) - R)));
}

int32_t main(void) {
    double R = 50.0;
    double* order = (double*)(calloc(30, 8));
    if (order == NULL) {
        return 1;
    }
    int32_t n = 0;
    int32_t r = 50;
    while (r >= 30) {
        if (FLOW_CHECKED_MOD((r), (2)) == 0) {
            order[n] = ((double)(r));
            n = (n + 1);
        }
        r = (r - 1);
    }
    r = 30;
    while (r <= 50) {
        if (FLOW_CHECKED_MOD((r), (2)) == 1) {
            order[n] = ((double)(r));
            n = (n + 1);
        }
        r = (r + 1);
    }
    double total = (order[0] + order[(n - 1)]);
    int32_t i = 0;
    while ((i + 1) < n) {
        total = (total + axial_f64_f64_f64(R, order[i], order[(i + 1)]));
        i = (i + 1);
    }
    int64_t um = ((int64_t)(((total * 1000.0) + 0.5)));
    printf("%lld\n", um);
    free(order);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @sqrt(f64) -> f64
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @axial(%arg0: f64, %arg1: f64, %arg2: f64) -> f64 {
    %0 = arith.constant 4.0 : f32
    %2 = arith.extf %0 : f32 to f64
    %1 = arith.mulf %2, %arg0 : f64
    %3 = arith.addf %arg1, %arg2 : f64
    %4 = arith.subf %3, %arg0 : f64
    %5 = arith.mulf %1, %4 : f64
    %6 = math.sqrt %5 : f64
    func.return %6 : f64
  }
  func.func @main() -> i32 {
    %7 = arith.constant 50.0 : f32
    %8 = arith.extf %7 : f32 to f64
    %10 = arith.constant 30 : i32
    %11 = arith.constant 8 : i32
    %12 = arith.extsi %10 : i32 to i64
    %13 = arith.extsi %11 : i32 to i64
    %9 = func.call @calloc(%12, %13) : (i64, i64) -> !llvm.ptr
    %14 = llvm.mlir.zero : !llvm.ptr
    %15 = llvm.icmp "eq" %9, %14 : !llvm.ptr
    cf.cond_br %15, ^bb0, ^bb1
    ^bb0:
      %16 = arith.constant 1 : i32
      func.return %16 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %17 = arith.constant 0 : i32
    %18 = llvm.mlir.constant(1 : i64) : i64
    %19 = llvm.alloca %18 x i32 : (i64) -> !llvm.ptr
    llvm.store %17, %19 : i32, !llvm.ptr
    %20 = arith.constant 50 : i32
    %21 = llvm.mlir.constant(1 : i64) : i64
    %22 = llvm.alloca %21 x i32 : (i64) -> !llvm.ptr
    llvm.store %20, %22 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %23 = llvm.load %22 : !llvm.ptr -> i32
    %24 = arith.constant 30 : i32
    %25 = arith.cmpi sge, %23, %24 : i32
    cf.cond_br %25, ^bb4, ^bb5
    ^bb4:
      %26 = llvm.load %22 : !llvm.ptr -> i32
      %27 = arith.constant 2 : i32
      %28 = arith.remsi %26, %27 : i32
      %29 = arith.constant 0 : i32
      %30 = arith.cmpi eq, %28, %29 : i32
      cf.cond_br %30, ^bb6, ^bb7
      ^bb6:
        %31 = llvm.load %22 : !llvm.ptr -> i32
        %32 = arith.sitofp %31 : i32 to f64
        %33 = llvm.load %19 : !llvm.ptr -> i32
        %34 = arith.extsi %33 : i32 to i64
        %35 = llvm.getelementptr %9[%34] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        llvm.store %32, %35 : f64, !llvm.ptr
        %36 = llvm.load %19 : !llvm.ptr -> i32
        %37 = arith.constant 1 : i32
        %38 = arith.addi %36, %37 : i32
        llvm.store %38, %19 : i32, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %39 = llvm.load %22 : !llvm.ptr -> i32
      %40 = arith.constant 1 : i32
      %41 = arith.subi %39, %40 : i32
      llvm.store %41, %22 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %42 = arith.constant 30 : i32
    llvm.store %42, %22 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %43 = llvm.load %22 : !llvm.ptr -> i32
    %44 = arith.constant 50 : i32
    %45 = arith.cmpi sle, %43, %44 : i32
    cf.cond_br %45, ^bb10, ^bb11
    ^bb10:
      %46 = llvm.load %22 : !llvm.ptr -> i32
      %47 = arith.constant 2 : i32
      %48 = arith.remsi %46, %47 : i32
      %49 = arith.constant 1 : i32
      %50 = arith.cmpi eq, %48, %49 : i32
      cf.cond_br %50, ^bb12, ^bb13
      ^bb12:
        %51 = llvm.load %22 : !llvm.ptr -> i32
        %52 = arith.sitofp %51 : i32 to f64
        %53 = llvm.load %19 : !llvm.ptr -> i32
        %54 = arith.extsi %53 : i32 to i64
        %55 = llvm.getelementptr %9[%54] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        llvm.store %52, %55 : f64, !llvm.ptr
        %56 = llvm.load %19 : !llvm.ptr -> i32
        %57 = arith.constant 1 : i32
        %58 = arith.addi %56, %57 : i32
        llvm.store %58, %19 : i32, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %59 = llvm.load %22 : !llvm.ptr -> i32
      %60 = arith.constant 1 : i32
      %61 = arith.addi %59, %60 : i32
      llvm.store %61, %22 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %63 = arith.constant 0 : i32
    %64 = arith.extsi %63 : i32 to i64
    %65 = llvm.getelementptr %9[%64] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %62 = llvm.load %65 : !llvm.ptr -> f64
    %67 = llvm.load %19 : !llvm.ptr -> i32
    %68 = arith.constant 1 : i32
    %69 = arith.subi %67, %68 : i32
    %70 = arith.extsi %69 : i32 to i64
    %71 = llvm.getelementptr %9[%70] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %66 = llvm.load %71 : !llvm.ptr -> f64
    %72 = arith.addf %62, %66 : f64
    %73 = llvm.mlir.constant(1 : i64) : i64
    %74 = llvm.alloca %73 x f64 : (i64) -> !llvm.ptr
    llvm.store %72, %74 : f64, !llvm.ptr
    %75 = arith.constant 0 : i32
    %76 = llvm.mlir.constant(1 : i64) : i64
    %77 = llvm.alloca %76 x i32 : (i64) -> !llvm.ptr
    llvm.store %75, %77 : i32, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %78 = llvm.load %77 : !llvm.ptr -> i32
    %79 = arith.constant 1 : i32
    %80 = arith.addi %78, %79 : i32
    %81 = llvm.load %19 : !llvm.ptr -> i32
    %82 = arith.cmpi slt, %80, %81 : i32
    cf.cond_br %82, ^bb16, ^bb17
    ^bb16:
      %83 = llvm.load %74 : !llvm.ptr -> f64
      %86 = llvm.load %77 : !llvm.ptr -> i32
      %87 = arith.extsi %86 : i32 to i64
      %88 = llvm.getelementptr %9[%87] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %85 = llvm.load %88 : !llvm.ptr -> f64
      %90 = llvm.load %77 : !llvm.ptr -> i32
      %91 = arith.constant 1 : i32
      %92 = arith.addi %90, %91 : i32
      %93 = arith.extsi %92 : i32 to i64
      %94 = llvm.getelementptr %9[%93] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %89 = llvm.load %94 : !llvm.ptr -> f64
      %84 = func.call @axial(%8, %85, %89) : (f64, f64, f64) -> f64
      %95 = arith.addf %83, %84 : f64
      llvm.store %95, %74 : f64, !llvm.ptr
      %96 = llvm.load %77 : !llvm.ptr -> i32
      %97 = arith.constant 1 : i32
      %98 = arith.addi %96, %97 : i32
      llvm.store %98, %77 : i32, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %99 = llvm.load %74 : !llvm.ptr -> f64
    %100 = arith.constant 1000.0 : f32
    %102 = arith.extf %100 : f32 to f64
    %101 = arith.mulf %99, %102 : f64
    %103 = arith.constant 0.5 : f32
    %105 = arith.extf %103 : f32 to f64
    %104 = arith.addf %101, %105 : f64
    %106 = arith.fptosi %104 : f64 to i64
    %107 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %108 = llvm.call @printf(%107, %106) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%9) : (!llvm.ptr) -> ()
    %110 = arith.constant 0 : i32
    func.return %110 : i32
  }
}