Problem 363
Bézier curve vs quarter circle: percentage error to 10 decimals.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(2^n) | O(n) |
| Space complexity | O(1) | O(1) |
| Approach | Flow solution | Numerical iteration |
| Verdict | Suboptimal |
Flow source
# Project Euler 363
# Bézier curve vs quarter circle: percentage error to 10 decimals.
extern {
function sqrt(x: f64) -> f64
}
function dist(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
let dx: f64 = x1 - x2
let dy: f64 = y1 - y2
return sqrt(dx * dx + dy * dy)
}
function bezier_x(v: f64, t: f64) -> f64 {
let u: f64 = 1.0 - t
return u * u * u + 3.0 * u * u * t + 3.0 * u * t * t * v
}
function bezier_y(v: f64, t: f64) -> f64 {
let u: f64 = 1.0 - t
return 3.0 * u * u * t * v + 3.0 * u * t * t + t * t * t
}
function get_length(v: f64, start: f64, end: f64, epsilon: f64) -> f64 {
let bisect: f64 = (start + end) / 2.0
let xs: f64 = bezier_x(v, start)
let ys: f64 = bezier_y(v, start)
let xm: f64 = bezier_x(v, bisect)
let ym: f64 = bezier_y(v, bisect)
let xe: f64 = bezier_x(v, end)
let ye: f64 = bezier_y(v, end)
let total: f64 = dist(xs, ys, xe, ye)
let first: f64 = dist(xm, ym, xs, ys)
let second: f64 = dist(xm, ym, xe, ye)
let more: f64 = first + second
if more < total + epsilon {
return more
}
return get_length(v, start, bisect, epsilon) + get_length(v, bisect, end, epsilon)
}
function main() -> i32 {
let v: f64 = 0.5517784778044677
let pi_div2: f64 = 1.5707963267948966
let epsilon: f64 = 0.00000000000000001
let length: f64 = get_length(v, 0.0, 1.0, epsilon)
let error: f64 = 100.0 * (length - pi_div2) / pi_div2
printf("%.10f\n", error)
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 dist_f64_f64_f64_f64(double x1, double y1, double x2, double y2);
double bezier_x_f64_f64(double v, double t);
double bezier_y_f64_f64(double v, double t);
double get_length_f64_f64_f64_f64(double v, double start, double end, double epsilon);
int32_t main(void);
double dist_f64_f64_f64_f64(double x1, double y1, double x2, double y2) {
double dx = (x1 - x2);
double dy = (y1 - y2);
return sqrt(((dx * dx) + (dy * dy)));
}
double bezier_x_f64_f64(double v, double t) {
double u = (1.0 - t);
return ((((u * u) * u) + (((3.0 * u) * u) * t)) + ((((3.0 * u) * t) * t) * v));
}
double bezier_y_f64_f64(double v, double t) {
double u = (1.0 - t);
return ((((((3.0 * u) * u) * t) * v) + (((3.0 * u) * t) * t)) + ((t * t) * t));
}
double get_length_f64_f64_f64_f64(double v, double start, double end, double epsilon) {
double bisect = ((start + end) / 2.0);
double xs = bezier_x_f64_f64(v, start);
double ys = bezier_y_f64_f64(v, start);
double xm = bezier_x_f64_f64(v, bisect);
double ym = bezier_y_f64_f64(v, bisect);
double xe = bezier_x_f64_f64(v, end);
double ye = bezier_y_f64_f64(v, end);
double total = dist_f64_f64_f64_f64(xs, ys, xe, ye);
double first = dist_f64_f64_f64_f64(xm, ym, xs, ys);
double second = dist_f64_f64_f64_f64(xm, ym, xe, ye);
double more = (first + second);
if (more < (total + epsilon)) {
return more;
}
return (get_length_f64_f64_f64_f64(v, start, bisect, epsilon) + get_length_f64_f64_f64_f64(v, bisect, end, epsilon));
}
int32_t main(void) {
double v = 0.5517784778044677;
double pi_div2 = 1.5707963267948966;
double epsilon = 0.00000000000000001;
double length = get_length_f64_f64_f64_f64(v, 0.0, 1.0, epsilon);
double error = ((100.0 * (length - pi_div2)) / pi_div2);
printf("%.10f\n", error);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.10f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
func.func private @sqrt(f64) -> f64
func.func @dist(%arg0: f64, %arg1: f64, %arg2: f64, %arg3: f64) -> f64 {
%0 = arith.subf %arg0, %arg2 : f64
%1 = arith.subf %arg1, %arg3 : f64
%2 = arith.mulf %0, %0 : f64
%3 = arith.mulf %1, %1 : f64
%4 = arith.addf %2, %3 : f64
%5 = math.sqrt %4 : f64
func.return %5 : f64
}
func.func @bezier_x(%arg0: f64, %arg1: f64) -> f64 {
%6 = arith.constant 1.0 : f32
%8 = arith.extf %6 : f32 to f64
%7 = arith.subf %8, %arg1 : f64
%9 = arith.mulf %7, %7 : f64
%10 = arith.mulf %9, %7 : f64
%11 = arith.constant 3.0 : f32
%13 = arith.extf %11 : f32 to f64
%12 = arith.mulf %13, %7 : f64
%14 = arith.mulf %12, %7 : f64
%15 = arith.mulf %14, %arg1 : f64
%16 = arith.addf %10, %15 : f64
%17 = arith.constant 3.0 : f32
%19 = arith.extf %17 : f32 to f64
%18 = arith.mulf %19, %7 : f64
%20 = arith.mulf %18, %arg1 : f64
%21 = arith.mulf %20, %arg1 : f64
%22 = arith.mulf %21, %arg0 : f64
%23 = arith.addf %16, %22 : f64
func.return %23 : f64
}
func.func @bezier_y(%arg0: f64, %arg1: f64) -> f64 {
%24 = arith.constant 1.0 : f32
%26 = arith.extf %24 : f32 to f64
%25 = arith.subf %26, %arg1 : f64
%27 = arith.constant 3.0 : f32
%29 = arith.extf %27 : f32 to f64
%28 = arith.mulf %29, %25 : f64
%30 = arith.mulf %28, %25 : f64
%31 = arith.mulf %30, %arg1 : f64
%32 = arith.mulf %31, %arg0 : f64
%33 = arith.constant 3.0 : f32
%35 = arith.extf %33 : f32 to f64
%34 = arith.mulf %35, %25 : f64
%36 = arith.mulf %34, %arg1 : f64
%37 = arith.mulf %36, %arg1 : f64
%38 = arith.addf %32, %37 : f64
%39 = arith.mulf %arg1, %arg1 : f64
%40 = arith.mulf %39, %arg1 : f64
%41 = arith.addf %38, %40 : f64
func.return %41 : f64
}
func.func @get_length(%arg0: f64, %arg1: f64, %arg2: f64, %arg3: f64) -> f64 {
%42 = arith.addf %arg1, %arg2 : f64
%43 = arith.constant 2.0 : f32
%45 = arith.extf %43 : f32 to f64
%44 = arith.divf %42, %45 : f64
%46 = func.call @bezier_x(%arg0, %arg1) : (f64, f64) -> f64
%47 = func.call @bezier_y(%arg0, %arg1) : (f64, f64) -> f64
%48 = func.call @bezier_x(%arg0, %44) : (f64, f64) -> f64
%49 = func.call @bezier_y(%arg0, %44) : (f64, f64) -> f64
%50 = func.call @bezier_x(%arg0, %arg2) : (f64, f64) -> f64
%51 = func.call @bezier_y(%arg0, %arg2) : (f64, f64) -> f64
%52 = func.call @dist(%46, %47, %50, %51) : (f64, f64, f64, f64) -> f64
%53 = func.call @dist(%48, %49, %46, %47) : (f64, f64, f64, f64) -> f64
%54 = func.call @dist(%48, %49, %50, %51) : (f64, f64, f64, f64) -> f64
%55 = arith.addf %53, %54 : f64
%56 = arith.addf %52, %arg3 : f64
%57 = arith.cmpf olt, %55, %56 : f64
cf.cond_br %57, ^bb0, ^bb1
^bb0:
func.return %55 : f64
^bb1:
cf.br ^bb2
^bb2:
%58 = func.call @get_length(%arg0, %arg1, %44, %arg3) : (f64, f64, f64, f64) -> f64
%59 = func.call @get_length(%arg0, %44, %arg2, %arg3) : (f64, f64, f64, f64) -> f64
%60 = arith.addf %58, %59 : f64
func.return %60 : f64
}
func.func @main() -> i32 {
%61 = arith.constant 0.5517784778044677 : f32
%62 = arith.extf %61 : f32 to f64
%63 = arith.constant 1.5707963267948966 : f32
%64 = arith.extf %63 : f32 to f64
%65 = arith.constant 0.00000000000000001 : f32
%66 = arith.extf %65 : f32 to f64
%68 = arith.constant 0.0 : f32
%69 = arith.constant 1.0 : f32
%70 = arith.extf %68 : f32 to f64
%71 = arith.extf %69 : f32 to f64
%67 = func.call @get_length(%62, %70, %71, %66) : (f64, f64, f64, f64) -> f64
%72 = arith.constant 100.0 : f32
%73 = arith.subf %67, %64 : f64
%75 = arith.extf %72 : f32 to f64
%74 = arith.mulf %75, %73 : f64
%76 = arith.divf %74, %64 : f64
%77 = llvm.mlir.addressof @str_0 : !llvm.ptr
%78 = llvm.call @printf(%77, %76) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%79 = arith.constant 0 : i32
func.return %79 : i32
}
}