← All problems
Problem 029
Count distinct a^b for 2 <= a <= 100 and 2 <= b <= 100. Canonicalize each base as root^scale. Then a^b is uniquely identified by (root, scale * b), so no big integers are needed.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n^2 log n)
Space complexity O(1)O(n^2)
Approach Flow solution Distinct powers via set or prime factoring
Verdict Optimal
Flow source
# Project Euler 029
# Count distinct a^b for 2 <= a <= 100 and 2 <= b <= 100.
#
# Canonicalize each base as root^scale.
# Then a^b is uniquely identified by (root, scale * b),
# so no big integers are needed.
struct Canonical { root: i32, exponent: i32 }
function ipow(base: i32, exp: i32) -> i32 {
let mut result: i32 = 1
for _ in 0 to exp {
result = result * base
}
return result
}
function canonical_power(n: i32) -> Canonical {
let mut result: Canonical = Canonical { root: n, exponent: 1 }
for power in 2 to 7 {
for candidate in 2 to n + 1 {
let value: i32 = ipow(candidate, power)
if value == n {
result.root = candidate
result.exponent = power
}
if value > n {
break
}
}
}
return result
}
function solve() -> i32 {
let mut seen: array<bool, 60701>
let mut i: i32 = 0
while i < 60701 {
seen[i] = false
i = i + 1
}
let mut count: i32 = 0
for a in 2 to 101 {
let c: Canonical = canonical_power(a)
let root: i32 = c.root
let scale: i32 = c.exponent
for b in 2 to 101 {
let key: i32 = root * 601 + scale * b
if !seen[key] {
seen[key] = true
count = count + 1
}
}
}
return count
}
function main() -> i32 {
println(solve())
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; }
typedef struct Canonical Canonical;
struct Canonical {
int32_t root;
int32_t exponent;
};
int32_t ipow_i32_i32(int32_t base, int32_t exp);
Canonical canonical_power_i32(int32_t n);
int32_t solve(void);
int32_t main(void);
int32_t ipow_i32_i32(int32_t base, int32_t exp) {
int32_t result = 1;
int32_t __flow_step_1 = 1;
for (int32_t _ = 0; (0 <= exp) ? _ < exp : _ > exp; _ += (0 <= exp) ? 1 : -1) {
result = (result * base);
}
return result;
}
Canonical canonical_power_i32(int32_t n) {
Canonical result = (Canonical){ .root = n, .exponent = 1 };
int32_t __flow_step_2 = 1;
for (int32_t power = 2; (2 <= 7) ? power < 7 : power > 7; power += (2 <= 7) ? 1 : -1) {
int32_t __flow_step_3 = 1;
for (int32_t candidate = 2; (2 <= (n + 1)) ? candidate < (n + 1) : candidate > (n + 1); candidate += (2 <= (n + 1)) ? 1 : -1) {
int32_t value = ipow_i32_i32(candidate, power);
if (value == n) {
result.root = candidate;
result.exponent = power;
}
if (value > n) {
break;
}
}
}
return result;
}
int32_t solve(void) {
bool seen[60701];
int32_t i = 0;
while (i < 60701) {
seen[i] = 0;
i = (i + 1);
}
int32_t count = 0;
int32_t __flow_step_4 = 1;
for (int32_t a = 2; (2 <= 101) ? a < 101 : a > 101; a += (2 <= 101) ? 1 : -1) {
Canonical c = canonical_power_i32(a);
int32_t root = c.root;
int32_t scale = c.exponent;
int32_t __flow_step_5 = 1;
for (int32_t b = 2; (2 <= 101) ? b < 101 : b > 101; b += (2 <= 101) ? 1 : -1) {
int32_t key = ((root * 601) + (scale * b));
if ((!((((unsigned)(key) < 60701) ? seen[key] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(key), 60701), flow_fault_handler("array index out of bounds"), seen[0]))))) {
seen[key] = 1;
count = (count + 1);
}
}
}
return count;
}
int32_t main(void) {
FLOW_LOG("%d\n", solve());
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%d\n\00") {addr_space = 0 : i32} : !llvm.array<4 x i8>
// Struct: Canonical
// Fields:
// root: i32
// exponent: i32
func.func @ipow(%arg0: i32, %arg1: i32) -> i32 {
%0 = arith.constant 1 : i32
%1 = llvm.mlir.constant(1 : i64) : i64
%2 = llvm.alloca %1 x i32 : (i64) -> !llvm.ptr
llvm.store %0, %2 : i32, !llvm.ptr
%3 = arith.constant 0 : i32
%4 = arith.index_cast %3 : i32 to index
%5 = arith.index_cast %arg1 : i32 to index
%7 = arith.constant 1 : index
%8 = arith.constant -1 : index
%9 = arith.cmpi sle, %4, %5 : index
%6 = arith.select %9, %7, %8 : index
cf.br ^bb0(%4 : index)
^bb0(%10: index):
%11 = arith.cmpi slt, %10, %5 : index
%12 = arith.cmpi sgt, %10, %5 : index
%13 = arith.select %9, %11, %12 : i1
cf.cond_br %13, ^bb1(%10 : index), ^bb2(%10 : index)
^bb1(%14: index):
%15 = llvm.load %2 : !llvm.ptr -> i32
%16 = arith.muli %15, %arg0 : i32
llvm.store %16, %2 : i32, !llvm.ptr
%17 = arith.addi %14, %6 : index
cf.br ^bb0(%17 : index)
^bb2(%18: index):
%19 = llvm.load %2 : !llvm.ptr -> i32
func.return %19 : i32
}
func.func @canonical_power(%arg0: i32) -> !llvm.struct<(i32, i32)> {
%20 = llvm.mlir.undef : !llvm.struct<(i32, i32)>
%21 = llvm.insertvalue %arg0, %20[0] : !llvm.struct<(i32, i32)>
%22 = arith.constant 1 : i32
%23 = llvm.insertvalue %22, %21[1] : !llvm.struct<(i32, i32)>
%24 = llvm.mlir.constant(1 : i64) : i64
%25 = llvm.alloca %24 x !llvm.struct<(i32, i32)> : (i64) -> !llvm.ptr
llvm.store %23, %25 : !llvm.struct<(i32, i32)>, !llvm.ptr
%26 = arith.constant 2 : i32
%27 = arith.constant 7 : i32
%28 = arith.index_cast %26 : i32 to index
%29 = arith.index_cast %27 : i32 to index
%31 = arith.constant 1 : index
%32 = arith.constant -1 : index
%33 = arith.cmpi sle, %28, %29 : index
%30 = arith.select %33, %31, %32 : index
cf.br ^bb3(%28 : index)
^bb3(%34: index):
%35 = arith.cmpi slt, %34, %29 : index
%36 = arith.cmpi sgt, %34, %29 : index
%37 = arith.select %33, %35, %36 : i1
cf.cond_br %37, ^bb4(%34 : index), ^bb5(%34 : index)
^bb4(%38: index):
%39 = arith.constant 2 : i32
%40 = arith.constant 1 : i32
%41 = arith.addi %arg0, %40 : i32
%42 = arith.index_cast %39 : i32 to index
%43 = arith.index_cast %41 : i32 to index
%45 = arith.constant 1 : index
%46 = arith.constant -1 : index
%47 = arith.cmpi sle, %42, %43 : index
%44 = arith.select %47, %45, %46 : index
cf.br ^bb6(%42 : index)
^bb6(%48: index):
%49 = arith.cmpi slt, %48, %43 : index
%50 = arith.cmpi sgt, %48, %43 : index
%51 = arith.select %47, %49, %50 : i1
cf.cond_br %51, ^bb7(%48 : index), ^bb8(%48 : index)
^bb7(%52: index):
%54 = arith.index_cast %52 : index to i32
%55 = arith.index_cast %38 : index to i32
%53 = func.call @ipow(%54, %55) : (i32, i32) -> i32
%56 = arith.cmpi eq, %53, %arg0 : i32
cf.cond_br %56, ^bb9, ^bb10
^bb9:
%57 = arith.index_cast %52 : index to i32
%58 = llvm.getelementptr %25[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
llvm.store %57, %58 : i32, !llvm.ptr
%59 = arith.index_cast %38 : index to i32
%60 = llvm.getelementptr %25[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
llvm.store %59, %60 : i32, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%61 = arith.cmpi sgt, %53, %arg0 : i32
cf.cond_br %61, ^bb12, ^bb13
^bb12:
cf.br ^bb8(%52 : index)
^bb13:
cf.br ^bb14
^bb14:
%62 = arith.addi %52, %44 : index
cf.br ^bb6(%62 : index)
^bb8(%63: index):
%64 = arith.addi %38, %30 : index
cf.br ^bb3(%64 : index)
^bb5(%65: index):
%66 = llvm.load %25 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%67 = llvm.mlir.undef : !llvm.struct<(i32, i32)>
%68 = llvm.extractvalue %66[0] : !llvm.struct<(i32, i32)>
%69 = llvm.insertvalue %68, %67[0] : !llvm.struct<(i32, i32)>
%70 = llvm.extractvalue %66[1] : !llvm.struct<(i32, i32)>
%71 = llvm.insertvalue %70, %69[1] : !llvm.struct<(i32, i32)>
%72 = llvm.mlir.constant(1 : i64) : i64
%73 = llvm.alloca %72 x !llvm.struct<(i32, i32)> : (i64) -> !llvm.ptr
llvm.store %71, %73 : !llvm.struct<(i32, i32)>, !llvm.ptr
%74 = llvm.load %73 : !llvm.ptr -> !llvm.struct<(i32, i32)>
func.return %74 : !llvm.struct<(i32, i32)>
}
func.func @solve() -> i32 {
%75 = memref.alloca() {type = memref<60701xi1>} : memref<60701xbool>
%76 = arith.constant 0 : i32
%77 = llvm.mlir.constant(1 : i64) : i64
%78 = llvm.alloca %77 x i32 : (i64) -> !llvm.ptr
llvm.store %76, %78 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%79 = llvm.load %78 : !llvm.ptr -> i32
%80 = arith.constant 60701 : i32
%81 = arith.cmpi slt, %79, %80 : i32
cf.cond_br %81, ^bb16, ^bb17
^bb16:
%82 = arith.constant 0 : i1
%83 = llvm.load %78 : !llvm.ptr -> i32
%84 = arith.index_cast %83 : i32 to index
memref.store %82, %75[%84] : memref<60701xi1>
%85 = llvm.load %78 : !llvm.ptr -> i32
%86 = arith.constant 1 : i32
%87 = arith.addi %85, %86 : i32
llvm.store %87, %78 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%88 = arith.constant 0 : i32
%89 = llvm.mlir.constant(1 : i64) : i64
%90 = llvm.alloca %89 x i32 : (i64) -> !llvm.ptr
llvm.store %88, %90 : i32, !llvm.ptr
%91 = arith.constant 2 : i32
%92 = arith.constant 101 : i32
%93 = arith.index_cast %91 : i32 to index
%94 = arith.index_cast %92 : i32 to index
%96 = arith.constant 1 : index
%97 = arith.constant -1 : index
%98 = arith.cmpi sle, %93, %94 : index
%95 = arith.select %98, %96, %97 : index
cf.br ^bb18(%93 : index)
^bb18(%99: index):
%100 = arith.cmpi slt, %99, %94 : index
%101 = arith.cmpi sgt, %99, %94 : index
%102 = arith.select %98, %100, %101 : i1
cf.cond_br %102, ^bb19(%99 : index), ^bb20(%99 : index)
^bb19(%103: index):
%105 = arith.index_cast %103 : index to i32
%104 = func.call @canonical_power(%105) : (i32) -> !llvm.struct<(i32, i32)>
%106 = llvm.mlir.undef : !llvm.struct<(i32, i32)>
%107 = llvm.extractvalue %104[0] : !llvm.struct<(i32, i32)>
%108 = llvm.insertvalue %107, %106[0] : !llvm.struct<(i32, i32)>
%109 = llvm.extractvalue %104[1] : !llvm.struct<(i32, i32)>
%110 = llvm.insertvalue %109, %108[1] : !llvm.struct<(i32, i32)>
%111 = llvm.mlir.constant(1 : i64) : i64
%112 = llvm.alloca %111 x !llvm.struct<(i32, i32)> : (i64) -> !llvm.ptr
llvm.store %110, %112 : !llvm.struct<(i32, i32)>, !llvm.ptr
%113 = llvm.load %112 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%114 = llvm.mlir.constant(1 : i64) : i64
%115 = llvm.alloca %114 x !llvm.struct<(i32, i32)> : (i64) -> !llvm.ptr
llvm.store %113, %115 : !llvm.struct<(i32, i32)>, !llvm.ptr
%116 = llvm.load %115 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%117 = llvm.getelementptr %115[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%118 = llvm.load %117 : !llvm.ptr -> i32
%119 = llvm.load %115 : !llvm.ptr -> !llvm.struct<(i32, i32)>
%120 = llvm.getelementptr %115[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%121 = llvm.load %120 : !llvm.ptr -> i32
%122 = arith.constant 2 : i32
%123 = arith.constant 101 : i32
%124 = arith.index_cast %122 : i32 to index
%125 = arith.index_cast %123 : i32 to index
%127 = arith.constant 1 : index
%128 = arith.constant -1 : index
%129 = arith.cmpi sle, %124, %125 : index
%126 = arith.select %129, %127, %128 : index
cf.br ^bb21(%124 : index)
^bb21(%130: index):
%131 = arith.cmpi slt, %130, %125 : index
%132 = arith.cmpi sgt, %130, %125 : index
%133 = arith.select %129, %131, %132 : i1
cf.cond_br %133, ^bb22(%130 : index), ^bb23(%130 : index)
^bb22(%134: index):
%135 = arith.constant 601 : i32
%136 = arith.muli %118, %135 : i32
%138 = arith.index_cast %134 : index to i32
%137 = arith.muli %121, %138 : i32
%139 = arith.addi %136, %137 : i32
%141 = arith.index_cast %139 : i32 to index
%140 = memref.load %75[%141] : memref<60701xi1>
%143 = arith.constant 1 : i1
%142 = arith.xori %140, %143 : i1
cf.cond_br %142, ^bb24, ^bb25
^bb24:
%145 = arith.constant 1 : i1
%146 = arith.index_cast %139 : i32 to index
memref.store %145, %75[%146] : memref<60701xi1>
%147 = llvm.load %90 : !llvm.ptr -> i32
%148 = arith.constant 1 : i32
%149 = arith.addi %147, %148 : i32
llvm.store %149, %90 : i32, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%150 = arith.addi %134, %126 : index
cf.br ^bb21(%150 : index)
^bb23(%151: index):
%152 = arith.addi %103, %95 : index
cf.br ^bb18(%152 : index)
^bb20(%153: index):
%154 = llvm.load %90 : !llvm.ptr -> i32
func.return %154 : i32
}
func.func @main() -> i32 {
%155 = func.call @solve() : () -> i32
%156 = llvm.mlir.addressof @str_0 : !llvm.ptr
%157 = llvm.call @printf(%156, %155) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i32) -> i32
%158 = arith.constant 0 : i32
%159 = arith.constant 0 : i32
func.return %159 : i32
}
}