Problem 115

Least n such that fill_ways(n, m=50) > 1_000_000.

Answer168
Output168
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n * m)
Space complexityO(n)O(n)
ApproachFlow solutionDynamic programming or generating function
VerdictUnknown

Flow source

# Project Euler 115
# Least n such that fill_ways(n, m=50) > 1_000_000.

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

function ways(n: i32, m: i32, a: ptr<i64>) -> i64 {
    a[0] = 1
    let mut i: i32 = 1
    while i <= n {
        a[i] = a[i - 1]
        let mut k: i32 = m
        while k <= i {
            if k == i {
                a[i] = a[i] + 1
            } else {
                a[i] = a[i] + a[i - k - 1]
            }
            k = k + 1
        }
        i = i + 1
    }
    return a[n]
}

function main() -> i32 {
    let m: i32 = 50
    let max_n: i32 = 200
    let a: ptr<i64> = calloc((max_n + 1) as i64, 8)
    if a == null { return 1 }
    let mut n: i32 = m
    while n <= max_n {
        # recompute from scratch each time (small)
        let mut j: i32 = 0
        while j <= n {
            a[j] = 0
            j = j + 1
        }
        let w: i64 = ways(n, m, a)
        if w > 1000000 {
            printf("%lld\n", n as i64)
            free(a)
            return 0
        }
        n = n + 1
    }
    free(a)
    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; }

int64_t ways_i32_i32_ptr_i64(int32_t n, int32_t m, int64_t* a);
int32_t main(void);



int64_t ways_i32_i32_ptr_i64(int32_t n, int32_t m, int64_t* a) {
    a[0] = 1;
    int32_t i = 1;
    while (i <= n) {
        a[i] = a[(i - 1)];
        int32_t k = m;
        while (k <= i) {
            if (k == i) {
                a[i] = (a[i] + 1);
            } else {
                a[i] = (a[i] + a[((i - k) - 1)]);
            }
            k = (k + 1);
        }
        i = (i + 1);
    }
    return a[n];
}

int32_t main(void) {
    int32_t m = 50;
    int32_t max_n = 200;
    int64_t* a = (int64_t*)(calloc(((int64_t)((max_n + 1))), 8));
    if (a == NULL) {
        return 1;
    }
    int32_t n = m;
    while (n <= max_n) {
        int32_t j = 0;
        while (j <= n) {
            a[j] = 0;
            j = (j + 1);
        }
        int64_t w = ways_i32_i32_ptr_i64(n, m, a);
        if (w > 1000000) {
            printf("%lld\n", ((int64_t)(n)));
            free(a);
            return 0;
        }
        n = (n + 1);
    }
    free(a);
    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 @ways(%arg0: i32, %arg1: i32, %arg2: !llvm.ptr) -> i64 {
    %0 = arith.constant 1 : i32
    %1 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %3 = arith.extsi %1 : i32 to i64
    %4 = llvm.getelementptr %arg2[%3] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %2, %4 : i64, !llvm.ptr
    %5 = arith.constant 1 : 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, %arg0 : i32
    cf.cond_br %9, ^bb1, ^bb2
    ^bb1:
      %11 = llvm.load %7 : !llvm.ptr -> i32
      %12 = arith.constant 1 : i32
      %13 = arith.subi %11, %12 : i32
      %14 = arith.extsi %13 : i32 to i64
      %15 = llvm.getelementptr %arg2[%14] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %10 = llvm.load %15 : !llvm.ptr -> i64
      %16 = llvm.load %7 : !llvm.ptr -> i32
      %17 = arith.extsi %16 : i32 to i64
      %18 = llvm.getelementptr %arg2[%17] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %10, %18 : i64, !llvm.ptr
      %19 = llvm.mlir.constant(1 : i64) : i64
      %20 = llvm.alloca %19 x i32 : (i64) -> !llvm.ptr
      llvm.store %arg1, %20 : i32, !llvm.ptr
      cf.br ^bb3
      ^bb3:
      %21 = llvm.load %20 : !llvm.ptr -> i32
      %22 = llvm.load %7 : !llvm.ptr -> i32
      %23 = arith.cmpi sle, %21, %22 : i32
      cf.cond_br %23, ^bb4, ^bb5
      ^bb4:
        %24 = llvm.load %20 : !llvm.ptr -> i32
        %25 = llvm.load %7 : !llvm.ptr -> i32
        %26 = arith.cmpi eq, %24, %25 : i32
        cf.cond_br %26, ^bb6, ^bb7
        ^bb6:
          %28 = llvm.load %7 : !llvm.ptr -> i32
          %29 = arith.extsi %28 : i32 to i64
          %30 = llvm.getelementptr %arg2[%29] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %27 = llvm.load %30 : !llvm.ptr -> i64
          %31 = arith.constant 1 : i32
          %33 = arith.extsi %31 : i32 to i64
          %32 = arith.addi %27, %33 : i64
          %34 = llvm.load %7 : !llvm.ptr -> i32
          %35 = arith.extsi %34 : i32 to i64
          %36 = llvm.getelementptr %arg2[%35] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %32, %36 : i64, !llvm.ptr
          cf.br ^bb8
        ^bb7:
          %38 = llvm.load %7 : !llvm.ptr -> i32
          %39 = arith.extsi %38 : i32 to i64
          %40 = llvm.getelementptr %arg2[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %37 = llvm.load %40 : !llvm.ptr -> i64
          %42 = llvm.load %7 : !llvm.ptr -> i32
          %43 = llvm.load %20 : !llvm.ptr -> i32
          %44 = arith.subi %42, %43 : i32
          %45 = arith.constant 1 : i32
          %46 = arith.subi %44, %45 : i32
          %47 = arith.extsi %46 : i32 to i64
          %48 = llvm.getelementptr %arg2[%47] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %41 = llvm.load %48 : !llvm.ptr -> i64
          %49 = arith.addi %37, %41 : i64
          %50 = llvm.load %7 : !llvm.ptr -> i32
          %51 = arith.extsi %50 : i32 to i64
          %52 = llvm.getelementptr %arg2[%51] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %49, %52 : i64, !llvm.ptr
          cf.br ^bb8
        ^bb8:
        %53 = llvm.load %20 : !llvm.ptr -> i32
        %54 = arith.constant 1 : i32
        %55 = arith.addi %53, %54 : i32
        llvm.store %55, %20 : i32, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %56 = llvm.load %7 : !llvm.ptr -> i32
      %57 = arith.constant 1 : i32
      %58 = arith.addi %56, %57 : i32
      llvm.store %58, %7 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %60 = arith.extsi %arg0 : i32 to i64
    %61 = llvm.getelementptr %arg2[%60] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %59 = llvm.load %61 : !llvm.ptr -> i64
    func.return %59 : i64
  }
  func.func @main() -> i32 {
    %62 = arith.constant 50 : i32
    %63 = arith.constant 200 : i32
    %65 = arith.constant 1 : i32
    %66 = arith.addi %63, %65 : i32
    %67 = arith.extsi %66 : i32 to i64
    %68 = arith.constant 8 : i32
    %69 = arith.extsi %68 : i32 to i64
    %64 = func.call @calloc(%67, %69) : (i64, i64) -> !llvm.ptr
    %70 = llvm.mlir.zero : !llvm.ptr
    %71 = llvm.icmp "eq" %64, %70 : !llvm.ptr
    cf.cond_br %71, ^bb9, ^bb10
    ^bb9:
      %72 = arith.constant 1 : i32
      func.return %72 : i32
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %73 = llvm.mlir.constant(1 : i64) : i64
    %74 = llvm.alloca %73 x i32 : (i64) -> !llvm.ptr
    llvm.store %62, %74 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %75 = llvm.load %74 : !llvm.ptr -> i32
    %76 = arith.cmpi sle, %75, %63 : i32
    cf.cond_br %76, ^bb13, ^bb14
    ^bb13:
      %77 = arith.constant 0 : i32
      %78 = llvm.mlir.constant(1 : i64) : i64
      %79 = llvm.alloca %78 x i32 : (i64) -> !llvm.ptr
      llvm.store %77, %79 : i32, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %80 = llvm.load %79 : !llvm.ptr -> i32
      %81 = llvm.load %74 : !llvm.ptr -> i32
      %82 = arith.cmpi sle, %80, %81 : i32
      cf.cond_br %82, ^bb16, ^bb17
      ^bb16:
        %83 = arith.constant 0 : i32
        %84 = llvm.load %79 : !llvm.ptr -> i32
        %85 = arith.extsi %83 : i32 to i64
        %86 = arith.extsi %84 : i32 to i64
        %87 = llvm.getelementptr %64[%86] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %85, %87 : i64, !llvm.ptr
        %88 = llvm.load %79 : !llvm.ptr -> i32
        %89 = arith.constant 1 : i32
        %90 = arith.addi %88, %89 : i32
        llvm.store %90, %79 : i32, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %92 = llvm.load %74 : !llvm.ptr -> i32
      %91 = func.call @ways(%92, %62, %64) : (i32, i32, !llvm.ptr) -> i64
      %93 = arith.constant 1000000 : i32
      %95 = arith.extsi %93 : i32 to i64
      %94 = arith.cmpi sgt, %91, %95 : i64
      cf.cond_br %94, ^bb18, ^bb19
      ^bb18:
        %96 = llvm.mlir.addressof @str_0 : !llvm.ptr
        %97 = llvm.load %74 : !llvm.ptr -> i32
        %98 = arith.extsi %97 : i32 to i64
        %99 = llvm.call @printf(%96, %98) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
        func.call @free(%64) : (!llvm.ptr) -> ()
        %101 = arith.constant 0 : i32
        func.return %101 : i32
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %102 = llvm.load %74 : !llvm.ptr -> i32
      %103 = arith.constant 1 : i32
      %104 = arith.addi %102, %103 : i32
      llvm.store %104, %74 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    func.call @free(%64) : (!llvm.ptr) -> ()
    %106 = arith.constant 1 : i32
    func.return %106 : i32
  }
}