Problem 727

For each triple ra < rb < rc <= 100 with gcd 1, place the three circle centers, take D as the incenter of the center triangle (circle through the tangency points is its incircle), find the inner Soddy circle center E by Descartes' theorem plus trilateration, and average d = |DE|.

Answer3.64039141
Output3.64039141
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^3) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n^2)
Space complexityO(1)O(n^2)
ApproachFlow solutionBottom-up DP
VerdictSuboptimal

Flow source

# Project Euler 727: Triangle of Circular Arcs
# For each triple ra < rb < rc <= 100 with gcd 1, place the three circle
# centers, take D as the incenter of the center triangle (circle through the
# tangency points is its incircle), find the inner Soddy circle center E by
# Descartes' theorem plus trilateration, and average d = |DE|.

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

function gcd(a: i64, b: i64) -> i64 {
    let mut x: i64 = a
    let mut y: i64 = b
    while y != 0 {
        let t: i64 = x % y
        x = y
        y = t
    }
    return x
}

function dist_de(ra: i64, rb: i64, rc: i64) -> f64 {
    let c: f64 = (ra + rb) as f64
    let b: f64 = (ra + rc) as f64
    let a: f64 = (rb + rc) as f64
    let cx: f64 = (b * b + c * c - a * a) / (2.0 * c)
    let cy: f64 = sqrt(b * b - cx * cx)
    let bx: f64 = c
    # D = incenter of the triangle of centers
    let s2: f64 = a + b + c
    let dx: f64 = (b * bx + c * cx) / s2
    let dy: f64 = (c * cy) / s2
    # inner Soddy circle radius via Descartes' theorem
    let k1: f64 = 1.0 / (ra as f64)
    let k2: f64 = 1.0 / (rb as f64)
    let k3: f64 = 1.0 / (rc as f64)
    let k4: f64 = k1 + k2 + k3 + 2.0 * sqrt(k1 * k2 + k2 * k3 + k3 * k1)
    let r4: f64 = 1.0 / k4
    let RA: f64 = (ra as f64) + r4
    let RB: f64 = (rb as f64) + r4
    let RC: f64 = (rc as f64) + r4
    # trilateration: subtract pairs of circle equations to get a linear system
    let a11: f64 = 2.0 * bx
    let b1: f64 = RA * RA - RB * RB + bx * bx
    let a21: f64 = 2.0 * cx
    let a22: f64 = 2.0 * cy
    let b2: f64 = RA * RA - RC * RC + cx * cx + cy * cy
    let ex: f64 = b1 / a11
    let ey: f64 = (b2 - a21 * ex) / a22
    let vx: f64 = dx - ex
    let vy: f64 = dy - ey
    return sqrt(vx * vx + vy * vy)
}

function main() -> i32 {
    let N: i64 = 100
    let mut total: f64 = 0.0
    let mut count: i64 = 0
    let mut ra: i64 = 1
    while ra <= N {
        let mut rb: i64 = ra + 1
        while rb <= N {
            let g: i64 = gcd(ra, rb)
            let mut rc: i64 = rb + 1
            while rc <= N {
                if gcd(g, rc) == 1 {
                    total = total + dist_de(ra, rb, rc)
                    count = count + 1
                }
                rc = rc + 1
            }
            rb = rb + 1
        }
        ra = ra + 1
    }
    printf("%.8f\n", total / (count as f64))
    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; }

int64_t gcd_i64_i64(int64_t a, int64_t b);
double dist_de_i64_i64_i64(int64_t ra, int64_t rb, int64_t rc);
int32_t main(void);


int64_t gcd_i64_i64(int64_t a, int64_t b) {
    int64_t x = a;
    int64_t y = b;
    while (y != 0) {
        int64_t t = FLOW_CHECKED_MOD((x), (y));
        x = y;
        y = t;
    }
    return x;
}

double dist_de_i64_i64_i64(int64_t ra, int64_t rb, int64_t rc) {
    double c = ((double)((ra + rb)));
    double b = ((double)((ra + rc)));
    double a = ((double)((rb + rc)));
    double cx = ((((b * b) + (c * c)) - (a * a)) / (2.0 * c));
    double cy = sqrt(((b * b) - (cx * cx)));
    double bx = c;
    double s2 = ((a + b) + c);
    double dx = (((b * bx) + (c * cx)) / s2);
    double dy = ((c * cy) / s2);
    double k1 = (1.0 / ((double)(ra)));
    double k2 = (1.0 / ((double)(rb)));
    double k3 = (1.0 / ((double)(rc)));
    double k4 = (((k1 + k2) + k3) + (2.0 * sqrt((((k1 * k2) + (k2 * k3)) + (k3 * k1)))));
    double r4 = (1.0 / k4);
    double RA = (((double)(ra)) + r4);
    double RB = (((double)(rb)) + r4);
    double RC = (((double)(rc)) + r4);
    double a11 = (2.0 * bx);
    double b1 = (((RA * RA) - (RB * RB)) + (bx * bx));
    double a21 = (2.0 * cx);
    double a22 = (2.0 * cy);
    double b2 = ((((RA * RA) - (RC * RC)) + (cx * cx)) + (cy * cy));
    double ex = (b1 / a11);
    double ey = ((b2 - (a21 * ex)) / a22);
    double vx = (dx - ex);
    double vy = (dy - ey);
    return sqrt(((vx * vx) + (vy * vy)));
}

int32_t main(void) {
    int64_t N = 100;
    double total = 0.0;
    int64_t count = 0;
    int64_t ra = 1;
    while (ra <= N) {
        int64_t rb = (ra + 1);
        while (rb <= N) {
            int64_t g = gcd_i64_i64(ra, rb);
            int64_t rc = (rb + 1);
            while (rc <= N) {
                if (gcd_i64_i64(g, rc) == 1) {
                    total = (total + dist_de_i64_i64_i64(ra, rb, rc));
                    count = (count + 1);
                }
                rc = (rc + 1);
            }
            rb = (rb + 1);
        }
        ra = (ra + 1);
    }
    printf("%.8f\n", (total / ((double)(count))));
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.8f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @sqrt(f64) -> f64
  func.func @gcd(%arg0: i64, %arg1: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %1 : i64, !llvm.ptr
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %3 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %4 = llvm.load %3 : !llvm.ptr -> i64
    %5 = arith.constant 0 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.cmpi ne, %4, %7 : i64
    cf.cond_br %6, ^bb1, ^bb2
    ^bb1:
      %8 = llvm.load %1 : !llvm.ptr -> i64
      %9 = llvm.load %3 : !llvm.ptr -> i64
      %10 = arith.remsi %8, %9 : i64
      %11 = llvm.load %3 : !llvm.ptr -> i64
      llvm.store %11, %1 : i64, !llvm.ptr
      llvm.store %10, %3 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %12 = llvm.load %1 : !llvm.ptr -> i64
    func.return %12 : i64
  }
  func.func @dist_de(%arg0: i64, %arg1: i64, %arg2: i64) -> f64 {
    %13 = arith.addi %arg0, %arg1 : i64
    %14 = arith.sitofp %13 : i64 to f64
    %15 = arith.addi %arg0, %arg2 : i64
    %16 = arith.sitofp %15 : i64 to f64
    %17 = arith.addi %arg1, %arg2 : i64
    %18 = arith.sitofp %17 : i64 to f64
    %19 = arith.mulf %16, %16 : f64
    %20 = arith.mulf %14, %14 : f64
    %21 = arith.addf %19, %20 : f64
    %22 = arith.mulf %18, %18 : f64
    %23 = arith.subf %21, %22 : f64
    %24 = arith.constant 2.0 : f32
    %26 = arith.extf %24 : f32 to f64
    %25 = arith.mulf %26, %14 : f64
    %27 = arith.divf %23, %25 : f64
    %28 = arith.mulf %16, %16 : f64
    %29 = arith.mulf %27, %27 : f64
    %30 = arith.subf %28, %29 : f64
    %31 = math.sqrt %30 : f64
    %32 = arith.addf %18, %16 : f64
    %33 = arith.addf %32, %14 : f64
    %34 = arith.mulf %16, %14 : f64
    %35 = arith.mulf %14, %27 : f64
    %36 = arith.addf %34, %35 : f64
    %37 = arith.divf %36, %33 : f64
    %38 = arith.mulf %14, %31 : f64
    %39 = arith.divf %38, %33 : f64
    %40 = arith.constant 1.0 : f32
    %41 = arith.sitofp %arg0 : i64 to f64
    %43 = arith.extf %40 : f32 to f64
    %42 = arith.divf %43, %41 : f64
    %44 = arith.constant 1.0 : f32
    %45 = arith.sitofp %arg1 : i64 to f64
    %47 = arith.extf %44 : f32 to f64
    %46 = arith.divf %47, %45 : f64
    %48 = arith.constant 1.0 : f32
    %49 = arith.sitofp %arg2 : i64 to f64
    %51 = arith.extf %48 : f32 to f64
    %50 = arith.divf %51, %49 : f64
    %52 = arith.addf %42, %46 : f64
    %53 = arith.addf %52, %50 : f64
    %54 = arith.constant 2.0 : f32
    %55 = arith.mulf %42, %46 : f64
    %56 = arith.mulf %46, %50 : f64
    %57 = arith.addf %55, %56 : f64
    %58 = arith.mulf %50, %42 : f64
    %59 = arith.addf %57, %58 : f64
    %60 = math.sqrt %59 : f64
    %62 = arith.extf %54 : f32 to f64
    %61 = arith.mulf %62, %60 : f64
    %63 = arith.addf %53, %61 : f64
    %64 = arith.constant 1.0 : f32
    %66 = arith.extf %64 : f32 to f64
    %65 = arith.divf %66, %63 : f64
    %67 = arith.sitofp %arg0 : i64 to f64
    %68 = arith.addf %67, %65 : f64
    %69 = arith.sitofp %arg1 : i64 to f64
    %70 = arith.addf %69, %65 : f64
    %71 = arith.sitofp %arg2 : i64 to f64
    %72 = arith.addf %71, %65 : f64
    %73 = arith.constant 2.0 : f32
    %75 = arith.extf %73 : f32 to f64
    %74 = arith.mulf %75, %14 : f64
    %76 = arith.mulf %68, %68 : f64
    %77 = arith.mulf %70, %70 : f64
    %78 = arith.subf %76, %77 : f64
    %79 = arith.mulf %14, %14 : f64
    %80 = arith.addf %78, %79 : f64
    %81 = arith.constant 2.0 : f32
    %83 = arith.extf %81 : f32 to f64
    %82 = arith.mulf %83, %27 : f64
    %84 = arith.constant 2.0 : f32
    %86 = arith.extf %84 : f32 to f64
    %85 = arith.mulf %86, %31 : f64
    %87 = arith.mulf %68, %68 : f64
    %88 = arith.mulf %72, %72 : f64
    %89 = arith.subf %87, %88 : f64
    %90 = arith.mulf %27, %27 : f64
    %91 = arith.addf %89, %90 : f64
    %92 = arith.mulf %31, %31 : f64
    %93 = arith.addf %91, %92 : f64
    %94 = arith.divf %80, %74 : f64
    %95 = arith.mulf %82, %94 : f64
    %96 = arith.subf %93, %95 : f64
    %97 = arith.divf %96, %85 : f64
    %98 = arith.subf %37, %94 : f64
    %99 = arith.subf %39, %97 : f64
    %100 = arith.mulf %98, %98 : f64
    %101 = arith.mulf %99, %99 : f64
    %102 = arith.addf %100, %101 : f64
    %103 = math.sqrt %102 : f64
    func.return %103 : f64
  }
  func.func @main() -> i32 {
    %104 = arith.constant 100 : i32
    %105 = arith.extsi %104 : i32 to i64
    %106 = arith.constant 0.0 : f32
    %107 = arith.extf %106 : f32 to f64
    %108 = llvm.mlir.constant(1 : i64) : i64
    %109 = llvm.alloca %108 x f64 : (i64) -> !llvm.ptr
    llvm.store %107, %109 : f64, !llvm.ptr
    %110 = arith.constant 0 : i32
    %111 = arith.extsi %110 : i32 to i64
    %112 = llvm.mlir.constant(1 : i64) : i64
    %113 = llvm.alloca %112 x i64 : (i64) -> !llvm.ptr
    llvm.store %111, %113 : i64, !llvm.ptr
    %114 = arith.constant 1 : i32
    %115 = arith.extsi %114 : i32 to i64
    %116 = llvm.mlir.constant(1 : i64) : i64
    %117 = llvm.alloca %116 x i64 : (i64) -> !llvm.ptr
    llvm.store %115, %117 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %118 = llvm.load %117 : !llvm.ptr -> i64
    %119 = arith.cmpi sle, %118, %105 : i64
    cf.cond_br %119, ^bb4, ^bb5
    ^bb4:
      %120 = llvm.load %117 : !llvm.ptr -> i64
      %121 = arith.constant 1 : i32
      %123 = arith.extsi %121 : i32 to i64
      %122 = arith.addi %120, %123 : i64
      %124 = llvm.mlir.constant(1 : i64) : i64
      %125 = llvm.alloca %124 x i64 : (i64) -> !llvm.ptr
      llvm.store %122, %125 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %126 = llvm.load %125 : !llvm.ptr -> i64
      %127 = arith.cmpi sle, %126, %105 : i64
      cf.cond_br %127, ^bb7, ^bb8
      ^bb7:
        %129 = llvm.load %117 : !llvm.ptr -> i64
        %130 = llvm.load %125 : !llvm.ptr -> i64
        %128 = func.call @gcd(%129, %130) : (i64, i64) -> i64
        %131 = llvm.load %125 : !llvm.ptr -> i64
        %132 = arith.constant 1 : i32
        %134 = arith.extsi %132 : i32 to i64
        %133 = arith.addi %131, %134 : i64
        %135 = llvm.mlir.constant(1 : i64) : i64
        %136 = llvm.alloca %135 x i64 : (i64) -> !llvm.ptr
        llvm.store %133, %136 : i64, !llvm.ptr
        cf.br ^bb9
        ^bb9:
        %137 = llvm.load %136 : !llvm.ptr -> i64
        %138 = arith.cmpi sle, %137, %105 : i64
        cf.cond_br %138, ^bb10, ^bb11
        ^bb10:
          %140 = llvm.load %136 : !llvm.ptr -> i64
          %139 = func.call @gcd(%128, %140) : (i64, i64) -> i64
          %141 = arith.constant 1 : i32
          %143 = arith.extsi %141 : i32 to i64
          %142 = arith.cmpi eq, %139, %143 : i64
          cf.cond_br %142, ^bb12, ^bb13
          ^bb12:
            %144 = llvm.load %109 : !llvm.ptr -> f64
            %146 = llvm.load %117 : !llvm.ptr -> i64
            %147 = llvm.load %125 : !llvm.ptr -> i64
            %148 = llvm.load %136 : !llvm.ptr -> i64
            %145 = func.call @dist_de(%146, %147, %148) : (i64, i64, i64) -> f64
            %149 = arith.addf %144, %145 : f64
            llvm.store %149, %109 : f64, !llvm.ptr
            %150 = llvm.load %113 : !llvm.ptr -> i64
            %151 = arith.constant 1 : i32
            %153 = arith.extsi %151 : i32 to i64
            %152 = arith.addi %150, %153 : i64
            llvm.store %152, %113 : i64, !llvm.ptr
            cf.br ^bb14
          ^bb13:
            cf.br ^bb14
          ^bb14:
          %154 = llvm.load %136 : !llvm.ptr -> i64
          %155 = arith.constant 1 : i32
          %157 = arith.extsi %155 : i32 to i64
          %156 = arith.addi %154, %157 : i64
          llvm.store %156, %136 : i64, !llvm.ptr
          cf.br ^bb9
        ^bb11:
        %158 = llvm.load %125 : !llvm.ptr -> i64
        %159 = arith.constant 1 : i32
        %161 = arith.extsi %159 : i32 to i64
        %160 = arith.addi %158, %161 : i64
        llvm.store %160, %125 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %162 = llvm.load %117 : !llvm.ptr -> i64
      %163 = arith.constant 1 : i32
      %165 = arith.extsi %163 : i32 to i64
      %164 = arith.addi %162, %165 : i64
      llvm.store %164, %117 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %166 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %167 = llvm.load %109 : !llvm.ptr -> f64
    %168 = llvm.load %113 : !llvm.ptr -> i64
    %169 = arith.sitofp %168 : i64 to f64
    %170 = arith.divf %167, %169 : f64
    %171 = llvm.call @printf(%166, %170) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %172 = arith.constant 0 : i32
    func.return %172 : i32
  }
}