Ma · Mathematics

Is There an Injection Between the Natural and Rational Numbers?

Published By the 9to5Science editorial team

At a glance

  1. 1. Map naturals into rationals

    Send each natural number n to the rational number n/1; distinct inputs give distinct rationals.

  2. 2. Write rationals uniquely

    Represent each rational as a reduced fraction p/q with q positive.

  3. 3. Encode the fraction

    Encode the signed numerator and positive denominator as a pair of natural numbers.

  4. 4. Pair the codes

    Apply an injective pairing function; add one if your natural numbers start at 1.

There is an injective function from the natural numbers to the rational numbers, and there is also an injective function in the reverse direction. The first map is immediate: regard each natural number as a rational number. The reverse map takes a little more work because the rationals include fractions and negative numbers, but each rational can be encoded by a pair of integers in a way that preserves distinctness.

The convention for whether 00 is a natural number varies. The construction below works either way; the only adjustment is whether you add one to the final code.

From natural numbers to rational numbers

Define

f(n)=n1.f(n)=\frac{n}{1}.

This is an injection: if f(m)=f(n)f(m)=f(n), then m/1=n/1m/1=n/1, so m=nm=n. If your natural numbers include 00, this sends 00 to the rational number 00; if they start at 11, it maps the positive integers into the rationals as usual.

In fact, this function is just the inclusion of the natural numbers among the rationals. It is not onto, since, for example, 1/21/2 is rational but is not the image of a natural number.

From rational numbers to natural numbers

To build an injection in the other direction, use the unique reduced representation of each rational number:

x=pq,p∈Z,q≥1,gcd⁡(∣p∣,q)=1.x=\frac{p}{q}, \qquad p\in\mathbb{Z},\quad q\geq 1,\quad \gcd(|p|,q)=1.

Requiring qq to be positive and the fraction to be reduced ensures that each rational has exactly one such representation. For example, 2/32/3 is in this form, while 4/64/6 is not. The number 00 is represented as 0/10/1.

First encode the signed numerator as a nonnegative integer. Define

z(p)={2p,p≥0,−2p−1,p<0.z(p)=\begin{cases} 2p, & p\geq 0,\\ -2p-1, & p<0. \end{cases}

This assigns different codes to different integers: 0,1,−1,2,−2,…0,1,-1,2,-2,\ldots receive codes 0,2,1,4,3,…0,2,1,4,3,\ldots. The denominator can be encoded by q−1q-1, which is also nonnegative.

Now pair the two codes using the Cantor pairing function

π(a,b)=(a+b)(a+b+1)2+b,\pi(a,b)=\frac{(a+b)(a+b+1)}{2}+b,

which is an injection from pairs of nonnegative integers to nonnegative integers. Thus, if natural numbers include zero, define

g(pq)=π(z(p),q−1).g\left(\frac{p}{q}\right)=\pi\bigl(z(p),q-1\bigr).

If natural numbers start at 11, use g(p/q)=π(z(p),q−1)+1g(p/q)=\pi(z(p),q-1)+1 instead.

Why the reverse map is injective

Suppose two rationals receive the same code. Because the pairing function is injective, their pairs (z(p),q−1)(z(p),q-1) must be equal. The encoding zz is injective, so the numerators are equal; equality of q−1q-1 gives equality of the denominators. Therefore the rationals have the same reduced numerator and denominator, and are equal. This proves that gg is injective.

For a concrete calculation, take −3/5-3/5. Its numerator code is z(−3)=−2(−3)−1=5z(-3)=-2(-3)-1=5, and its denominator code is 5−1=45-1=4. Therefore

g(−35)=π(5,4)=9⋅102+4=49.g\left(-\frac{3}{5}\right)=\pi(5,4)=\frac{9\cdot 10}{2}+4=49.

Under the convention that natural numbers begin at 11, the code is 5050. The exact numerical code is not important; what matters is that distinct reduced fractions get distinct codes.

What the two injections tell you

The existence of injections both ways shows that the rationals and natural numbers have the same cardinality. More formally, the Cantor–Schröder–Bernstein theorem says that if there is an injection from AA to BB and an injection from BB to AA, then there is a bijection between AA and BB. Hence the rationals are countably infinite, despite being dense in the real line: between any two distinct real numbers there is a rational number, but the rationals can still be listed in a sequence.

The two injections above do not themselves need to be inverses. An injection from N\mathbb{N} to Q\mathbb{Q} only guarantees that different natural numbers have different rational images; it need not reach every rational. Likewise, the encoding of rationals into natural numbers assigns codes to rationals, but not every natural-number code needs to correspond to a reduced fraction.

Common mistakes

  • Assuming density means uncountability. A set can be dense in a larger set and still be countable. Density describes how points are distributed, not how many there are in the sense of cardinality.
  • Using unreduced fractions without handling duplicates. The same rational, such as 1/21/2, has many fraction representations, including 2/42/4. Reduce fractions first, or use a different encoding that explicitly handles duplicates.
  • Forgetting negative numerators. A code for nonnegative integers alone cannot represent all rational numbers. The zigzag encoding zz gives every integer a distinct nonnegative code.
  • Mixing conventions for natural numbers. If N\mathbb{N} starts at 11, shift a code in the nonnegative integers by one; if it includes 00, no shift is needed.

References

  1. Herbert B. Enderton, Elements of Set Theory
  2. Thomas Jech, Set Theory

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.