0

I used to think that there is only one to convert to a higher base ie: https://math.stackexchange.com/a/111158/407302

If we want to convert $(1200)_3$ to $(45)_{10}$ we do the following:

$1\cdot 3^3 + 2\cdot 3^2 + 0 + 0 = 27 + 18 = 45$

But I have found a weird way of by multiplying only a single power of the "incoming base" from the right.

It goes in the following way:

  1. Start with $[0,0]$
  2. Add 1 to it -> $[0,1]$ $1$ is from the left hand digit of $1200$
  3. Multiply with 3 (base) and add 2 (ie. 2 from $1200$, second digit) we get $[0,5]$
  4. Multiply with 3 (base) and add 0 (ie. 0 from $1200$, third digit) we get $[1,5]$
  5. Multiply with 3 (base) and add 0 (ie. 0 from $1200$, last digit) we get $[4,5]$

Here is the same calculation but in base-16(hex):

[0,0] + 1 = [0,1]
[0,1] X 3 + 2 = [0,5]
[0,5] X 3 + 0 = [0,F]
[0,F] X 3 + 0 = [2,D] -> 2D

Disclaimer: I have arbitrarily taken the construct $[0,0]$ represent the result because this method is taken from the Java library(BigInteger) which uses arrays to convert really large integers to an internal representation of base $2^{32}$. You can try it with an arbitrary length of zeroes too.

I have no idea why this method works.

Is this methods only valid for when converting to a base higher than the base of the given number ?

I have some intuition about why the "normal" method works : each place in the number from left to right denotes how many times a the base has "overflown", hence we add a digit to the left and reset the digit to the right.

But this I am unable to figure out the arithmetic.

How does this method work ? Why does it work ?

Am I only one impressed by this ? Am I missing something really basic ?

ng.newbie
  • 1,017
  • 1
  • 12
  • 22
  • 1
    This is a pretty common way to evaluate polynomials $\sum_{i=0}^n a_nx^n,$ which is what you are doing. It takes fewer multiplications. – Thomas Andrews Jun 30 '23 at 17:33
  • Note what happened to your initial $1$: it gets multiplied by $3$ three times, equaling $3^3$; and so forth. – Piita Jun 30 '23 at 17:36
  • 1
    In a computer, this isn't really changing a base - it is doing the computations in base $2$ and then, outside the algorithm, computing the base $10$ representation. – Thomas Andrews Jun 30 '23 at 17:36
  • In any event, the polynomial evaluation algorithm is often covered when teaching ways you can improve algorithms. It requires $n$ multiplications and $n$ additions, which is an improvement from the naive algorithm, which requires $2n$ multiplications and $n$ additions. – Thomas Andrews Jun 30 '23 at 17:41
  • @ThomasAndrews Could you please elaborate what you mean by "doing computations in base 2" ? It still uses decimal arithmetic to do this, correct ? What do you mean by computing base 10 outside the algorithm ? – ng.newbie Jun 30 '23 at 19:11
  • @ThomasAndrews Could you point me to where this is used specifically for base conversions ? I could only find something called Horner's Method https://en.wikipedia.org/wiki/Horner%27s_method and shift and add https://en.wikipedia.org/wiki/Multiplication_algorithm#Shift_and_add – ng.newbie Jun 30 '23 at 19:15
  • 1
    @ng.newbie As I said, this is polynomial evaluation. The fact that base conversion uses polym9mial evaluation is trivially true. Not sure where to find a reference, nor why you'd need one. – Thomas Andrews Jun 30 '23 at 19:21
  • @ThomasAndrews Can you elaborate on how the polynomial method takes fewer multiplications ? I understand that it is the same as "classic" method, but doesn't that then mean the number of multiplications are same ? – ng.newbie Jul 03 '23 at 20:17
  • @ng.newbie I'm sure you can work that out yourself. – Thomas Andrews Jul 03 '23 at 21:05

0 Answers0