Sub topic 4
From decimal to base n by repeated division
This chapter assumes you can do long division with remainders, know the powers of 2, 3 and 10, and have read the previous chapter on converting from base n to decimal. Here you go the other way. You take an ordinary decimal number and write it in base n.
The idea in one line
A numeral in base n is a sum of digits times powers of n:
N = dk·n^k + d(k-1)·n^(k-1) + ... + d1·n + d0
Each digit d_i is between 0 and n − 1. To find the digits, peel them off one at a time, starting with the last one.
Divide N by n. Every term except d_0 contains a factor of n, so:
- The remainder is d_0, the last digit.
- The quotient is dk·n^(k-1) + ... + d1. This is the same numeral with its last digit removed.
Repeat on the quotient to get d1, then d2, and so on. Stop when the quotient reaches 0.
The procedure
- Divide the current number by the base. Write down the quotient and the remainder.
- Divide the quotient by the base again.
- Continue until the quotient is 0. Do not stop at quotient 1, because that last 1 is also a digit.
- Read the remainders from the last one to the first one. The last remainder is the leading digit.
Worked example 1: 45 in base 2 and base 16
For base 2:
| Step | Division | Quotient | Remainder |
|---|---|---|---|
| 1 | 45 ÷ 2 | 22 | 1 |
| 2 | 22 ÷ 2 | 11 | 0 |
| 3 | 11 ÷ 2 | 5 | 1 |
| 4 | 5 ÷ 2 | 2 | 1 |
| 5 | 2 ÷ 2 | 1 | 0 |
| 6 | 1 ÷ 2 | 0 | 1 |
Reading the remainders from step 6 up to step 1 gives 101101. A check by place value: 32 + 8 + 4 + 1 = 45.
For base 16: 45 ÷ 16 = 2 remainder 13, and 2 ÷ 16 = 0 remainder 2. The remainder 13 is the single digit D, so 45 is 2D in base 16. Check: 2·16 + 13 = 45.
Here is the same procedure in Python.
def to_base(n, base):
symbols = "0123456789ABCDEF"
if n == 0:
return "0"
out = []
while n > 0:
n, r = divmod(n, base)
out.append(symbols[r])
return "".join(reversed(out))
print(to_base(45, 2)) # 101101
print(to_base(45, 16)) # 2Ddivmod(n, base) returns the quotient and remainder together. The list out collects digits last-first, so reversed puts them in reading order.
Worked example 2: 2024 in base 9
- 2024 ÷ 9 = 224 remainder 8
- 224 ÷ 9 = 24 remainder 8
- 24 ÷ 9 = 2 remainder 6
- 2 ÷ 9 = 0 remainder 2
The remainders from last to first are 2, 6, 8, 8, so 2024 = 2688 in base 9.
Verify by expanding: 2·729 + 6·81 + 8·9 + 8 = 1458 + 486 + 72 + 8 = 2024.
An alternative: subtract the largest powers
Repeated division works for every case. A second method is faster when the number is small or close to a power of the base. You list the powers of the base and find how many of each fit.
Convert 200 to base 7. The powers of 7 are 1, 7, 49 and 343.
- 343 is larger than 200, so the numeral has 3 digits.
- 200 ÷ 49 = 4 with 196 used and 4 left. The 49s digit is 4.
- 4 ÷ 7 = 0, so the 7s digit is 0.
- The ones digit is 4.
The result is 404 in base 7, which matches the diagram above.
| Situation | Better method |
|---|---|
| Large number, small base (for example base 2) | Repeated division |
| Number just above a power of the base | Subtract powers |
| You need to know the number of digits first | Powers, or the digit-count rule below |
| Any mixed or confusing case | Repeated division, because it never needs judgment |
How many digits will the answer have?
A numeral with k digits in base n lies in the range n^(k-1) ≤ N < n^k. This lets you predict the length before you divide.
- 200 in base 7: 49 ≤ 200 < 343, so there are 3 digits.
- 1000 in base 2: 512 ≤ 1000 < 1024, so there are 10 digits.
CAT questions sometimes ask only for the number of digits. In that case you can answer from the powers of the base and skip the conversion.
Information you can read without converting
Because the first remainder is the last digit, some facts come immediately:
- The last digit of N in base n is N mod n. For example, 200 mod 7 = 4, and 404 ends in 4.
- N ends in a zero in base n exactly when n divides N.
- N ends in m zeros when n^m divides N. For example, 1000 = 13000 in base 5. It ends in three zeros because 5³ = 125 divides 1000, while 5⁴ = 625 does not.
The last point holds directly when n is prime. For a composite base such as 6, a zero count depends on the prime factors 2 and 3 together, so test with n^m itself.
Worked example 3: a larger case, 100 in base 3
- 100 ÷ 3 = 33 remainder 1
- 33 ÷ 3 = 11 remainder 0
- 11 ÷ 3 = 3 remainder 2
- 3 ÷ 3 = 1 remainder 0
- 1 ÷ 3 = 0 remainder 1
The remainders from last to first give 10201 in base 3. Check: 81 + 0 + 18 + 0 + 1 = 100.
The digit-count rule agrees. 81 ≤ 100 < 243, so there are 5 digits.
Seeing the steps in code
This helper prints each division line, which is useful for checking your hand work.
def steps(n, base):
while n > 0:
q, r = divmod(n, base)
print(f"{n} = {base} * {q} + {r}")
n = q
steps(200, 7)
# 200 = 7 * 28 + 4
# 28 = 7 * 4 + 0
# 4 = 7 * 0 + 4You can also test the whole conversion by going round trip. Python's int(text, base) reads a numeral in bases 2 to 36.
def to_base(n, base):
symbols = "0123456789ABCDEF"
if n == 0:
return "0"
out = []
while n > 0:
n, r = divmod(n, base)
out.append(symbols[r])
return "".join(reversed(out))
ok = all(int(to_base(n, b), b) == n
for b in range(2, 17)
for n in range(0, 2000))
print(ok) # TrueCommon errors
- Reading the remainders in the wrong order. The first remainder is the last digit. If you write them top to bottom you get the reversal. For 45 in base 2 that would give 101101 reversed, which here reads the same, so test with 200 in base 7: top to bottom gives 404, also a palindrome. Try 2024 in base 9 and you would write 8862, which is wrong.
- Stopping when the quotient is 1. The 1 is the leading digit. Keep dividing until the quotient is 0.
- Writing the quotient instead of the remainder. Only remainders are digits.
- Using digits that are too large. A remainder is always less than the base. If you see a remainder equal to or larger than the base, you made a division error. In base 7, no digit can be 7 or 8.
- Forgetting letters for bases above 10. In hexadecimal, 10 to 15 are written A to F. A remainder of 13 is the one digit D, not "13".
- Mixing bases in an answer. If a question gives numbers in several bases, convert them all to decimal first. Do the arithmetic there, then convert the final answer back once.
Converting between two non-decimal bases
To convert from base a to base b, where neither is 10, go through decimal. First expand the base-a numeral using the method from the previous chapter. Then divide repeatedly by b.
Example: convert 213 in base 5 to base 7.
- Expand: 2·25 + 1·5 + 3 = 58.
- Divide: 58 ÷ 7 = 8 remainder 2, and 8 ÷ 7 = 1 remainder 1, and 1 ÷ 7 = 0 remainder 1.
- The remainders from last to first give 112 in base 7.
- Check: 49 + 7 + 2 = 58.
When both bases are powers of the same number, such as 2, 4 and 8, there is a faster grouping method. The next chapter covers it.
Edge cases
- Zero. 0 is written 0 in every base. The loop in the code handles it with a special case, because it would otherwise produce an empty string.
- Base 1. It is not a positional system, and CAT questions do not use it.
- Fractions. Repeated division only works for whole numbers. Fractional parts use repeated multiplication, which is covered in a later chapter.
- Negative numbers. Convert the absolute value, then put the sign in front.
Summary of the method
- Divide by the base, keep the remainder, and repeat on the quotient until it is 0.
- Read the remainders from last to first.
- Predict the digit count from n^(k-1) ≤ N < n^k.
- Expand the answer back to check it.
Exercise
Write 365 in base 6. Show the divisions and verify your answer.
Show answer
365 ÷ 6 = 60 remainder 5. 60 ÷ 6 = 10 remainder 0. 10 ÷ 6 = 1 remainder 4. 1 ÷ 6 = 0 remainder 1. Reading the remainders from last to first gives 1405 in base 6. Check: 1·216 + 4·36 + 0·6 + 5 = 216 + 144 + 0 + 5 = 365.
Exercise
How many digits does 500 have in base 4, and what is its last digit? Answer without doing the full conversion.
Show answer
The powers of 4 are 1, 4, 16, 64, 256 and 1024. Since 256 ≤ 500 < 1024, the numeral has 5 digits. The last digit is 500 mod 4 = 0, because 500 = 4·125.
Exercise
Convert 1000 to base 8 and to base 16.
Show answer
Base 8: 1000 ÷ 8 = 125 remainder 0. 125 ÷ 8 = 15 remainder 5. 15 ÷ 8 = 1 remainder 7. 1 ÷ 8 = 0 remainder 1. The result is 1750 in base 8. Check: 512 + 7·64 + 5·8 = 512 + 448 + 40 = 1000. Base 16: 1000 ÷ 16 = 62 remainder 8. 62 ÷ 16 = 3 remainder 14 (E). 3 ÷ 16 = 0 remainder 3. The result is 3E8 in base 16. Check: 3·256 + 14·16 + 8 = 768 + 224 + 8 = 1000.
Exercise
A student converts 58 to base 7 and writes 211. Find the mistake and give the correct numeral.
Show answer
The student read the remainders in the wrong order. The divisions are 58 ÷ 7 = 8 remainder 2, 8 ÷ 7 = 1 remainder 1, and 1 ÷ 7 = 0 remainder 1. The remainders are 2, 1, 1, so reading from last to first gives 112. A quick check shows the error: 211 in base 7 equals 2·49 + 7 + 1 = 106, not 58. The correct numeral is 112 in base 7, which equals 49 + 7 + 2 = 58.