TITLE: Produce an explicit bijection between rationals and naturals?
QUESTION [107 upvotes]: I remember my professor in college challenging me with this question, which I failed to answer satisfactorily: I know there exists a bijection between the rational numbers and the natural numbers, but can anyone produce an explicit formula for such a bijection?

REPLY [3 votes]: Set $\Bbb N = \{1,2,3,4,\dots\}$.
Here we will define a bijective mapping between $\Bbb N$ and the subset of rational numbers
$\quad \Bbb Q_{\gt 0}^{\lt 1} = \{q \in \Bbb Q \mid 0 \lt q \lt 1\}$
For integer $n \ge 1$ define the set
$\tag 1 F_n = \{s/t\in \Bbb Q \mid [s, t \in \Bbb N] \land [t \le n] \land [s \lt t\}]$
We have a chain of inclusions
$\tag 2 \emptyset = F_1 \subset F_2 \subset F_3 \subset \dots \subset F_k \subset \dots $
and denote the union of these sets by $F$; observe that $F = \Bbb Q_{\gt 0}^{\lt 1}$.
Let the number of elements in the set $F_k$ be denoted by $f_k$.
If $k \gt 1$ let $G_k = F_k \setminus F_{k-1}$; observe that $G_k$ has an ordering relation $\le$ defined on it.
We define a function $\Gamma: \Bbb N  \to F$ by the specification
$\quad$ Given $m  \in \Bbb N$
$\quad$ Find $k \in \Bbb N$ such that $f_k \lt m \le f_{k+1}$
$\quad$ Set $\Gamma(m)$ to the
$\tag 3  [m-f_k]^\text{-th} \text{ element of } G_{k+1}$
Exercise: Show that $\Gamma$ is a well defined function that puts the sets into a $\text{1:1 correspondence}$.
See also the wikipedia article Farey sequence.