Problem 178
Count pandigital step numbers with at most 40 digits.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^3) | O(n!) |
| Space complexity | O(n) | O(n) |
| Approach | Flow solution | Permutation enumeration or constraint search |
| Verdict | Optimal |
Flow source
# Project Euler 178
# Count pandigital step numbers with at most 40 digits.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let N: i32 = 40
# dp[2][10][1024] rolling
let dp: ptr<i64> = calloc(2 * 10 * 1024, 8)
if dp == null { return 1 }
let mut d: i32 = 1
while d <= 9 {
dp[0 * 10 * 1024 + d * 1024 + (1 << d)] = 1
d = d + 1
}
let mut total: i64 = 0
# count length 1: none pandigital
let mut n: i32 = 1
while n < N {
let cur: i32 = (n - 1) % 2
let nxt: i32 = n % 2
# clear nxt
let mut i: i32 = 0
while i < 10 * 1024 {
dp[nxt * 10 * 1024 + i] = 0
i = i + 1
}
let mut last: i32 = 0
while last < 10 {
let mut mask: i32 = 0
while mask < 1024 {
let v: i64 = dp[cur * 10 * 1024 + last * 1024 + mask]
if v != 0 {
if last > 0 {
let nd: i32 = last - 1
let nm: i32 = mask | (1 << nd)
dp[nxt * 10 * 1024 + nd * 1024 + nm] = dp[nxt * 10 * 1024 + nd * 1024 + nm] + v
}
if last < 9 {
let nd: i32 = last + 1
let nm: i32 = mask | (1 << nd)
dp[nxt * 10 * 1024 + nd * 1024 + nm] = dp[nxt * 10 * 1024 + nd * 1024 + nm] + v
}
}
mask = mask + 1
}
last = last + 1
}
# after computing length n+1, add pandigital counts
last = 0
while last < 10 {
total = total + dp[nxt * 10 * 1024 + last * 1024 + 1023]
last = last + 1
}
n = n + 1
}
printf("%lld\n", total)
free(dp)
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 N = 40;
int64_t* dp = (int64_t*)(calloc(((2 * 10) * 1024), 8));
if (dp == NULL) {
return 1;
}
int32_t d = 1;
while (d <= 9) {
dp[((((0 * 10) * 1024) + (d * 1024)) + FLOW_CHECKED_SHL((1), (d)))] = 1;
d = (d + 1);
}
int64_t total = 0;
int32_t n = 1;
while (n < N) {
int32_t cur = FLOW_CHECKED_MOD(((n - 1)), (2));
int32_t nxt = FLOW_CHECKED_MOD((n), (2));
int32_t i = 0;
while (i < (10 * 1024)) {
dp[(((nxt * 10) * 1024) + i)] = 0;
i = (i + 1);
}
int32_t last = 0;
while (last < 10) {
int32_t mask = 0;
while (mask < 1024) {
int64_t v = dp[((((cur * 10) * 1024) + (last * 1024)) + mask)];
if (v != 0) {
if (last > 0) {
int32_t nd = (last - 1);
int32_t nm = (mask | FLOW_CHECKED_SHL((1), (nd)));
dp[((((nxt * 10) * 1024) + (nd * 1024)) + nm)] = (dp[((((nxt * 10) * 1024) + (nd * 1024)) + nm)] + v);
}
if (last < 9) {
int32_t nd = (last + 1);
int32_t nm = (mask | FLOW_CHECKED_SHL((1), (nd)));
dp[((((nxt * 10) * 1024) + (nd * 1024)) + nm)] = (dp[((((nxt * 10) * 1024) + (nd * 1024)) + nm)] + v);
}
}
mask = (mask + 1);
}
last = (last + 1);
}
last = 0;
while (last < 10) {
total = (total + dp[((((nxt * 10) * 1024) + (last * 1024)) + 1023)]);
last = (last + 1);
}
n = (n + 1);
}
printf("%lld\n", total);
free(dp);
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 40 : i32
%2 = arith.constant 2 : i32
%3 = arith.constant 10 : i32
%4 = arith.muli %2, %3 : i32
%5 = arith.constant 1024 : i32
%6 = arith.muli %4, %5 : i32
%7 = arith.constant 8 : i32
%8 = arith.extsi %6 : i32 to i64
%9 = arith.extsi %7 : i32 to i64
%1 = func.call @calloc(%8, %9) : (i64, i64) -> !llvm.ptr
%10 = llvm.mlir.zero : !llvm.ptr
%11 = llvm.icmp "eq" %1, %10 : !llvm.ptr
cf.cond_br %11, ^bb0, ^bb1
^bb0:
%12 = arith.constant 1 : i32
func.return %12 : i32
^bb1:
cf.br ^bb2
^bb2:
%13 = arith.constant 1 : i32
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i32 : (i64) -> !llvm.ptr
llvm.store %13, %15 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%16 = llvm.load %15 : !llvm.ptr -> i32
%17 = arith.constant 9 : i32
%18 = arith.cmpi sle, %16, %17 : i32
cf.cond_br %18, ^bb4, ^bb5
^bb4:
%19 = arith.constant 1 : i32
%20 = arith.constant 0 : i32
%21 = arith.constant 10 : i32
%22 = arith.muli %20, %21 : i32
%23 = arith.constant 1024 : i32
%24 = arith.muli %22, %23 : i32
%25 = llvm.load %15 : !llvm.ptr -> i32
%26 = arith.constant 1024 : i32
%27 = arith.muli %25, %26 : i32
%28 = arith.addi %24, %27 : i32
%29 = arith.constant 1 : i32
%30 = llvm.load %15 : !llvm.ptr -> i32
%31 = arith.shli %29, %30 : i32
%32 = arith.addi %28, %31 : i32
%33 = arith.extsi %19 : i32 to i64
%34 = arith.extsi %32 : i32 to i64
%35 = llvm.getelementptr %1[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %33, %35 : i64, !llvm.ptr
%36 = llvm.load %15 : !llvm.ptr -> i32
%37 = arith.constant 1 : i32
%38 = arith.addi %36, %37 : i32
llvm.store %38, %15 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%39 = arith.constant 0 : 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
%43 = arith.constant 1 : i32
%44 = llvm.mlir.constant(1 : i64) : i64
%45 = llvm.alloca %44 x i32 : (i64) -> !llvm.ptr
llvm.store %43, %45 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%46 = llvm.load %45 : !llvm.ptr -> i32
%47 = arith.cmpi slt, %46, %0 : i32
cf.cond_br %47, ^bb7, ^bb8
^bb7:
%48 = llvm.load %45 : !llvm.ptr -> i32
%49 = arith.constant 1 : i32
%50 = arith.subi %48, %49 : i32
%51 = arith.constant 2 : i32
%52 = arith.remsi %50, %51 : i32
%53 = llvm.load %45 : !llvm.ptr -> i32
%54 = arith.constant 2 : i32
%55 = arith.remsi %53, %54 : i32
%56 = arith.constant 0 : i32
%57 = llvm.mlir.constant(1 : i64) : i64
%58 = llvm.alloca %57 x i32 : (i64) -> !llvm.ptr
llvm.store %56, %58 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%59 = llvm.load %58 : !llvm.ptr -> i32
%60 = arith.constant 10 : i32
%61 = arith.constant 1024 : i32
%62 = arith.muli %60, %61 : i32
%63 = arith.cmpi slt, %59, %62 : i32
cf.cond_br %63, ^bb10, ^bb11
^bb10:
%64 = arith.constant 0 : i32
%65 = arith.constant 10 : i32
%66 = arith.muli %55, %65 : i32
%67 = arith.constant 1024 : i32
%68 = arith.muli %66, %67 : i32
%69 = llvm.load %58 : !llvm.ptr -> i32
%70 = arith.addi %68, %69 : i32
%71 = arith.extsi %64 : i32 to i64
%72 = arith.extsi %70 : i32 to i64
%73 = llvm.getelementptr %1[%72] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %71, %73 : i64, !llvm.ptr
%74 = llvm.load %58 : !llvm.ptr -> i32
%75 = arith.constant 1 : i32
%76 = arith.addi %74, %75 : i32
llvm.store %76, %58 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
%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 ^bb12
^bb12:
%80 = llvm.load %79 : !llvm.ptr -> i32
%81 = arith.constant 10 : i32
%82 = arith.cmpi slt, %80, %81 : i32
cf.cond_br %82, ^bb13, ^bb14
^bb13:
%83 = arith.constant 0 : i32
%84 = llvm.mlir.constant(1 : i64) : i64
%85 = llvm.alloca %84 x i32 : (i64) -> !llvm.ptr
llvm.store %83, %85 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%86 = llvm.load %85 : !llvm.ptr -> i32
%87 = arith.constant 1024 : i32
%88 = arith.cmpi slt, %86, %87 : i32
cf.cond_br %88, ^bb16, ^bb17
^bb16:
%90 = arith.constant 10 : i32
%91 = arith.muli %52, %90 : i32
%92 = arith.constant 1024 : i32
%93 = arith.muli %91, %92 : i32
%94 = llvm.load %79 : !llvm.ptr -> i32
%95 = arith.constant 1024 : i32
%96 = arith.muli %94, %95 : i32
%97 = arith.addi %93, %96 : i32
%98 = llvm.load %85 : !llvm.ptr -> i32
%99 = arith.addi %97, %98 : i32
%100 = arith.extsi %99 : i32 to i64
%101 = llvm.getelementptr %1[%100] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%89 = llvm.load %101 : !llvm.ptr -> i64
%102 = arith.constant 0 : i32
%104 = arith.extsi %102 : i32 to i64
%103 = arith.cmpi ne, %89, %104 : i64
cf.cond_br %103, ^bb18, ^bb19
^bb18:
%105 = llvm.load %79 : !llvm.ptr -> i32
%106 = arith.constant 0 : i32
%107 = arith.cmpi sgt, %105, %106 : i32
cf.cond_br %107, ^bb21, ^bb22
^bb21:
%108 = llvm.load %79 : !llvm.ptr -> i32
%109 = arith.constant 1 : i32
%110 = arith.subi %108, %109 : i32
%111 = llvm.load %85 : !llvm.ptr -> i32
%112 = arith.constant 1 : i32
%113 = arith.shli %112, %110 : i32
%114 = arith.ori %111, %113 : i32
%116 = arith.constant 10 : i32
%117 = arith.muli %55, %116 : i32
%118 = arith.constant 1024 : i32
%119 = arith.muli %117, %118 : i32
%120 = arith.constant 1024 : i32
%121 = arith.muli %110, %120 : i32
%122 = arith.addi %119, %121 : i32
%123 = arith.addi %122, %114 : i32
%124 = arith.extsi %123 : i32 to i64
%125 = llvm.getelementptr %1[%124] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%115 = llvm.load %125 : !llvm.ptr -> i64
%126 = arith.addi %115, %89 : i64
%127 = arith.constant 10 : i32
%128 = arith.muli %55, %127 : i32
%129 = arith.constant 1024 : i32
%130 = arith.muli %128, %129 : i32
%131 = arith.constant 1024 : i32
%132 = arith.muli %110, %131 : i32
%133 = arith.addi %130, %132 : i32
%134 = arith.addi %133, %114 : i32
%135 = arith.extsi %134 : i32 to i64
%136 = llvm.getelementptr %1[%135] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %126, %136 : i64, !llvm.ptr
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%137 = llvm.load %79 : !llvm.ptr -> i32
%138 = arith.constant 9 : i32
%139 = arith.cmpi slt, %137, %138 : i32
cf.cond_br %139, ^bb24, ^bb25
^bb24:
%140 = llvm.load %79 : !llvm.ptr -> i32
%141 = arith.constant 1 : i32
%142 = arith.addi %140, %141 : i32
%143 = llvm.load %85 : !llvm.ptr -> i32
%144 = arith.constant 1 : i32
%145 = arith.shli %144, %142 : i32
%146 = arith.ori %143, %145 : i32
%148 = arith.constant 10 : i32
%149 = arith.muli %55, %148 : i32
%150 = arith.constant 1024 : i32
%151 = arith.muli %149, %150 : i32
%152 = arith.constant 1024 : i32
%153 = arith.muli %142, %152 : i32
%154 = arith.addi %151, %153 : i32
%155 = arith.addi %154, %146 : i32
%156 = arith.extsi %155 : i32 to i64
%157 = llvm.getelementptr %1[%156] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%147 = llvm.load %157 : !llvm.ptr -> i64
%158 = arith.addi %147, %89 : i64
%159 = arith.constant 10 : i32
%160 = arith.muli %55, %159 : i32
%161 = arith.constant 1024 : i32
%162 = arith.muli %160, %161 : i32
%163 = arith.constant 1024 : i32
%164 = arith.muli %142, %163 : i32
%165 = arith.addi %162, %164 : i32
%166 = arith.addi %165, %146 : i32
%167 = arith.extsi %166 : i32 to i64
%168 = llvm.getelementptr %1[%167] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %158, %168 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%169 = llvm.load %85 : !llvm.ptr -> i32
%170 = arith.constant 1 : i32
%171 = arith.addi %169, %170 : i32
llvm.store %171, %85 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%172 = llvm.load %79 : !llvm.ptr -> i32
%173 = arith.constant 1 : i32
%174 = arith.addi %172, %173 : i32
llvm.store %174, %79 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%175 = arith.constant 0 : i32
llvm.store %175, %79 : i32, !llvm.ptr
cf.br ^bb27
^bb27:
%176 = llvm.load %79 : !llvm.ptr -> i32
%177 = arith.constant 10 : i32
%178 = arith.cmpi slt, %176, %177 : i32
cf.cond_br %178, ^bb28, ^bb29
^bb28:
%179 = llvm.load %42 : !llvm.ptr -> i64
%181 = arith.constant 10 : i32
%182 = arith.muli %55, %181 : i32
%183 = arith.constant 1024 : i32
%184 = arith.muli %182, %183 : i32
%185 = llvm.load %79 : !llvm.ptr -> i32
%186 = arith.constant 1024 : i32
%187 = arith.muli %185, %186 : i32
%188 = arith.addi %184, %187 : i32
%189 = arith.constant 1023 : i32
%190 = arith.addi %188, %189 : i32
%191 = arith.extsi %190 : i32 to i64
%192 = llvm.getelementptr %1[%191] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%180 = llvm.load %192 : !llvm.ptr -> i64
%193 = arith.addi %179, %180 : i64
llvm.store %193, %42 : i64, !llvm.ptr
%194 = llvm.load %79 : !llvm.ptr -> i32
%195 = arith.constant 1 : i32
%196 = arith.addi %194, %195 : i32
llvm.store %196, %79 : i32, !llvm.ptr
cf.br ^bb27
^bb29:
%197 = llvm.load %45 : !llvm.ptr -> i32
%198 = arith.constant 1 : i32
%199 = arith.addi %197, %198 : i32
llvm.store %199, %45 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%200 = llvm.mlir.addressof @str_0 : !llvm.ptr
%201 = llvm.load %42 : !llvm.ptr -> i64
%202 = llvm.call @printf(%200, %201) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%1) : (!llvm.ptr) -> ()
%204 = arith.constant 0 : i32
func.return %204 : i32
}
}