Let $S = \{q \in \mathbb Q \, | \, 0 \le q \lt 1\}$.
Let $\mathbb N = \text{ the positive integers}$.
Claim a function
$\tag 1 f: \mathbb N \to S$
can be defined using recursion that is a bijective correspondence.
There is a demonstration of the validity of this claim in the next section. But for now just assume we have such an explicit bijection $f$.
Now it is easy to see that $S \times \mathbb Z$ can be naturally put into a bijective correspondence with the set of all rational numbers $\mathbb Q$ (the coordinate $(p, m)$ corresponding to $p + m$). But since $S \times \mathbb Z$ is now countable (we have $f$ and see also Asaf Karagila's answer), the set $\mathbb Q$ is countable.
The reader is invited to edit the next section to get an explicit bijection between $\mathbb N$ and $\mathbb Q$.
The implementation of this function $f$ is described by the following computer program using Python:
#--------*---------*---------*---------*---------*---------*---------*---------*
# Desc: Implement a bijective corresponce between the integers {n | n > 0} and
# the set of rational numbers {q | 0 <= q < 1}
#--------*---------*---------*---------*---------*---------*---------*---------*
from fractions import Fraction
import sys
def daFunctionGraph(curCoordinate):
nextInteger = curCoordinate[0] + 1
p = curCoordinate[1]
tickPrecision = curCoordinate[1].denominator
if p + Fraction(1, tickPrecision) == 1:
return [nextInteger, Fraction(1, tickPrecision + 1)]
else:
while True:
q = p + Fraction(1, tickPrecision)
if q.denominator == tickPrecision:
return [nextInteger, q]
else:
p = p + Fraction(1, tickPrecision)
#--------*---------*---------*---------*---------*---------*---------*---------#
while True:# M A I N L I N E #
#--------*---------*---------*---------*---------*---------*---------*---------#
current = [1, Fraction(0, 1)]
while True:
print(current[1], end=', ')
if current[0] == 100:
break
current = daFunctionGraph(current)
sys.exit() # END PROGRAM
The first 100 outputs from $f$ are enumerated by this program; you can use the 'slider' to see all the outputs:
0, 1/2, 1/3, 2/3, 1/4, 3/4, 1/5, 2/5, 3/5, 4/5, 1/6, 5/6, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7, 1/8, 3/8, 5/8, 7/8, 1/9, 2/9, 4/9, 5/9, 7/9, 8/9, 1/10, 3/10, 7/10, 9/10, 1/11, 2/11, 3/11, 4/11, 5/11, 6/11, 7/11, 8/11, 9/11, 10/11, 1/12, 5/12, 7/12, 11/12, 1/13, 2/13, 3/13, 4/13, 5/13, 6/13, 7/13, 8/13, 9/13, 10/13, 11/13, 12/13, 1/14, 3/14, 5/14, 9/14, 11/14, 13/14, 1/15, 2/15, 4/15, 7/15, 8/15, 11/15, 13/15, 14/15, 1/16, 3/16, 5/16, 7/16, 9/16, 11/16, 13/16, 15/16, 1/17, 2/17, 3/17, 4/17, 5/17, 6/17, 7/17, 8/17, 9/17, 10/17, 11/17, 12/17, 13/17, 14/17, 15/17, 16/17, 1/18, 5/18, 7/18, 11/18,