Some notes on the Stern–Brocot tree
Stern–Brocot tree
Over the past century, continued fractions and the Stern–Brocot tree have gradually become less prominent in curriculae around the world. Although not being completely extinct, they eke out an existence more as objects of recreational math than as active research topics, and it wouldn't be surprising if they still hold treasures.
The Stern–Brocot tree is a binary tree constructed by recursive mediant building. This mediant building can be conveniently encoded using matrices. Each node in the binary tree is represented by a two by two matrix where the root of the tree is the unit matrix .
From this root the tree is constructed by branching left or right applying or .
In words: we branch left by adding the right column to the left one, and we branch right by adding the left column to the right one.
The mapping from a matrix to an actual fraction is done by dividing the sum of the top row by the sum of the bottom row.
Rational numbers are now paths in this binary tree. Some examples:
Contrary to positional number systems, values of Stern–Brocot sequences do not distribute evenly, but exponentially along the number line, which can be seen in the following diagram of the first six levels.
Every fraction in the Stern–Brocot tree is an irreducible fraction with .
means the nominator and denominator are coprime — or relatively prime — they do not share any common factor.
Furthermore, fully covering means that the tree contains all irreducible fractions — respectively all coprime pairs.
ω(N) in the Stern–Brocot tree
Having a tree of all coprime pairs to hand can also be used to say something about integers. I could not find anything about this in the literature, so I will go into more detail here.
We replace by the map
so instead of dividing by , we build the product . Unlike division, multiplication commutes and the tree now has a right-left symmetry. The property of an ordered mapping to the number line is also lost, so the following diagram better illustrates the structure of the tree.
Furthermore, bijectivity to any fundamental number set is lost because certain integers appear multiple times in the tree.
Take the number . It can be written as and is therefore a composite of three distinct primes. From those three distinct ones, eight distinct coprime pairs can be formed, so there are eight different representations of
When trying out other numbers, the reader will come across the pattern that every natural number with distinct prime factors has decompositions into coprime products.
The distinct number of primes is called small prime omega — denoted , OEIS A001221 — so it follows that every occurs times in the Stern–Brocot tree.
Or put the other way round
So we can determine the number of distinct primes of by counting how many times appears in the Stern–Brocot tree — no factorization required.
Also, we do not have to go through the whole tree. As soon as we encounter a tree node that is we can stop the traversal, since all its child nodes will have values .
The following Python program does just that.
from collections import Counter from math import log2 I = [1, 0, 0, 1] # root of the tree def L(p): # left child of node p a, b, c, d = p return [a+b, b, c+d, d] def R(p): # right child of node p a, b, c, d = p return [a, b+a, c, d+c] def N(p): # node p -> integer a, b, c, d = p return (a+b)*(c+d) def Ns(n, p): # collect all integers <= n if N(p) <= n: return [N(p)] + Ns(n, L(p)) + Ns(n, R(p)) else: return [] # count occurrences of each integer in aggregate and log2 omegas = {k: int(log2(v)) for k, v in sorted(Counter(Ns(42, I)).items())} print(omegas) => {1: 0, 2: 1, 3: 1, 4: 1, 5: 1, 6: 2, 7: 1, 8: 1, 9: 1, 10: 2, 11: 1, 12: 2, 13: 1, 14: 2, 15: 2, 16: 1 ...
I hope to be able to be more precise about the time and space complexity in the future, but for now, just to get an idea of how many nodes we can truncate, the following graph shows the number of nodes traversed to determine .
The purple curve is simply the dual logarithm of . It can be seen that while initially above, asymptotically the number of nodes traversed divided by falls below .
Now I'd like to return to the original Stern-Brocot tree. I want to apply the concept of Shannon entropy to fractions.
Entropy and Irrationality
Let's ignore the fractions and focus on the binary sequences of 's and 's themselves.
We'll pick a random number from the Stern–Brocot tree and retrieve its information one or at a time. Assuming we have no prior knowledge, getting an or an should be equally likely. This means that at each branch we get exactly one bit of information.
It follows that each natural number carries bits of information. The same is true for unit — or Egyptian — fractions . In this picture the popular approximation of carries nine bits of information.
As usual in information theory, we can also define the entropy of a binary sequence as
For every natural number we get and , because they live on the rightmost branch. For them the entropy is
For a similar reason, the entropy of all the Egyptian fractions is zero because and holds all of them.
The fraction has and , so the entropy is
The entropy is at its maximum for numbers that branch as often to the right as to the left. For them and so
While I cannot give a concise interpretation of the entropy of rational numbers, there is something that jumps out at you when you look at irrational numbers.
Irrational numbers carry infinite information because they are never ending sequences in the Stern–Brocot tree.
It is also remarkable, that all square roots have periodic patterns. And conversley, every periodic sequence is a square root. Also, has an interesting pattern.
Since we cannot store an infinite amount of information, we can only work with approximations of irrational numbers. It follows that we can only talk about entropy of the first bits of an irrational number.
The following three graphs show how the entropy of several irrational numbers evolves with increasing information. The plots show the same data at three different zoom levels: 40bit, 20kbit, and 180kbit.
We see that and lead the field. They maximise the entropy because they balance out 's and 's in the most granular way. is a repeating while is a repeating change between and .
is known to be the most irrational number. and has, together with and their inverses, the highest entropy — asymptotically the entropy goes to one. We also see that 's entropy converges to a value below one because its period has an uneven number of 's and 's.
I suspect that entropy could be a good measure for the convergence of irrational numbers and I hope to gain more insight in the very near future.
References
[W] Norman J. Wildberger, MathFoundations 96: Fractions and the Stern–Brocot tree, 2012
[GKP] Graham, Knuth, and Patashnik, Concrete Mathematics Second Edition, 1994, Addison-Wesley Professional, p. 116
[Baez] John C. Baez, What is Entropy?, 2024