Problem 009

Special Pythagorean triplet: a + b + c = 1000, a² + b² = c². Return the product a·b·c.

Answer31875000
Output31875000
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n)
Space complexityO(1)O(1)
ApproachFlow solutionEuclid parametrisation of Pythagorean triples
VerdictSuboptimal

Flow source

# Project Euler 009
# Special Pythagorean triplet: a + b + c = 1000, a² + b² = c².
# Return the product a·b·c.

function solve(perimeter: i64) -> i64 {
    for a in 1..(perimeter / 3) {
        for b in (a + 1)..(perimeter / 2) {
            let c: i64 = perimeter - a - b
            if b >= c {
                break
            }
            if a * a + b * b == c * c {
                return a * b * c
            }
        }
    }
    return 0
}

function main() -> i32 {
    printf("%lld\n", solve(1000))
    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 solve_i64(int64_t perimeter);
int32_t main(void);

int64_t solve_i64(int64_t perimeter) {
    int32_t __flow_step_1 = 1;
    for (int32_t a = 1; (1 <= FLOW_CHECKED_DIV((perimeter), (3))) ? a < FLOW_CHECKED_DIV((perimeter), (3)) : a > FLOW_CHECKED_DIV((perimeter), (3)); a += (1 <= FLOW_CHECKED_DIV((perimeter), (3))) ? 1 : -1) {
        int32_t __flow_step_2 = 1;
        for (int32_t b = (a + 1); ((a + 1) <= FLOW_CHECKED_DIV((perimeter), (2))) ? b < FLOW_CHECKED_DIV((perimeter), (2)) : b > FLOW_CHECKED_DIV((perimeter), (2)); b += ((a + 1) <= FLOW_CHECKED_DIV((perimeter), (2))) ? 1 : -1) {
            int64_t c = ((perimeter - a) - b);
            if (b >= c) {
                break;
            }
            if (((a * a) + (b * b)) == (c * c)) {
                return ((a * b) * c);
            }
        }
    }
    return 0;
}

int32_t main(void) {
    printf("%lld\n", solve_i64(1000));
    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 @solve(%arg0: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %1 = arith.constant 3 : i32
    %3 = arith.extsi %1 : i32 to i64
    %2 = arith.divsi %arg0, %3 : i64
    %4 = arith.index_cast %0 : i32 to index
    %5 = arith.index_cast %2 : i32 to index
    %7 = arith.constant 1 : index
    %8 = arith.constant -1 : index
    %9 = arith.cmpi sle, %4, %5 : index
    %6 = arith.select %9, %7, %8 : index
    cf.br ^bb0(%4 : index)
    ^bb0(%10: index):
    %11 = arith.cmpi slt, %10, %5 : index
    %12 = arith.cmpi sgt, %10, %5 : index
    %13 = arith.select %9, %11, %12 : i1
    cf.cond_br %13, ^bb1(%10 : index), ^bb2(%10 : index)
    ^bb1(%14: index):
      %15 = arith.constant 1 : i32
      %17 = arith.index_cast %14 : index to i32
      %16 = arith.addi %17, %15 : i32
      %18 = arith.constant 2 : i32
      %20 = arith.extsi %18 : i32 to i64
      %19 = arith.divsi %arg0, %20 : i64
      %21 = arith.index_cast %16 : i32 to index
      %22 = arith.index_cast %19 : i32 to index
      %24 = arith.constant 1 : index
      %25 = arith.constant -1 : index
      %26 = arith.cmpi sle, %21, %22 : index
      %23 = arith.select %26, %24, %25 : index
      cf.br ^bb3(%21 : index)
      ^bb3(%27: index):
      %28 = arith.cmpi slt, %27, %22 : index
      %29 = arith.cmpi sgt, %27, %22 : index
      %30 = arith.select %26, %28, %29 : i1
      cf.cond_br %30, ^bb4(%27 : index), ^bb5(%27 : index)
      ^bb4(%31: index):
        %33 = arith.trunci %arg0 : i64 to i32
        %34 = arith.index_cast %14 : index to i32
        %32 = arith.subi %33, %34 : i32
        %36 = arith.index_cast %31 : index to i32
        %35 = arith.subi %32, %36 : i32
        %37 = arith.extsi %35 : i32 to i64
        %39 = arith.index_cast %31 : index to i32
        %40 = arith.trunci %37 : i64 to i32
        %38 = arith.cmpi sge, %39, %40 : i32
        cf.cond_br %38, ^bb6, ^bb7
        ^bb6:
          cf.br ^bb5(%31 : index)
        ^bb7:
          cf.br ^bb8
        ^bb8:
        %41 = arith.muli %14, %14 : index
        %42 = arith.muli %31, %31 : index
        %43 = arith.addi %41, %42 : index
        %44 = arith.muli %37, %37 : i64
        %46 = arith.index_cast %43 : index to i32
        %47 = arith.trunci %44 : i64 to i32
        %45 = arith.cmpi eq, %46, %47 : i32
        cf.cond_br %45, ^bb9, ^bb10
        ^bb9:
          %48 = arith.muli %14, %31 : index
          %50 = arith.index_cast %48 : index to i32
          %51 = arith.trunci %37 : i64 to i32
          %49 = arith.muli %50, %51 : i32
          %52 = arith.extsi %49 : i32 to i64
          func.return %52 : i64
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %53 = arith.addi %31, %23 : index
        cf.br ^bb3(%53 : index)
      ^bb5(%54: index):
      %55 = arith.addi %14, %6 : index
      cf.br ^bb0(%55 : index)
    ^bb2(%56: index):
    %57 = arith.constant 0 : i32
    %58 = arith.extsi %57 : i32 to i64
    func.return %58 : i64
  }
  func.func @main() -> i32 {
    %59 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %61 = arith.constant 1000 : i32
    %62 = arith.extsi %61 : i32 to i64
    %60 = func.call @solve(%62) : (i64) -> i64
    %63 = llvm.call @printf(%59, %60) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %64 = arith.constant 0 : i32
    func.return %64 : i32
  }
}