Why those two
numerals? Smells a little like an assignment.
Note that you can perform the conversion directly without using an intermediate base. Repeatedly take the remainder and integer quotient of the number (i.e. divide the number by the base). The remainders are the digits from least to greatest significance. The quotient is the number used in the next round. Stop when the number is 0 (or stop a round early when the quotient < base, using the quotient as the most significant digit). For example, consider converting the base-10 numeral 123 (often written "123₁₀"; subscripts in numerals are used to indicate base) to its base 7 equivalent.
- 123 % 7 = 4, 123 / 7 = 17. The least digit is 4.
- 17 % 7 = 3, 17 / 7 = 2. The next digit is 3.
- 2 % 7 = 2, 2 / 7 = 0. The next (and final) digit is 2
The answer: 123₁₀ = 234₇.
The same process works if neither base is a familiar one. Let's convert 123₉ to base 7. First, the beginning of the multiplication table for 7 in base 9 and (for comparison) 10:
Code:
base: 9 | 10
--------+---
1*7= 7 | 7
2*7= 15 | 14
3*7= 23 | 21
4*7= 31 | 28
5*7= 38 | 35
6*7= 46 | 42
- A bit of long division to calculate 123₉ / 7:
Code:
015₉ ← The quotient
7 ) 123₉
12₉
- 7
---
43₉
-38₉ = 5 * 7
---
4 ← The remainder
So 123₉ / 7 = 15₉, 123₉ % 7 = 4
- 15₉ / 7 = 2, 15₉ % 7 = 0
Thus 123₉ = 204₇
The reason this works becomes obvious if you examine what the process does to a numeral in the base you're converting to. The number
M is represented in base
b using the sum M = Σ aᵢ*bⁱ = a_n * bⁿ + ... + a₁ * b¹ + a₀ * b⁰, where the coefficients aᵢ are the digits of the numeral. Note the summation, like conversion, is representation-neutral; they work in any numeric representation. Conversion is easiest in base
b[/b], for if you examine the numeral in base b, you'll see that the remainder is the least significant digit and division by b is a right shift. The general process is:
- M = Σ aᵢ*bⁱ = a_n * bⁿ + ... + a₁ * b¹ + a₀ * b⁰
- M₁ = Σ aᵢ*bⁱ⁻¹ = a_n * bⁿ⁻¹ + ... + a₂ * b¹ + a₁ * b⁰; next digit: a₀
- M₂ = Σ aᵢ*bⁱ⁻² = a_n * bⁿ⁻² + ... + a₃ * b¹ + a₂ * b⁰; next digit: a₁
- ...
(Note: I use "_n" for a subscripted "n" because the Unicode "subscript n" character, ₙ, isn't recognized on any computer I tested; it's not in any font, and it's not even listed in the system character viewers.)
Or, using base b numerals:
- Start: M = a_n ... a₃ a₂ a₁ a₀
- M₁ = a_n ... a₃ a₂ a₁; next digit: a₀
- M₂ = a_n ... a₃ a₂ ; next digit: a₁
- ...
Isn't working with different bases easy?
See also the many questions about bases from Ask Dr. Math.