Изменения рейтингов за последние раунды временно удалены. Скоро они будут возвращены. ×

Converting to base k

Правка en1, от ebanner, 2018-04-24 04:41:10

Let's consider the problem of converting a number to base k. Initially I was confused by this algorithm, but the more I think about it the more it makes sense.

Consider the number 11. We want to convert it to base 3. Notice that the answer is 102 for the following reason:

10 = 1*3^2 + 0*3^1 + 2*3^0

When we start we don't actually know this factorization yet but it is what we want to get to. You can simply read off the coefficients of this expression and that's the base 3 number.

Because we know we can express any number in terms of a polynomial of this form (for any natural number base), let us turn to the problem of extracting these coefficients.

To make it easy, how would be extract the first coefficient? Well we would just use the modulus operator (by the base). So we do

10 % 3 =
(1*3^2 + 0*3^1 + 2*3^0) % 3 = 2

Notice that the modulus of all terms except the rightmost have a modulus of zero. This is because each of those terms have a factor of three. So doing the modulus reads off the rightmost coefficient.

Now consider the operation of integer division.

10 / 3 = 
(1*3^2 + 0*3^1 + 2*3^0) / 3 = 
(1*3^2)/3 + (0*3^1)/3 + (2*3^0)/3 = 
1*(3^2)/3 + 0*(3^1)/3 + 2*(3^0)/3 = 
1*[(3^2)/3] + 0*[(3^1)/3] + 2*[(3^0)/3] = 
1*(3^1) + 0*3^0 + 0 = 
1*(3^1) + 0*3^0

Notice the last term zeros out because there is no factor of 3.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en5 Английский ebanner 2018-04-24 20:34:36 2
en4 Английский ebanner 2018-04-24 05:16:29 3
en3 Английский ebanner 2018-04-24 05:13:25 955 (published)
en2 Английский ebanner 2018-04-24 04:58:42 1326
en1 Английский ebanner 2018-04-24 04:41:10 1433 Initial revision (saved to drafts)