Stern-Brocot system - spotting patterns easier

Mathos Core Library has some new functionality in the Fraction class. Here are some of the new methods:

All these methods are thought to be used to spot different kinds of patterns in numbers. Let's take a look at a some simple examples.

Simple fraction representation and conversion

When you use ToSternBrocotSystem on fraction, you will get an expression in terms of L's and R's. For example,

  • 3/2 ⇒ RL
  • 28/9 ⇒ RRRLLLLLLLL

You can use From SternBrocotSystem to convert it back to the actual fraction. In our Ruby Interpreter, this can be achieved by the following lines of code:

Approximation of real numbers (transcendental and non-transcendental)

The static method ToSternBrocotSystem has two functions. Either it approximates any real number such as e, or real numbers that can be expressed as a fraction for instance 0.66666.....  6/9=2/3.

When you approximate a number, you must assign the number of times it should iterate. If you know in advance that it is a continuous fraction, iteration count does not have to be specified. For example,


If you know that the number is continuous, you can find an equivalent fraction:

  • 1.9.... ⇒ R
  • R ⇒ 2

NOTE: 0 and 1 cannot be expressed with Stern Brocot notation. This also means that 0.9.... cannot be expressed since it's 1.

In Ruby Interpreter,

Spotting the pattern with condensed form

In order to make it easier to spot the pattern, there is a method called ToCondensedSternBrocot System. Instead of having a long array of L's and R's, this function will put a number that will tell you how many of each L and R there are in the string. For example,

  • e ⇒ R(2)L(1)R(2)L(1)R(1)L(4)R(1)L(1)R(6)
  • sqrt(3)/2 ⇒ L(1)R(6)L(2)R(6)L(2)R(6)L(2)R(6)L(2)R(6)

The Ruby Interpreter code that was used is:


In order to access these functions, you will have to download the latest changeset of the library. If you would have any questions, please feel free to ask at