Problem 090
Distinct cube digit pairs (sets of 6 faces) that display all square numbers.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n) |
| Space complexity | O(1) | O(1) |
| Approach | Flow solution | Direct hand evaluation or enumeration |
| Verdict | Suboptimal |
Flow source
# Project Euler 090
# Distinct cube digit pairs (sets of 6 faces) that display all square numbers.
function has_digit(mask: i32, d: i32) -> bool {
return (mask & (1 << d)) != 0
}
function can_form(a: i32, b: i32, x: i32, y: i32) -> bool {
# 6 and 9 are interchangeable
let mut ax: bool = has_digit(a, x)
let mut ay: bool = has_digit(a, y)
let mut bx: bool = has_digit(b, x)
let mut by: bool = has_digit(b, y)
if x == 6 || x == 9 {
ax = ax || has_digit(a, 6) || has_digit(a, 9)
bx = bx || has_digit(b, 6) || has_digit(b, 9)
}
if y == 6 || y == 9 {
ay = ay || has_digit(a, 6) || has_digit(a, 9)
by = by || has_digit(b, 6) || has_digit(b, 9)
}
return (ax && by) || (ay && bx)
}
function valid_pair(a: i32, b: i32) -> bool {
# squares: 01,04,09,16,25,36,49,64,81
if !can_form(a, b, 0, 1) { return false }
if !can_form(a, b, 0, 4) { return false }
if !can_form(a, b, 0, 9) { return false }
if !can_form(a, b, 1, 6) { return false }
if !can_form(a, b, 2, 5) { return false }
if !can_form(a, b, 3, 6) { return false }
if !can_form(a, b, 4, 9) { return false }
if !can_form(a, b, 6, 4) { return false }
if !can_form(a, b, 8, 1) { return false }
return true
}
function popcount(x: i32) -> i32 {
let mut n: i32 = 0
let mut v: i32 = x
while v > 0 {
n = n + (v & 1)
v = v / 2
}
return n
}
function main() -> i32 {
let mut count: i64 = 0
let mut a: i32 = 0
while a < 1024 {
if popcount(a) == 6 {
let mut b: i32 = a
while b < 1024 {
if popcount(b) == 6 {
if valid_pair(a, b) {
count = count + 1
}
}
b = b + 1
}
}
a = a + 1
}
printf("%lld\n", count)
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; }
bool has_digit_i32_i32(int32_t mask, int32_t d);
bool can_form_i32_i32_i32_i32(int32_t a, int32_t b, int32_t x, int32_t y);
bool valid_pair_i32_i32(int32_t a, int32_t b);
int32_t popcount_i32(int32_t x);
int32_t main(void);
bool has_digit_i32_i32(int32_t mask, int32_t d) {
return (mask & FLOW_CHECKED_SHL((1), (d))) != 0;
}
bool can_form_i32_i32_i32_i32(int32_t a, int32_t b, int32_t x, int32_t y) {
bool ax = has_digit_i32_i32(a, x);
bool ay = has_digit_i32_i32(a, y);
bool bx = has_digit_i32_i32(b, x);
bool by = has_digit_i32_i32(b, y);
if ((x == 6 || x == 9)) {
ax = ((ax || has_digit_i32_i32(a, 6)) || has_digit_i32_i32(a, 9));
bx = ((bx || has_digit_i32_i32(b, 6)) || has_digit_i32_i32(b, 9));
}
if ((y == 6 || y == 9)) {
ay = ((ay || has_digit_i32_i32(a, 6)) || has_digit_i32_i32(a, 9));
by = ((by || has_digit_i32_i32(b, 6)) || has_digit_i32_i32(b, 9));
}
return ((ax && by) || (ay && bx));
}
bool valid_pair_i32_i32(int32_t a, int32_t b) {
if ((!(can_form_i32_i32_i32_i32(a, b, 0, 1)))) {
return 0;
}
if ((!(can_form_i32_i32_i32_i32(a, b, 0, 4)))) {
return 0;
}
if ((!(can_form_i32_i32_i32_i32(a, b, 0, 9)))) {
return 0;
}
if ((!(can_form_i32_i32_i32_i32(a, b, 1, 6)))) {
return 0;
}
if ((!(can_form_i32_i32_i32_i32(a, b, 2, 5)))) {
return 0;
}
if ((!(can_form_i32_i32_i32_i32(a, b, 3, 6)))) {
return 0;
}
if ((!(can_form_i32_i32_i32_i32(a, b, 4, 9)))) {
return 0;
}
if ((!(can_form_i32_i32_i32_i32(a, b, 6, 4)))) {
return 0;
}
if ((!(can_form_i32_i32_i32_i32(a, b, 8, 1)))) {
return 0;
}
return 1;
}
int32_t popcount_i32(int32_t x) {
int32_t n = 0;
int32_t v = x;
while (v > 0) {
n = (n + (v & 1));
v = FLOW_CHECKED_DIV((v), (2));
}
return n;
}
int32_t main(void) {
int64_t count = 0;
int32_t a = 0;
while (a < 1024) {
if (popcount_i32(a) == 6) {
int32_t b = a;
while (b < 1024) {
if (popcount_i32(b) == 6) {
if (valid_pair_i32_i32(a, b)) {
count = (count + 1);
}
}
b = (b + 1);
}
}
a = (a + 1);
}
printf("%lld\n", count);
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 @has_digit(%arg0: i32, %arg1: i32) -> i1 {
%0 = arith.constant 1 : i32
%1 = arith.shli %0, %arg1 : i32
%2 = arith.andi %arg0, %1 : i32
%3 = arith.constant 0 : i32
%4 = arith.cmpi ne, %2, %3 : i32
func.return %4 : i1
}
func.func @can_form(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32) -> i1 {
%5 = func.call @has_digit(%arg0, %arg2) : (i32, i32) -> i1
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i1 : (i64) -> !llvm.ptr
llvm.store %5, %7 : i1, !llvm.ptr
%8 = func.call @has_digit(%arg0, %arg3) : (i32, i32) -> i1
%9 = llvm.mlir.constant(1 : i64) : i64
%10 = llvm.alloca %9 x i1 : (i64) -> !llvm.ptr
llvm.store %8, %10 : i1, !llvm.ptr
%11 = func.call @has_digit(%arg1, %arg2) : (i32, i32) -> i1
%12 = llvm.mlir.constant(1 : i64) : i64
%13 = llvm.alloca %12 x i1 : (i64) -> !llvm.ptr
llvm.store %11, %13 : i1, !llvm.ptr
%14 = func.call @has_digit(%arg1, %arg3) : (i32, i32) -> i1
%15 = llvm.mlir.constant(1 : i64) : i64
%16 = llvm.alloca %15 x i1 : (i64) -> !llvm.ptr
llvm.store %14, %16 : i1, !llvm.ptr
%17 = arith.constant 6 : i32
%18 = arith.cmpi eq, %arg2, %17 : i32
%19 = scf.if %18 -> (i1) {
%20 = arith.constant true
scf.yield %20 : i1
} else {
%21 = arith.constant 9 : i32
%22 = arith.cmpi eq, %arg2, %21 : i32
scf.yield %22 : i1
}
cf.cond_br %19, ^bb0, ^bb1
^bb0:
%23 = llvm.load %7 : !llvm.ptr -> i1
%24 = scf.if %23 -> (i1) {
%25 = arith.constant true
scf.yield %25 : i1
} else {
%27 = arith.constant 6 : i32
%26 = func.call @has_digit(%arg0, %27) : (i32, i32) -> i1
scf.yield %26 : i1
}
%28 = scf.if %24 -> (i1) {
%29 = arith.constant true
scf.yield %29 : i1
} else {
%31 = arith.constant 9 : i32
%30 = func.call @has_digit(%arg0, %31) : (i32, i32) -> i1
scf.yield %30 : i1
}
llvm.store %28, %7 : i1, !llvm.ptr
%32 = llvm.load %13 : !llvm.ptr -> i1
%33 = scf.if %32 -> (i1) {
%34 = arith.constant true
scf.yield %34 : i1
} else {
%36 = arith.constant 6 : i32
%35 = func.call @has_digit(%arg1, %36) : (i32, i32) -> i1
scf.yield %35 : i1
}
%37 = scf.if %33 -> (i1) {
%38 = arith.constant true
scf.yield %38 : i1
} else {
%40 = arith.constant 9 : i32
%39 = func.call @has_digit(%arg1, %40) : (i32, i32) -> i1
scf.yield %39 : i1
}
llvm.store %37, %13 : i1, !llvm.ptr
cf.br ^bb2
^bb1:
cf.br ^bb2
^bb2:
%41 = arith.constant 6 : i32
%42 = arith.cmpi eq, %arg3, %41 : i32
%43 = scf.if %42 -> (i1) {
%44 = arith.constant true
scf.yield %44 : i1
} else {
%45 = arith.constant 9 : i32
%46 = arith.cmpi eq, %arg3, %45 : i32
scf.yield %46 : i1
}
cf.cond_br %43, ^bb3, ^bb4
^bb3:
%47 = llvm.load %10 : !llvm.ptr -> i1
%48 = scf.if %47 -> (i1) {
%49 = arith.constant true
scf.yield %49 : i1
} else {
%51 = arith.constant 6 : i32
%50 = func.call @has_digit(%arg0, %51) : (i32, i32) -> i1
scf.yield %50 : i1
}
%52 = scf.if %48 -> (i1) {
%53 = arith.constant true
scf.yield %53 : i1
} else {
%55 = arith.constant 9 : i32
%54 = func.call @has_digit(%arg0, %55) : (i32, i32) -> i1
scf.yield %54 : i1
}
llvm.store %52, %10 : i1, !llvm.ptr
%56 = llvm.load %16 : !llvm.ptr -> i1
%57 = scf.if %56 -> (i1) {
%58 = arith.constant true
scf.yield %58 : i1
} else {
%60 = arith.constant 6 : i32
%59 = func.call @has_digit(%arg1, %60) : (i32, i32) -> i1
scf.yield %59 : i1
}
%61 = scf.if %57 -> (i1) {
%62 = arith.constant true
scf.yield %62 : i1
} else {
%64 = arith.constant 9 : i32
%63 = func.call @has_digit(%arg1, %64) : (i32, i32) -> i1
scf.yield %63 : i1
}
llvm.store %61, %16 : i1, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%65 = llvm.load %7 : !llvm.ptr -> i1
%66 = scf.if %65 -> (i1) {
%67 = llvm.load %16 : !llvm.ptr -> i1
scf.yield %67 : i1
} else {
%68 = arith.constant false
scf.yield %68 : i1
}
%69 = scf.if %66 -> (i1) {
%70 = arith.constant true
scf.yield %70 : i1
} else {
%71 = llvm.load %10 : !llvm.ptr -> i1
%72 = scf.if %71 -> (i1) {
%73 = llvm.load %13 : !llvm.ptr -> i1
scf.yield %73 : i1
} else {
%74 = arith.constant false
scf.yield %74 : i1
}
scf.yield %72 : i1
}
func.return %69 : i1
}
func.func @valid_pair(%arg0: i32, %arg1: i32) -> i1 {
%76 = arith.constant 0 : i32
%77 = arith.constant 1 : i32
%75 = func.call @can_form(%arg0, %arg1, %76, %77) : (i32, i32, i32, i32) -> i1
%79 = arith.constant 1 : i1
%78 = arith.xori %75, %79 : i1
cf.cond_br %78, ^bb6, ^bb7
^bb6:
%81 = arith.constant 0 : i1
func.return %81 : i1
^bb7:
cf.br ^bb8
^bb8:
%83 = arith.constant 0 : i32
%84 = arith.constant 4 : i32
%82 = func.call @can_form(%arg0, %arg1, %83, %84) : (i32, i32, i32, i32) -> i1
%86 = arith.constant 1 : i1
%85 = arith.xori %82, %86 : i1
cf.cond_br %85, ^bb9, ^bb10
^bb9:
%88 = arith.constant 0 : i1
func.return %88 : i1
^bb10:
cf.br ^bb11
^bb11:
%90 = arith.constant 0 : i32
%91 = arith.constant 9 : i32
%89 = func.call @can_form(%arg0, %arg1, %90, %91) : (i32, i32, i32, i32) -> i1
%93 = arith.constant 1 : i1
%92 = arith.xori %89, %93 : i1
cf.cond_br %92, ^bb12, ^bb13
^bb12:
%95 = arith.constant 0 : i1
func.return %95 : i1
^bb13:
cf.br ^bb14
^bb14:
%97 = arith.constant 1 : i32
%98 = arith.constant 6 : i32
%96 = func.call @can_form(%arg0, %arg1, %97, %98) : (i32, i32, i32, i32) -> i1
%100 = arith.constant 1 : i1
%99 = arith.xori %96, %100 : i1
cf.cond_br %99, ^bb15, ^bb16
^bb15:
%102 = arith.constant 0 : i1
func.return %102 : i1
^bb16:
cf.br ^bb17
^bb17:
%104 = arith.constant 2 : i32
%105 = arith.constant 5 : i32
%103 = func.call @can_form(%arg0, %arg1, %104, %105) : (i32, i32, i32, i32) -> i1
%107 = arith.constant 1 : i1
%106 = arith.xori %103, %107 : i1
cf.cond_br %106, ^bb18, ^bb19
^bb18:
%109 = arith.constant 0 : i1
func.return %109 : i1
^bb19:
cf.br ^bb20
^bb20:
%111 = arith.constant 3 : i32
%112 = arith.constant 6 : i32
%110 = func.call @can_form(%arg0, %arg1, %111, %112) : (i32, i32, i32, i32) -> i1
%114 = arith.constant 1 : i1
%113 = arith.xori %110, %114 : i1
cf.cond_br %113, ^bb21, ^bb22
^bb21:
%116 = arith.constant 0 : i1
func.return %116 : i1
^bb22:
cf.br ^bb23
^bb23:
%118 = arith.constant 4 : i32
%119 = arith.constant 9 : i32
%117 = func.call @can_form(%arg0, %arg1, %118, %119) : (i32, i32, i32, i32) -> i1
%121 = arith.constant 1 : i1
%120 = arith.xori %117, %121 : i1
cf.cond_br %120, ^bb24, ^bb25
^bb24:
%123 = arith.constant 0 : i1
func.return %123 : i1
^bb25:
cf.br ^bb26
^bb26:
%125 = arith.constant 6 : i32
%126 = arith.constant 4 : i32
%124 = func.call @can_form(%arg0, %arg1, %125, %126) : (i32, i32, i32, i32) -> i1
%128 = arith.constant 1 : i1
%127 = arith.xori %124, %128 : i1
cf.cond_br %127, ^bb27, ^bb28
^bb27:
%130 = arith.constant 0 : i1
func.return %130 : i1
^bb28:
cf.br ^bb29
^bb29:
%132 = arith.constant 8 : i32
%133 = arith.constant 1 : i32
%131 = func.call @can_form(%arg0, %arg1, %132, %133) : (i32, i32, i32, i32) -> i1
%135 = arith.constant 1 : i1
%134 = arith.xori %131, %135 : i1
cf.cond_br %134, ^bb30, ^bb31
^bb30:
%137 = arith.constant 0 : i1
func.return %137 : i1
^bb31:
cf.br ^bb32
^bb32:
%138 = arith.constant 1 : i1
func.return %138 : i1
}
func.func @popcount(%arg0: i32) -> i32 {
%139 = arith.constant 0 : i32
%140 = llvm.mlir.constant(1 : i64) : i64
%141 = llvm.alloca %140 x i32 : (i64) -> !llvm.ptr
llvm.store %139, %141 : i32, !llvm.ptr
%142 = llvm.mlir.constant(1 : i64) : i64
%143 = llvm.alloca %142 x i32 : (i64) -> !llvm.ptr
llvm.store %arg0, %143 : i32, !llvm.ptr
cf.br ^bb33
^bb33:
%144 = llvm.load %143 : !llvm.ptr -> i32
%145 = arith.constant 0 : i32
%146 = arith.cmpi sgt, %144, %145 : i32
cf.cond_br %146, ^bb34, ^bb35
^bb34:
%147 = llvm.load %141 : !llvm.ptr -> i32
%148 = llvm.load %143 : !llvm.ptr -> i32
%149 = arith.constant 1 : i32
%150 = arith.andi %148, %149 : i32
%151 = arith.addi %147, %150 : i32
llvm.store %151, %141 : i32, !llvm.ptr
%152 = llvm.load %143 : !llvm.ptr -> i32
%153 = arith.constant 2 : i32
%154 = arith.divsi %152, %153 : i32
llvm.store %154, %143 : i32, !llvm.ptr
cf.br ^bb33
^bb35:
%155 = llvm.load %141 : !llvm.ptr -> i32
func.return %155 : i32
}
func.func @main() -> i32 {
%156 = arith.constant 0 : i32
%157 = arith.extsi %156 : i32 to i64
%158 = llvm.mlir.constant(1 : i64) : i64
%159 = llvm.alloca %158 x i64 : (i64) -> !llvm.ptr
llvm.store %157, %159 : i64, !llvm.ptr
%160 = arith.constant 0 : i32
%161 = llvm.mlir.constant(1 : i64) : i64
%162 = llvm.alloca %161 x i32 : (i64) -> !llvm.ptr
llvm.store %160, %162 : i32, !llvm.ptr
cf.br ^bb36
^bb36:
%163 = llvm.load %162 : !llvm.ptr -> i32
%164 = arith.constant 1024 : i32
%165 = arith.cmpi slt, %163, %164 : i32
cf.cond_br %165, ^bb37, ^bb38
^bb37:
%167 = llvm.load %162 : !llvm.ptr -> i32
%166 = func.call @popcount(%167) : (i32) -> i32
%168 = arith.constant 6 : i32
%169 = arith.cmpi eq, %166, %168 : i32
cf.cond_br %169, ^bb39, ^bb40
^bb39:
%170 = llvm.load %162 : !llvm.ptr -> i32
%171 = llvm.mlir.constant(1 : i64) : i64
%172 = llvm.alloca %171 x i32 : (i64) -> !llvm.ptr
llvm.store %170, %172 : i32, !llvm.ptr
cf.br ^bb42
^bb42:
%173 = llvm.load %172 : !llvm.ptr -> i32
%174 = arith.constant 1024 : i32
%175 = arith.cmpi slt, %173, %174 : i32
cf.cond_br %175, ^bb43, ^bb44
^bb43:
%177 = llvm.load %172 : !llvm.ptr -> i32
%176 = func.call @popcount(%177) : (i32) -> i32
%178 = arith.constant 6 : i32
%179 = arith.cmpi eq, %176, %178 : i32
cf.cond_br %179, ^bb45, ^bb46
^bb45:
%181 = llvm.load %162 : !llvm.ptr -> i32
%182 = llvm.load %172 : !llvm.ptr -> i32
%180 = func.call @valid_pair(%181, %182) : (i32, i32) -> i1
cf.cond_br %180, ^bb48, ^bb49
^bb48:
%183 = llvm.load %159 : !llvm.ptr -> i64
%184 = arith.constant 1 : i32
%186 = arith.extsi %184 : i32 to i64
%185 = arith.addi %183, %186 : i64
llvm.store %185, %159 : i64, !llvm.ptr
cf.br ^bb50
^bb49:
cf.br ^bb50
^bb50:
cf.br ^bb47
^bb46:
cf.br ^bb47
^bb47:
%187 = llvm.load %172 : !llvm.ptr -> i32
%188 = arith.constant 1 : i32
%189 = arith.addi %187, %188 : i32
llvm.store %189, %172 : i32, !llvm.ptr
cf.br ^bb42
^bb44:
cf.br ^bb41
^bb40:
cf.br ^bb41
^bb41:
%190 = llvm.load %162 : !llvm.ptr -> i32
%191 = arith.constant 1 : i32
%192 = arith.addi %190, %191 : i32
llvm.store %192, %162 : i32, !llvm.ptr
cf.br ^bb36
^bb38:
%193 = llvm.mlir.addressof @str_0 : !llvm.ptr
%194 = llvm.load %159 : !llvm.ptr -> i64
%195 = llvm.call @printf(%193, %194) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%196 = arith.constant 0 : i32
func.return %196 : i32
}
}