Problem 914

Triangles inside Circles — max inradius of primitive Pythagorean triple with hypotenuse < 2R. F(10^18).

Answer414213562371805310
Output414213562371805310
StatusPASS
Native helperno
Runtime50 ms
Peak memory1104 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^2)
ApproachFlow solutionBottom-up DP
VerdictOptimal

Flow source

# Project Euler 914
# Triangles inside Circles — max inradius of primitive Pythagorean
# triple with hypotenuse < 2R. F(10^18).

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

function igcd(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 isqrt(n: i64) -> i64 {
    if n <= 0 { return 0 }
    let mut x: i64 = sqrt(n as f64) as i64
    while (x + 1) * (x + 1) <= n { x = x + 1 }
    while x > 0 && x * x > n { x = x - 1 }
    return x
}

function F(R: i64) -> i64 {
    if R <= 0 { return 0 }
    let limit: i64 = 2 * R - 1
    if limit <= 0 { return 0 }
    let sqrt2: f64 = sqrt(2.0)
    let n0: i64 = sqrt(R as f64 / (2.0 + sqrt2)) as i64
    let mut W: i64 = n0 / 2000 + 10000
    if W < 10000 { W = 10000 }
    let mut best: i64 = 0
    let mut lo: i64 = n0 - W
    if lo < 1 { lo = 1 }
    let hi: i64 = n0 + W
    let mut n: i64 = lo
    while n <= hi {
        let t: i64 = limit - n * n
        if t > 0 {
            let mut m: i64 = isqrt(t)
            if m > n {
                if ((m - n) & 1) == 0 {
                    m = m - 1
                }
                while m > n && igcd(m, n) != 1 {
                    m = m - 2
                }
                if m > n {
                    let r: i64 = n * (m - n)
                    if r > best { best = r }
                }
            }
        }
        n = n + 1
    }
    return best
}

function main() -> i32 {
    printf("%lld\n", F(1000000000000000000))
    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 igcd_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t F_i64(int64_t R);
int32_t main(void);


int64_t igcd_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;
}

int64_t isqrt_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    int64_t x = ((int64_t)(sqrt(((double)(n)))));
    while (((x + 1) * (x + 1)) <= n) {
        x = (x + 1);
    }
    while ((x > 0 && (x * x) > n)) {
        x = (x - 1);
    }
    return x;
}

int64_t F_i64(int64_t R) {
    if (R <= 0) {
        return 0;
    }
    int64_t limit = ((2 * R) - 1);
    if (limit <= 0) {
        return 0;
    }
    double sqrt2 = sqrt(2.0);
    int64_t n0 = ((int64_t)(sqrt((((double)(R)) / (2.0 + sqrt2)))));
    int64_t W = (FLOW_CHECKED_DIV((n0), (2000)) + 10000);
    if (W < 10000) {
        W = 10000;
    }
    int64_t best = 0;
    int64_t lo = (n0 - W);
    if (lo < 1) {
        lo = 1;
    }
    int64_t hi = (n0 + W);
    int64_t n = lo;
    while (n <= hi) {
        int64_t t = (limit - (n * n));
        if (t > 0) {
            int64_t m = isqrt_i64(t);
            if (m > n) {
                if (((m - n) & 1) == 0) {
                    m = (m - 1);
                }
                while ((m > n && igcd_i64_i64(m, n) != 1)) {
                    m = (m - 2);
                }
                if (m > n) {
                    int64_t r = (n * (m - n));
                    if (r > best) {
                        best = r;
                    }
                }
            }
        }
        n = (n + 1);
    }
    return best;
}

int32_t main(void) {
    printf("%lld\n", F_i64(1000000000000000000));
    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 @igcd(%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 @isqrt(%arg0: i64) -> i64 {
    %13 = arith.constant 0 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.cmpi sle, %arg0, %15 : i64
    cf.cond_br %14, ^bb3, ^bb4
    ^bb3:
      %16 = arith.constant 0 : i32
      %17 = arith.extsi %16 : i32 to i64
      func.return %17 : i64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %18 = arith.sitofp %arg0 : i64 to f64
    %19 = math.sqrt %18 : f64
    %20 = arith.fptosi %19 : f64 to i64
    %21 = llvm.mlir.constant(1 : i64) : i64
    %22 = llvm.alloca %21 x i64 : (i64) -> !llvm.ptr
    llvm.store %20, %22 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %23 = llvm.load %22 : !llvm.ptr -> i64
    %24 = arith.constant 1 : i32
    %26 = arith.extsi %24 : i32 to i64
    %25 = arith.addi %23, %26 : i64
    %27 = llvm.load %22 : !llvm.ptr -> i64
    %28 = arith.constant 1 : i32
    %30 = arith.extsi %28 : i32 to i64
    %29 = arith.addi %27, %30 : i64
    %31 = arith.muli %25, %29 : i64
    %32 = arith.cmpi sle, %31, %arg0 : i64
    cf.cond_br %32, ^bb7, ^bb8
    ^bb7:
      %33 = llvm.load %22 : !llvm.ptr -> i64
      %34 = arith.constant 1 : i32
      %36 = arith.extsi %34 : i32 to i64
      %35 = arith.addi %33, %36 : i64
      llvm.store %35, %22 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    cf.br ^bb9
    ^bb9:
    %37 = llvm.load %22 : !llvm.ptr -> i64
    %38 = arith.constant 0 : i32
    %40 = arith.extsi %38 : i32 to i64
    %39 = arith.cmpi sgt, %37, %40 : i64
    %41 = scf.if %39 -> (i1) {
      %42 = llvm.load %22 : !llvm.ptr -> i64
      %43 = llvm.load %22 : !llvm.ptr -> i64
      %44 = arith.muli %42, %43 : i64
      %45 = arith.cmpi sgt, %44, %arg0 : i64
      scf.yield %45 : i1
    } else {
      %46 = arith.constant false
      scf.yield %46 : i1
    }
    cf.cond_br %41, ^bb10, ^bb11
    ^bb10:
      %47 = llvm.load %22 : !llvm.ptr -> i64
      %48 = arith.constant 1 : i32
      %50 = arith.extsi %48 : i32 to i64
      %49 = arith.subi %47, %50 : i64
      llvm.store %49, %22 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %51 = llvm.load %22 : !llvm.ptr -> i64
    func.return %51 : i64
  }
  func.func @F(%arg0: i64) -> i64 {
    %52 = arith.constant 0 : i32
    %54 = arith.extsi %52 : i32 to i64
    %53 = arith.cmpi sle, %arg0, %54 : i64
    cf.cond_br %53, ^bb12, ^bb13
    ^bb12:
      %55 = arith.constant 0 : i32
      %56 = arith.extsi %55 : i32 to i64
      func.return %56 : i64
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %57 = arith.constant 2 : i32
    %59 = arith.extsi %57 : i32 to i64
    %58 = arith.muli %59, %arg0 : i64
    %60 = arith.constant 1 : i32
    %62 = arith.extsi %60 : i32 to i64
    %61 = arith.subi %58, %62 : i64
    %63 = arith.constant 0 : i32
    %65 = arith.extsi %63 : i32 to i64
    %64 = arith.cmpi sle, %61, %65 : i64
    cf.cond_br %64, ^bb15, ^bb16
    ^bb15:
      %66 = arith.constant 0 : i32
      %67 = arith.extsi %66 : i32 to i64
      func.return %67 : i64
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %68 = arith.constant 2.0 : f32
    %69 = math.sqrt %68 : f32
    %70 = arith.extf %69 : f32 to f64
    %71 = arith.sitofp %arg0 : i64 to f64
    %72 = arith.constant 2.0 : f32
    %74 = arith.extf %72 : f32 to f64
    %73 = arith.addf %74, %70 : f64
    %75 = arith.divf %71, %73 : f64
    %76 = math.sqrt %75 : f64
    %77 = arith.fptosi %76 : f64 to i64
    %78 = arith.constant 2000 : i32
    %80 = arith.extsi %78 : i32 to i64
    %79 = arith.divsi %77, %80 : i64
    %81 = arith.constant 10000 : i32
    %83 = arith.extsi %81 : i32 to i64
    %82 = arith.addi %79, %83 : i64
    %84 = llvm.mlir.constant(1 : i64) : i64
    %85 = llvm.alloca %84 x i64 : (i64) -> !llvm.ptr
    llvm.store %82, %85 : i64, !llvm.ptr
    %86 = llvm.load %85 : !llvm.ptr -> i64
    %87 = arith.constant 10000 : i32
    %89 = arith.extsi %87 : i32 to i64
    %88 = arith.cmpi slt, %86, %89 : i64
    cf.cond_br %88, ^bb18, ^bb19
    ^bb18:
      %90 = arith.constant 10000 : i32
      %91 = arith.extsi %90 : i32 to i64
      llvm.store %91, %85 : i64, !llvm.ptr
      cf.br ^bb20
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %92 = arith.constant 0 : i32
    %93 = arith.extsi %92 : i32 to i64
    %94 = llvm.mlir.constant(1 : i64) : i64
    %95 = llvm.alloca %94 x i64 : (i64) -> !llvm.ptr
    llvm.store %93, %95 : i64, !llvm.ptr
    %96 = llvm.load %85 : !llvm.ptr -> i64
    %97 = arith.subi %77, %96 : i64
    %98 = llvm.mlir.constant(1 : i64) : i64
    %99 = llvm.alloca %98 x i64 : (i64) -> !llvm.ptr
    llvm.store %97, %99 : i64, !llvm.ptr
    %100 = llvm.load %99 : !llvm.ptr -> i64
    %101 = arith.constant 1 : i32
    %103 = arith.extsi %101 : i32 to i64
    %102 = arith.cmpi slt, %100, %103 : i64
    cf.cond_br %102, ^bb21, ^bb22
    ^bb21:
      %104 = arith.constant 1 : i32
      %105 = arith.extsi %104 : i32 to i64
      llvm.store %105, %99 : i64, !llvm.ptr
      cf.br ^bb23
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %106 = llvm.load %85 : !llvm.ptr -> i64
    %107 = arith.addi %77, %106 : i64
    %108 = llvm.load %99 : !llvm.ptr -> i64
    %109 = llvm.mlir.constant(1 : i64) : i64
    %110 = llvm.alloca %109 x i64 : (i64) -> !llvm.ptr
    llvm.store %108, %110 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %111 = llvm.load %110 : !llvm.ptr -> i64
    %112 = arith.cmpi sle, %111, %107 : i64
    cf.cond_br %112, ^bb25, ^bb26
    ^bb25:
      %113 = llvm.load %110 : !llvm.ptr -> i64
      %114 = llvm.load %110 : !llvm.ptr -> i64
      %115 = arith.muli %113, %114 : i64
      %116 = arith.subi %61, %115 : i64
      %117 = arith.constant 0 : i32
      %119 = arith.extsi %117 : i32 to i64
      %118 = arith.cmpi sgt, %116, %119 : i64
      cf.cond_br %118, ^bb27, ^bb28
      ^bb27:
        %120 = func.call @isqrt(%116) : (i64) -> i64
        %121 = llvm.mlir.constant(1 : i64) : i64
        %122 = llvm.alloca %121 x i64 : (i64) -> !llvm.ptr
        llvm.store %120, %122 : i64, !llvm.ptr
        %123 = llvm.load %122 : !llvm.ptr -> i64
        %124 = llvm.load %110 : !llvm.ptr -> i64
        %125 = arith.cmpi sgt, %123, %124 : i64
        cf.cond_br %125, ^bb30, ^bb31
        ^bb30:
          %126 = llvm.load %122 : !llvm.ptr -> i64
          %127 = llvm.load %110 : !llvm.ptr -> i64
          %128 = arith.subi %126, %127 : i64
          %129 = arith.constant 1 : i32
          %131 = arith.extsi %129 : i32 to i64
          %130 = arith.andi %128, %131 : i64
          %132 = arith.constant 0 : i32
          %134 = arith.extsi %132 : i32 to i64
          %133 = arith.cmpi eq, %130, %134 : i64
          cf.cond_br %133, ^bb33, ^bb34
          ^bb33:
            %135 = llvm.load %122 : !llvm.ptr -> i64
            %136 = arith.constant 1 : i32
            %138 = arith.extsi %136 : i32 to i64
            %137 = arith.subi %135, %138 : i64
            llvm.store %137, %122 : i64, !llvm.ptr
            cf.br ^bb35
          ^bb34:
            cf.br ^bb35
          ^bb35:
          cf.br ^bb36
          ^bb36:
          %139 = llvm.load %122 : !llvm.ptr -> i64
          %140 = llvm.load %110 : !llvm.ptr -> i64
          %141 = arith.cmpi sgt, %139, %140 : i64
          %142 = scf.if %141 -> (i1) {
            %144 = llvm.load %122 : !llvm.ptr -> i64
            %145 = llvm.load %110 : !llvm.ptr -> i64
            %143 = func.call @igcd(%144, %145) : (i64, i64) -> i64
            %146 = arith.constant 1 : i32
            %148 = arith.extsi %146 : i32 to i64
            %147 = arith.cmpi ne, %143, %148 : i64
            scf.yield %147 : i1
          } else {
            %149 = arith.constant false
            scf.yield %149 : i1
          }
          cf.cond_br %142, ^bb37, ^bb38
          ^bb37:
            %150 = llvm.load %122 : !llvm.ptr -> i64
            %151 = arith.constant 2 : i32
            %153 = arith.extsi %151 : i32 to i64
            %152 = arith.subi %150, %153 : i64
            llvm.store %152, %122 : i64, !llvm.ptr
            cf.br ^bb36
          ^bb38:
          %154 = llvm.load %122 : !llvm.ptr -> i64
          %155 = llvm.load %110 : !llvm.ptr -> i64
          %156 = arith.cmpi sgt, %154, %155 : i64
          cf.cond_br %156, ^bb39, ^bb40
          ^bb39:
            %157 = llvm.load %110 : !llvm.ptr -> i64
            %158 = llvm.load %122 : !llvm.ptr -> i64
            %159 = llvm.load %110 : !llvm.ptr -> i64
            %160 = arith.subi %158, %159 : i64
            %161 = arith.muli %157, %160 : i64
            %162 = llvm.load %95 : !llvm.ptr -> i64
            %163 = arith.cmpi sgt, %161, %162 : i64
            cf.cond_br %163, ^bb42, ^bb43
            ^bb42:
              llvm.store %161, %95 : i64, !llvm.ptr
              cf.br ^bb44
            ^bb43:
              cf.br ^bb44
            ^bb44:
            cf.br ^bb41
          ^bb40:
            cf.br ^bb41
          ^bb41:
          cf.br ^bb32
        ^bb31:
          cf.br ^bb32
        ^bb32:
        cf.br ^bb29
      ^bb28:
        cf.br ^bb29
      ^bb29:
      %164 = llvm.load %110 : !llvm.ptr -> i64
      %165 = arith.constant 1 : i32
      %167 = arith.extsi %165 : i32 to i64
      %166 = arith.addi %164, %167 : i64
      llvm.store %166, %110 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %168 = llvm.load %95 : !llvm.ptr -> i64
    func.return %168 : i64
  }
  func.func @main() -> i32 {
    %169 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %171 = arith.constant 999999995705032704 : i32
    %172 = arith.extsi %171 : i32 to i64
    %170 = func.call @F(%172) : (i64) -> i64
    %173 = llvm.call @printf(%169, %170) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %174 = arith.constant 0 : i32
    func.return %174 : i32
  }
}