Problem 113

How many numbers below a googol (10^100) are not bouncy? Non-bouncy = increasing + decreasing - both (flat numbers counted twice). Increasing (non-decreasing) d-digit with digits 1-9: C(9+d-1,d) wait digits can repeat: Combinations with repetition: C(9+d-1, d) for digits 1-9? Actually 0 not leading — Non-decreasing positive: choose multisets from 1-9 of size 1..100: sum_d C(9+d-1,d) = C(9+100,100)-1? Actually: numbers with non-decreasing digits = nonempty multisets of {1..9}: 2^9 - 1 = 511 total of any length ≤9 digits max unique... with repetition: for ≤100 digits: C(9+100, 9) - 1. Standard: count of non-decreasing ≤ 10^n - 1 is C(n+9,9)-1? For n digits max: sum_{k=1}^{n} C(k+8,8) = C(n+9,9)-1.

Answer51161058134250
Output51161058134250
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(1) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 113
# How many numbers below a googol (10^100) are not bouncy?

# Non-bouncy = increasing + decreasing - both (flat numbers counted twice).
# Increasing (non-decreasing) d-digit with digits 1-9: C(9+d-1,d) wait digits can repeat:
# Combinations with repetition: C(9+d-1, d) for digits 1-9? Actually 0 not leading —
# Non-decreasing positive: choose multisets from 1-9 of size 1..100: sum_d C(9+d-1,d) = C(9+100,100)-1? 
# Actually: numbers with non-decreasing digits = nonempty multisets of {1..9}: 2^9 - 1 = 511 total of any length ≤9 digits max unique... with repetition: for ≤100 digits: C(9+100, 9) - 1.
# Standard: count of non-decreasing ≤ 10^n - 1 is C(n+9,9)-1? 
# For n digits max: sum_{k=1}^{n} C(k+8,8) = C(n+9,9)-1.

function comb(n: i64, k: i64) -> i64 {
    if k < 0 || k > n { return 0 }
    if k > n - k { k = n - k }
    let mut r: i64 = 1
    let mut i: i64 = 1
    while i <= k {
        r = r * (n - k + i) / i
        i = i + 1
    }
    return r
}

function main() -> i32 {
    let n: i64 = 100
    # increasing (non-decreasing digits 1-9): C(n+9,9)-1
    let inc: i64 = comb(n + 9, 9) - 1
    # decreasing: digits 0-9 with leading zeros representing shorter, exclude all-zero,
    # but flat numbers like 111 counted in both — and 0-padded means C(n+10,10)-1 total
    # "decreasing" including those with 0: C(n+10,10)-1, but exclude numbers with leading zeros
    # which are already shorter decreasing numbers. Standard result:
    # decreasing count (incl flats, excl 0) = C(n+10,10) - 1 - n
    # because C(n+10,10)-1 counts multisets of size ≤n from 0-9 nonempty, minus the n pure-zero-padded? 
    # Actually: combinations with repetition from 0-9 of length exactly allowing leading zeros =
    # for numbers with at most n digits that are non-increasing: C(n+10,10)-1 (nonempty), 
    # but this double-counts nothing yet; numbers like 000... are excluded by -1 for empty.
    # All-zero representations: there are none of positive value.
    # However 10...0 style: multiset {0,1} → "100..0" sorted decreasing "100..0" ok.
    # Known formula for non-bouncy below 10^n:
    # inc + dec - flats = (C(n+9,9)-1) + (C(n+10,10)-1-n) - 9*n
    # flats: 9 per digit length (1,2,..9 and 11,22,..99 etc) = 9*n
    let dec: i64 = comb(n + 10, 10) - 1 - n
    let flats: i64 = 9 * n
    let ans: i64 = inc + dec - flats
    printf("%lld\n", ans)
    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 comb_i64_i64(int64_t n, int64_t k);
int32_t main(void);

int64_t comb_i64_i64(int64_t n, int64_t k) {
    if ((k < 0 || k > n)) {
        return 0;
    }
    if (k > (n - k)) {
        k = (n - k);
    }
    int64_t r = 1;
    int64_t i = 1;
    while (i <= k) {
        r = FLOW_CHECKED_DIV(((r * ((n - k) + i))), (i));
        i = (i + 1);
    }
    return r;
}

int32_t main(void) {
    int64_t n = 100;
    int64_t inc = (comb_i64_i64((n + 9), 9) - 1);
    int64_t dec = ((comb_i64_i64((n + 10), 10) - 1) - n);
    int64_t flats = (9 * n);
    int64_t ans = ((inc + dec) - flats);
    printf("%lld\n", ans);
    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 @comb(%arg0: i64, %arg1: i64) -> i64 {
    %0 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi slt, %arg1, %2 : i64
    %3 = scf.if %1 -> (i1) {
      %4 = arith.constant true
      scf.yield %4 : i1
    } else {
      %5 = arith.cmpi sgt, %arg1, %arg0 : i64
      scf.yield %5 : i1
    }
    cf.cond_br %3, ^bb0, ^bb1
    ^bb0:
      %6 = arith.constant 0 : i32
      %7 = arith.extsi %6 : i32 to i64
      func.return %7 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %8 = arith.subi %arg0, %arg1 : i64
    %9 = arith.cmpi sgt, %arg1, %8 : i64
    %10 = scf.if %9 -> (i64) {
      %11 = arith.subi %arg0, %arg1 : i64
      scf.yield %11 : i64
    } else {
      scf.yield %arg1 : i64
    }
    %12 = arith.constant 1 : i32
    %13 = arith.extsi %12 : i32 to i64
    %14 = llvm.mlir.constant(1 : i64) : i64
    %15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
    llvm.store %13, %15 : i64, !llvm.ptr
    %16 = arith.constant 1 : i32
    %17 = arith.extsi %16 : i32 to i64
    %18 = llvm.mlir.constant(1 : i64) : i64
    %19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
    llvm.store %17, %19 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %20 = llvm.load %19 : !llvm.ptr -> i64
    %21 = arith.cmpi sle, %20, %10 : i64
    cf.cond_br %21, ^bb4, ^bb5
    ^bb4:
      %22 = llvm.load %15 : !llvm.ptr -> i64
      %23 = arith.subi %arg0, %10 : i64
      %24 = llvm.load %19 : !llvm.ptr -> i64
      %25 = arith.addi %23, %24 : i64
      %26 = arith.muli %22, %25 : i64
      %27 = llvm.load %19 : !llvm.ptr -> i64
      %28 = arith.divsi %26, %27 : i64
      llvm.store %28, %15 : i64, !llvm.ptr
      %29 = llvm.load %19 : !llvm.ptr -> i64
      %30 = arith.constant 1 : i32
      %32 = arith.extsi %30 : i32 to i64
      %31 = arith.addi %29, %32 : i64
      llvm.store %31, %19 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %33 = llvm.load %15 : !llvm.ptr -> i64
    func.return %33 : i64
  }
  func.func @main() -> i32 {
    %34 = arith.constant 100 : i32
    %35 = arith.extsi %34 : i32 to i64
    %37 = arith.constant 9 : i32
    %39 = arith.extsi %37 : i32 to i64
    %38 = arith.addi %35, %39 : i64
    %40 = arith.constant 9 : i32
    %41 = arith.extsi %40 : i32 to i64
    %36 = func.call @comb(%38, %41) : (i64, i64) -> i64
    %42 = arith.constant 1 : i32
    %44 = arith.extsi %42 : i32 to i64
    %43 = arith.subi %36, %44 : i64
    %46 = arith.constant 10 : i32
    %48 = arith.extsi %46 : i32 to i64
    %47 = arith.addi %35, %48 : i64
    %49 = arith.constant 10 : i32
    %50 = arith.extsi %49 : i32 to i64
    %45 = func.call @comb(%47, %50) : (i64, i64) -> i64
    %51 = arith.constant 1 : i32
    %53 = arith.extsi %51 : i32 to i64
    %52 = arith.subi %45, %53 : i64
    %54 = arith.subi %52, %35 : i64
    %55 = arith.constant 9 : i32
    %57 = arith.extsi %55 : i32 to i64
    %56 = arith.muli %57, %35 : i64
    %58 = arith.addi %43, %54 : i64
    %59 = arith.subi %58, %56 : i64
    %60 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %61 = llvm.call @printf(%60, %59) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %62 = arith.constant 0 : i32
    func.return %62 : i32
  }
}