Problem 074

How many chains starting below one million contain exactly sixty non-repeating terms?

Answer402
Output402
StatusPASS
Native helperno
Runtime420 ms
Peak memory1104 KB
Time complexityO(n^3) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 074
# How many chains starting below one million contain exactly sixty non-repeating terms?

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

function fact_digit_sum(n0: i64, fact: ptr<i64>) -> i64 {
    if n0 == 0 { return fact[0] }
    let mut n: i64 = n0
    let mut s: i64 = 0
    while n > 0 {
        s = s + fact[(n % 10) as i32]
        n = n / 10
    }
    return s
}

function main() -> i32 {
    let fact: ptr<i64> = calloc(10, 8)
    if fact == null { return 1 }
    fact[0] = 1
    let mut i: i32 = 1
    while i < 10 {
        fact[i] = fact[i - 1] * (i as i64)
        i = i + 1
    }

    let seen: ptr<i64> = calloc(80, 8)
    if seen == null { return 1 }

    let mut count: i64 = 0
    let mut start: i64 = 0
    while start < 1000000 {
        let mut n: i64 = start
        let mut len: i32 = 0
        while true {
            let mut j: i32 = 0
            let mut looped: bool = false
            while j < len {
                if seen[j] == n {
                    looped = true
                    break
                }
                j = j + 1
            }
            if looped { break }
            seen[len] = n
            len = len + 1
            if len > 60 { break }
            n = fact_digit_sum(n, fact)
        }
        if len == 60 {
            count = count + 1
        }
        start = start + 1
    }
    printf("%lld\n", count)
    free(seen)
    free(fact)
    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 fact_digit_sum_i64_ptr_i64(int64_t n0, int64_t* fact);
int32_t main(void);



int64_t fact_digit_sum_i64_ptr_i64(int64_t n0, int64_t* fact) {
    if (n0 == 0) {
        return fact[0];
    }
    int64_t n = n0;
    int64_t s = 0;
    while (n > 0) {
        s = (s + fact[((int32_t)(FLOW_CHECKED_MOD((n), (10))))]);
        n = FLOW_CHECKED_DIV((n), (10));
    }
    return s;
}

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