Problem 557

Cutting Triangles Enumerate (s,a,d) with s=t+a, bc=a^2*d/s integer, disc square; sum areas t<=10000.

Answer2699929328
Output2699929328
StatusPASS
Native helperno
Runtime7510 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 557
# Cutting Triangles
# Enumerate (s,a,d) with s=t+a, bc=a^2*d/s integer, disc square; sum areas t<=10000.

function gcd(a0: i64, b0: i64) -> i64 {
    let mut a: i64 = a0
    let mut b: i64 = b0
    if a < 0 { a = -a }
    if b < 0 { b = -b }
    while b != 0 {
        let t: i64 = a % b
        a = b
        b = t
    }
    return a
}

function isqrt(n: i64) -> i64 {
    if n <= 0 { return 0 }
    let mut x: i64 = n
    let mut y: i64 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

function S(N: i64) -> i64 {
    let mut total: i64 = 0
    let mut s: i64 = 3
    while s <= 2 * N {
        let mut a_min: i64 = s - N
        if a_min < 1 { a_min = 1 }
        let a_max: i64 = (s - 3) / 2
        if a_min <= a_max {
            let mut a: i64 = a_min
            while a <= a_max {
                let a2: i64 = a * a
                let g: i64 = gcd(s, a2)
                if g > 1 {
                    let step: i64 = s / g
                    let max_k: i64 = (s - 2 * a - 2) / step
                    if max_k > 0 {
                        let base_prod: i64 = a2 / g
                        let mut k: i64 = 1
                        while k <= max_k {
                            let d: i64 = step * k
                            let w: i64 = s - 2 * a - d
                            let prod: i64 = base_prod * k
                            let disc: i64 = w * w - 4 * prod
                            if disc >= 0 {
                                let r: i64 = isqrt(disc)
                                if r * r == disc {
                                    if ((w - r) & 1) == 0 {
                                        let b: i64 = (w - r) / 2
                                        if b > 0 {
                                            let c: i64 = (w + r) / 2
                                            if b <= c {
                                                total = total + (s - a)
                                            }
                                        }
                                    }
                                }
                            }
                            k = k + 1
                        }
                    }
                }
                a = a + 1
            }
        }
        s = s + 1
    }
    return total
}

function main() -> i32 {
    printf("%lld\n", S(10000))
    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 a0, int64_t b0);
int64_t isqrt_i64(int64_t n);
int64_t S_i64(int64_t N);
int32_t main(void);

int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
    int64_t a = a0;
    int64_t b = b0;
    if (a < 0) {
        a = (-a);
    }
    if (b < 0) {
        b = (-b);
    }
    while (b != 0) {
        int64_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
    }
    return a;
}

int64_t isqrt_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    int64_t x = n;
    int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

int64_t S_i64(int64_t N) {
    int64_t total = 0;
    int64_t s = 3;
    while (s <= (2 * N)) {
        int64_t a_min = (s - N);
        if (a_min < 1) {
            a_min = 1;
        }
        int64_t a_max = FLOW_CHECKED_DIV(((s - 3)), (2));
        if (a_min <= a_max) {
            int64_t a = a_min;
            while (a <= a_max) {
                int64_t a2 = (a * a);
                int64_t g = gcd_i64_i64(s, a2);
                if (g > 1) {
                    int64_t step = FLOW_CHECKED_DIV((s), (g));
                    int64_t max_k = FLOW_CHECKED_DIV((((s - (2 * a)) - 2)), (step));
                    if (max_k > 0) {
                        int64_t base_prod = FLOW_CHECKED_DIV((a2), (g));
                        int64_t k = 1;
                        while (k <= max_k) {
                            int64_t d = (step * k);
                            int64_t w = ((s - (2 * a)) - d);
                            int64_t prod = (base_prod * k);
                            int64_t disc = ((w * w) - (4 * prod));
                            if (disc >= 0) {
                                int64_t r = isqrt_i64(disc);
                                if ((r * r) == disc) {
                                    if (((w - r) & 1) == 0) {
                                        int64_t b = FLOW_CHECKED_DIV(((w - r)), (2));
                                        if (b > 0) {
                                            int64_t c = FLOW_CHECKED_DIV(((w + r)), (2));
                                            if (b <= c) {
                                                total = (total + (s - a));
                                            }
                                        }
                                    }
                                }
                            }
                            k = (k + 1);
                        }
                    }
                }
                a = (a + 1);
            }
        }
        s = (s + 1);
    }
    return total;
}

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