Problem 577

Counting Hexagons H(n) = sum_y y * T(n-2-3*(y-1)), sum H(3)..H(12345).

Answer265695031399260211
Output265695031399260211
StatusPASS
Native helperno
Runtime10 ms
Peak memory1216 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 577
# Counting Hexagons
# H(n) = sum_y y * T(n-2-3*(y-1)), sum H(3)..H(12345).

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function main() -> i32 {
    let limit: i64 = 12345
    let tri: ptr<i64> = calloc(limit + 10, 8)
    if tri == null { return 1 }
    let mut n: i64 = 1
    while n <= limit + 5 {
        tri[n] = n * (n + 1) / 2
        n = n + 1
    }
    let mut total: i64 = 0
    let mut x: i64 = 3
    while x <= limit {
        let mut h: i64 = 0
        let mut y: i64 = 1
        while 3 * y <= x {
            h = h + y * tri[x - 2 - 3 * (y - 1)]
            y = y + 1
        }
        total = total + h
        x = x + 1
    }
    printf("%lld\n", total)
    free(tri)
    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) {
    int64_t limit = 12345;
    int64_t* tri = (int64_t*)(calloc((limit + 10), 8));
    if (tri == NULL) {
        return 1;
    }
    int64_t n = 1;
    while (n <= (limit + 5)) {
        tri[n] = FLOW_CHECKED_DIV(((n * (n + 1))), (2));
        n = (n + 1);
    }
    int64_t total = 0;
    int64_t x = 3;
    while (x <= limit) {
        int64_t h = 0;
        int64_t y = 1;
        while ((3 * y) <= x) {
            h = (h + (y * tri[((x - 2) - (3 * (y - 1)))]));
            y = (y + 1);
        }
        total = (total + h);
        x = (x + 1);
    }
    printf("%lld\n", total);
    free(tri);
    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 @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @main() -> i32 {
    %0 = arith.constant 12345 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 10 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.addi %1, %5 : i64
    %6 = arith.constant 8 : i32
    %7 = arith.extsi %6 : i32 to i64
    %2 = func.call @calloc(%4, %7) : (i64, i64) -> !llvm.ptr
    %8 = llvm.mlir.zero : !llvm.ptr
    %9 = llvm.icmp "eq" %2, %8 : !llvm.ptr
    cf.cond_br %9, ^bb0, ^bb1
    ^bb0:
      %10 = arith.constant 1 : i32
      func.return %10 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %11 = arith.constant 1 : i32
    %12 = arith.extsi %11 : i32 to i64
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %14 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %14 : !llvm.ptr -> i64
    %16 = arith.constant 5 : i32
    %18 = arith.extsi %16 : i32 to i64
    %17 = arith.addi %1, %18 : i64
    %19 = arith.cmpi sle, %15, %17 : i64
    cf.cond_br %19, ^bb4, ^bb5
    ^bb4:
      %20 = llvm.load %14 : !llvm.ptr -> i64
      %21 = llvm.load %14 : !llvm.ptr -> i64
      %22 = arith.constant 1 : i32
      %24 = arith.extsi %22 : i32 to i64
      %23 = arith.addi %21, %24 : i64
      %25 = arith.muli %20, %23 : i64
      %26 = arith.constant 2 : i32
      %28 = arith.extsi %26 : i32 to i64
      %27 = arith.divsi %25, %28 : i64
      %29 = llvm.load %14 : !llvm.ptr -> i64
      %30 = llvm.getelementptr %2[%29] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %27, %30 : i64, !llvm.ptr
      %31 = llvm.load %14 : !llvm.ptr -> i64
      %32 = arith.constant 1 : i32
      %34 = arith.extsi %32 : i32 to i64
      %33 = arith.addi %31, %34 : i64
      llvm.store %33, %14 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %35 = arith.constant 0 : i32
    %36 = arith.extsi %35 : i32 to i64
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : i64, !llvm.ptr
    %39 = arith.constant 3 : i32
    %40 = arith.extsi %39 : i32 to i64
    %41 = llvm.mlir.constant(1 : i64) : i64
    %42 = llvm.alloca %41 x i64 : (i64) -> !llvm.ptr
    llvm.store %40, %42 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %43 = llvm.load %42 : !llvm.ptr -> i64
    %44 = arith.cmpi sle, %43, %1 : i64
    cf.cond_br %44, ^bb7, ^bb8
    ^bb7:
      %45 = arith.constant 0 : i32
      %46 = arith.extsi %45 : i32 to i64
      %47 = llvm.mlir.constant(1 : i64) : i64
      %48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
      llvm.store %46, %48 : i64, !llvm.ptr
      %49 = arith.constant 1 : i32
      %50 = arith.extsi %49 : i32 to i64
      %51 = llvm.mlir.constant(1 : i64) : i64
      %52 = llvm.alloca %51 x i64 : (i64) -> !llvm.ptr
      llvm.store %50, %52 : i64, !llvm.ptr
      cf.br ^bb9
      ^bb9:
      %53 = arith.constant 3 : i32
      %54 = llvm.load %52 : !llvm.ptr -> i64
      %56 = arith.extsi %53 : i32 to i64
      %55 = arith.muli %56, %54 : i64
      %57 = llvm.load %42 : !llvm.ptr -> i64
      %58 = arith.cmpi sle, %55, %57 : i64
      cf.cond_br %58, ^bb10, ^bb11
      ^bb10:
        %59 = llvm.load %48 : !llvm.ptr -> i64
        %60 = llvm.load %52 : !llvm.ptr -> i64
        %62 = llvm.load %42 : !llvm.ptr -> i64
        %63 = arith.constant 2 : i32
        %65 = arith.extsi %63 : i32 to i64
        %64 = arith.subi %62, %65 : i64
        %66 = arith.constant 3 : i32
        %67 = llvm.load %52 : !llvm.ptr -> i64
        %68 = arith.constant 1 : i32
        %70 = arith.extsi %68 : i32 to i64
        %69 = arith.subi %67, %70 : i64
        %72 = arith.extsi %66 : i32 to i64
        %71 = arith.muli %72, %69 : i64
        %73 = arith.subi %64, %71 : i64
        %74 = llvm.getelementptr %2[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %61 = llvm.load %74 : !llvm.ptr -> i64
        %75 = arith.muli %60, %61 : i64
        %76 = arith.addi %59, %75 : i64
        llvm.store %76, %48 : i64, !llvm.ptr
        %77 = llvm.load %52 : !llvm.ptr -> i64
        %78 = arith.constant 1 : i32
        %80 = arith.extsi %78 : i32 to i64
        %79 = arith.addi %77, %80 : i64
        llvm.store %79, %52 : i64, !llvm.ptr
        cf.br ^bb9
      ^bb11:
      %81 = llvm.load %38 : !llvm.ptr -> i64
      %82 = llvm.load %48 : !llvm.ptr -> i64
      %83 = arith.addi %81, %82 : i64
      llvm.store %83, %38 : i64, !llvm.ptr
      %84 = llvm.load %42 : !llvm.ptr -> i64
      %85 = arith.constant 1 : i32
      %87 = arith.extsi %85 : i32 to i64
      %86 = arith.addi %84, %87 : i64
      llvm.store %86, %42 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %88 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %89 = llvm.load %38 : !llvm.ptr -> i64
    %90 = llvm.call @printf(%88, %89) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    %92 = arith.constant 0 : i32
    func.return %92 : i32
  }
}