Problem 174

Count tile totals with 1..10 lamina types, using <= 10^6 tiles.

Answer209566
Output209566
StatusPASS
Native helperno
Runtime0 ms
Peak memory5024 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n)
Space complexityO(n)O(1)
ApproachFlow solutionEnumerative counting
VerdictSuboptimal

Flow source

# Project Euler 174
# Count tile totals with 1..10 lamina types, using <= 10^6 tiles.

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

function main() -> i32 {
    let limit: i64 = 1000000
    let typec: ptr<i32> = calloc(limit + 1, 4)
    if typec == null { return 1 }

    let mut n: i64 = 3
    while n <= limit / 4 + 1 {
        let mut m: i64 = n - 2
        while m > 0 {
            let tiles: i64 = n * n - m * m
            if tiles > limit { break }
            typec[tiles] = typec[tiles] + 1
            m = m - 2
        }
        n = n + 1
    }

    let mut ans: i64 = 0
    let mut t: i64 = 1
    while t <= limit {
        if typec[t] >= 1 && typec[t] <= 10 {
            ans = ans + 1
        }
        t = t + 1
    }
    printf("%lld\n", ans)
    free(typec)
    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 = 1000000;
    int32_t* typec = (int32_t*)(calloc((limit + 1), 4));
    if (typec == NULL) {
        return 1;
    }
    int64_t n = 3;
    while (n <= (FLOW_CHECKED_DIV((limit), (4)) + 1)) {
        int64_t m = (n - 2);
        while (m > 0) {
            int64_t tiles = ((n * n) - (m * m));
            if (tiles > limit) {
                break;
            }
            typec[tiles] = (typec[tiles] + 1);
            m = (m - 2);
        }
        n = (n + 1);
    }
    int64_t ans = 0;
    int64_t t = 1;
    while (t <= limit) {
        if ((typec[t] >= 1 && typec[t] <= 10)) {
            ans = (ans + 1);
        }
        t = (t + 1);
    }
    printf("%lld\n", ans);
    free(typec);
    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 1000000 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 1 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.addi %1, %5 : i64
    %6 = arith.constant 4 : 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 3 : 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 4 : i32
    %18 = arith.extsi %16 : i32 to i64
    %17 = arith.divsi %1, %18 : i64
    %19 = arith.constant 1 : i32
    %21 = arith.extsi %19 : i32 to i64
    %20 = arith.addi %17, %21 : i64
    %22 = arith.cmpi sle, %15, %20 : i64
    cf.cond_br %22, ^bb4, ^bb5
    ^bb4:
      %23 = llvm.load %14 : !llvm.ptr -> i64
      %24 = arith.constant 2 : i32
      %26 = arith.extsi %24 : i32 to i64
      %25 = arith.subi %23, %26 : i64
      %27 = llvm.mlir.constant(1 : i64) : i64
      %28 = llvm.alloca %27 x i64 : (i64) -> !llvm.ptr
      llvm.store %25, %28 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %29 = llvm.load %28 : !llvm.ptr -> i64
      %30 = arith.constant 0 : i32
      %32 = arith.extsi %30 : i32 to i64
      %31 = arith.cmpi sgt, %29, %32 : i64
      cf.cond_br %31, ^bb7, ^bb8
      ^bb7:
        %33 = llvm.load %14 : !llvm.ptr -> i64
        %34 = llvm.load %14 : !llvm.ptr -> i64
        %35 = arith.muli %33, %34 : i64
        %36 = llvm.load %28 : !llvm.ptr -> i64
        %37 = llvm.load %28 : !llvm.ptr -> i64
        %38 = arith.muli %36, %37 : i64
        %39 = arith.subi %35, %38 : i64
        %40 = arith.cmpi sgt, %39, %1 : i64
        cf.cond_br %40, ^bb9, ^bb10
        ^bb9:
          cf.br ^bb8
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %42 = llvm.getelementptr %2[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %41 = llvm.load %42 : !llvm.ptr -> i32
        %43 = arith.constant 1 : i32
        %44 = arith.addi %41, %43 : i32
        %45 = llvm.getelementptr %2[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %44, %45 : i32, !llvm.ptr
        %46 = llvm.load %28 : !llvm.ptr -> i64
        %47 = arith.constant 2 : i32
        %49 = arith.extsi %47 : i32 to i64
        %48 = arith.subi %46, %49 : i64
        llvm.store %48, %28 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %50 = llvm.load %14 : !llvm.ptr -> i64
      %51 = arith.constant 1 : i32
      %53 = arith.extsi %51 : i32 to i64
      %52 = arith.addi %50, %53 : i64
      llvm.store %52, %14 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %54 = arith.constant 0 : i32
    %55 = arith.extsi %54 : i32 to i64
    %56 = llvm.mlir.constant(1 : i64) : i64
    %57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
    llvm.store %55, %57 : i64, !llvm.ptr
    %58 = arith.constant 1 : i32
    %59 = arith.extsi %58 : i32 to i64
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %62 = llvm.load %61 : !llvm.ptr -> i64
    %63 = arith.cmpi sle, %62, %1 : i64
    cf.cond_br %63, ^bb13, ^bb14
    ^bb13:
      %65 = llvm.load %61 : !llvm.ptr -> i64
      %66 = llvm.getelementptr %2[%65] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %64 = llvm.load %66 : !llvm.ptr -> i32
      %67 = arith.constant 1 : i32
      %68 = arith.cmpi sge, %64, %67 : i32
      %69 = scf.if %68 -> (i1) {
        %71 = llvm.load %61 : !llvm.ptr -> i64
        %72 = llvm.getelementptr %2[%71] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %70 = llvm.load %72 : !llvm.ptr -> i32
        %73 = arith.constant 10 : i32
        %74 = arith.cmpi sle, %70, %73 : i32
        scf.yield %74 : i1
      } else {
        %75 = arith.constant false
        scf.yield %75 : i1
      }
      cf.cond_br %69, ^bb15, ^bb16
      ^bb15:
        %76 = llvm.load %57 : !llvm.ptr -> i64
        %77 = arith.constant 1 : i32
        %79 = arith.extsi %77 : i32 to i64
        %78 = arith.addi %76, %79 : i64
        llvm.store %78, %57 : i64, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %80 = llvm.load %61 : !llvm.ptr -> i64
      %81 = arith.constant 1 : i32
      %83 = arith.extsi %81 : i32 to i64
      %82 = arith.addi %80, %83 : i64
      llvm.store %82, %61 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %84 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %85 = llvm.load %57 : !llvm.ptr -> i64
    %86 = llvm.call @printf(%84, %85) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    %88 = arith.constant 0 : i32
    func.return %88 : i32
  }
}