## Stern-Brocot system - spotting patterns easier

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

- ToSternBrocotSystem (for fractions)
- FromSternBrocotSystem
- ToSternBrocotSystem (for approximations and continuous decimals)
- ToCondensedSternBrocotSystem

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:

1 2 3 4 5 6 7 8 9 |
# Some simple fractions: puts "3/2".tsb puts "28/9".tsb # Convert back to a fraction puts "RL".fsb puts "RRRLLLLLLLL".fsb |

**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,

**e ⇒ RRLRRLRLLLLRLRRRRRRLRLLLLLLLLRLRRRRRRRRRRLRLLLLLLL**

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,

1 2 3 4 5 6 7 8 9 10 11 |
# Approximating e puts e.tsb # Approximating e with 10 iterations puts e.tsb(false, 10) # Continious number puts (1.9).tsb(true) puts "R".fsb |

**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:

1 2 |
puts e.tsb.csb puts 0.86602540378.tsb.csb |

**Notes**

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 http://support.mathosproject.com/.