Problem 091

Right triangles with integer coordinates on 0..50 grid, right angle at any vertex.

Answer14234
Output14234
StatusPASS
Native helperno
Runtime0 ms
Peak memory1104 KB
Time complexityO(n^4) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 091
# Right triangles with integer coordinates on 0..50 grid, right angle at any vertex.

function main() -> i32 {
    let lim: i32 = 50
    let mut count: i64 = 0
    let mut x1: i32 = 0
    while x1 <= lim {
        let mut y1: i32 = 0
        while y1 <= lim {
            let mut x2: i32 = 0
            while x2 <= lim {
                let mut y2: i32 = 0
                while y2 <= lim {
                    if !(x1 == 0 && y1 == 0) && !(x2 == 0 && y2 == 0) && !(x1 == x2 && y1 == y2) {
                        # vectors OP1, OP2, P1P2
                        let a2: i32 = x1 * x1 + y1 * y1
                        let b2: i32 = x2 * x2 + y2 * y2
                        let c2: i32 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
                        if a2 + b2 == c2 || a2 + c2 == b2 || b2 + c2 == a2 {
                            count = count + 1
                        }
                    }
                    y2 = y2 + 1
                }
                x2 = x2 + 1
            }
            y1 = y1 + 1
        }
        x1 = x1 + 1
    }
    # each triangle counted twice (order of P1,P2)
    printf("%lld\n", count / 2)
    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; }

int32_t main(void);

int32_t main(void) {
    int32_t lim = 50;
    int64_t count = 0;
    int32_t x1 = 0;
    while (x1 <= lim) {
        int32_t y1 = 0;
        while (y1 <= lim) {
            int32_t x2 = 0;
            while (x2 <= lim) {
                int32_t y2 = 0;
                while (y2 <= lim) {
                    if ((((!((x1 == 0 && y1 == 0))) && (!((x2 == 0 && y2 == 0)))) && (!((x1 == x2 && y1 == y2))))) {
                        int32_t a2 = ((x1 * x1) + (y1 * y1));
                        int32_t b2 = ((x2 * x2) + (y2 * y2));
                        int32_t c2 = (((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
                        if ((((a2 + b2) == c2 || (a2 + c2) == b2) || (b2 + c2) == a2)) {
                            count = (count + 1);
                        }
                    }
                    y2 = (y2 + 1);
                }
                x2 = (x2 + 1);
            }
            y1 = (y1 + 1);
        }
        x1 = (x1 + 1);
    }
    printf("%lld\n", FLOW_CHECKED_DIV((count), (2)));
    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 @main() -> i32 {
    %0 = arith.constant 50 : i32
    %1 = arith.constant 0 : i32
    %2 = arith.extsi %1 : i32 to i64
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
    llvm.store %2, %4 : i64, !llvm.ptr
    %5 = arith.constant 0 : i32
    %6 = llvm.mlir.constant(1 : i64) : i64
    %7 = llvm.alloca %6 x i32 : (i64) -> !llvm.ptr
    llvm.store %5, %7 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %8 = llvm.load %7 : !llvm.ptr -> i32
    %9 = arith.cmpi sle, %8, %0 : i32
    cf.cond_br %9, ^bb1, ^bb2
    ^bb1:
      %10 = arith.constant 0 : i32
      %11 = llvm.mlir.constant(1 : i64) : i64
      %12 = llvm.alloca %11 x i32 : (i64) -> !llvm.ptr
      llvm.store %10, %12 : i32, !llvm.ptr
      cf.br ^bb3
      ^bb3:
      %13 = llvm.load %12 : !llvm.ptr -> i32
      %14 = arith.cmpi sle, %13, %0 : i32
      cf.cond_br %14, ^bb4, ^bb5
      ^bb4:
        %15 = arith.constant 0 : i32
        %16 = llvm.mlir.constant(1 : i64) : i64
        %17 = llvm.alloca %16 x i32 : (i64) -> !llvm.ptr
        llvm.store %15, %17 : i32, !llvm.ptr
        cf.br ^bb6
        ^bb6:
        %18 = llvm.load %17 : !llvm.ptr -> i32
        %19 = arith.cmpi sle, %18, %0 : i32
        cf.cond_br %19, ^bb7, ^bb8
        ^bb7:
          %20 = arith.constant 0 : i32
          %21 = llvm.mlir.constant(1 : i64) : i64
          %22 = llvm.alloca %21 x i32 : (i64) -> !llvm.ptr
          llvm.store %20, %22 : i32, !llvm.ptr
          cf.br ^bb9
          ^bb9:
          %23 = llvm.load %22 : !llvm.ptr -> i32
          %24 = arith.cmpi sle, %23, %0 : i32
          cf.cond_br %24, ^bb10, ^bb11
          ^bb10:
            %25 = llvm.load %7 : !llvm.ptr -> i32
            %26 = arith.constant 0 : i32
            %27 = arith.cmpi eq, %25, %26 : i32
            %28 = scf.if %27 -> (i1) {
              %29 = llvm.load %12 : !llvm.ptr -> i32
              %30 = arith.constant 0 : i32
              %31 = arith.cmpi eq, %29, %30 : i32
              scf.yield %31 : i1
            } else {
              %32 = arith.constant false
              scf.yield %32 : i1
            }
            %34 = arith.constant 1 : i1
            %33 = arith.xori %28, %34 : i1
            %36 = scf.if %33 -> (i1) {
              %37 = llvm.load %17 : !llvm.ptr -> i32
              %38 = arith.constant 0 : i32
              %39 = arith.cmpi eq, %37, %38 : i32
              %40 = scf.if %39 -> (i1) {
                %41 = llvm.load %22 : !llvm.ptr -> i32
                %42 = arith.constant 0 : i32
                %43 = arith.cmpi eq, %41, %42 : i32
                scf.yield %43 : i1
              } else {
                %44 = arith.constant false
                scf.yield %44 : i1
              }
              %46 = arith.constant 1 : i1
              %45 = arith.xori %40, %46 : i1
              scf.yield %45 : i1
            } else {
              %48 = arith.constant false
              scf.yield %48 : i1
            }
            %49 = scf.if %36 -> (i1) {
              %50 = llvm.load %7 : !llvm.ptr -> i32
              %51 = llvm.load %17 : !llvm.ptr -> i32
              %52 = arith.cmpi eq, %50, %51 : i32
              %53 = scf.if %52 -> (i1) {
                %54 = llvm.load %12 : !llvm.ptr -> i32
                %55 = llvm.load %22 : !llvm.ptr -> i32
                %56 = arith.cmpi eq, %54, %55 : i32
                scf.yield %56 : i1
              } else {
                %57 = arith.constant false
                scf.yield %57 : i1
              }
              %59 = arith.constant 1 : i1
              %58 = arith.xori %53, %59 : i1
              scf.yield %58 : i1
            } else {
              %61 = arith.constant false
              scf.yield %61 : i1
            }
            cf.cond_br %49, ^bb12, ^bb13
            ^bb12:
              %62 = llvm.load %7 : !llvm.ptr -> i32
              %63 = llvm.load %7 : !llvm.ptr -> i32
              %64 = arith.muli %62, %63 : i32
              %65 = llvm.load %12 : !llvm.ptr -> i32
              %66 = llvm.load %12 : !llvm.ptr -> i32
              %67 = arith.muli %65, %66 : i32
              %68 = arith.addi %64, %67 : i32
              %69 = llvm.load %17 : !llvm.ptr -> i32
              %70 = llvm.load %17 : !llvm.ptr -> i32
              %71 = arith.muli %69, %70 : i32
              %72 = llvm.load %22 : !llvm.ptr -> i32
              %73 = llvm.load %22 : !llvm.ptr -> i32
              %74 = arith.muli %72, %73 : i32
              %75 = arith.addi %71, %74 : i32
              %76 = llvm.load %7 : !llvm.ptr -> i32
              %77 = llvm.load %17 : !llvm.ptr -> i32
              %78 = arith.subi %76, %77 : i32
              %79 = llvm.load %7 : !llvm.ptr -> i32
              %80 = llvm.load %17 : !llvm.ptr -> i32
              %81 = arith.subi %79, %80 : i32
              %82 = arith.muli %78, %81 : i32
              %83 = llvm.load %12 : !llvm.ptr -> i32
              %84 = llvm.load %22 : !llvm.ptr -> i32
              %85 = arith.subi %83, %84 : i32
              %86 = llvm.load %12 : !llvm.ptr -> i32
              %87 = llvm.load %22 : !llvm.ptr -> i32
              %88 = arith.subi %86, %87 : i32
              %89 = arith.muli %85, %88 : i32
              %90 = arith.addi %82, %89 : i32
              %91 = arith.addi %68, %75 : i32
              %92 = arith.cmpi eq, %91, %90 : i32
              %93 = scf.if %92 -> (i1) {
                %94 = arith.constant true
                scf.yield %94 : i1
              } else {
                %95 = arith.addi %68, %90 : i32
                %96 = arith.cmpi eq, %95, %75 : i32
                scf.yield %96 : i1
              }
              %97 = scf.if %93 -> (i1) {
                %98 = arith.constant true
                scf.yield %98 : i1
              } else {
                %99 = arith.addi %75, %90 : i32
                %100 = arith.cmpi eq, %99, %68 : i32
                scf.yield %100 : i1
              }
              cf.cond_br %97, ^bb15, ^bb16
              ^bb15:
                %101 = llvm.load %4 : !llvm.ptr -> i64
                %102 = arith.constant 1 : i32
                %104 = arith.extsi %102 : i32 to i64
                %103 = arith.addi %101, %104 : i64
                llvm.store %103, %4 : i64, !llvm.ptr
                cf.br ^bb17
              ^bb16:
                cf.br ^bb17
              ^bb17:
              cf.br ^bb14
            ^bb13:
              cf.br ^bb14
            ^bb14:
            %105 = llvm.load %22 : !llvm.ptr -> i32
            %106 = arith.constant 1 : i32
            %107 = arith.addi %105, %106 : i32
            llvm.store %107, %22 : i32, !llvm.ptr
            cf.br ^bb9
          ^bb11:
          %108 = llvm.load %17 : !llvm.ptr -> i32
          %109 = arith.constant 1 : i32
          %110 = arith.addi %108, %109 : i32
          llvm.store %110, %17 : i32, !llvm.ptr
          cf.br ^bb6
        ^bb8:
        %111 = llvm.load %12 : !llvm.ptr -> i32
        %112 = arith.constant 1 : i32
        %113 = arith.addi %111, %112 : i32
        llvm.store %113, %12 : i32, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %114 = llvm.load %7 : !llvm.ptr -> i32
      %115 = arith.constant 1 : i32
      %116 = arith.addi %114, %115 : i32
      llvm.store %116, %7 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %117 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %118 = llvm.load %4 : !llvm.ptr -> i64
    %119 = arith.constant 2 : i32
    %121 = arith.extsi %119 : i32 to i64
    %120 = arith.divsi %118, %121 : i64
    %122 = llvm.call @printf(%117, %120) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %123 = arith.constant 0 : i32
    func.return %123 : i32
  }
}