Problem 164

20-digit numbers where no 3 consecutive digits sum to more than 9.

Answer378158756814587
Output378158756814587
StatusPASS
Native helperno
Runtime0 ms
Peak memory1088 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n * d * s)
Space complexityO(n)O(d * s)
ApproachFlow solutionDigit DP
VerdictUnknown

Flow source

# Project Euler 164
# 20-digit numbers where no 3 consecutive digits sum to more than 9.

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

function digit_sum3(n: i32) -> i32 {
    return (n / 100) + ((n / 10) % 10) + (n % 10)
}

function main() -> i32 {
    let inner: i32 = 1000
    let prev: ptr<i64> = calloc(inner as i64, 8)
    let cur: ptr<i64> = calloc(inner as i64, 8)
    if prev == null || cur == null { return 1 }
    prev[0] = 1

    let mut last0: i64 = 0
    let mut prev0: i64 = 0
    let mut d: i32 = 1
    while d <= 23 {
        let mut prefix: i32 = 0
        while prefix < inner {
            let mut s: i64 = 0
            if digit_sum3(prefix) <= 9 {
                let mut nd: i32 = 0
                while nd < 10 {
                    let p: i32 = (prefix % 100) * 10 + nd
                    s = s + prev[p]
                    nd = nd + 1
                }
            }
            cur[prefix] = s
            prefix = prefix + 1
        }
        prev0 = last0
        last0 = cur[0]
        let mut i: i32 = 0
        while i < inner {
            prev[i] = cur[i]
            i = i + 1
        }
        d = d + 1
    }
    printf("%lld\n", last0 - prev0)
    free(prev)
    free(cur)
    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 digit_sum3_i32(int32_t n);
int32_t main(void);



int32_t digit_sum3_i32(int32_t n) {
    return ((FLOW_CHECKED_DIV((n), (100)) + FLOW_CHECKED_MOD((FLOW_CHECKED_DIV((n), (10))), (10))) + FLOW_CHECKED_MOD((n), (10)));
}

int32_t main(void) {
    int32_t inner = 1000;
    int64_t* prev = (int64_t*)(calloc(((int64_t)(inner)), 8));
    int64_t* cur = (int64_t*)(calloc(((int64_t)(inner)), 8));
    if ((prev == NULL || cur == NULL)) {
        return 1;
    }
    prev[0] = 1;
    int64_t last0 = 0;
    int64_t prev0 = 0;
    int32_t d = 1;
    while (d <= 23) {
        int32_t prefix = 0;
        while (prefix < inner) {
            int64_t s = 0;
            if (digit_sum3_i32(prefix) <= 9) {
                int32_t nd = 0;
                while (nd < 10) {
                    int32_t p = ((FLOW_CHECKED_MOD((prefix), (100)) * 10) + nd);
                    s = (s + prev[p]);
                    nd = (nd + 1);
                }
            }
            cur[prefix] = s;
            prefix = (prefix + 1);
        }
        prev0 = last0;
        last0 = cur[0];
        int32_t i = 0;
        while (i < inner) {
            prev[i] = cur[i];
            i = (i + 1);
        }
        d = (d + 1);
    }
    printf("%lld\n", (last0 - prev0));
    free(prev);
    free(cur);
    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_sum3(%arg0: i32) -> i32 {
    %0 = arith.constant 100 : i32
    %1 = arith.divsi %arg0, %0 : i32
    %2 = arith.constant 10 : i32
    %3 = arith.divsi %arg0, %2 : i32
    %4 = arith.constant 10 : i32
    %5 = arith.remsi %3, %4 : i32
    %6 = arith.addi %1, %5 : i32
    %7 = arith.constant 10 : i32
    %8 = arith.remsi %arg0, %7 : i32
    %9 = arith.addi %6, %8 : i32
    func.return %9 : i32
  }
  func.func @main() -> i32 {
    %10 = arith.constant 1000 : i32
    %12 = arith.extsi %10 : i32 to i64
    %13 = arith.constant 8 : i32
    %14 = arith.extsi %13 : i32 to i64
    %11 = func.call @calloc(%12, %14) : (i64, i64) -> !llvm.ptr
    %16 = arith.extsi %10 : i32 to i64
    %17 = arith.constant 8 : i32
    %18 = arith.extsi %17 : i32 to i64
    %15 = func.call @calloc(%16, %18) : (i64, i64) -> !llvm.ptr
    %19 = llvm.mlir.zero : !llvm.ptr
    %20 = llvm.icmp "eq" %11, %19 : !llvm.ptr
    %21 = scf.if %20 -> (i1) {
      %22 = arith.constant true
      scf.yield %22 : i1
    } else {
      %23 = llvm.mlir.zero : !llvm.ptr
      %24 = llvm.icmp "eq" %15, %23 : !llvm.ptr
      scf.yield %24 : i1
    }
    cf.cond_br %21, ^bb0, ^bb1
    ^bb0:
      %25 = arith.constant 1 : i32
      func.return %25 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %26 = arith.constant 1 : i32
    %27 = arith.constant 0 : i32
    %28 = arith.extsi %26 : i32 to i64
    %29 = arith.extsi %27 : i32 to i64
    %30 = llvm.getelementptr %11[%29] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %28, %30 : i64, !llvm.ptr
    %31 = arith.constant 0 : i32
    %32 = arith.extsi %31 : i32 to i64
    %33 = llvm.mlir.constant(1 : i64) : i64
    %34 = llvm.alloca %33 x i64 : (i64) -> !llvm.ptr
    llvm.store %32, %34 : i64, !llvm.ptr
    %35 = arith.constant 0 : i32
    %36 = arith.extsi %35 : i32 to i64
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : i64, !llvm.ptr
    %39 = arith.constant 1 : i32
    %40 = llvm.mlir.constant(1 : i64) : i64
    %41 = llvm.alloca %40 x i32 : (i64) -> !llvm.ptr
    llvm.store %39, %41 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %42 = llvm.load %41 : !llvm.ptr -> i32
    %43 = arith.constant 23 : i32
    %44 = arith.cmpi sle, %42, %43 : i32
    cf.cond_br %44, ^bb4, ^bb5
    ^bb4:
      %45 = arith.constant 0 : 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 ^bb6
      ^bb6:
      %48 = llvm.load %47 : !llvm.ptr -> i32
      %49 = arith.cmpi slt, %48, %10 : i32
      cf.cond_br %49, ^bb7, ^bb8
      ^bb7:
        %50 = arith.constant 0 : i32
        %51 = arith.extsi %50 : i32 to i64
        %52 = llvm.mlir.constant(1 : i64) : i64
        %53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
        llvm.store %51, %53 : i64, !llvm.ptr
        %55 = llvm.load %47 : !llvm.ptr -> i32
        %54 = func.call @digit_sum3(%55) : (i32) -> i32
        %56 = arith.constant 9 : i32
        %57 = arith.cmpi sle, %54, %56 : i32
        cf.cond_br %57, ^bb9, ^bb10
        ^bb9:
          %58 = arith.constant 0 : i32
          %59 = llvm.mlir.constant(1 : i64) : i64
          %60 = llvm.alloca %59 x i32 : (i64) -> !llvm.ptr
          llvm.store %58, %60 : i32, !llvm.ptr
          cf.br ^bb12
          ^bb12:
          %61 = llvm.load %60 : !llvm.ptr -> i32
          %62 = arith.constant 10 : i32
          %63 = arith.cmpi slt, %61, %62 : i32
          cf.cond_br %63, ^bb13, ^bb14
          ^bb13:
            %64 = llvm.load %47 : !llvm.ptr -> i32
            %65 = arith.constant 100 : i32
            %66 = arith.remsi %64, %65 : i32
            %67 = arith.constant 10 : i32
            %68 = arith.muli %66, %67 : i32
            %69 = llvm.load %60 : !llvm.ptr -> i32
            %70 = arith.addi %68, %69 : i32
            %71 = llvm.load %53 : !llvm.ptr -> i64
            %73 = arith.extsi %70 : i32 to i64
            %74 = llvm.getelementptr %11[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %72 = llvm.load %74 : !llvm.ptr -> i64
            %75 = arith.addi %71, %72 : i64
            llvm.store %75, %53 : i64, !llvm.ptr
            %76 = llvm.load %60 : !llvm.ptr -> i32
            %77 = arith.constant 1 : i32
            %78 = arith.addi %76, %77 : i32
            llvm.store %78, %60 : i32, !llvm.ptr
            cf.br ^bb12
          ^bb14:
          cf.br ^bb11
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %79 = llvm.load %53 : !llvm.ptr -> i64
        %80 = llvm.load %47 : !llvm.ptr -> i32
        %81 = arith.extsi %80 : i32 to i64
        %82 = llvm.getelementptr %15[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %79, %82 : i64, !llvm.ptr
        %83 = llvm.load %47 : !llvm.ptr -> i32
        %84 = arith.constant 1 : i32
        %85 = arith.addi %83, %84 : i32
        llvm.store %85, %47 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %86 = llvm.load %34 : !llvm.ptr -> i64
      llvm.store %86, %38 : i64, !llvm.ptr
      %88 = arith.constant 0 : i32
      %89 = arith.extsi %88 : i32 to i64
      %90 = llvm.getelementptr %15[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %87 = llvm.load %90 : !llvm.ptr -> i64
      llvm.store %87, %34 : i64, !llvm.ptr
      %91 = arith.constant 0 : i32
      %92 = llvm.mlir.constant(1 : i64) : i64
      %93 = llvm.alloca %92 x i32 : (i64) -> !llvm.ptr
      llvm.store %91, %93 : i32, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %94 = llvm.load %93 : !llvm.ptr -> i32
      %95 = arith.cmpi slt, %94, %10 : i32
      cf.cond_br %95, ^bb16, ^bb17
      ^bb16:
        %97 = llvm.load %93 : !llvm.ptr -> i32
        %98 = arith.extsi %97 : i32 to i64
        %99 = llvm.getelementptr %15[%98] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %96 = llvm.load %99 : !llvm.ptr -> i64
        %100 = llvm.load %93 : !llvm.ptr -> i32
        %101 = arith.extsi %100 : i32 to i64
        %102 = llvm.getelementptr %11[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %96, %102 : i64, !llvm.ptr
        %103 = llvm.load %93 : !llvm.ptr -> i32
        %104 = arith.constant 1 : i32
        %105 = arith.addi %103, %104 : i32
        llvm.store %105, %93 : i32, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %106 = llvm.load %41 : !llvm.ptr -> i32
      %107 = arith.constant 1 : i32
      %108 = arith.addi %106, %107 : i32
      llvm.store %108, %41 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %109 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %110 = llvm.load %34 : !llvm.ptr -> i64
    %111 = llvm.load %38 : !llvm.ptr -> i64
    %112 = arith.subi %110, %111 : i64
    %113 = llvm.call @printf(%109, %112) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%11) : (!llvm.ptr) -> ()
    func.call @free(%15) : (!llvm.ptr) -> ()
    %116 = arith.constant 0 : i32
    func.return %116 : i32
  }
}