Is There an Injection Between the Natural and Rational Numbers?
At a glance
- 1. Map naturals into rationals
Send each natural number n to the rational number n/1; distinct inputs give distinct rationals.
- 2. Write rationals uniquely
Represent each rational as a reduced fraction p/q with q positive.
- 3. Encode the fraction
Encode the signed numerator and positive denominator as a pair of natural numbers.
- 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 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
This is an injection: if , then , so . If your natural numbers include , this sends to the rational number ; if they start at , 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, 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:
Requiring to be positive and the fraction to be reduced ensures that each rational has exactly one such representation. For example, is in this form, while is not. The number is represented as .
First encode the signed numerator as a nonnegative integer. Define
This assigns different codes to different integers: receive codes . The denominator can be encoded by , which is also nonnegative.
Now pair the two codes using the Cantor pairing function
which is an injection from pairs of nonnegative integers to nonnegative integers. Thus, if natural numbers include zero, define
If natural numbers start at , use instead.
Why the reverse map is injective
Suppose two rationals receive the same code. Because the pairing function is injective, their pairs must be equal. The encoding is injective, so the numerators are equal; equality of gives equality of the denominators. Therefore the rationals have the same reduced numerator and denominator, and are equal. This proves that is injective.
For a concrete calculation, take . Its numerator code is , and its denominator code is . Therefore
Under the convention that natural numbers begin at , the code is . 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 to and an injection from to , then there is a bijection between and . 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 to 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 , has many fraction representations, including . 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 gives every integer a distinct nonnegative code.
- Mixing conventions for natural numbers. If starts at , shift a code in the nonnegative integers by one; if it includes , no shift is needed.
References
- Herbert B. Enderton, Elements of Set Theory
- Thomas Jech, Set Theory
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.