Problem 092

How many starting numbers below ten million arrive at 89 in square-digit chain.

Answer8581146
Output8581146
StatusPASS
Native helperno
Runtime40 ms
Peak memory1104 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n)
Space complexityO(1)O(n)
ApproachFlow solutionSquare digit chain memoisation
VerdictSuboptimal

Flow source

# Project Euler 092
# How many starting numbers below ten million arrive at 89 in square-digit chain.

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

function sqdig(n: i32) -> i32 {
    let mut s: i32 = 0
    let mut x: i32 = n
    while x > 0 {
        let d: i32 = x % 10
        s = s + d * d
        x = x / 10
    }
    return s
}

function main() -> i32 {
    # max sqdig sum for 9999999 is 7*81=567
    let cache: ptr<i8> = calloc(600, 1)
    if cache == null { return 1 }
    # 0 = unknown, 1 = ends at 1, 2 = ends at 89
    cache[1] = 1
    cache[89] = 2

    let mut i: i32 = 2
    while i < 600 {
        if cache[i] == 0 {
            let mut x: i32 = i
            while x != 1 && x != 89 && cache[x] == 0 {
                x = sqdig(x)
            }
            let end: i8 = 1
            if x == 89 || cache[x] == 2 {
                end = 2
            }
            # fill chain
            x = i
            while x != 1 && x != 89 && cache[x] == 0 {
                cache[x] = end
                x = sqdig(x)
            }
        }
        i = i + 1
    }

    let mut count: i64 = 0
    let mut n: i32 = 1
    while n < 10000000 {
        let s: i32 = sqdig(n)
        if cache[s] == 2 {
            count = count + 1
        }
        n = n + 1
    }
    printf("%lld\n", count)
    free(cache)
    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 sqdig_i32(int32_t n);
int32_t main(void);



int32_t sqdig_i32(int32_t n) {
    int32_t s = 0;
    int32_t x = n;
    while (x > 0) {
        int32_t d = FLOW_CHECKED_MOD((x), (10));
        s = (s + (d * d));
        x = FLOW_CHECKED_DIV((x), (10));
    }
    return s;
}

int32_t main(void) {
    int8_t* cache = (int8_t*)(calloc(600, 1));
    if (cache == NULL) {
        return 1;
    }
    cache[1] = 1;
    cache[89] = 2;
    int32_t i = 2;
    while (i < 600) {
        if (cache[i] == 0) {
            int32_t x = i;
            while (((x != 1 && x != 89) && cache[x] == 0)) {
                x = sqdig_i32(x);
            }
            int8_t end = 1;
            if ((x == 89 || cache[x] == 2)) {
                end = 2;
            }
            x = i;
            while (((x != 1 && x != 89) && cache[x] == 0)) {
                cache[x] = end;
                x = sqdig_i32(x);
            }
        }
        i = (i + 1);
    }
    int64_t count = 0;
    int32_t n = 1;
    while (n < 10000000) {
        int32_t s = sqdig_i32(n);
        if (cache[s] == 2) {
            count = (count + 1);
        }
        n = (n + 1);
    }
    printf("%lld\n", count);
    free(cache);
    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 @sqdig(%arg0: i32) -> i32 {
    %0 = arith.constant 0 : 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 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i32 : (i64) -> !llvm.ptr
    llvm.store %arg0, %4 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %5 = llvm.load %4 : !llvm.ptr -> i32
    %6 = arith.constant 0 : i32
    %7 = arith.cmpi sgt, %5, %6 : i32
    cf.cond_br %7, ^bb1, ^bb2
    ^bb1:
      %8 = llvm.load %4 : !llvm.ptr -> i32
      %9 = arith.constant 10 : i32
      %10 = arith.remsi %8, %9 : i32
      %11 = llvm.load %2 : !llvm.ptr -> i32
      %12 = arith.muli %10, %10 : i32
      %13 = arith.addi %11, %12 : i32
      llvm.store %13, %2 : i32, !llvm.ptr
      %14 = llvm.load %4 : !llvm.ptr -> i32
      %15 = arith.constant 10 : i32
      %16 = arith.divsi %14, %15 : i32
      llvm.store %16, %4 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %17 = llvm.load %2 : !llvm.ptr -> i32
    func.return %17 : i32
  }
  func.func @main() -> i32 {
    %19 = arith.constant 600 : i32
    %20 = arith.constant 1 : i32
    %21 = arith.extsi %19 : i32 to i64
    %22 = arith.extsi %20 : i32 to i64
    %18 = func.call @calloc(%21, %22) : (i64, i64) -> !llvm.ptr
    %23 = llvm.mlir.zero : !llvm.ptr
    %24 = llvm.icmp "eq" %18, %23 : !llvm.ptr
    cf.cond_br %24, ^bb3, ^bb4
    ^bb3:
      %25 = arith.constant 1 : i32
      func.return %25 : i32
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %26 = arith.constant 1 : i32
    %27 = arith.constant 1 : i32
    %28 = arith.trunci %26 : i32 to i8
    %29 = arith.extsi %27 : i32 to i64
    %30 = llvm.getelementptr %18[%29] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %28, %30 : i8, !llvm.ptr
    %31 = arith.constant 2 : i32
    %32 = arith.constant 89 : i32
    %33 = arith.trunci %31 : i32 to i8
    %34 = arith.extsi %32 : i32 to i64
    %35 = llvm.getelementptr %18[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %33, %35 : i8, !llvm.ptr
    %36 = arith.constant 2 : i32
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x i32 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : i32, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %39 = llvm.load %38 : !llvm.ptr -> i32
    %40 = arith.constant 600 : i32
    %41 = arith.cmpi slt, %39, %40 : i32
    cf.cond_br %41, ^bb7, ^bb8
    ^bb7:
      %43 = llvm.load %38 : !llvm.ptr -> i32
      %44 = arith.extsi %43 : i32 to i64
      %45 = llvm.getelementptr %18[%44] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %42 = llvm.load %45 : !llvm.ptr -> i8
      %46 = arith.constant 0 : i32
      %48 = arith.extsi %42 : i8 to i32
      %47 = arith.cmpi eq, %48, %46 : i32
      cf.cond_br %47, ^bb9, ^bb10
      ^bb9:
        %49 = llvm.load %38 : !llvm.ptr -> i32
        %50 = llvm.mlir.constant(1 : i64) : i64
        %51 = llvm.alloca %50 x i32 : (i64) -> !llvm.ptr
        llvm.store %49, %51 : i32, !llvm.ptr
        cf.br ^bb12
        ^bb12:
        %52 = llvm.load %51 : !llvm.ptr -> i32
        %53 = arith.constant 1 : i32
        %54 = arith.cmpi ne, %52, %53 : i32
        %55 = scf.if %54 -> (i1) {
          %56 = llvm.load %51 : !llvm.ptr -> i32
          %57 = arith.constant 89 : i32
          %58 = arith.cmpi ne, %56, %57 : i32
          scf.yield %58 : i1
        } else {
          %59 = arith.constant false
          scf.yield %59 : i1
        }
        %60 = scf.if %55 -> (i1) {
          %62 = llvm.load %51 : !llvm.ptr -> i32
          %63 = arith.extsi %62 : i32 to i64
          %64 = llvm.getelementptr %18[%63] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          %61 = llvm.load %64 : !llvm.ptr -> i8
          %65 = arith.constant 0 : i32
          %67 = arith.extsi %61 : i8 to i32
          %66 = arith.cmpi eq, %67, %65 : i32
          scf.yield %66 : i1
        } else {
          %68 = arith.constant false
          scf.yield %68 : i1
        }
        cf.cond_br %60, ^bb13, ^bb14
        ^bb13:
          %70 = llvm.load %51 : !llvm.ptr -> i32
          %69 = func.call @sqdig(%70) : (i32) -> i32
          llvm.store %69, %51 : i32, !llvm.ptr
          cf.br ^bb12
        ^bb14:
        %71 = arith.constant 1 : i32
        %72 = arith.trunci %71 : i32 to i8
        %73 = llvm.load %51 : !llvm.ptr -> i32
        %74 = arith.constant 89 : i32
        %75 = arith.cmpi eq, %73, %74 : i32
        %76 = scf.if %75 -> (i1) {
          %77 = arith.constant true
          scf.yield %77 : i1
        } else {
          %79 = llvm.load %51 : !llvm.ptr -> i32
          %80 = arith.extsi %79 : i32 to i64
          %81 = llvm.getelementptr %18[%80] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          %78 = llvm.load %81 : !llvm.ptr -> i8
          %82 = arith.constant 2 : i32
          %84 = arith.extsi %78 : i8 to i32
          %83 = arith.cmpi eq, %84, %82 : i32
          scf.yield %83 : i1
        }
        %85 = scf.if %76 -> (i8) {
          %86 = arith.constant 2 : i32
          %87 = arith.trunci %86 : i32 to i8
          scf.yield %87 : i8
        } else {
          scf.yield %72 : i8
        }
        %88 = llvm.load %38 : !llvm.ptr -> i32
        llvm.store %88, %51 : i32, !llvm.ptr
        cf.br ^bb15
        ^bb15:
        %89 = llvm.load %51 : !llvm.ptr -> i32
        %90 = arith.constant 1 : i32
        %91 = arith.cmpi ne, %89, %90 : i32
        %92 = scf.if %91 -> (i1) {
          %93 = llvm.load %51 : !llvm.ptr -> i32
          %94 = arith.constant 89 : i32
          %95 = arith.cmpi ne, %93, %94 : i32
          scf.yield %95 : i1
        } else {
          %96 = arith.constant false
          scf.yield %96 : i1
        }
        %97 = scf.if %92 -> (i1) {
          %99 = llvm.load %51 : !llvm.ptr -> i32
          %100 = arith.extsi %99 : i32 to i64
          %101 = llvm.getelementptr %18[%100] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          %98 = llvm.load %101 : !llvm.ptr -> i8
          %102 = arith.constant 0 : i32
          %104 = arith.extsi %98 : i8 to i32
          %103 = arith.cmpi eq, %104, %102 : i32
          scf.yield %103 : i1
        } else {
          %105 = arith.constant false
          scf.yield %105 : i1
        }
        cf.cond_br %97, ^bb16, ^bb17
        ^bb16:
          %106 = llvm.load %51 : !llvm.ptr -> i32
          %107 = arith.extsi %106 : i32 to i64
          %108 = llvm.getelementptr %18[%107] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %85, %108 : i8, !llvm.ptr
          %110 = llvm.load %51 : !llvm.ptr -> i32
          %109 = func.call @sqdig(%110) : (i32) -> i32
          llvm.store %109, %51 : i32, !llvm.ptr
          cf.br ^bb15
        ^bb17:
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %111 = llvm.load %38 : !llvm.ptr -> i32
      %112 = arith.constant 1 : i32
      %113 = arith.addi %111, %112 : i32
      llvm.store %113, %38 : i32, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %114 = arith.constant 0 : i32
    %115 = arith.extsi %114 : i32 to i64
    %116 = llvm.mlir.constant(1 : i64) : i64
    %117 = llvm.alloca %116 x i64 : (i64) -> !llvm.ptr
    llvm.store %115, %117 : i64, !llvm.ptr
    %118 = arith.constant 1 : i32
    %119 = llvm.mlir.constant(1 : i64) : i64
    %120 = llvm.alloca %119 x i32 : (i64) -> !llvm.ptr
    llvm.store %118, %120 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %121 = llvm.load %120 : !llvm.ptr -> i32
    %122 = arith.constant 10000000 : i32
    %123 = arith.cmpi slt, %121, %122 : i32
    cf.cond_br %123, ^bb19, ^bb20
    ^bb19:
      %125 = llvm.load %120 : !llvm.ptr -> i32
      %124 = func.call @sqdig(%125) : (i32) -> i32
      %127 = arith.extsi %124 : i32 to i64
      %128 = llvm.getelementptr %18[%127] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %126 = llvm.load %128 : !llvm.ptr -> i8
      %129 = arith.constant 2 : i32
      %131 = arith.extsi %126 : i8 to i32
      %130 = arith.cmpi eq, %131, %129 : i32
      cf.cond_br %130, ^bb21, ^bb22
      ^bb21:
        %132 = llvm.load %117 : !llvm.ptr -> i64
        %133 = arith.constant 1 : i32
        %135 = arith.extsi %133 : i32 to i64
        %134 = arith.addi %132, %135 : i64
        llvm.store %134, %117 : i64, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %136 = llvm.load %120 : !llvm.ptr -> i32
      %137 = arith.constant 1 : i32
      %138 = arith.addi %136, %137 : i32
      llvm.store %138, %120 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %139 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %140 = llvm.load %117 : !llvm.ptr -> i64
    %141 = llvm.call @printf(%139, %140) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%18) : (!llvm.ptr) -> ()
    %143 = arith.constant 0 : i32
    func.return %143 : i32
  }
}