Problem 491
Double pandigital numbers divisible by 11 (digit-mask enumeration).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n!) |
| Space complexity | O(1) | O(n) |
| Approach | Flow solution | Permutation enumeration or constraint search |
| Verdict | Optimal |
Flow source
# Project Euler 491
# Double pandigital numbers divisible by 11 (digit-mask enumeration).
function snoob(x0: i64) -> i64 {
let x: i64 = x0
let smallest: i64 = x & -x
let ripple: i64 = x + smallest
let ones: i64 = ripple ^ x
return ((ones >> 2) / smallest) | ripple
}
function main() -> i32 {
let maxDigit: i32 = 9
let digitSum: i32 = (maxDigit + 1) * maxDigit
let numDigits: i32 = 2 * (maxDigit + 1)
let mut factorial: i64 = 1
let mut i: i32 = 1
while i <= maxDigit + 1 {
factorial = factorial * (i as i64)
i = i + 1
}
let perms: array<i64, 10> = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
i = 0
while i <= maxDigit {
perms[i] = factorial >> i
i = i + 1
}
let minBitmask: i64 = (1 << (maxDigit + 1)) - 1
let maxBitmask: i64 = minBitmask << (maxDigit + 1)
let mut result: i64 = 0
let mut bitmask: i64 = minBitmask
while true {
let mut reduce: i64 = bitmask
let mut ok: bool = true
while reduce > 0 {
if (reduce & 3) == 2 {
ok = false
break
}
reduce = reduce >> 2
}
if ok {
let mut sumOdd: i32 = 0
let mut repeated: i32 = 0
let mut pos: i32 = 0
while pos < numDigits {
if (bitmask & (1 << pos)) != 0 {
sumOdd = sumOdd + pos / 2
if (pos & 1) != 0 {
repeated = repeated + 1
}
}
pos = pos + 1
}
let diff: i32 = digitSum - 2 * sumOdd
if diff % 11 == 0 {
result = result + perms[repeated] * perms[repeated]
}
}
if bitmask == maxBitmask { break }
bitmask = snoob(bitmask)
}
printf("%lld\n", result * (maxDigit as i64) / ((maxDigit + 1) as i64))
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; }
int64_t snoob_i64(int64_t x0);
int32_t main(void);
int64_t snoob_i64(int64_t x0) {
int64_t x = x0;
int64_t smallest = (x & (-x));
int64_t ripple = (x + smallest);
int64_t ones = (ripple ^ x);
return (FLOW_CHECKED_DIV((FLOW_CHECKED_SHR((ones), (2))), (smallest)) | ripple);
}
int32_t main(void) {
int32_t maxDigit = 9;
int32_t digitSum = ((maxDigit + 1) * maxDigit);
int32_t numDigits = (2 * (maxDigit + 1));
int64_t factorial = 1;
int32_t i = 1;
while (i <= (maxDigit + 1)) {
factorial = (factorial * ((int64_t)(i)));
i = (i + 1);
}
int64_t perms[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
i = 0;
while (i <= maxDigit) {
perms[i] = FLOW_CHECKED_SHR((factorial), (i));
i = (i + 1);
}
int64_t minBitmask = (FLOW_CHECKED_SHL((1), ((maxDigit + 1))) - 1);
int64_t maxBitmask = FLOW_CHECKED_SHL((minBitmask), ((maxDigit + 1)));
int64_t result = 0;
int64_t bitmask = minBitmask;
while (1) {
int64_t reduce = bitmask;
bool ok = 1;
while (reduce > 0) {
if ((reduce & 3) == 2) {
ok = 0;
break;
}
reduce = FLOW_CHECKED_SHR((reduce), (2));
}
if (ok) {
int32_t sumOdd = 0;
int32_t repeated = 0;
int32_t pos = 0;
while (pos < numDigits) {
if ((bitmask & FLOW_CHECKED_SHL((1), (pos))) != 0) {
sumOdd = (sumOdd + FLOW_CHECKED_DIV((pos), (2)));
if ((pos & 1) != 0) {
repeated = (repeated + 1);
}
}
pos = (pos + 1);
}
int32_t diff = (digitSum - (2 * sumOdd));
if (FLOW_CHECKED_MOD((diff), (11)) == 0) {
result = (result + ((((unsigned)(repeated) < 10) ? perms[repeated] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(repeated), 10), flow_fault_handler("array index out of bounds"), perms[0])) * (((unsigned)(repeated) < 10) ? perms[repeated] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(repeated), 10), flow_fault_handler("array index out of bounds"), perms[0]))));
}
}
if (bitmask == maxBitmask) {
break;
}
bitmask = snoob_i64(bitmask);
}
printf("%lld\n", FLOW_CHECKED_DIV(((result * ((int64_t)(maxDigit)))), (((int64_t)((maxDigit + 1))))));
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 @snoob(%arg0: i64) -> i64 {
%1 = arith.constant 0 : i64
%0 = arith.subi %1, %arg0 : i64
%2 = arith.andi %arg0, %0 : i64
%3 = arith.addi %arg0, %2 : i64
%4 = arith.xori %3, %arg0 : i64
%5 = arith.constant 2 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.shrsi %4, %7 : i64
%8 = arith.divsi %6, %2 : i64
%9 = arith.ori %8, %3 : i64
func.return %9 : i64
}
func.func @main() -> i32 {
%10 = arith.constant 9 : i32
%11 = arith.constant 1 : i32
%12 = arith.addi %10, %11 : i32
%13 = arith.muli %12, %10 : i32
%14 = arith.constant 2 : i32
%15 = arith.constant 1 : i32
%16 = arith.addi %10, %15 : i32
%17 = arith.muli %14, %16 : i32
%18 = arith.constant 1 : i32
%19 = arith.extsi %18 : i32 to i64
%20 = llvm.mlir.constant(1 : i64) : i64
%21 = llvm.alloca %20 x i64 : (i64) -> !llvm.ptr
llvm.store %19, %21 : i64, !llvm.ptr
%22 = arith.constant 1 : i32
%23 = llvm.mlir.constant(1 : i64) : i64
%24 = llvm.alloca %23 x i32 : (i64) -> !llvm.ptr
llvm.store %22, %24 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%25 = llvm.load %24 : !llvm.ptr -> i32
%26 = arith.constant 1 : i32
%27 = arith.addi %10, %26 : i32
%28 = arith.cmpi sle, %25, %27 : i32
cf.cond_br %28, ^bb1, ^bb2
^bb1:
%29 = llvm.load %21 : !llvm.ptr -> i64
%30 = llvm.load %24 : !llvm.ptr -> i32
%31 = arith.extsi %30 : i32 to i64
%32 = arith.muli %29, %31 : i64
llvm.store %32, %21 : i64, !llvm.ptr
%33 = llvm.load %24 : !llvm.ptr -> i32
%34 = arith.constant 1 : i32
%35 = arith.addi %33, %34 : i32
llvm.store %35, %24 : i32, !llvm.ptr
cf.br ^bb0
^bb2:
%37 = arith.constant 0 : i32
%38 = arith.constant 0 : i32
%39 = arith.constant 0 : i32
%40 = arith.constant 0 : i32
%41 = arith.constant 0 : i32
%42 = arith.constant 0 : i32
%43 = arith.constant 0 : i32
%44 = arith.constant 0 : i32
%45 = arith.constant 0 : i32
%46 = arith.constant 0 : i32
%47 = llvm.mlir.constant(1 : i64) : i64
%48 = llvm.alloca %47 x !llvm.array<10 x i64> : (i64) -> !llvm.ptr
%49 = llvm.mlir.zero : !llvm.array<10 x i64>
llvm.store %49, %48 : !llvm.array<10 x i64>, !llvm.ptr
%50 = arith.extsi %37 : i32 to i64
%51 = arith.extsi %38 : i32 to i64
%52 = arith.extsi %39 : i32 to i64
%53 = arith.extsi %40 : i32 to i64
%54 = arith.extsi %41 : i32 to i64
%55 = arith.extsi %42 : i32 to i64
%56 = arith.extsi %43 : i32 to i64
%57 = arith.extsi %44 : i32 to i64
%58 = arith.extsi %45 : i32 to i64
%59 = arith.extsi %46 : i32 to i64
%60 = llvm.mlir.constant(0 : i64) : i64
%61 = llvm.getelementptr %48[0, %60] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %50, %61 : i64, !llvm.ptr
%62 = llvm.mlir.constant(1 : i64) : i64
%63 = llvm.getelementptr %48[0, %62] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %51, %63 : i64, !llvm.ptr
%64 = llvm.mlir.constant(2 : i64) : i64
%65 = llvm.getelementptr %48[0, %64] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %52, %65 : i64, !llvm.ptr
%66 = llvm.mlir.constant(3 : i64) : i64
%67 = llvm.getelementptr %48[0, %66] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %53, %67 : i64, !llvm.ptr
%68 = llvm.mlir.constant(4 : i64) : i64
%69 = llvm.getelementptr %48[0, %68] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %54, %69 : i64, !llvm.ptr
%70 = llvm.mlir.constant(5 : i64) : i64
%71 = llvm.getelementptr %48[0, %70] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %55, %71 : i64, !llvm.ptr
%72 = llvm.mlir.constant(6 : i64) : i64
%73 = llvm.getelementptr %48[0, %72] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %56, %73 : i64, !llvm.ptr
%74 = llvm.mlir.constant(7 : i64) : i64
%75 = llvm.getelementptr %48[0, %74] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %57, %75 : i64, !llvm.ptr
%76 = llvm.mlir.constant(8 : i64) : i64
%77 = llvm.getelementptr %48[0, %76] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %58, %77 : i64, !llvm.ptr
%78 = llvm.mlir.constant(9 : i64) : i64
%79 = llvm.getelementptr %48[0, %78] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %59, %79 : i64, !llvm.ptr
%80 = arith.constant 0 : i32
llvm.store %80, %24 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%81 = llvm.load %24 : !llvm.ptr -> i32
%82 = arith.cmpi sle, %81, %10 : i32
cf.cond_br %82, ^bb4, ^bb5
^bb4:
%83 = llvm.load %21 : !llvm.ptr -> i64
%84 = llvm.load %24 : !llvm.ptr -> i32
%86 = arith.extsi %84 : i32 to i64
%85 = arith.shrsi %83, %86 : i64
%87 = llvm.load %24 : !llvm.ptr -> i32
%88 = arith.extsi %87 : i32 to i64
%89 = llvm.getelementptr %48[0, %88] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %85, %89 : i64, !llvm.ptr
%90 = llvm.load %24 : !llvm.ptr -> i32
%91 = arith.constant 1 : i32
%92 = arith.addi %90, %91 : i32
llvm.store %92, %24 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%93 = arith.constant 1 : i32
%94 = arith.constant 1 : i32
%95 = arith.addi %10, %94 : i32
%96 = arith.shli %93, %95 : i32
%97 = arith.constant 1 : i32
%98 = arith.subi %96, %97 : i32
%99 = arith.extsi %98 : i32 to i64
%100 = arith.constant 1 : i32
%101 = arith.addi %10, %100 : i32
%103 = arith.extsi %101 : i32 to i64
%102 = arith.shli %99, %103 : i64
%104 = arith.constant 0 : i32
%105 = arith.extsi %104 : i32 to i64
%106 = llvm.mlir.constant(1 : i64) : i64
%107 = llvm.alloca %106 x i64 : (i64) -> !llvm.ptr
llvm.store %105, %107 : i64, !llvm.ptr
%108 = llvm.mlir.constant(1 : i64) : i64
%109 = llvm.alloca %108 x i64 : (i64) -> !llvm.ptr
llvm.store %99, %109 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%110 = arith.constant 1 : i1
cf.cond_br %110, ^bb7, ^bb8
^bb7:
%111 = llvm.load %109 : !llvm.ptr -> i64
%112 = llvm.mlir.constant(1 : i64) : i64
%113 = llvm.alloca %112 x i64 : (i64) -> !llvm.ptr
llvm.store %111, %113 : i64, !llvm.ptr
%114 = arith.constant 1 : i1
%115 = llvm.mlir.constant(1 : i64) : i64
%116 = llvm.alloca %115 x i1 : (i64) -> !llvm.ptr
llvm.store %114, %116 : i1, !llvm.ptr
cf.br ^bb9
^bb9:
%117 = llvm.load %113 : !llvm.ptr -> i64
%118 = arith.constant 0 : i32
%120 = arith.extsi %118 : i32 to i64
%119 = arith.cmpi sgt, %117, %120 : i64
cf.cond_br %119, ^bb10, ^bb11
^bb10:
%121 = llvm.load %113 : !llvm.ptr -> i64
%122 = arith.constant 3 : i32
%124 = arith.extsi %122 : i32 to i64
%123 = arith.andi %121, %124 : i64
%125 = arith.constant 2 : i32
%127 = arith.extsi %125 : i32 to i64
%126 = arith.cmpi eq, %123, %127 : i64
cf.cond_br %126, ^bb12, ^bb13
^bb12:
%128 = arith.constant 0 : i1
llvm.store %128, %116 : i1, !llvm.ptr
cf.br ^bb11
^bb13:
cf.br ^bb14
^bb14:
%129 = llvm.load %113 : !llvm.ptr -> i64
%130 = arith.constant 2 : i32
%132 = arith.extsi %130 : i32 to i64
%131 = arith.shrsi %129, %132 : i64
llvm.store %131, %113 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%133 = llvm.load %116 : !llvm.ptr -> i1
cf.cond_br %133, ^bb15, ^bb16
^bb15:
%134 = arith.constant 0 : i32
%135 = llvm.mlir.constant(1 : i64) : i64
%136 = llvm.alloca %135 x i32 : (i64) -> !llvm.ptr
llvm.store %134, %136 : i32, !llvm.ptr
%137 = arith.constant 0 : i32
%138 = llvm.mlir.constant(1 : i64) : i64
%139 = llvm.alloca %138 x i32 : (i64) -> !llvm.ptr
llvm.store %137, %139 : i32, !llvm.ptr
%140 = arith.constant 0 : i32
%141 = llvm.mlir.constant(1 : i64) : i64
%142 = llvm.alloca %141 x i32 : (i64) -> !llvm.ptr
llvm.store %140, %142 : i32, !llvm.ptr
cf.br ^bb18
^bb18:
%143 = llvm.load %142 : !llvm.ptr -> i32
%144 = arith.cmpi slt, %143, %17 : i32
cf.cond_br %144, ^bb19, ^bb20
^bb19:
%145 = llvm.load %109 : !llvm.ptr -> i64
%146 = arith.constant 1 : i32
%147 = llvm.load %142 : !llvm.ptr -> i32
%148 = arith.shli %146, %147 : i32
%150 = arith.extsi %148 : i32 to i64
%149 = arith.andi %145, %150 : i64
%151 = arith.constant 0 : i32
%153 = arith.extsi %151 : i32 to i64
%152 = arith.cmpi ne, %149, %153 : i64
cf.cond_br %152, ^bb21, ^bb22
^bb21:
%154 = llvm.load %136 : !llvm.ptr -> i32
%155 = llvm.load %142 : !llvm.ptr -> i32
%156 = arith.constant 2 : i32
%157 = arith.divsi %155, %156 : i32
%158 = arith.addi %154, %157 : i32
llvm.store %158, %136 : i32, !llvm.ptr
%159 = llvm.load %142 : !llvm.ptr -> i32
%160 = arith.constant 1 : i32
%161 = arith.andi %159, %160 : i32
%162 = arith.constant 0 : i32
%163 = arith.cmpi ne, %161, %162 : i32
cf.cond_br %163, ^bb24, ^bb25
^bb24:
%164 = llvm.load %139 : !llvm.ptr -> i32
%165 = arith.constant 1 : i32
%166 = arith.addi %164, %165 : i32
llvm.store %166, %139 : i32, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%167 = llvm.load %142 : !llvm.ptr -> i32
%168 = arith.constant 1 : i32
%169 = arith.addi %167, %168 : i32
llvm.store %169, %142 : i32, !llvm.ptr
cf.br ^bb18
^bb20:
%170 = arith.constant 2 : i32
%171 = llvm.load %136 : !llvm.ptr -> i32
%172 = arith.muli %170, %171 : i32
%173 = arith.subi %13, %172 : i32
%174 = arith.constant 11 : i32
%175 = arith.remsi %173, %174 : i32
%176 = arith.constant 0 : i32
%177 = arith.cmpi eq, %175, %176 : i32
cf.cond_br %177, ^bb27, ^bb28
^bb27:
%178 = llvm.load %107 : !llvm.ptr -> i64
%180 = llvm.load %139 : !llvm.ptr -> i32
%181 = arith.extsi %180 : i32 to i64
%182 = llvm.getelementptr %48[0, %181] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
%179 = llvm.load %182 : !llvm.ptr -> i64
%184 = llvm.load %139 : !llvm.ptr -> i32
%185 = arith.extsi %184 : i32 to i64
%186 = llvm.getelementptr %48[0, %185] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
%183 = llvm.load %186 : !llvm.ptr -> i64
%187 = arith.muli %179, %183 : i64
%188 = arith.addi %178, %187 : i64
llvm.store %188, %107 : i64, !llvm.ptr
cf.br ^bb29
^bb28:
cf.br ^bb29
^bb29:
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%189 = llvm.load %109 : !llvm.ptr -> i64
%190 = arith.cmpi eq, %189, %102 : i64
cf.cond_br %190, ^bb30, ^bb31
^bb30:
cf.br ^bb8
^bb31:
cf.br ^bb32
^bb32:
%192 = llvm.load %109 : !llvm.ptr -> i64
%191 = func.call @snoob(%192) : (i64) -> i64
llvm.store %191, %109 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%193 = llvm.mlir.addressof @str_0 : !llvm.ptr
%194 = llvm.load %107 : !llvm.ptr -> i64
%195 = arith.extsi %10 : i32 to i64
%196 = arith.muli %194, %195 : i64
%197 = arith.constant 1 : i32
%198 = arith.addi %10, %197 : i32
%199 = arith.extsi %198 : i32 to i64
%200 = arith.divsi %196, %199 : i64
%201 = llvm.call @printf(%193, %200) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%202 = arith.constant 0 : i32
func.return %202 : i32
}
}