← Recreational maths

Lab · Metric number theory

The digits of a random number

Expand a number as a continued fraction and count its digits. For almost every real number the counts follow one law, and nobody knows a single natural constant that provably obeys it.

Every real number $x$ has a continued fraction expansion $$x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{a_3 + \cdots}}} = [a_0; a_1, a_2, a_3, \dots],$$ with $a_0 \in \mathbb{Z}$ and digits $a_1, a_2, \dots \ge 1$. The digits are produced by the Gauss map $T(x) = 1/x - \lfloor 1/x \rfloor$ on $(0,1)$: the first digit is $\lfloor 1/x \rfloor$, and the rest of the expansion is the expansion of $T(x)$. Decimal digits are evenly spread for almost every number, with each of $0, \dots, 9$ appearing a tenth of the time. Continued fraction digits are not evenly spread, but they too follow one law for almost every $x$, and the lab below lets you watch it appear.

Three laws for almost every number

Gauss–Kuzmin. For almost every $x$, the proportion of digits equal to $k$ is $$\lim_{n\to\infty} \frac{\#\{ i \le n : a_i = k \}}{n} = -\log_2\!\Big(1 - \frac{1}{(k+1)^2}\Big).$$ So about $41.5\%$ of digits are $1$, $17.0\%$ are $2$, $9.3\%$ are $3$, and large digits are rare but keep turning up: the share of digits that are at least $k$ is $\log_2\frac{k+1}{k} \approx \frac{1}{k \ln 2}$, which decays so slowly that the average digit is infinite. Gauss found this distribution around 1800, without publishing a proof; Kuzmin proved it in 1928.

Khinchin. Since the average digit is infinite, the arithmetic mean is no use, but the geometric mean settles down: for almost every $x$, $$\lim_{n\to\infty} (a_1 a_2 \cdots a_n)^{1/n} = K = \prod_{k=1}^{\infty} \Big(1 + \frac{1}{k(k+2)}\Big)^{\log_2 k} = 2.685452\ldots$$ (Khinchin, 1935).

Lévy. The convergents $p_n/q_n = [a_0; a_1, \dots, a_n]$ are the best rational approximations to $x$. Their denominators grow exponentially at a fixed rate: for almost every $x$, $$\lim_{n\to\infty} q_n^{1/n} = e^{\pi^2/(12 \ln 2)} = 3.275822\ldots$$ (Lévy, 1936). Each digit therefore buys about $\pi^2/(6 \ln 2 \ln 10) \approx 1.03$ decimal places of accuracy.

Why these laws hold

The digits of $x$ are the values of the function $a(x) = \lfloor 1/x \rfloor$ along the orbit $x, T(x), T^2(x), \dots$. Gauss noticed that the probability measure $$\mu(A) = \frac{1}{\ln 2} \int_A \frac{dx}{1+x}$$ is invariant under $T$, and $T$ is ergodic for it. Birkhoff's ergodic theorem then says that for almost every $x$, time averages along the orbit equal space averages. Counting digits equal to $k$ is the time average of the indicator of $\big(\tfrac{1}{k+1}, \tfrac1k\big]$, which gives Gauss–Kuzmin. Averaging $\log a$ gives Khinchin. Lévy's law comes the same way from $\log q_n = -\sum_{i

Full proof: the Gauss measure is invariant, and the digit law follows

Lemma 1. For every $t \in [0,1]$, $\mu\big(T^{-1}[0,t]\big) = \mu([0,t])$.

Proof. We have $T(x) \le t$ exactly when $1/x \in [k, k+t]$ for some integer $k \ge 1$, that is $x \in \big[\tfrac{1}{k+t}, \tfrac{1}{k}\big]$. These intervals are disjoint, so $$\ln 2 \cdot \mu\big(T^{-1}[0,t]\big) = \sum_{k \ge 1} \Big[\ln\Big(1 + \frac1k\Big) - \ln\Big(1 + \frac{1}{k+t}\Big)\Big] = \sum_{k\ge1} \Big[\ln\frac{k+1}{k} + \ln\frac{k+t}{k+t+1}\Big].$$ Both sums telescope: the first $N$ terms add up to $\ln(N+1) + \ln\frac{1+t}{N+1+t} = \ln\frac{(N+1)(1+t)}{N+1+t}$, which tends to $\ln(1+t)$. And $\ln 2 \cdot \mu([0,t]) = \ln(1+t)$. $\square$

Intervals $[0,t]$ generate the Borel sets, so $\mu(T^{-1}A) = \mu(A)$ for every Borel set $A$.

Lemma 2. $\mu\{x : a_1(x) = k\} = -\log_2\!\big(1 - \frac{1}{(k+1)^2}\big)$.

Proof. $a_1(x) = k$ exactly when $x \in \big(\tfrac{1}{k+1}, \tfrac1k\big]$, and $$\mu\Big(\Big(\tfrac{1}{k+1}, \tfrac1k\Big]\Big) = \log_2 \frac{1 + 1/k}{1 + 1/(k+1)} = \log_2 \frac{(k+1)^2}{k(k+2)} = -\log_2\Big(1 - \frac{1}{(k+1)^2}\Big). \qquad \square$$

Theorem. For Lebesgue-almost every $x$, the proportion of $i \le n$ with $a_i(x) = k$ tends to $-\log_2\big(1 - \frac{1}{(k+1)^2}\big)$.

Proof. Since $a_{i+1}(x) = a_1(T^i x)$, the proportion is $\frac1n \sum_{i

Taking $f = \log a_1$ instead, which is $\mu$-integrable, gives Khinchin's constant as $\exp \int \log a_1 \, d\mu = \prod_k \big(\frac{(k+1)^2}{k(k+2)}\big)^{\log_2 k}$.

The numbers you can try

The laws hold for almost every $x$, yet it is very hard to prove them for any particular number.

  • A random number. The applet builds one from 6000 random bits and expands it with exact integer arithmetic, so every digit shown is correct.
  • $\pi$ looks typical: $[3; 7, 15, 1, 292, 1, 1, 1, 2, \dots]$. But nobody can prove it satisfies Khinchin's law, or even that its digits are unbounded.
  • $\sqrt[3]{2}$ also looks typical. It is not known whether the digits of any algebraic number of degree at least $3$ are bounded.
  • $e$ follows Euler's pattern $[2; 1, 2, 1, 1, 4, 1, 1, 6, \dots]$, so its geometric mean grows without bound.
  • $\sqrt2$ and the golden ratio have periodic expansions, as every quadratic irrational does (Euler and Lagrange). The golden ratio, with every digit equal to $1$, is the number hardest to approximate by fractions.
  • Your own decimal, such as the Euler–Mascheroni constant. A decimal with $d$ places fixes only roughly the first $d$ digits, and the applet shows exactly as many as the decimal determines.

Further reading

  • A. Ya. Khinchin, Continued Fractions, Dover, 1997 (translation of the 1935 Russian edition).
  • M. Einsiedler and T. Ward, Ergodic Theory with a view towards Number Theory, Springer GTM 259, 2011, Chapter 3.
  • D. Bailey, J. Borwein and R. Crandall, On the Khintchine constant, Mathematics of Computation 66 (1997).