---
title: Countable and uncountable sets
heading: Listing the rationals, and why the reals can't be listed
eyebrow: A14 · Lesson 2 · Countability
xp: 40
---

::: hook
The rationals are packed everywhere along the number line: between any two reals there are infinitely many. Yet they can be listed one after another, first, second, third, without missing any. The reals can't: every list of real numbers misses most of them. These two facts, both due to Cantor, explain why a computer that works with finitely many numbers can't represent almost any real exactly, why the rationals take up no length at all, and why the completeness of lesson 1 is a genuinely strong property.
:::

::: play
- The curves count fractions $\frac pq$ in (0, 1] with denominator at most N: all of them (repeats like $\frac24$ included), and only those in lowest terms, which number about $\frac{3N^2}{\pi^2}$.
- Every rational in (0, 1] shows up once N reaches its denominator, so listing by denominator, then numerator, eventually reaches each one.
- Slide N: the counts grow like $N^2$, which is fast but finite at every stage. That's what makes the list possible.
:::

```sim plot
view: graph
x: N = 1..200
y: count = 0..21000
slider: n = 1..200 step 1 = 50
plot: N*(N + 1)/2 | label: all fractions p/q with q ≤ N
plot: 3*N^2/pi^2 | label: those in lowest terms | color: ch3
readout: about this many in lowest terms at the slider = 3*n^2/pi^2 | digits: 4
```

::: predict
## Which of these sets can be listed as a sequence $a_1, a_2, a_3, \ldots$ that includes every element?

- [ ] The real numbers in [0, 1]
- [x] The rational numbers
- [ ] All infinite sequences of 0s and 1s
- [ ] None of these

::: reveal
Only the rationals. The real numbers in [0, 1] and the infinite binary sequences are both uncountable: Cantor's diagonal argument builds, from any proposed list, an element the list misses. The rationals, dense as they are, can be listed by denominator.
:::
:::

::: nameit
## Comparing sizes

Two sets have the same cardinality if there's a bijection between them. A set is **countable** if it's finite or in bijection with $\mathbb N$, that is, if its elements can be listed as a sequence. Otherwise it's **uncountable**.

## Countable sets

- The integers: 0, 1, −1, 2, −2, 3, … .
- Pairs of natural numbers: walk the diagonals $m + n = 0, 1, 2, \ldots$. The **Cantor pairing** $\pi(m, n) = \frac{(m + n)(m + n + 1)}{2} + n$ is a bijection $\mathbb N^2 \to \mathbb N$ (with 0 counted as natural).
- A countable union of countable sets is countable. So the rationals, the finite strings over any finite alphabet, the polynomials with integer coefficients, and their roots (the algebraic numbers) are all countable.

## The reals are uncountable

**Diagonal argument.** Suppose $x_1, x_2, \ldots$ lists every number in [0, 1], written in binary. Build y by making its k-th digit differ from the k-th digit of $x_k$. Then y differs from every $x_k$ in at least one digit, so it isn't on the list. (Care is needed with numbers that have two expansions, like $0.0111\ldots = 0.1000\ldots$, but it doesn't change the conclusion.)

## What follows

- Almost every real number is transcendental and irrational: the algebraic numbers and the rationals are countable leftovers.
- Programs are finite strings, so there are countably many; most real numbers can't be computed by any program at all.
- A countable set has length zero: cover its k-th point by an interval of length $\varepsilon/2^k$, total length ε, for any ε > 0 (lesson 46 builds measure on this idea).
- Floating-point formats are finite, so arithmetic on a computer happens on a finite set of rationals.
:::

::: example
## Find the Cantor pairing of (2, 3), and invert the value 18.

::: solution
1. **Pair (2, 3).**
   $$\begin{aligned} \pi(2, 3) &= \frac{5 \times 6}{2} + 3 \\ &= 18 \end{aligned}$$
2. **Find the diagonal of 18.** The largest w with $\frac{w(w + 1)}{2} \le 18$ is w = 5, since
   $$15 \le 18 < 21$$
3. **Read off the pair.**
   $$\begin{aligned} n &= 18 - 15 \\ &= 3 \\ m &= w - n \\ &= 2 \end{aligned}$$
4. **Why it matters.** Every natural number comes from exactly one pair, which is what makes $\mathbb N \times \mathbb N$ countable, and with it the rationals.

**Answer:** $\pi(2, 3) = 18$, and 18 inverts back to the pair (2, 3).
:::
:::

::: example
## Someone lists infinite binary sequences: $x_1 = 0101\ldots$, $x_2 = 1100\ldots$, $x_3 = 0011\ldots$, $x_4 = 1110\ldots$. Build a sequence that isn't on the list.

::: solution
1. **Take the diagonal digits.** The 1st digit of $x_1$ is 0, the 2nd of $x_2$ is 1, the 3rd of $x_3$ is 1, and the 4th of $x_4$ is 0.
2. **Flip each.** y starts 1 0 0 1 … .
3. **y is not on the list.** y differs from $x_1$ in digit 1, from $x_2$ in digit 2, and so on: it can't equal any $x_k$, because it disagrees with $x_k$ in digit k.
4. **No list is complete.** Since this works for every list, no list contains all binary sequences, and so none contains all the reals in [0, 1].

**Answer:** y = 1 0 0 1 …, the flipped diagonal, differs from every sequence on the list.
:::
:::

::: example
## How many double-precision numbers lie in [1, 2), and what fraction of the reals there do they represent exactly?

::: solution
1. **Count the doubles.** Doubles in [1, 2) have the form $1.b_1b_2\ldots b_{52}$ in binary, so there are
   $$2^{52} \approx 4.5 \times 10^{15}$$
   of them. That's a lot, but finite.
2. **Compare with the reals.** The reals in [1, 2) are uncountable, and even the rationals there are infinite, so the doubles represent a fraction zero of them: a randomly chosen real is never exactly a double.
3. **What it means in practice.** Everything a computer calculates is an approximation from a finite set, which is why error analysis (A13) matters.

**Answer:** there are $2^{52} \approx 4.5 \times 10^{15}$ doubles in [1, 2), a fraction zero of the reals there.
:::
:::

::: check
given: m = 2 | 5; n = 3 | 7
answer: (m + n)*(m + n + 1)/2 + n
tolerance: 0

## What is the Cantor pairing $\pi(\given{m}, \given{n})$?

::: right
Right. The pairs on earlier diagonals come first, then n more on this one.
:::

::: misses
- [(m + n)*(m + n + 1)/2 + m] The formula adds n, the second coordinate.
- [any] $\frac{(m + n)(m + n + 1)}{2} + n$.
:::
:::

::: check
kind: parts
given: k = 18 | 25 | 40

## Invert the Cantor pairing: which (m, n) has $\pi(m, n)$ = {k}?

::: part
answer: k - floor((sqrt(8*k + 1) - 1)/2)*(floor((sqrt(8*k + 1) - 1)/2) + 1)/2
tolerance: 0

### What is n?

::: misses
- [any] Find the largest w with $\frac{w(w + 1)}{2} \le k$; then $n = k - \frac{w(w + 1)}{2}$.
:::
:::

::: part
answer: floor((sqrt(8*k + 1) - 1)/2) - (k - floor((sqrt(8*k + 1) - 1)/2)*(floor((sqrt(8*k + 1) - 1)/2) + 1)/2)
tolerance: 0

### What is m?

::: misses
- [any] $m = w - n$.
:::
:::
:::

::: check
given: k = 3 | 10 | 50
answer: 2*k + 1
tolerance: 0

## The integers are listed as 0, 1, −1, 2, −2, 3, −3, … . In what position is −{k}, counting 0 as position 1?

::: right
Right. k sits at position 2k and −k right after it.
:::

::: misses
- [2*k] That's +k.
- [any] Each positive number and its negative take two consecutive places.
:::
:::

::: check
given: n = 8 | 16 | 32
answer: 2^(n + 1) - 1
tolerance: 0

## How many binary strings have length at most {n} (counting the empty string)?

::: right
Right. $1 + 2 + 4 + \cdots + 2^n = 2^{n+1} - 1$: finitely many of each length, so the strings of all finite lengths are countable.
:::

::: misses
- [2^n] Include the shorter strings too.
- [any] Sum $2^\ell$ over lengths $\ell = 0, \ldots, n$.
:::
:::

::: check
given: p = 11 | 24 | 53
answer: 2^(p - 1)
tolerance: 0.01%

## A format has p = {p} significant bits. How many of its numbers lie in [1, 2)?

::: right
Right. $2^{p-1}$: the leading bit is fixed and the rest are free.
:::

::: misses
- [2^p] The leading 1 isn't free.
- [any] $p - 1$ free bits after the leading 1.
:::
:::

::: check
given: e = 0.1 | 0.05 | 0.01 | 0.005 | 0.001 | 0.0001
answer: e/2^10
tolerance: 0.5%

## The rationals in [0, 1] are listed, and the k-th is covered by an interval of length $\given{e}/2^k$. How long is the interval around the 10th one?

::: right
Right. The lengths add up to at most {e}, however many rationals there are.
:::

::: misses
- [e/10] The lengths halve each time: $\varepsilon/2^k$.
- [any] $\varepsilon/2^{10}$.
:::
:::

::: check
kind: choice

## Which of these sets is uncountable?

- [ ] The algebraic numbers (roots of integer polynomials)
- [ ] All finite strings of letters
- [x] The irrational numbers
- [ ] The points with rational coordinates in the plane

::: right
Right. If the irrationals were countable, the reals would be a union of two countable sets, hence countable, which they aren't.
:::

::: misses
- [1] Integer polynomials are countable and each has finitely many roots.
- [2] Finitely many strings of each length, countably many lengths.
- [4] That's $\mathbb Q \times \mathbb Q$, countable like $\mathbb N \times \mathbb N$.
:::
:::

::: check
kind: derivation
vars: m = 0..20, n = 0..20, w = 1..40

## The Cantor pairing walks the diagonals m + n = 0, 1, 2, … in order.

::: step
answer: w*(w + 1)/2

### How many pairs lie on the diagonals before diagonal w?
:::

::: step
answer: (m + n)*(m + n + 1)/2 + n

### So what position (counting from 0) does (m, n) get, if pairs on a diagonal are ordered by n?
:::

::: right
Right. Diagonal $w = m + n$ starts at $\frac{w(w + 1)}{2}$, and (m, n) is n steps along it.
:::
:::

::: explain
## Why doesn't the diagonal argument also prove the rationals uncountable?

::: model
Applied to a list of all rationals in [0, 1], the diagonal construction does produce a number y that differs from every listed number. But nothing makes y rational: its digits are chosen one at a time to dodge the list, so they need not repeat, and a number whose expansion never repeats is irrational. The argument shows only that the list misses some real number. For the reals that's a contradiction, since the list was supposed to contain every real; for the rationals it's no contradiction at all, because the missed number lies outside the set being listed.
:::
:::

::: practice

::: check
given: m = 1 | 4; n = 0 | 6
answer: (m + n)*(m + n + 1)/2 + n
tolerance: 0

## $\pi(\given{m}, \given{n})$?

::: misses
- [any] $\frac{(m + n)(m + n + 1)}{2} + n$.
:::
:::

::: check
given: n = 3..20 int
answer: 2^(n + 1) - 1
tolerance: 0

## Binary strings of length at most {n}?

::: misses
- [any] $2^{n+1} - 1$.
:::
:::
:::
