Problem 126

Least n such that C(n) = 1000 cuboid layer counts.

Answer18522
Output18522
StatusPASS
Native helperno
Runtime0 ms
Peak memory1264 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n)
Space complexityO(n)O(n)
ApproachFlow solutionEnumerate and count
VerdictSuboptimal

Flow source

# Project Euler 126
# Least n such that C(n) = 1000 cuboid layer counts.

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

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

    let mut a: i64 = 1
    while 2 * (3 * a) < limit {
        let mut b: i64 = 1
        while b <= a && 2 * (a * b + a + b) < limit {
            let mut c: i64 = 1
            while c <= b {
                let mut s: i64 = 2 * (a * b + b * c + c * a)
                if s >= limit { break }
                let mut inc: i64 = 4 * (a + b + c)
                while s < limit {
                    cnt[s] = cnt[s] + 1
                    s = s + inc
                    inc = inc + 8
                }
                c = c + 1
            }
            b = b + 1
        }
        a = a + 1
    }

    let mut n: i64 = 1
    while n < limit {
        if cnt[n] == 1000 {
            printf("%lld\n", n)
            free(cnt)
            return 0
        }
        n = n + 1
    }
    free(cnt)
    return 1
}

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 = 30000;
    int32_t* cnt = (int32_t*)(calloc(limit, 4));
    if (cnt == NULL) {
        return 1;
    }
    int64_t a = 1;
    while ((2 * (3 * a)) < limit) {
        int64_t b = 1;
        while ((b <= a && (2 * (((a * b) + a) + b)) < limit)) {
            int64_t c = 1;
            while (c <= b) {
                int64_t s = (2 * (((a * b) + (b * c)) + (c * a)));
                if (s >= limit) {
                    break;
                }
                int64_t inc = (4 * ((a + b) + c));
                while (s < limit) {
                    cnt[s] = (cnt[s] + 1);
                    s = (s + inc);
                    inc = (inc + 8);
                }
                c = (c + 1);
            }
            b = (b + 1);
        }
        a = (a + 1);
    }
    int64_t n = 1;
    while (n < limit) {
        if (cnt[n] == 1000) {
            printf("%lld\n", n);
            free(cnt);
            return 0;
        }
        n = (n + 1);
    }
    free(cnt);
    return 1;
}

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 30000 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 4 : i32
    %4 = arith.extsi %3 : i32 to i64
    %2 = func.call @calloc(%1, %4) : (i64, i64) -> !llvm.ptr
    %5 = llvm.mlir.zero : !llvm.ptr
    %6 = llvm.icmp "eq" %2, %5 : !llvm.ptr
    cf.cond_br %6, ^bb0, ^bb1
    ^bb0:
      %7 = arith.constant 1 : i32
      func.return %7 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %8 = arith.constant 1 : i32
    %9 = arith.extsi %8 : i32 to i64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %12 = arith.constant 2 : i32
    %13 = arith.constant 3 : i32
    %14 = llvm.load %11 : !llvm.ptr -> i64
    %16 = arith.extsi %13 : i32 to i64
    %15 = arith.muli %16, %14 : i64
    %18 = arith.extsi %12 : i32 to i64
    %17 = arith.muli %18, %15 : i64
    %19 = arith.cmpi slt, %17, %1 : i64
    cf.cond_br %19, ^bb4, ^bb5
    ^bb4:
      %20 = arith.constant 1 : i32
      %21 = arith.extsi %20 : i32 to i64
      %22 = llvm.mlir.constant(1 : i64) : i64
      %23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
      llvm.store %21, %23 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %24 = llvm.load %23 : !llvm.ptr -> i64
      %25 = llvm.load %11 : !llvm.ptr -> i64
      %26 = arith.cmpi sle, %24, %25 : i64
      %27 = scf.if %26 -> (i1) {
        %28 = arith.constant 2 : i32
        %29 = llvm.load %11 : !llvm.ptr -> i64
        %30 = llvm.load %23 : !llvm.ptr -> i64
        %31 = arith.muli %29, %30 : i64
        %32 = llvm.load %11 : !llvm.ptr -> i64
        %33 = arith.addi %31, %32 : i64
        %34 = llvm.load %23 : !llvm.ptr -> i64
        %35 = arith.addi %33, %34 : i64
        %37 = arith.extsi %28 : i32 to i64
        %36 = arith.muli %37, %35 : i64
        %38 = arith.cmpi slt, %36, %1 : i64
        scf.yield %38 : i1
      } else {
        %39 = arith.constant false
        scf.yield %39 : i1
      }
      cf.cond_br %27, ^bb7, ^bb8
      ^bb7:
        %40 = arith.constant 1 : i32
        %41 = arith.extsi %40 : i32 to i64
        %42 = llvm.mlir.constant(1 : i64) : i64
        %43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
        llvm.store %41, %43 : i64, !llvm.ptr
        cf.br ^bb9
        ^bb9:
        %44 = llvm.load %43 : !llvm.ptr -> i64
        %45 = llvm.load %23 : !llvm.ptr -> i64
        %46 = arith.cmpi sle, %44, %45 : i64
        cf.cond_br %46, ^bb10, ^bb11
        ^bb10:
          %47 = arith.constant 2 : i32
          %48 = llvm.load %11 : !llvm.ptr -> i64
          %49 = llvm.load %23 : !llvm.ptr -> i64
          %50 = arith.muli %48, %49 : i64
          %51 = llvm.load %23 : !llvm.ptr -> i64
          %52 = llvm.load %43 : !llvm.ptr -> i64
          %53 = arith.muli %51, %52 : i64
          %54 = arith.addi %50, %53 : i64
          %55 = llvm.load %43 : !llvm.ptr -> i64
          %56 = llvm.load %11 : !llvm.ptr -> i64
          %57 = arith.muli %55, %56 : i64
          %58 = arith.addi %54, %57 : i64
          %60 = arith.extsi %47 : i32 to i64
          %59 = arith.muli %60, %58 : i64
          %61 = llvm.mlir.constant(1 : i64) : i64
          %62 = llvm.alloca %61 x i64 : (i64) -> !llvm.ptr
          llvm.store %59, %62 : i64, !llvm.ptr
          %63 = llvm.load %62 : !llvm.ptr -> i64
          %64 = arith.cmpi sge, %63, %1 : i64
          cf.cond_br %64, ^bb12, ^bb13
          ^bb12:
            cf.br ^bb11
          ^bb13:
            cf.br ^bb14
          ^bb14:
          %65 = arith.constant 4 : i32
          %66 = llvm.load %11 : !llvm.ptr -> i64
          %67 = llvm.load %23 : !llvm.ptr -> i64
          %68 = arith.addi %66, %67 : i64
          %69 = llvm.load %43 : !llvm.ptr -> i64
          %70 = arith.addi %68, %69 : i64
          %72 = arith.extsi %65 : i32 to i64
          %71 = arith.muli %72, %70 : i64
          %73 = llvm.mlir.constant(1 : i64) : i64
          %74 = llvm.alloca %73 x i64 : (i64) -> !llvm.ptr
          llvm.store %71, %74 : i64, !llvm.ptr
          cf.br ^bb15
          ^bb15:
          %75 = llvm.load %62 : !llvm.ptr -> i64
          %76 = arith.cmpi slt, %75, %1 : i64
          cf.cond_br %76, ^bb16, ^bb17
          ^bb16:
            %78 = llvm.load %62 : !llvm.ptr -> i64
            %79 = llvm.getelementptr %2[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %77 = llvm.load %79 : !llvm.ptr -> i32
            %80 = arith.constant 1 : i32
            %81 = arith.addi %77, %80 : i32
            %82 = llvm.load %62 : !llvm.ptr -> i64
            %83 = llvm.getelementptr %2[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            llvm.store %81, %83 : i32, !llvm.ptr
            %84 = llvm.load %62 : !llvm.ptr -> i64
            %85 = llvm.load %74 : !llvm.ptr -> i64
            %86 = arith.addi %84, %85 : i64
            llvm.store %86, %62 : i64, !llvm.ptr
            %87 = llvm.load %74 : !llvm.ptr -> i64
            %88 = arith.constant 8 : i32
            %90 = arith.extsi %88 : i32 to i64
            %89 = arith.addi %87, %90 : i64
            llvm.store %89, %74 : i64, !llvm.ptr
            cf.br ^bb15
          ^bb17:
          %91 = llvm.load %43 : !llvm.ptr -> i64
          %92 = arith.constant 1 : i32
          %94 = arith.extsi %92 : i32 to i64
          %93 = arith.addi %91, %94 : i64
          llvm.store %93, %43 : i64, !llvm.ptr
          cf.br ^bb9
        ^bb11:
        %95 = llvm.load %23 : !llvm.ptr -> i64
        %96 = arith.constant 1 : i32
        %98 = arith.extsi %96 : i32 to i64
        %97 = arith.addi %95, %98 : i64
        llvm.store %97, %23 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %99 = llvm.load %11 : !llvm.ptr -> i64
      %100 = arith.constant 1 : i32
      %102 = arith.extsi %100 : i32 to i64
      %101 = arith.addi %99, %102 : i64
      llvm.store %101, %11 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %103 = arith.constant 1 : i32
    %104 = arith.extsi %103 : i32 to i64
    %105 = llvm.mlir.constant(1 : i64) : i64
    %106 = llvm.alloca %105 x i64 : (i64) -> !llvm.ptr
    llvm.store %104, %106 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %107 = llvm.load %106 : !llvm.ptr -> i64
    %108 = arith.cmpi slt, %107, %1 : i64
    cf.cond_br %108, ^bb19, ^bb20
    ^bb19:
      %110 = llvm.load %106 : !llvm.ptr -> i64
      %111 = llvm.getelementptr %2[%110] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %109 = llvm.load %111 : !llvm.ptr -> i32
      %112 = arith.constant 1000 : i32
      %113 = arith.cmpi eq, %109, %112 : i32
      cf.cond_br %113, ^bb21, ^bb22
      ^bb21:
        %114 = llvm.mlir.addressof @str_0 : !llvm.ptr
        %115 = llvm.load %106 : !llvm.ptr -> i64
        %116 = llvm.call @printf(%114, %115) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
        func.call @free(%2) : (!llvm.ptr) -> ()
        %118 = arith.constant 0 : i32
        func.return %118 : i32
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %119 = llvm.load %106 : !llvm.ptr -> i64
      %120 = arith.constant 1 : i32
      %122 = arith.extsi %120 : i32 to i64
      %121 = arith.addi %119, %122 : i64
      llvm.store %121, %106 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    func.call @free(%2) : (!llvm.ptr) -> ()
    %124 = arith.constant 1 : i32
    func.return %124 : i32
  }
}