Problem 031

Number of ways to make £2 with standard UK coins.

Answer73682
Output73682
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 solutionCoin change DP
VerdictUnknown

Flow source

# Project Euler 031
# Number of ways to make £2 with standard UK coins.

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

function main() -> i32 {
    let coins: array<i32, 8> = [1, 2, 5, 10, 20, 50, 100, 200]
    let target: i32 = 200
    let ways: ptr<i64> = calloc((target + 1) as i64, 8)
    if ways == null { return 1 }
    ways[0] = 1
    for ci in 0..8 {
        let c: i32 = coins[ci]
        for x in c..(target + 1) {
            ways[x] = ways[x] + ways[x - c]
        }
    }
    printf("%lld\n", ways[target])
    free(ways)
    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) {
    int32_t coins[8] = { 1, 2, 5, 10, 20, 50, 100, 200 };
    int32_t target = 200;
    int64_t* ways = (int64_t*)(calloc(((int64_t)((target + 1))), 8));
    if (ways == NULL) {
        return 1;
    }
    ways[0] = 1;
    int32_t __flow_step_1 = 1;
    for (int32_t ci = 0; (0 <= 8) ? ci < 8 : ci > 8; ci += (0 <= 8) ? 1 : -1) {
        int32_t c = (((unsigned)(ci) < 8) ? coins[ci] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(ci), 8), flow_fault_handler("array index out of bounds"), coins[0]));
        int32_t __flow_step_2 = 1;
        for (int32_t x = c; (c <= (target + 1)) ? x < (target + 1) : x > (target + 1); x += (c <= (target + 1)) ? 1 : -1) {
            ways[x] = (ways[x] + ways[(x - c)]);
        }
    }
    printf("%lld\n", ways[target]);
    free(ways);
    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 {
    %1 = arith.constant 1 : i32
    %2 = arith.constant 2 : i32
    %3 = arith.constant 5 : i32
    %4 = arith.constant 10 : i32
    %5 = arith.constant 20 : i32
    %6 = arith.constant 50 : i32
    %7 = arith.constant 100 : i32
    %8 = arith.constant 200 : i32
    %9 = llvm.mlir.constant(1 : i64) : i64
    %10 = llvm.alloca %9 x !llvm.array<8 x i32> : (i64) -> !llvm.ptr
    %11 = llvm.mlir.zero : !llvm.array<8 x i32>
    llvm.store %11, %10 : !llvm.array<8 x i32>, !llvm.ptr
    %12 = llvm.mlir.constant(0 : i64) : i64
    %13 = llvm.getelementptr %10[0, %12] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
    llvm.store %1, %13 : i32, !llvm.ptr
    %14 = llvm.mlir.constant(1 : i64) : i64
    %15 = llvm.getelementptr %10[0, %14] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
    llvm.store %2, %15 : i32, !llvm.ptr
    %16 = llvm.mlir.constant(2 : i64) : i64
    %17 = llvm.getelementptr %10[0, %16] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
    llvm.store %3, %17 : i32, !llvm.ptr
    %18 = llvm.mlir.constant(3 : i64) : i64
    %19 = llvm.getelementptr %10[0, %18] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
    llvm.store %4, %19 : i32, !llvm.ptr
    %20 = llvm.mlir.constant(4 : i64) : i64
    %21 = llvm.getelementptr %10[0, %20] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
    llvm.store %5, %21 : i32, !llvm.ptr
    %22 = llvm.mlir.constant(5 : i64) : i64
    %23 = llvm.getelementptr %10[0, %22] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
    llvm.store %6, %23 : i32, !llvm.ptr
    %24 = llvm.mlir.constant(6 : i64) : i64
    %25 = llvm.getelementptr %10[0, %24] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
    llvm.store %7, %25 : i32, !llvm.ptr
    %26 = llvm.mlir.constant(7 : i64) : i64
    %27 = llvm.getelementptr %10[0, %26] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
    llvm.store %8, %27 : i32, !llvm.ptr
    %28 = arith.constant 200 : i32
    %30 = arith.constant 1 : i32
    %31 = arith.addi %28, %30 : i32
    %32 = arith.extsi %31 : i32 to i64
    %33 = arith.constant 8 : i32
    %34 = arith.extsi %33 : i32 to i64
    %29 = func.call @calloc(%32, %34) : (i64, i64) -> !llvm.ptr
    %35 = llvm.mlir.zero : !llvm.ptr
    %36 = llvm.icmp "eq" %29, %35 : !llvm.ptr
    cf.cond_br %36, ^bb0, ^bb1
    ^bb0:
      %37 = arith.constant 1 : i32
      func.return %37 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %38 = arith.constant 1 : i32
    %39 = arith.constant 0 : i32
    %40 = arith.extsi %38 : i32 to i64
    %41 = arith.extsi %39 : i32 to i64
    %42 = llvm.getelementptr %29[%41] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %40, %42 : i64, !llvm.ptr
    %43 = arith.constant 0 : i32
    %44 = arith.constant 8 : i32
    %45 = arith.index_cast %43 : i32 to index
    %46 = arith.index_cast %44 : i32 to index
    %48 = arith.constant 1 : index
    %49 = arith.constant -1 : index
    %50 = arith.cmpi sle, %45, %46 : index
    %47 = arith.select %50, %48, %49 : index
    cf.br ^bb3(%45 : index)
    ^bb3(%51: index):
    %52 = arith.cmpi slt, %51, %46 : index
    %53 = arith.cmpi sgt, %51, %46 : index
    %54 = arith.select %50, %52, %53 : i1
    cf.cond_br %54, ^bb4(%51 : index), ^bb5(%51 : index)
    ^bb4(%55: index):
      %57 = arith.index_cast %55 : index to i64
      %58 = llvm.getelementptr %10[0, %57] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<8 x i32>
      %56 = llvm.load %58 : !llvm.ptr -> i32
      %59 = arith.constant 1 : i32
      %60 = arith.addi %28, %59 : i32
      %61 = arith.index_cast %56 : i32 to index
      %62 = arith.index_cast %60 : i32 to index
      %64 = arith.constant 1 : index
      %65 = arith.constant -1 : index
      %66 = arith.cmpi sle, %61, %62 : index
      %63 = arith.select %66, %64, %65 : index
      cf.br ^bb6(%61 : index)
      ^bb6(%67: index):
      %68 = arith.cmpi slt, %67, %62 : index
      %69 = arith.cmpi sgt, %67, %62 : index
      %70 = arith.select %66, %68, %69 : i1
      cf.cond_br %70, ^bb7(%67 : index), ^bb8(%67 : index)
      ^bb7(%71: index):
        %73 = arith.index_cast %71 : index to i64
        %74 = llvm.getelementptr %29[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %72 = llvm.load %74 : !llvm.ptr -> i64
        %77 = arith.index_cast %71 : index to i32
        %76 = arith.subi %77, %56 : i32
        %78 = arith.extsi %76 : i32 to i64
        %79 = llvm.getelementptr %29[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %75 = llvm.load %79 : !llvm.ptr -> i64
        %80 = arith.addi %72, %75 : i64
        %81 = arith.index_cast %71 : index to i64
        %82 = llvm.getelementptr %29[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %80, %82 : i64, !llvm.ptr
        %83 = arith.addi %71, %63 : index
        cf.br ^bb6(%83 : index)
      ^bb8(%84: index):
      %85 = arith.addi %55, %47 : index
      cf.br ^bb3(%85 : index)
    ^bb5(%86: index):
    %87 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %89 = arith.extsi %28 : i32 to i64
    %90 = llvm.getelementptr %29[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %88 = llvm.load %90 : !llvm.ptr -> i64
    %91 = llvm.call @printf(%87, %88) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%29) : (!llvm.ptr) -> ()
    %93 = arith.constant 0 : i32
    func.return %93 : i32
  }
}