How to enumerate all positive rationals (strictly) less than a given value

272 Views Asked by At

It is well known that all positive rationals can be generated by traversing the Stern Brocot tree.

How might this be modified to produce all positive rationals strictly less than a given value?

2

There are 2 best solutions below

2
On

It looks like the left half of the Stern-Brocot tree consists of all and only positive rational numbers strictly less than one.

So if you want to enumerate all positive rational numbers less than a particular rational number $q > 0$, you could simply traverse the left half of the Stern-Brocot tree and multiply those results by $q$.

0
On

I intended to post this as a comment as I'm not sure this is a satisfactory answer, but it was too long. If your bound is written as a (possibly infinite) continued fraction

$$a = [a_0, a_1,a_2, \ldots] = a_0 + \frac{1}{a_1+\frac{1}{a_2+_{\ddots}}}$$

You can compute the Stern-Brocot tree and throw away the nodes that are above that bound. Of course, if a node is thrown away (or is exactly the bound $a$), you need not compute the right descendants, but you may need to compute the left descendants.

To compare two continued fractions $a = [a_0, a_1,a_2, \ldots]$ and $b = [b_0, b_1,b_2, \ldots]$, compare the sequences $((-1)^k a_k)$ and $((-1)^k b_k)$ in lexicographical order : Compare $a_0$ and $b_0$ ($a_0 > b_0$, then $a > b$), if they are equal then compare $-a_1$ and $-b_1$, if they are equal compare $a_2$ and $b_2$ and so on...