Problem 119

The 30th number that is a power a^b (b≥2) equal to the sum of its digits raised to some power.

Answer248155780267521
Output248155780267521
StatusPASS
Native helperno
Runtime0 ms
Peak memory1088 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(1)O(n)
ApproachFlow solutionSieve or enumeration
VerdictSuboptimal

Flow source

# Project Euler 119
# The 30th number that is a power a^b (b≥2) equal to the sum of its digits raised to some power.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function digit_sum(n0: i64) -> i64 {
    let mut n: i64 = n0
    let mut s: i64 = 0
    while n > 0 {
        s = s + n % 10
        n = n / 10
    }
    return s
}

function ipow(base: i64, exp: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base
    let mut e: i64 = exp
    while e > 0 {
        if e % 2 == 1 {
            # overflow guard
            if r > 9223372036854775807 / b { return -1 }
            r = r * b
        }
        e = e / 2
        if e > 0 {
            if b > 9223372036854775807 / b { return -1 }
            b = b * b
        }
    }
    return r
}

function insert_sorted(arr: ptr<i64>, n: i32, v: i64) -> i32 {
    # insert unique sorted
    let mut i: i32 = 0
    while i < n {
        if arr[i] == v { return n }
        if arr[i] > v { break }
        i = i + 1
    }
    let mut j: i32 = n
    while j > i {
        arr[j] = arr[j - 1]
        j = j - 1
    }
    arr[i] = v
    return n + 1
}

function main() -> i32 {
    let arr: ptr<i64> = calloc(2000, 8)
    if arr == null { return 1 }
    let mut count: i32 = 0
    let mut base: i64 = 2
    while base <= 200 {
        let mut exp: i64 = 2
        while exp <= 50 {
            let p: i64 = ipow(base, exp)
            if p < 0 { break }
            if p >= 10 && digit_sum(p) == base {
                count = insert_sorted(arr, count, p)
            }
            exp = exp + 1
        }
        base = base + 1
    }
    # Also bases that are digit sums may be > 9*digits; base up to 9*20=180 for ~20 digits ok
    # 30th term (1-indexed)
    printf("%lld\n", arr[29])
    free(arr)
    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 digit_sum_i64(int64_t n0);
int64_t ipow_i64_i64(int64_t base, int64_t exp);
int32_t insert_sorted_ptr_i64_i32_i64(int64_t* arr, int32_t n, int64_t v);
int32_t main(void);



int64_t digit_sum_i64(int64_t n0) {
    int64_t n = n0;
    int64_t s = 0;
    while (n > 0) {
        s = (s + FLOW_CHECKED_MOD((n), (10)));
        n = FLOW_CHECKED_DIV((n), (10));
    }
    return s;
}

int64_t ipow_i64_i64(int64_t base, int64_t exp) {
    int64_t r = 1;
    int64_t b = base;
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            if (r > FLOW_CHECKED_DIV((9223372036854775807), (b))) {
                return (-1);
            }
            r = (r * b);
        }
        e = FLOW_CHECKED_DIV((e), (2));
        if (e > 0) {
            if (b > FLOW_CHECKED_DIV((9223372036854775807), (b))) {
                return (-1);
            }
            b = (b * b);
        }
    }
    return r;
}

int32_t insert_sorted_ptr_i64_i32_i64(int64_t* arr, int32_t n, int64_t v) {
    int32_t i = 0;
    while (i < n) {
        if (arr[i] == v) {
            return n;
        }
        if (arr[i] > v) {
            break;
        }
        i = (i + 1);
    }
    int32_t j = n;
    while (j > i) {
        arr[j] = arr[(j - 1)];
        j = (j - 1);
    }
    arr[i] = v;
    return (n + 1);
}

int32_t main(void) {
    int64_t* arr = (int64_t*)(calloc(2000, 8));
    if (arr == NULL) {
        return 1;
    }
    int32_t count = 0;
    int64_t base = 2;
    while (base <= 200) {
        int64_t exp = 2;
        while (exp <= 50) {
            int64_t p = ipow_i64_i64(base, exp);
            if (p < 0) {
                break;
            }
            if ((p >= 10 && digit_sum_i64(p) == base)) {
                count = insert_sorted_ptr_i64_i32_i64(arr, count, p);
            }
            exp = (exp + 1);
        }
        base = (base + 1);
    }
    printf("%lld\n", arr[29]);
    free(arr);
    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 @digit_sum(%arg0: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %1 : i64, !llvm.ptr
    %2 = arith.constant 0 : i32
    %3 = arith.extsi %2 : i32 to i64
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %3, %5 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %6 = llvm.load %1 : !llvm.ptr -> i64
    %7 = arith.constant 0 : i32
    %9 = arith.extsi %7 : i32 to i64
    %8 = arith.cmpi sgt, %6, %9 : i64
    cf.cond_br %8, ^bb1, ^bb2
    ^bb1:
      %10 = llvm.load %5 : !llvm.ptr -> i64
      %11 = llvm.load %1 : !llvm.ptr -> i64
      %12 = arith.constant 10 : i32
      %14 = arith.extsi %12 : i32 to i64
      %13 = arith.remsi %11, %14 : i64
      %15 = arith.addi %10, %13 : i64
      llvm.store %15, %5 : i64, !llvm.ptr
      %16 = llvm.load %1 : !llvm.ptr -> i64
      %17 = arith.constant 10 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.divsi %16, %19 : i64
      llvm.store %18, %1 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %20 = llvm.load %5 : !llvm.ptr -> i64
    func.return %20 : i64
  }
  func.func @ipow(%arg0: i64, %arg1: i64) -> i64 {
    %21 = arith.constant 1 : i32
    %22 = arith.extsi %21 : i32 to i64
    %23 = llvm.mlir.constant(1 : i64) : i64
    %24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
    llvm.store %22, %24 : i64, !llvm.ptr
    %25 = llvm.mlir.constant(1 : i64) : i64
    %26 = llvm.alloca %25 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %26 : i64, !llvm.ptr
    %27 = llvm.mlir.constant(1 : i64) : i64
    %28 = llvm.alloca %27 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %28 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %29 = llvm.load %28 : !llvm.ptr -> i64
    %30 = arith.constant 0 : i32
    %32 = arith.extsi %30 : i32 to i64
    %31 = arith.cmpi sgt, %29, %32 : i64
    cf.cond_br %31, ^bb4, ^bb5
    ^bb4:
      %33 = llvm.load %28 : !llvm.ptr -> i64
      %34 = arith.constant 2 : i32
      %36 = arith.extsi %34 : i32 to i64
      %35 = arith.remsi %33, %36 : i64
      %37 = arith.constant 1 : i32
      %39 = arith.extsi %37 : i32 to i64
      %38 = arith.cmpi eq, %35, %39 : i64
      cf.cond_br %38, ^bb6, ^bb7
      ^bb6:
        %40 = llvm.load %24 : !llvm.ptr -> i64
        %41 = arith.constant 9223372032559808511 : i32
        %42 = llvm.load %26 : !llvm.ptr -> i64
        %44 = arith.extsi %41 : i32 to i64
        %43 = arith.divsi %44, %42 : i64
        %45 = arith.cmpi sgt, %40, %43 : i64
        cf.cond_br %45, ^bb9, ^bb10
        ^bb9:
          %46 = arith.constant 1 : i32
          %48 = arith.constant 0 : i32
          %47 = arith.subi %48, %46 : i32
          %49 = arith.extsi %47 : i32 to i64
          func.return %49 : i64
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %50 = llvm.load %24 : !llvm.ptr -> i64
        %51 = llvm.load %26 : !llvm.ptr -> i64
        %52 = arith.muli %50, %51 : i64
        llvm.store %52, %24 : i64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %53 = llvm.load %28 : !llvm.ptr -> i64
      %54 = arith.constant 2 : i32
      %56 = arith.extsi %54 : i32 to i64
      %55 = arith.divsi %53, %56 : i64
      llvm.store %55, %28 : i64, !llvm.ptr
      %57 = llvm.load %28 : !llvm.ptr -> i64
      %58 = arith.constant 0 : i32
      %60 = arith.extsi %58 : i32 to i64
      %59 = arith.cmpi sgt, %57, %60 : i64
      cf.cond_br %59, ^bb12, ^bb13
      ^bb12:
        %61 = llvm.load %26 : !llvm.ptr -> i64
        %62 = arith.constant 9223372032559808511 : i32
        %63 = llvm.load %26 : !llvm.ptr -> i64
        %65 = arith.extsi %62 : i32 to i64
        %64 = arith.divsi %65, %63 : i64
        %66 = arith.cmpi sgt, %61, %64 : i64
        cf.cond_br %66, ^bb15, ^bb16
        ^bb15:
          %67 = arith.constant 1 : i32
          %69 = arith.constant 0 : i32
          %68 = arith.subi %69, %67 : i32
          %70 = arith.extsi %68 : i32 to i64
          func.return %70 : i64
        ^bb16:
          cf.br ^bb17
        ^bb17:
        %71 = llvm.load %26 : !llvm.ptr -> i64
        %72 = llvm.load %26 : !llvm.ptr -> i64
        %73 = arith.muli %71, %72 : i64
        llvm.store %73, %26 : i64, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      cf.br ^bb3
    ^bb5:
    %74 = llvm.load %24 : !llvm.ptr -> i64
    func.return %74 : i64
  }
  func.func @insert_sorted(%arg0: !llvm.ptr, %arg1: i32, %arg2: i64) -> i32 {
    %75 = arith.constant 0 : i32
    %76 = llvm.mlir.constant(1 : i64) : i64
    %77 = llvm.alloca %76 x i32 : (i64) -> !llvm.ptr
    llvm.store %75, %77 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %78 = llvm.load %77 : !llvm.ptr -> i32
    %79 = arith.cmpi slt, %78, %arg1 : i32
    cf.cond_br %79, ^bb19, ^bb20
    ^bb19:
      %81 = llvm.load %77 : !llvm.ptr -> i32
      %82 = arith.extsi %81 : i32 to i64
      %83 = llvm.getelementptr %arg0[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %80 = llvm.load %83 : !llvm.ptr -> i64
      %84 = arith.cmpi eq, %80, %arg2 : i64
      cf.cond_br %84, ^bb21, ^bb22
      ^bb21:
        func.return %arg1 : i32
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %86 = llvm.load %77 : !llvm.ptr -> i32
      %87 = arith.extsi %86 : i32 to i64
      %88 = llvm.getelementptr %arg0[%87] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %85 = llvm.load %88 : !llvm.ptr -> i64
      %89 = arith.cmpi sgt, %85, %arg2 : i64
      cf.cond_br %89, ^bb24, ^bb25
      ^bb24:
        cf.br ^bb20
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %90 = llvm.load %77 : !llvm.ptr -> i32
      %91 = arith.constant 1 : i32
      %92 = arith.addi %90, %91 : i32
      llvm.store %92, %77 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %93 = llvm.mlir.constant(1 : i64) : i64
    %94 = llvm.alloca %93 x i32 : (i64) -> !llvm.ptr
    llvm.store %arg1, %94 : i32, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %95 = llvm.load %94 : !llvm.ptr -> i32
    %96 = llvm.load %77 : !llvm.ptr -> i32
    %97 = arith.cmpi sgt, %95, %96 : i32
    cf.cond_br %97, ^bb28, ^bb29
    ^bb28:
      %99 = llvm.load %94 : !llvm.ptr -> i32
      %100 = arith.constant 1 : i32
      %101 = arith.subi %99, %100 : i32
      %102 = arith.extsi %101 : i32 to i64
      %103 = llvm.getelementptr %arg0[%102] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %98 = llvm.load %103 : !llvm.ptr -> i64
      %104 = llvm.load %94 : !llvm.ptr -> i32
      %105 = arith.extsi %104 : i32 to i64
      %106 = llvm.getelementptr %arg0[%105] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %98, %106 : i64, !llvm.ptr
      %107 = llvm.load %94 : !llvm.ptr -> i32
      %108 = arith.constant 1 : i32
      %109 = arith.subi %107, %108 : i32
      llvm.store %109, %94 : i32, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %110 = llvm.load %77 : !llvm.ptr -> i32
    %111 = arith.extsi %110 : i32 to i64
    %112 = llvm.getelementptr %arg0[%111] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %arg2, %112 : i64, !llvm.ptr
    %113 = arith.constant 1 : i32
    %114 = arith.addi %arg1, %113 : i32
    func.return %114 : i32
  }
  func.func @main() -> i32 {
    %116 = arith.constant 2000 : i32
    %117 = arith.constant 8 : i32
    %118 = arith.extsi %116 : i32 to i64
    %119 = arith.extsi %117 : i32 to i64
    %115 = func.call @calloc(%118, %119) : (i64, i64) -> !llvm.ptr
    %120 = llvm.mlir.zero : !llvm.ptr
    %121 = llvm.icmp "eq" %115, %120 : !llvm.ptr
    cf.cond_br %121, ^bb30, ^bb31
    ^bb30:
      %122 = arith.constant 1 : i32
      func.return %122 : i32
    ^bb31:
      cf.br ^bb32
    ^bb32:
    %123 = arith.constant 0 : i32
    %124 = llvm.mlir.constant(1 : i64) : i64
    %125 = llvm.alloca %124 x i32 : (i64) -> !llvm.ptr
    llvm.store %123, %125 : i32, !llvm.ptr
    %126 = arith.constant 2 : i32
    %127 = arith.extsi %126 : i32 to i64
    %128 = llvm.mlir.constant(1 : i64) : i64
    %129 = llvm.alloca %128 x i64 : (i64) -> !llvm.ptr
    llvm.store %127, %129 : i64, !llvm.ptr
    cf.br ^bb33
    ^bb33:
    %130 = llvm.load %129 : !llvm.ptr -> i64
    %131 = arith.constant 200 : i32
    %133 = arith.extsi %131 : i32 to i64
    %132 = arith.cmpi sle, %130, %133 : i64
    cf.cond_br %132, ^bb34, ^bb35
    ^bb34:
      %134 = arith.constant 2 : i32
      %135 = arith.extsi %134 : i32 to i64
      %136 = llvm.mlir.constant(1 : i64) : i64
      %137 = llvm.alloca %136 x i64 : (i64) -> !llvm.ptr
      llvm.store %135, %137 : i64, !llvm.ptr
      cf.br ^bb36
      ^bb36:
      %138 = llvm.load %137 : !llvm.ptr -> i64
      %139 = arith.constant 50 : i32
      %141 = arith.extsi %139 : i32 to i64
      %140 = arith.cmpi sle, %138, %141 : i64
      cf.cond_br %140, ^bb37, ^bb38
      ^bb37:
        %143 = llvm.load %129 : !llvm.ptr -> i64
        %144 = llvm.load %137 : !llvm.ptr -> i64
        %142 = func.call @ipow(%143, %144) : (i64, i64) -> i64
        %145 = arith.constant 0 : i32
        %147 = arith.extsi %145 : i32 to i64
        %146 = arith.cmpi slt, %142, %147 : i64
        cf.cond_br %146, ^bb39, ^bb40
        ^bb39:
          cf.br ^bb38
        ^bb40:
          cf.br ^bb41
        ^bb41:
        %148 = arith.constant 10 : i32
        %150 = arith.extsi %148 : i32 to i64
        %149 = arith.cmpi sge, %142, %150 : i64
        %151 = scf.if %149 -> (i1) {
          %152 = func.call @digit_sum(%142) : (i64) -> i64
          %153 = llvm.load %129 : !llvm.ptr -> i64
          %154 = arith.cmpi eq, %152, %153 : i64
          scf.yield %154 : i1
        } else {
          %155 = arith.constant false
          scf.yield %155 : i1
        }
        cf.cond_br %151, ^bb42, ^bb43
        ^bb42:
          %157 = llvm.load %125 : !llvm.ptr -> i32
          %156 = func.call @insert_sorted(%115, %157, %142) : (!llvm.ptr, i32, i64) -> i32
          llvm.store %156, %125 : i32, !llvm.ptr
          cf.br ^bb44
        ^bb43:
          cf.br ^bb44
        ^bb44:
        %158 = llvm.load %137 : !llvm.ptr -> i64
        %159 = arith.constant 1 : i32
        %161 = arith.extsi %159 : i32 to i64
        %160 = arith.addi %158, %161 : i64
        llvm.store %160, %137 : i64, !llvm.ptr
        cf.br ^bb36
      ^bb38:
      %162 = llvm.load %129 : !llvm.ptr -> i64
      %163 = arith.constant 1 : i32
      %165 = arith.extsi %163 : i32 to i64
      %164 = arith.addi %162, %165 : i64
      llvm.store %164, %129 : i64, !llvm.ptr
      cf.br ^bb33
    ^bb35:
    %166 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %168 = arith.constant 29 : i32
    %169 = arith.extsi %168 : i32 to i64
    %170 = llvm.getelementptr %115[%169] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %167 = llvm.load %170 : !llvm.ptr -> i64
    %171 = llvm.call @printf(%166, %167) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%115) : (!llvm.ptr) -> ()
    %173 = arith.constant 0 : i32
    func.return %173 : i32
  }
}