Problem 587
Concave triangle under the circles; least n with area ratio < 0.1%.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n^2) |
| Space complexity | O(1) | O(n^2) |
| Approach | Flow solution | Bottom-up DP |
| Verdict | Optimal |
Flow source
# Project Euler 587
# Concave triangle under the circles; least n with area ratio < 0.1%.
extern {
function sqrt(x: f64) -> f64
function fabs(x: f64) -> f64
}
function get_intersection(slope: f64) -> f64 {
let mut x: f64 = 0.5
let mut step: f64 = 0.1
let eps: f64 = 0.00000000001
while true {
let left: f64 = slope * x
let dx: f64 = x - 1.0
let right: f64 = 1.0 - sqrt(1.0 - dx * dx)
if fabs(left - right) < eps {
return x
}
if left > right {
x = x - step
} else {
x = x + step
}
step = step * 0.99
}
return x
}
function get_area_l(slope: f64) -> f64 {
let mut intersection: f64 = 0.0
let mut left_area: f64 = 0.0
if slope > 0.0 {
intersection = get_intersection(slope)
left_area = intersection * (intersection * slope) / 2.0
}
let mut right_area: f64 = 0.0
let step: f64 = (1.0 - intersection) / 100000.0
let mut x: f64 = intersection
while x < 1.0 {
let dx: f64 = x - 1.0
let y: f64 = 1.0 - sqrt(1.0 - dx * dx)
right_area = right_area + y * step
x = x + step
}
return left_area + right_area
}
function solve(limit: f64) -> i64 {
let l_section: f64 = get_area_l(0.0)
let mut num_circles: i64 = 1
let mut step: i64 = 64
while true {
let slope: f64 = 1.0 / (num_circles as f64)
let area: f64 = get_area_l(slope)
let percentage: f64 = 100.0 * area / l_section
if percentage < limit {
if step == 1 {
break
}
num_circles = num_circles - step
step = step / 2
}
num_circles = num_circles + step
}
return num_circles
}
function main() -> i32 {
printf("%lld\n", solve(0.1))
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 get_intersection_f64(double slope);
double get_area_l_f64(double slope);
int64_t solve_f64(double limit);
int32_t main(void);
double get_intersection_f64(double slope) {
double x = 0.5;
double step = 0.1;
double eps = 0.00000000001;
while (1) {
double left = (slope * x);
double dx = (x - 1.0);
double right = (1.0 - sqrt((1.0 - (dx * dx))));
if (fabs((left - right)) < eps) {
return x;
}
if (left > right) {
x = (x - step);
} else {
x = (x + step);
}
step = (step * 0.99);
}
return x;
}
double get_area_l_f64(double slope) {
double intersection = 0.0;
double left_area = 0.0;
if (slope > 0.0) {
intersection = get_intersection_f64(slope);
left_area = ((intersection * (intersection * slope)) / 2.0);
}
double right_area = 0.0;
double step = ((1.0 - intersection) / 100000.0);
double x = intersection;
while (x < 1.0) {
double dx = (x - 1.0);
double y = (1.0 - sqrt((1.0 - (dx * dx))));
right_area = (right_area + (y * step));
x = (x + step);
}
return (left_area + right_area);
}
int64_t solve_f64(double limit) {
double l_section = get_area_l_f64(0.0);
int64_t num_circles = 1;
int64_t step = 64;
while (1) {
double slope = (1.0 / ((double)(num_circles)));
double area = get_area_l_f64(slope);
double percentage = ((100.0 * area) / l_section);
if (percentage < limit) {
if (step == 1) {
break;
}
num_circles = (num_circles - step);
step = FLOW_CHECKED_DIV((step), (2));
}
num_circles = (num_circles + step);
}
return num_circles;
}
int32_t main(void) {
printf("%lld\n", solve_f64(0.1));
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 @fabs(f64) -> f64
func.func @get_intersection(%arg0: f64) -> f64 {
%0 = arith.constant 0.5 : 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 0.1 : 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.00000000001 : f32
%9 = arith.extf %8 : f32 to f64
cf.br ^bb0
^bb0:
%10 = arith.constant 1 : i1
cf.cond_br %10, ^bb1, ^bb2
^bb1:
%11 = llvm.load %3 : !llvm.ptr -> f64
%12 = arith.mulf %arg0, %11 : f64
%13 = llvm.load %3 : !llvm.ptr -> f64
%14 = arith.constant 1.0 : f32
%16 = arith.extf %14 : f32 to f64
%15 = arith.subf %13, %16 : f64
%17 = arith.constant 1.0 : f32
%18 = arith.constant 1.0 : f32
%19 = arith.mulf %15, %15 : f64
%21 = arith.extf %18 : f32 to f64
%20 = arith.subf %21, %19 : f64
%22 = math.sqrt %20 : f64
%24 = arith.extf %17 : f32 to f64
%23 = arith.subf %24, %22 : f64
%25 = arith.subf %12, %23 : f64
%26 = math.absf %25 : f64
%27 = arith.cmpf olt, %26, %9 : f64
cf.cond_br %27, ^bb3, ^bb4
^bb3:
%28 = llvm.load %3 : !llvm.ptr -> f64
func.return %28 : f64
^bb4:
cf.br ^bb5
^bb5:
%29 = arith.cmpf ogt, %12, %23 : f64
cf.cond_br %29, ^bb6, ^bb7
^bb6:
%30 = llvm.load %3 : !llvm.ptr -> f64
%31 = llvm.load %7 : !llvm.ptr -> f64
%32 = arith.subf %30, %31 : f64
llvm.store %32, %3 : f64, !llvm.ptr
cf.br ^bb8
^bb7:
%33 = llvm.load %3 : !llvm.ptr -> f64
%34 = llvm.load %7 : !llvm.ptr -> f64
%35 = arith.addf %33, %34 : f64
llvm.store %35, %3 : f64, !llvm.ptr
cf.br ^bb8
^bb8:
%36 = llvm.load %7 : !llvm.ptr -> f64
%37 = arith.constant 0.99 : f32
%39 = arith.extf %37 : f32 to f64
%38 = arith.mulf %36, %39 : f64
llvm.store %38, %7 : f64, !llvm.ptr
cf.br ^bb0
^bb2:
%40 = llvm.load %3 : !llvm.ptr -> f64
func.return %40 : f64
}
func.func @get_area_l(%arg0: f64) -> f64 {
%41 = arith.constant 0.0 : f32
%42 = arith.extf %41 : f32 to f64
%43 = llvm.mlir.constant(1 : i64) : i64
%44 = llvm.alloca %43 x f64 : (i64) -> !llvm.ptr
llvm.store %42, %44 : f64, !llvm.ptr
%45 = arith.constant 0.0 : f32
%46 = arith.extf %45 : f32 to f64
%47 = llvm.mlir.constant(1 : i64) : i64
%48 = llvm.alloca %47 x f64 : (i64) -> !llvm.ptr
llvm.store %46, %48 : f64, !llvm.ptr
%49 = arith.constant 0.0 : f32
%51 = arith.extf %49 : f32 to f64
%50 = arith.cmpf ogt, %arg0, %51 : f64
cf.cond_br %50, ^bb9, ^bb10
^bb9:
%52 = func.call @get_intersection(%arg0) : (f64) -> f64
llvm.store %52, %44 : f64, !llvm.ptr
%53 = llvm.load %44 : !llvm.ptr -> f64
%54 = llvm.load %44 : !llvm.ptr -> f64
%55 = arith.mulf %54, %arg0 : f64
%56 = arith.mulf %53, %55 : f64
%57 = arith.constant 2.0 : f32
%59 = arith.extf %57 : f32 to f64
%58 = arith.divf %56, %59 : f64
llvm.store %58, %48 : f64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%60 = arith.constant 0.0 : f32
%61 = arith.extf %60 : f32 to f64
%62 = llvm.mlir.constant(1 : i64) : i64
%63 = llvm.alloca %62 x f64 : (i64) -> !llvm.ptr
llvm.store %61, %63 : f64, !llvm.ptr
%64 = arith.constant 1.0 : f32
%65 = llvm.load %44 : !llvm.ptr -> f64
%67 = arith.extf %64 : f32 to f64
%66 = arith.subf %67, %65 : f64
%68 = arith.constant 100000.0 : f32
%70 = arith.extf %68 : f32 to f64
%69 = arith.divf %66, %70 : f64
%71 = llvm.load %44 : !llvm.ptr -> f64
%72 = llvm.mlir.constant(1 : i64) : i64
%73 = llvm.alloca %72 x f64 : (i64) -> !llvm.ptr
llvm.store %71, %73 : f64, !llvm.ptr
cf.br ^bb12
^bb12:
%74 = llvm.load %73 : !llvm.ptr -> f64
%75 = arith.constant 1.0 : f32
%77 = arith.extf %75 : f32 to f64
%76 = arith.cmpf olt, %74, %77 : f64
cf.cond_br %76, ^bb13, ^bb14
^bb13:
%78 = llvm.load %73 : !llvm.ptr -> f64
%79 = arith.constant 1.0 : f32
%81 = arith.extf %79 : f32 to f64
%80 = arith.subf %78, %81 : f64
%82 = arith.constant 1.0 : f32
%83 = arith.constant 1.0 : f32
%84 = arith.mulf %80, %80 : f64
%86 = arith.extf %83 : f32 to f64
%85 = arith.subf %86, %84 : f64
%87 = math.sqrt %85 : f64
%89 = arith.extf %82 : f32 to f64
%88 = arith.subf %89, %87 : f64
%90 = llvm.load %63 : !llvm.ptr -> f64
%91 = arith.mulf %88, %69 : f64
%92 = arith.addf %90, %91 : f64
llvm.store %92, %63 : f64, !llvm.ptr
%93 = llvm.load %73 : !llvm.ptr -> f64
%94 = arith.addf %93, %69 : f64
llvm.store %94, %73 : f64, !llvm.ptr
cf.br ^bb12
^bb14:
%95 = llvm.load %48 : !llvm.ptr -> f64
%96 = llvm.load %63 : !llvm.ptr -> f64
%97 = arith.addf %95, %96 : f64
func.return %97 : f64
}
func.func @solve(%arg0: f64) -> i64 {
%99 = arith.constant 0.0 : f32
%100 = arith.extf %99 : f32 to f64
%98 = func.call @get_area_l(%100) : (f64) -> f64
%101 = arith.constant 1 : i32
%102 = arith.extsi %101 : i32 to i64
%103 = llvm.mlir.constant(1 : i64) : i64
%104 = llvm.alloca %103 x i64 : (i64) -> !llvm.ptr
llvm.store %102, %104 : i64, !llvm.ptr
%105 = arith.constant 64 : i32
%106 = arith.extsi %105 : i32 to i64
%107 = llvm.mlir.constant(1 : i64) : i64
%108 = llvm.alloca %107 x i64 : (i64) -> !llvm.ptr
llvm.store %106, %108 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%109 = arith.constant 1 : i1
cf.cond_br %109, ^bb16, ^bb17
^bb16:
%110 = arith.constant 1.0 : f32
%111 = llvm.load %104 : !llvm.ptr -> i64
%112 = arith.sitofp %111 : i64 to f64
%114 = arith.extf %110 : f32 to f64
%113 = arith.divf %114, %112 : f64
%115 = func.call @get_area_l(%113) : (f64) -> f64
%116 = arith.constant 100.0 : f32
%118 = arith.extf %116 : f32 to f64
%117 = arith.mulf %118, %115 : f64
%119 = arith.divf %117, %98 : f64
%120 = arith.cmpf olt, %119, %arg0 : f64
cf.cond_br %120, ^bb18, ^bb19
^bb18:
%121 = llvm.load %108 : !llvm.ptr -> i64
%122 = arith.constant 1 : i32
%124 = arith.extsi %122 : i32 to i64
%123 = arith.cmpi eq, %121, %124 : i64
cf.cond_br %123, ^bb21, ^bb22
^bb21:
cf.br ^bb17
^bb22:
cf.br ^bb23
^bb23:
%125 = llvm.load %104 : !llvm.ptr -> i64
%126 = llvm.load %108 : !llvm.ptr -> i64
%127 = arith.subi %125, %126 : i64
llvm.store %127, %104 : i64, !llvm.ptr
%128 = llvm.load %108 : !llvm.ptr -> i64
%129 = arith.constant 2 : i32
%131 = arith.extsi %129 : i32 to i64
%130 = arith.divsi %128, %131 : i64
llvm.store %130, %108 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%132 = llvm.load %104 : !llvm.ptr -> i64
%133 = llvm.load %108 : !llvm.ptr -> i64
%134 = arith.addi %132, %133 : i64
llvm.store %134, %104 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%135 = llvm.load %104 : !llvm.ptr -> i64
func.return %135 : i64
}
func.func @main() -> i32 {
%136 = llvm.mlir.addressof @str_0 : !llvm.ptr
%138 = arith.constant 0.1 : f32
%139 = arith.extf %138 : f32 to f64
%137 = func.call @solve(%139) : (f64) -> i64
%140 = llvm.call @printf(%136, %137) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%141 = arith.constant 0 : i32
func.return %141 : i32
}
}