← All problems
Problem 063
How many n-digit positive integers exist that are also an nth power? 10^(n-1) <= a^n < 10^n => a < 10, and a >= 10^((n-1)/n) For a from 1..9, count n where floor(n*log10(a))+1 == n
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n)
Space complexity O(n)O(1)
Approach Flow solution Count n-digit nth powers
Verdict Suboptimal
Flow source
# Project Euler 063
# How many n-digit positive integers exist that are also an nth power?
# 10^(n-1) <= a^n < 10^n => a < 10, and a >= 10^((n-1)/n)
# For a from 1..9, count n where floor(n*log10(a))+1 == n
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function mul_small(digits: ptr<i32>, len: i32, m: i64) -> i32 {
let mut carry: i64 = 0
let mut i: i32 = 0
while i < len {
let v: i64 = (digits[i] as i64) * m + carry
digits[i] = (v % 10) as i32
carry = v / 10
i = i + 1
}
while carry > 0 {
digits[len] = (carry % 10) as i32
carry = carry / 10
len = len + 1
}
return len
}
function main() -> i32 {
let width: i32 = 50
let digits: ptr<i32> = calloc(width as i64, 4)
if digits == null { return 1 }
let mut count: i64 = 0
let mut a: i64 = 1
while a <= 9 {
let mut i: i32 = 0
while i < width {
digits[i] = 0
i = i + 1
}
digits[0] = 1
let mut len: i32 = 1
let mut n: i32 = 1
while n <= 40 {
len = mul_small(digits, len, a)
if len == n {
count = count + 1
}
if len > n {
break
}
n = n + 1
}
a = a + 1
}
printf("%lld\n", count)
free(digits)
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 mul_small_ptr_i32_i32_i64(int32_t* digits, int32_t len, int64_t m);
int32_t main(void);
int32_t mul_small_ptr_i32_i32_i64(int32_t* digits, int32_t len, int64_t m) {
int64_t carry = 0;
int32_t i = 0;
while (i < len) {
int64_t v = ((((int64_t)(digits[i])) * m) + carry);
digits[i] = ((int32_t)(FLOW_CHECKED_MOD((v), (10))));
carry = FLOW_CHECKED_DIV((v), (10));
i = (i + 1);
}
while (carry > 0) {
digits[len] = ((int32_t)(FLOW_CHECKED_MOD((carry), (10))));
carry = FLOW_CHECKED_DIV((carry), (10));
len = (len + 1);
}
return len;
}
int32_t main(void) {
int32_t width = 50;
int32_t* digits = (int32_t*)(calloc(((int64_t)(width)), 4));
if (digits == NULL) {
return 1;
}
int64_t count = 0;
int64_t a = 1;
while (a <= 9) {
int32_t i = 0;
while (i < width) {
digits[i] = 0;
i = (i + 1);
}
digits[0] = 1;
int32_t len = 1;
int32_t n = 1;
while (n <= 40) {
len = mul_small_ptr_i32_i32_i64(digits, len, a);
if (len == n) {
count = (count + 1);
}
if (len > n) {
break;
}
n = (n + 1);
}
a = (a + 1);
}
printf("%lld\n", count);
free(digits);
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 @mul_small(%arg0: !llvm.ptr, %arg1: i32, %arg2: i64) -> i32 {
%0 = arith.constant 0 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
%4 = arith.constant 0 : i32
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x i32 : (i64) -> !llvm.ptr
llvm.store %4, %6 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%7 = llvm.load %6 : !llvm.ptr -> i32
%8 = arith.cmpi slt, %7, %arg1 : i32
cf.cond_br %8, ^bb1, ^bb2
^bb1:
%10 = llvm.load %6 : !llvm.ptr -> i32
%11 = arith.extsi %10 : i32 to i64
%12 = llvm.getelementptr %arg0[%11] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%9 = llvm.load %12 : !llvm.ptr -> i32
%13 = arith.extsi %9 : i32 to i64
%14 = arith.muli %13, %arg2 : i64
%15 = llvm.load %3 : !llvm.ptr -> i64
%16 = arith.addi %14, %15 : i64
%17 = arith.constant 10 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.remsi %16, %19 : i64
%20 = arith.trunci %18 : i64 to i32
%21 = llvm.load %6 : !llvm.ptr -> i32
%22 = arith.extsi %21 : i32 to i64
%23 = llvm.getelementptr %arg0[%22] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %20, %23 : i32, !llvm.ptr
%24 = arith.constant 10 : i32
%26 = arith.extsi %24 : i32 to i64
%25 = arith.divsi %16, %26 : i64
llvm.store %25, %3 : i64, !llvm.ptr
%27 = llvm.load %6 : !llvm.ptr -> i32
%28 = arith.constant 1 : i32
%29 = arith.addi %27, %28 : i32
llvm.store %29, %6 : i32, !llvm.ptr
cf.br ^bb0
^bb2:
cf.br ^bb3(%arg1 : i32)
^bb3(%30: i32):
%31 = llvm.load %3 : !llvm.ptr -> i64
%32 = arith.constant 0 : i32
%34 = arith.extsi %32 : i32 to i64
%33 = arith.cmpi sgt, %31, %34 : i64
cf.cond_br %33, ^bb4(%30 : i32), ^bb5(%30 : i32)
^bb4(%35: i32):
%36 = llvm.load %3 : !llvm.ptr -> i64
%37 = arith.constant 10 : i32
%39 = arith.extsi %37 : i32 to i64
%38 = arith.remsi %36, %39 : i64
%40 = arith.trunci %38 : i64 to i32
%41 = arith.extsi %35 : i32 to i64
%42 = llvm.getelementptr %arg0[%41] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %40, %42 : i32, !llvm.ptr
%43 = llvm.load %3 : !llvm.ptr -> i64
%44 = arith.constant 10 : i32
%46 = arith.extsi %44 : i32 to i64
%45 = arith.divsi %43, %46 : i64
llvm.store %45, %3 : i64, !llvm.ptr
%47 = arith.constant 1 : i32
%48 = arith.addi %35, %47 : i32
cf.br ^bb3(%48 : i32)
^bb5(%49: i32):
func.return %49 : i32
}
func.func @main() -> i32 {
%50 = arith.constant 50 : i32
%52 = arith.extsi %50 : i32 to i64
%53 = arith.constant 4 : i32
%54 = arith.extsi %53 : i32 to i64
%51 = func.call @calloc(%52, %54) : (i64, i64) -> !llvm.ptr
%55 = llvm.mlir.zero : !llvm.ptr
%56 = llvm.icmp "eq" %51, %55 : !llvm.ptr
cf.cond_br %56, ^bb6, ^bb7
^bb6:
%57 = arith.constant 1 : i32
func.return %57 : i32
^bb7:
cf.br ^bb8
^bb8:
%58 = arith.constant 0 : i32
%59 = arith.extsi %58 : i32 to i64
%60 = llvm.mlir.constant(1 : i64) : i64
%61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
llvm.store %59, %61 : i64, !llvm.ptr
%62 = arith.constant 1 : i32
%63 = arith.extsi %62 : i32 to i64
%64 = llvm.mlir.constant(1 : i64) : i64
%65 = llvm.alloca %64 x i64 : (i64) -> !llvm.ptr
llvm.store %63, %65 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%66 = llvm.load %65 : !llvm.ptr -> i64
%67 = arith.constant 9 : i32
%69 = arith.extsi %67 : i32 to i64
%68 = arith.cmpi sle, %66, %69 : i64
cf.cond_br %68, ^bb10, ^bb11
^bb10:
%70 = arith.constant 0 : i32
%71 = llvm.mlir.constant(1 : i64) : i64
%72 = llvm.alloca %71 x i32 : (i64) -> !llvm.ptr
llvm.store %70, %72 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%73 = llvm.load %72 : !llvm.ptr -> i32
%74 = arith.cmpi slt, %73, %50 : i32
cf.cond_br %74, ^bb13, ^bb14
^bb13:
%75 = arith.constant 0 : i32
%76 = llvm.load %72 : !llvm.ptr -> i32
%77 = arith.extsi %76 : i32 to i64
%78 = llvm.getelementptr %51[%77] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %75, %78 : i32, !llvm.ptr
%79 = llvm.load %72 : !llvm.ptr -> i32
%80 = arith.constant 1 : i32
%81 = arith.addi %79, %80 : i32
llvm.store %81, %72 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%82 = arith.constant 1 : i32
%83 = arith.constant 0 : i32
%84 = arith.extsi %83 : i32 to i64
%85 = llvm.getelementptr %51[%84] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %82, %85 : i32, !llvm.ptr
%86 = arith.constant 1 : i32
%87 = llvm.mlir.constant(1 : i64) : i64
%88 = llvm.alloca %87 x i32 : (i64) -> !llvm.ptr
llvm.store %86, %88 : i32, !llvm.ptr
%89 = arith.constant 1 : i32
%90 = llvm.mlir.constant(1 : i64) : i64
%91 = llvm.alloca %90 x i32 : (i64) -> !llvm.ptr
llvm.store %89, %91 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%92 = llvm.load %91 : !llvm.ptr -> i32
%93 = arith.constant 40 : i32
%94 = arith.cmpi sle, %92, %93 : i32
cf.cond_br %94, ^bb16, ^bb17
^bb16:
%96 = llvm.load %88 : !llvm.ptr -> i32
%97 = llvm.load %65 : !llvm.ptr -> i64
%95 = func.call @mul_small(%51, %96, %97) : (!llvm.ptr, i32, i64) -> i32
llvm.store %95, %88 : i32, !llvm.ptr
%98 = llvm.load %88 : !llvm.ptr -> i32
%99 = llvm.load %91 : !llvm.ptr -> i32
%100 = arith.cmpi eq, %98, %99 : i32
cf.cond_br %100, ^bb18, ^bb19
^bb18:
%101 = llvm.load %61 : !llvm.ptr -> i64
%102 = arith.constant 1 : i32
%104 = arith.extsi %102 : i32 to i64
%103 = arith.addi %101, %104 : i64
llvm.store %103, %61 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%105 = llvm.load %88 : !llvm.ptr -> i32
%106 = llvm.load %91 : !llvm.ptr -> i32
%107 = arith.cmpi sgt, %105, %106 : i32
cf.cond_br %107, ^bb21, ^bb22
^bb21:
cf.br ^bb17
^bb22:
cf.br ^bb23
^bb23:
%108 = llvm.load %91 : !llvm.ptr -> i32
%109 = arith.constant 1 : i32
%110 = arith.addi %108, %109 : i32
llvm.store %110, %91 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%111 = llvm.load %65 : !llvm.ptr -> i64
%112 = arith.constant 1 : i32
%114 = arith.extsi %112 : i32 to i64
%113 = arith.addi %111, %114 : i64
llvm.store %113, %65 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%115 = llvm.mlir.addressof @str_0 : !llvm.ptr
%116 = llvm.load %61 : !llvm.ptr -> i64
%117 = llvm.call @printf(%115, %116) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%51) : (!llvm.ptr) -> ()
%119 = arith.constant 0 : i32
func.return %119 : i32
}
}