# Discrete Mathematics/Number representations

You are already familiar with writing a number down, and having it mean a certain combination of tens, hundreds, and so on. This is one form of number representation, but there are others. We will look at number bases and continued fractions.

## Number bases

You are already familiar with base-10 number representation. For example, the number 2818 is the same as

2×103+8×102+1×101+8×100

We can replace the number 10 here with any number and we obtain a different number. In general, we can represent an integer n in a base b by the following:

akbk+ak-1bk-1+...+a0b0

where the ai are all less than b.

We write a number base b as (akak-1...a0)b.

For example, if we have the numeral 243 in base 6, we write it (243)6. When we are in base 10 we simply write the number: for example the numeral 155 in base 10 is simply written 155.

However, given a number in a base b, how can we convert it to our natural base 10 system? Likewise, how can we convert a number from our base 10 system to base b?

The first is relatively easy, the other more difficult.

### Converting base b to base 10

We simply use the definition of a base-b number to convert a base-b number to base 10 - that is we simply multiply out.

For example

(919)12=9×122+121+9=1317.

### Converting base 10 to base b

This task however is slightly more difficult, and there are several ways of performing this task.

One method is to write each step using the division algorithm from the Number theory section. For example, if we wish to convert 1317 to base 12:

1317 = 12 × 109 + 9
109 = 12 × 9 + 1
9 = 12 × 0 + 9

So in base 12, (919)12=1317.

## Real numbers

We've just seen how we can convert integers from base to base, but how do we work with converting real numbers?

Recall in base 10 we write a number such as 11.341 as

1×101+1×100+3×10-1+4×10-2+1×10-3

and so we can extend our definition above of a base-b number to be

akbk+ak-1bk-1+...+a0b0+a-1b-1+...

where the ai are all less than b, and the sum could terminate or go on indefinitely.

Again, how are we to convert these numbers from base to base? We can convert the integral part, but what about the fractional part (the part less than 1)?

### Converting fractional n to base-b

Say we wish to convert .341 in base 10 to base 6.

We write a table, using the following rules

${\displaystyle c_{i}=\lfloor {6\gamma _{i-1}}\rfloor }$
${\displaystyle \gamma _{i}=6\gamma _{i-1}-c_{i}}$
i      ci                   γi                6γi
0      0                   .341                2.046
1      2                   .046                0.276
2      0                   .276                1.656
3      1                   .656                3.936
4      3                   .936                5.616
5      5                   .616                3.696
6      3                   .696                4.176
7      4                   .176                1.056
8      1                   .056                0.336
9      0                   .336                2.016


It looks like this expansion will go on forever! We need to calculate for further values of i to see whether we hit a repeat value of γi to see whether we get a repetition.

So we have the approximation that .341 is near to (.20135341)6. (Calculate this using the definition. How close is our approximation?)

If we obtain a base-b representation for example, that looks something like (.18191819181918191819...)b we call the representation periodic. We often write this as

${\displaystyle (.{\overline {1819}})_{b}}$

We use this same procedure to convert a fractional number to base-b by replacing the 6 above with b.

## Converting fractional n to base 10

We have a nifty trick we can use to convert a fractional n in base-b to base 10 provided the representation repeats. Let us look at an example:

Consider ${\displaystyle (.{\overline {3145}})_{7}=\alpha }$. Now then

${\displaystyle (3145.{\overline {3145}})_{7}=7^{4}\alpha }$

And now

${\displaystyle (3145)_{7}+(.{\overline {3145}})_{7}=7^{4}\alpha }$

which is

${\displaystyle (3145)_{7}+\alpha =7^{4}\alpha }$

Then

${\displaystyle (3145)_{7}=7^{4}\alpha -\alpha }$
${\displaystyle (3145)_{7}=(7^{4}-1)\alpha }$

And finally

${\displaystyle {(3145)_{7} \over 7^{4}-1}=\alpha }$

On converting (3145)7 to base 10, we obtain the following

${\displaystyle \alpha =1111/2400}$

## Continued fractions

In a sense, the base-b representation is nice, but it has a few shortcomings in respect to accuracy. We cannot reliably represent the number ${\displaystyle {\sqrt {2}}}$ using base-b representation. This is where the continued fraction representation comes in handy, which has some nice properties regarding quadratic irrationals.

A continued fraction is a number in the form

${\displaystyle q_{0}+{1 \over q_{1}+{1 \over q_{2}+{1 \over q_{3}+\ldots }}}}$

Since this notation is clearly rather cumbersome, we abbreviate the above to

${\displaystyle [q_{0};q_{1},q_{2},q_{3},\ldots ]}$

Again we ask ourselves how can we convert from and to this number representation? Again converting from is simpler than converting to.

### Converting from continued fraction representation

We simply use our definition of the continued fraction to convert from a continued fraction. This may look difficult, but in fact is quite simple depending on which end one starts with. Let's look at an example

Consider

${\displaystyle \alpha =[3;1,2,5]}$

Now, we work from right to left. We first have the fraction

${\displaystyle {1 \over 5}}$

The next digit 2 tells us to perform

${\displaystyle 2+{1 \over 5}=11/5}$

and then take the reciprocal to get

${\displaystyle {5 \over 11}}$

Now the next digit 1 tells us to perform

${\displaystyle 1+{5 \over 11}={16 \over 11}}$

and then to take the reciprocal to get

${\displaystyle {11 \over 16}}$

Now we must add q0 which is always greater than 1 and we get the result:

${\displaystyle \alpha ={59 \over 16}}$

### Converting to continued fraction representation

Again, we draw up a table.

Consider the fraction 12/22, and use the following rules in the table

${\displaystyle \theta _{i}=\theta _{i-1}^{-1}-q_{i-1}}$
${\displaystyle q_{i}=\lfloor \theta _{i}^{-1}\rfloor }$
i   θi      θi-1     qi
0   12/22   .       0
1   12/22   22/12   1
2   5/6     6/5     1
3   1/5     5/1     5


(stop since next row will be full of 0s)

So now the continued fraction for 12/22 is [0; 1, 1, 5].

### Converting a periodic continued fraction to quadratic irrational

Firstly, we make note of a nice property of periodic continued fractions (where the sequence of qi repeat), that

every periodic continued fraction is a number in the form
${\displaystyle {a\pm {\sqrt {b}} \over c}\ \mathrm {for} \ a,b,c\in \mathbb {Z} }$

For example, consider the continued fraction

${\displaystyle {\begin{matrix}\alpha &=&[2;2,1,2,2,1...]\\&=&[2;{\overline {2,1,2}}]\end{matrix}}}$

Now

${\displaystyle [2;{\overline {2,1,2}}]=[2;2,1,{\overline {2,2,1}}]}$

which we can rewrite as

${\displaystyle \alpha =2+{1 \over {2+{1 \over {1+{1 \over \alpha }}}}}}$

Now we can simplify to obtain

${\displaystyle \alpha =2+{1 \over {2+{1 \over {\alpha +1 \over \alpha }}}}}$
${\displaystyle \alpha =2+{1 \over {2+{\alpha \over \alpha +1}}}}$
${\displaystyle \alpha =2+{1 \over {3\alpha +2 \over \alpha +1}}}$
${\displaystyle \alpha =2+{\alpha +1 \over 3\alpha +2}}$
${\displaystyle \alpha ={7\alpha +5 \over 3\alpha +2}}$
${\displaystyle \alpha {(3\alpha +2)}={7\alpha +5}}$
${\displaystyle \alpha {(3\alpha +2)}-5=7\alpha }$
${\displaystyle 3\alpha ^{2}+2\alpha -5=7\alpha }$
${\displaystyle 3\alpha ^{2}+2\alpha -7\alpha -5=0}$
${\displaystyle 3\alpha ^{2}-5\alpha -5=0}$

which is a quadratic equation and can be solved to obtain

${\displaystyle \alpha ={\frac {5+{\sqrt {85}}}{6}}}$.

Note that we can always roll up the continued fraction into the form (aα+b)/(cα+d)=α, which demonstrates the point that every quadratic irrational has a repeating continued fraction representation

### Convergents

Say we have a continued fraction [q0; q1, ... ] which represents a number n. Let us examine the following series of fractions [q0], [q0; q1], [q0; q1, q2] and so on. Each element of the series is known as a convergent. It turns out that the series of convergents provide the best rational approximations to n.

These can be calculated from the continued fraction representation, but also from the calculation table. Let us take ${\displaystyle {\sqrt {6}}}$.

Continue as before, but place an extra -1 row, and set u-1=1, v-1=0. Iterate with the rules

${\displaystyle u_{i+1}=q_{i+1}u_{i}+v_{i-1}}$
${\displaystyle v_{i+1}=q_{i+1}v_{i}+v_{i-1}}$
${\displaystyle {\begin{matrix}i&\theta _{i}&\theta _{i}^{-1}&q_{i}&u_{i}&v_{i}\\-1&&&&1&0\\0&{\sqrt {6}}&&2&2&1\\1&2-{\sqrt {6}}&1+{\sqrt {3/2}}&2&5&2\\2&-1+{\sqrt {3/2}}&2+{\sqrt {6}}&4&22&9\\3&-2+{\sqrt {6}}&1+{\sqrt {3/2}}&2&49&20\\\end{matrix}}}$

and the sequence repeats and the continued fraction is [2; 2, 4, 2, 4, ... ]. We can continue the process to generate more convergents - the convergents are 2, 5/2, 22/9, 49/20, ...