← All problems
Problem 476
Circle Packing II — average max 3-circle area for triangles with perimeter ≤ 1803.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^3)O(n^2)
Space complexity O(1)O(n^2)
Approach Flow solution Bottom-up DP
Verdict Suboptimal
Flow source
# Project Euler 476
# Circle Packing II — average max 3-circle area for triangles with perimeter ≤ 1803.
extern {
function sqrt(x: f64) -> f64
}
function main() -> i32 {
let n: i64 = 1803
let PI: f64 = 3.14159265358979323846
let mut total_count: i64 = 0
let n2: i64 = n / 2
let mut a: i64 = 1
while a <= n2 {
total_count = total_count + a * (n - 2 * a + 1)
a = a + 1
}
let mut total: f64 = 0.0
a = 1
while a <= n2 {
let twoa: i64 = 2 * a
let foura: i64 = 4 * a
let mut b: i64 = a
while b <= n - a {
let s_ab: i64 = a + b
let twos: i64 = 2 * s_ab
let twob: i64 = 2 * b
let fourb: i64 = 4 * b
let mut x: i64 = 1
while x <= a {
let c: i64 = s_ab - x
let t1: i64 = twoa - x
let t2: i64 = twob - x
let r2: f64 = ((x * t1 * t2) as f64) / (4.0 * ((twos - x) as f64))
let sA: f64 = sqrt(((x * t1) as f64) / ((fourb * c) as f64))
let kA: f64 = (1.0 - sA) / (1.0 + sA)
let kA2: f64 = kA * kA
let sB: f64 = sqrt(((x * t2) as f64) / ((foura * c) as f64))
let mut factor: f64 = 0.0
if sB <= (2.0 * sA) / (1.0 + sA * sA) {
let kB: f64 = (1.0 - sB) / (1.0 + sB)
let kB2: f64 = kB * kB
factor = 1.0 + kA2 + kB2
} else {
factor = 1.0 + kA2 + kA2 * kA2
}
total = total + r2 * factor
x = x + 1
}
b = b + 1
}
a = a + 1
}
let ans: f64 = PI * total / (total_count as f64)
printf("%.5f\n", ans)
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; }
int32_t main(void);
int32_t main(void) {
int64_t n = 1803;
double PI = 3.14159265358979323846;
int64_t total_count = 0;
int64_t n2 = FLOW_CHECKED_DIV((n), (2));
int64_t a = 1;
while (a <= n2) {
total_count = (total_count + (a * ((n - (2 * a)) + 1)));
a = (a + 1);
}
double total = 0.0;
a = 1;
while (a <= n2) {
int64_t twoa = (2 * a);
int64_t foura = (4 * a);
int64_t b = a;
while (b <= (n - a)) {
int64_t s_ab = (a + b);
int64_t twos = (2 * s_ab);
int64_t twob = (2 * b);
int64_t fourb = (4 * b);
int64_t x = 1;
while (x <= a) {
int64_t c = (s_ab - x);
int64_t t1 = (twoa - x);
int64_t t2 = (twob - x);
double r2 = (((double)(((x * t1) * t2))) / (4.0 * ((double)((twos - x)))));
double sA = sqrt((((double)((x * t1))) / ((double)((fourb * c)))));
double kA = ((1.0 - sA) / (1.0 + sA));
double kA2 = (kA * kA);
double sB = sqrt((((double)((x * t2))) / ((double)((foura * c)))));
double factor = 0.0;
if (sB <= ((2.0 * sA) / (1.0 + (sA * sA)))) {
double kB = ((1.0 - sB) / (1.0 + sB));
double kB2 = (kB * kB);
factor = ((1.0 + kA2) + kB2);
} else {
factor = ((1.0 + kA2) + (kA2 * kA2));
}
total = (total + (r2 * factor));
x = (x + 1);
}
b = (b + 1);
}
a = (a + 1);
}
double ans = ((PI * total) / ((double)(total_count)));
printf("%.5f\n", ans);
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 @main() -> i32 {
%0 = arith.constant 1803 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 3.14159265358979323846 : f32
%3 = arith.extf %2 : f32 to f64
%4 = arith.constant 0 : 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 2 : i32
%10 = arith.extsi %8 : i32 to i64
%9 = arith.divsi %1, %10 : i64
%11 = arith.constant 1 : i32
%12 = arith.extsi %11 : i32 to i64
%13 = llvm.mlir.constant(1 : i64) : i64
%14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
llvm.store %12, %14 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%15 = llvm.load %14 : !llvm.ptr -> i64
%16 = arith.cmpi sle, %15, %9 : i64
cf.cond_br %16, ^bb1, ^bb2
^bb1:
%17 = llvm.load %7 : !llvm.ptr -> i64
%18 = llvm.load %14 : !llvm.ptr -> i64
%19 = arith.constant 2 : i32
%20 = llvm.load %14 : !llvm.ptr -> i64
%22 = arith.extsi %19 : i32 to i64
%21 = arith.muli %22, %20 : i64
%23 = arith.subi %1, %21 : i64
%24 = arith.constant 1 : i32
%26 = arith.extsi %24 : i32 to i64
%25 = arith.addi %23, %26 : i64
%27 = arith.muli %18, %25 : i64
%28 = arith.addi %17, %27 : i64
llvm.store %28, %7 : i64, !llvm.ptr
%29 = llvm.load %14 : !llvm.ptr -> i64
%30 = arith.constant 1 : i32
%32 = arith.extsi %30 : i32 to i64
%31 = arith.addi %29, %32 : i64
llvm.store %31, %14 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%33 = arith.constant 0.0 : f32
%34 = arith.extf %33 : f32 to f64
%35 = llvm.mlir.constant(1 : i64) : i64
%36 = llvm.alloca %35 x f64 : (i64) -> !llvm.ptr
llvm.store %34, %36 : f64, !llvm.ptr
%37 = arith.constant 1 : i32
%38 = arith.extsi %37 : i32 to i64
llvm.store %38, %14 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%39 = llvm.load %14 : !llvm.ptr -> i64
%40 = arith.cmpi sle, %39, %9 : i64
cf.cond_br %40, ^bb4, ^bb5
^bb4:
%41 = arith.constant 2 : i32
%42 = llvm.load %14 : !llvm.ptr -> i64
%44 = arith.extsi %41 : i32 to i64
%43 = arith.muli %44, %42 : i64
%45 = arith.constant 4 : i32
%46 = llvm.load %14 : !llvm.ptr -> i64
%48 = arith.extsi %45 : i32 to i64
%47 = arith.muli %48, %46 : i64
%49 = llvm.load %14 : !llvm.ptr -> i64
%50 = llvm.mlir.constant(1 : i64) : i64
%51 = llvm.alloca %50 x i64 : (i64) -> !llvm.ptr
llvm.store %49, %51 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%52 = llvm.load %51 : !llvm.ptr -> i64
%53 = llvm.load %14 : !llvm.ptr -> i64
%54 = arith.subi %1, %53 : i64
%55 = arith.cmpi sle, %52, %54 : i64
cf.cond_br %55, ^bb7, ^bb8
^bb7:
%56 = llvm.load %14 : !llvm.ptr -> i64
%57 = llvm.load %51 : !llvm.ptr -> i64
%58 = arith.addi %56, %57 : i64
%59 = arith.constant 2 : i32
%61 = arith.extsi %59 : i32 to i64
%60 = arith.muli %61, %58 : i64
%62 = arith.constant 2 : i32
%63 = llvm.load %51 : !llvm.ptr -> i64
%65 = arith.extsi %62 : i32 to i64
%64 = arith.muli %65, %63 : i64
%66 = arith.constant 4 : i32
%67 = llvm.load %51 : !llvm.ptr -> i64
%69 = arith.extsi %66 : i32 to i64
%68 = arith.muli %69, %67 : i64
%70 = arith.constant 1 : i32
%71 = arith.extsi %70 : i32 to i64
%72 = llvm.mlir.constant(1 : i64) : i64
%73 = llvm.alloca %72 x i64 : (i64) -> !llvm.ptr
llvm.store %71, %73 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%74 = llvm.load %73 : !llvm.ptr -> i64
%75 = llvm.load %14 : !llvm.ptr -> i64
%76 = arith.cmpi sle, %74, %75 : i64
cf.cond_br %76, ^bb10, ^bb11
^bb10:
%77 = llvm.load %73 : !llvm.ptr -> i64
%78 = arith.subi %58, %77 : i64
%79 = llvm.load %73 : !llvm.ptr -> i64
%80 = arith.subi %43, %79 : i64
%81 = llvm.load %73 : !llvm.ptr -> i64
%82 = arith.subi %64, %81 : i64
%83 = llvm.load %73 : !llvm.ptr -> i64
%84 = arith.muli %83, %80 : i64
%85 = arith.muli %84, %82 : i64
%86 = arith.sitofp %85 : i64 to f64
%87 = arith.constant 4.0 : f32
%88 = llvm.load %73 : !llvm.ptr -> i64
%89 = arith.subi %60, %88 : i64
%90 = arith.sitofp %89 : i64 to f64
%92 = arith.extf %87 : f32 to f64
%91 = arith.mulf %92, %90 : f64
%93 = arith.divf %86, %91 : f64
%94 = llvm.load %73 : !llvm.ptr -> i64
%95 = arith.muli %94, %80 : i64
%96 = arith.sitofp %95 : i64 to f64
%97 = arith.muli %68, %78 : i64
%98 = arith.sitofp %97 : i64 to f64
%99 = arith.divf %96, %98 : f64
%100 = math.sqrt %99 : f64
%101 = arith.constant 1.0 : f32
%103 = arith.extf %101 : f32 to f64
%102 = arith.subf %103, %100 : f64
%104 = arith.constant 1.0 : f32
%106 = arith.extf %104 : f32 to f64
%105 = arith.addf %106, %100 : f64
%107 = arith.divf %102, %105 : f64
%108 = arith.mulf %107, %107 : f64
%109 = llvm.load %73 : !llvm.ptr -> i64
%110 = arith.muli %109, %82 : i64
%111 = arith.sitofp %110 : i64 to f64
%112 = arith.muli %47, %78 : i64
%113 = arith.sitofp %112 : i64 to f64
%114 = arith.divf %111, %113 : f64
%115 = math.sqrt %114 : f64
%116 = arith.constant 0.0 : f32
%117 = arith.extf %116 : f32 to f64
%118 = llvm.mlir.constant(1 : i64) : i64
%119 = llvm.alloca %118 x f64 : (i64) -> !llvm.ptr
llvm.store %117, %119 : f64, !llvm.ptr
%120 = arith.constant 2.0 : f32
%122 = arith.extf %120 : f32 to f64
%121 = arith.mulf %122, %100 : f64
%123 = arith.constant 1.0 : f32
%124 = arith.mulf %100, %100 : f64
%126 = arith.extf %123 : f32 to f64
%125 = arith.addf %126, %124 : f64
%127 = arith.divf %121, %125 : f64
%128 = arith.cmpf ole, %115, %127 : f64
cf.cond_br %128, ^bb12, ^bb13
^bb12:
%129 = arith.constant 1.0 : f32
%131 = arith.extf %129 : f32 to f64
%130 = arith.subf %131, %115 : f64
%132 = arith.constant 1.0 : f32
%134 = arith.extf %132 : f32 to f64
%133 = arith.addf %134, %115 : f64
%135 = arith.divf %130, %133 : f64
%136 = arith.mulf %135, %135 : f64
%137 = arith.constant 1.0 : f32
%139 = arith.extf %137 : f32 to f64
%138 = arith.addf %139, %108 : f64
%140 = arith.addf %138, %136 : f64
llvm.store %140, %119 : f64, !llvm.ptr
cf.br ^bb14
^bb13:
%141 = arith.constant 1.0 : f32
%143 = arith.extf %141 : f32 to f64
%142 = arith.addf %143, %108 : f64
%144 = arith.mulf %108, %108 : f64
%145 = arith.addf %142, %144 : f64
llvm.store %145, %119 : f64, !llvm.ptr
cf.br ^bb14
^bb14:
%146 = llvm.load %36 : !llvm.ptr -> f64
%147 = llvm.load %119 : !llvm.ptr -> f64
%148 = arith.mulf %93, %147 : f64
%149 = arith.addf %146, %148 : f64
llvm.store %149, %36 : f64, !llvm.ptr
%150 = llvm.load %73 : !llvm.ptr -> i64
%151 = arith.constant 1 : i32
%153 = arith.extsi %151 : i32 to i64
%152 = arith.addi %150, %153 : i64
llvm.store %152, %73 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%154 = llvm.load %51 : !llvm.ptr -> i64
%155 = arith.constant 1 : i32
%157 = arith.extsi %155 : i32 to i64
%156 = arith.addi %154, %157 : i64
llvm.store %156, %51 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%158 = llvm.load %14 : !llvm.ptr -> i64
%159 = arith.constant 1 : i32
%161 = arith.extsi %159 : i32 to i64
%160 = arith.addi %158, %161 : i64
llvm.store %160, %14 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%162 = llvm.load %36 : !llvm.ptr -> f64
%163 = arith.mulf %3, %162 : f64
%164 = llvm.load %7 : !llvm.ptr -> i64
%165 = arith.sitofp %164 : i64 to f64
%166 = arith.divf %163, %165 : f64
%167 = llvm.mlir.addressof @str_0 : !llvm.ptr
%168 = llvm.call @printf(%167, %166) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%169 = arith.constant 0 : i32
func.return %169 : i32
}
}