All Euler problems
Project Euler

Non-bouncy Numbers

A positive integer is increasing if its digits are non-decreasing from left to right, decreasing if non-increasing, and bouncy otherwise. How many numbers below a googol (10^100) are not bouncy?

Source sync May 21, 2026
Problem #0113
Level Level 04
Solved By 12,575
Languages C++, Python
Answer 51161058134250
Length 454 words
sequencecombinatoricsalgebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, $134468$.

Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, $66420$.

We shall call a positive integer that is neither increasing nor decreasing a "bouncy" number; for example, $155349$.

As $n$ increases, the proportion of bouncy numbers below $n$ increases such that there are only $12951$ numbers below one-million that are not bouncy and only $277032$ non-bouncy numbers below $10^{10}$.

How many numbers below a googol ($10^{100}$) are not bouncy?

Problem 113: Non-bouncy Numbers

Mathematical Development

Theorem 1 (Counting increasing numbers). The number of positive increasing integers with at most nn digits is

I(n)=(n+99)−1.I(n) = \binom{n+9}{9} - 1.

Proof. An increasing number with at most nn digits corresponds bijectively to a non-decreasing sequence (d1,d2,…,dn)∈{0,1,…,9}n(d_1, d_2, \ldots, d_n) \in \{0, 1, \ldots, 9\}^n, where leading zeros represent shorter numbers. The number of such non-decreasing sequences is the number of multisets of size nn drawn from an alphabet of size 10. By the stars-and-bars theorem, this equals

(n+10−110−1)=(n+99).\binom{n + 10 - 1}{10 - 1} = \binom{n + 9}{9}.

This count includes the all-zeros sequence (0,0,…,0)(0, 0, \ldots, 0), which represents the integer 0, not a positive integer. Subtracting this single case yields I(n)=(n+99)−1I(n) = \binom{n+9}{9} - 1. ■\blacksquare

Theorem 2 (Counting decreasing numbers). The number of positive decreasing integers with at most nn digits is

D(n)=(n+1010)−1−n.D(n) = \binom{n+10}{10} - 1 - n.

Proof. A decreasing number of exactly kk digits is a non-increasing sequence (d1,…,dk)∈{0,…,9}k(d_1, \ldots, d_k) \in \{0, \ldots, 9\}^k with d1≥d2≥⋯≥dkd_1 \geq d_2 \geq \cdots \geq d_k and d1≥1d_1 \geq 1. By reversal, the count of non-increasing sequences of length kk from {0,…,9}\{0, \ldots, 9\} equals the count of non-decreasing sequences, which is (k+99)\binom{k+9}{9}. Exactly one of these (the all-zeros sequence) has d1=0d_1 = 0, so the count with d1≥1d_1 \geq 1 is (k+99)−1\binom{k+9}{9} - 1.

Summing over all lengths k=1,2,…,nk = 1, 2, \ldots, n:

D(n)=∑k=1n[(k+99)−1]=∑k=1n(k+99)−n.D(n) = \sum_{k=1}^{n} \left[\binom{k+9}{9} - 1\right] = \sum_{k=1}^{n} \binom{k+9}{9} - n.

By the hockey-stick (Christmas stocking) identity,

∑k=1n(k+99)=∑j=10n+9(j9)=(n+1010)−(99)=(n+1010)−1.\sum_{k=1}^{n} \binom{k+9}{9} = \sum_{j=10}^{n+9} \binom{j}{9} = \binom{n+10}{10} - \binom{9}{9} = \binom{n+10}{10} - 1.

Therefore D(n)=(n+1010)−1−nD(n) = \binom{n+10}{10} - 1 - n. ■\blacksquare

Lemma 1 (Overlap: numbers both increasing and decreasing). The number of positive integers with at most nn digits that are simultaneously increasing and decreasing is 9n9n.

Proof. A number is both increasing and decreasing if and only if all its digits are identical, i.e., it is a repdigit. For each digit length k∈{1,2,…,n}k \in \{1, 2, \ldots, n\}, there are exactly 9 positive repdigits: dd⋯d⏟k\underbrace{dd \cdots d}_k for d∈{1,2,…,9}d \in \{1, 2, \ldots, 9\}. The total is 9n9n. ■\blacksquare

Theorem 3 (Non-bouncy count by inclusion-exclusion). The number of non-bouncy positive integers below 10n10^n is

NonBouncy⁡(n)=(n+99)+(n+1010)−10n−2.\operatorname{NonBouncy}(n) = \binom{n+9}{9} + \binom{n+10}{10} - 10n - 2.

Proof. By inclusion-exclusion, the non-bouncy count equals I(n)+D(n)−Overlap(n)I(n) + D(n) - \text{Overlap}(n). Substituting:

NonBouncy⁡(n)=[(n+99)−1]+[(n+1010)−1−n]−9n=(n+99)+(n+1010)−10n−2.\begin{aligned} \operatorname{NonBouncy}(n) &= \left[\binom{n+9}{9} - 1\right] + \left[\binom{n+10}{10} - 1 - n\right] - 9n \\ &= \binom{n+9}{9} + \binom{n+10}{10} - 10n - 2. \end{aligned}

■\blacksquare

Corollary 2 (Asymptotic non-bouncy fraction). The fraction of non-bouncy numbers below 10n10^n satisfies

NonBouncy⁡(n)10n−1=O ⁣(n1010n)→0as n→∞.\frac{\operatorname{NonBouncy}(n)}{10^n - 1} = O\!\left(\frac{n^{10}}{10^n}\right) \to 0 \quad \text{as } n \to \infty.

Proof. The leading term of (n+1010)\binom{n+10}{10} is n10/10!n^{10}/10!, which is polynomial in nn. Since polynomial growth is dominated by exponential growth, the ratio tends to 0. ■\blacksquare

Editorial

This problem is purely a counting argument, so the algorithm is really just an evaluation of the closed forms proved above. Increasing numbers are counted by treating a number with at most 100 digits as a non-decreasing digit sequence with leading zeros allowed, while decreasing numbers are counted by the analogous non-increasing interpretation. Those two counts overlap exactly on the repdigits, so inclusion-exclusion finishes the job.

The implementation therefore does not enumerate any numbers at all. It computes the two binomial coefficients appearing in the formulas, subtracts the duplicated repdigits, and returns the resulting non-bouncy count below 1010010^{100}.

Pseudocode

Set n = 100.
Count the increasing numbers with the formula involving binomial(n + 9, 9).
Count the decreasing numbers with the formula involving binomial(n + 10, 10).
Subtract the repdigits that were counted in both totals.
Return the resulting number of non-bouncy integers below 10^n.

Complexity Analysis

  • Time. O(1)O(1) arithmetic operations: computing (1099)\binom{109}{9} requires 9 multiplications and 9 divisions, and (11010)\binom{110}{10} requires 10 of each. With big-integer arithmetic on numbers of O(n)O(n) digits, each operation costs O(nlog⁡n)O(n \log n) via FFT-based multiplication, giving O(nlog⁡n)O(n \log n) total. For n=100n = 100, this is negligible.
  • Space. O(1)O(1): a constant number of big integers, each with O(n)O(n) digits.

Answer

51161058134250\boxed{51161058134250}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_113/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef __int128 lll;

long long binom(int n, int k) {
    lll result = 1;
    for (int i = 0; i < k; i++)
        result = result * (n - i) / (i + 1);
    return (long long)result;
}

int main() {
    int n = 100;
    long long inc = binom(n + 9, 9) - 1;
    long long dec = binom(n + 10, 10) - 1 - n;
    long long overlap = 9LL * n;
    cout << inc + dec - overlap << endl;
    return 0;
}