Objective
A rational number is a quotient of two integers. For our purposes, we will restrict ourselves to positive integers and rational numbers in the interval (0, 1).
For any positive integer n, our goal is to obtain a list of all the unique rational numbers h/k in the interval (0,1) where h < k ≤ n. We call the set of such rational numbers Qn. We also want to know the number of elements in Qn, denoted |Qn|.
E.g. if n = 5, there are 9 qualifying rational numbers, namely 1/2, 1/3, 1/4, 1/5, 2/3, 2/5, 3/4, 3/5, and 4/5. There are a maximum of C(n,2) pairs of such integers. When n = 5, C(5,2) = 10, but 2/4 is a duplicate entry since ½ is already on the list.
As you can see, the number of such rational numbers is related to the number of pairs of coprime integers, namely
Qn = {h/k : 0 < h < k ≤ n and gcd(h, k) = 1}
In fact, if you don’t take the order of the two numbers into account, the number of such rational numbers is one less than the number of coprimes since 1 is coprime with itself but 1/1 is not in the interval (0,1).
Worksheet Functions
Real Statistics Functions: The Real Statistics Resource Pack provides the following worksheet functions.
FRACS(n): returns an array with three columns. Column 1 contains the values in Qn in decimal form, and the other two columns contain the corresponding h and k values for an h/k in Qn
NFRAC(n) = the number of unique elements in Qn
Example 1: What are the elements in Q7?
Using the formula =FRACS(7), we obtain the output shown in A2:C18 of Figure 1.
Figure 1 – Q7
We see that the first element in Figure 1 is 1/7 = .142857.
We also see that |Q7| = 17. You can get the same result using the formula =NFRAC(7).
Note that there are C(7,2) = 21 distinct pairs of integers between 1 and 7, and so there are a maximum of 21 elements in Q7. The four fractions 2/4, 2/6, 4/6, and 3/6 are already in the list of elements in Q7, namely as 1/2, 1/3, 2/3, and 1/2, bringing the total down to 21-4 = 17 elements.
Size of Qn for large n
Define
Clearly
As we observed in Property 2 of Coprime Numbers
where
Thus, for large n we expect that
This means that
For example, when n = 1000
The actual number is 304,191.
Fraction of h/k in Qn
The fraction of pairs of integers h < k <= n for which h/k is in Qn is
As n → ∞, this has the same limit as An/n2, namely 6/π2 per Property 2 of Coprime Numbers.
Examples Workbook
Click here to download the Excel workbook with the examples described on this webpage.
References
Stack Exchange (2022) Probability that two random numbers are coprime is 6/π2
https://math.stackexchange.com/questions/64498/probability-that-two-random-numbers-are-coprime-is-frac6-pi2
Wikipedia (2023) Basel problem
https://en.wikipedia.org/wiki/Basel_problem
Lei, J., Kadane, J. B. (2019) On the probability that two random integers are coprime
https://arxiv.org/abs/1806.00053