Sub topic 2
Writing numbers in base n
This chapter covers how a numeral is written in base n. It covers which digits are allowed, how counting works, and how many values fit in a given number of digits. It does not cover full conversion procedures, which come in the next two chapters.
You should already be comfortable with place value (each position carries a weight), powers of 2, 3 and 10, and basic algebra.
What "base n" means
A base is the number of distinct digit symbols a system uses. It is also the factor by which each position's weight grows as you move left. Decimal is base 10. It uses ten symbols, 0 to 9, and its weights are 1, 10, 100, and so on.
Base n works the same way:
- It has exactly n digit symbols, which are 0, 1, ..., n − 1.
- The weights of the positions from right to left are n⁰, n¹, n², and so on.
- Writing the numeral 10 always means "one group of n and zero units". So 10 in base n is the number n.
The last point is the most useful single fact in this topic. Decimal "ten" is only written 10 because the base is ten. In base 5, the numeral 10 is five. In base 2, it is two. In base 16, it is sixteen.
Notation
A subscript tells you the base. Both of these notations are common:
- 213₅ or (213)₅ means the numeral 213 read in base 5.
- A numeral with no subscript is decimal unless the problem says otherwise.
CAT questions often write the base in words, such as "the number 213 in base 5". They sometimes write only a subscript, such as "x₇". Always find the base before you read the digits.
The subscript base is written in decimal. A base-5 numeral is always labeled 5, not 10, even though 10 in base 5 would mean five. Otherwise the label would be ambiguous.
Which digits are allowed
In base n, every digit must be an integer from 0 to n − 1. The symbol n itself never appears as a single digit. When a count reaches n, it carries into the next position, and the numeral becomes 10.
| Base | Allowed digits | Largest single digit |
|---|---|---|
| 2 | 0, 1 | 1 |
| 3 | 0, 1, 2 | 2 |
| 5 | 0, 1, 2, 3, 4 | 4 |
| 8 | 0 to 7 | 7 |
| 10 | 0 to 9 | 9 |
| 16 | 0 to 9, A to F | F (fifteen) |
Bases above 10 need extra symbols. The standard choice is A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. Hexadecimal (base 16) is the usual example. CAT problems rarely go beyond base 16.
Minimum possible base
The digits in a numeral give a lower bound on the base. If the largest digit is d, the base must be at least d + 1.
For example, the numeral 3405 contains the digit 5. A numeral with a 5 cannot be in base 5, because the largest digit there is 4. So the base is at least 6. This reasoning is the first step in many unknown-base questions, and it removes impossible answers before you do any algebra.
Counting in base n
Counting in any base follows the same odometer rule. The units digit goes up by one. When it would reach n, it resets to 0 and the next position goes up by one.
In base 3, the count runs like this:
0, 1, 2, 10, 11, 12, 20, 21, 22, 100, 101, ...
The numeral 10 in this list is the number three. The numeral 100 is the number nine, which is 3². In general, the numeral 1 followed by k zeros is the number nⁿ... more precisely nᵏ. This is the place-value idea from the previous chapter, seen from the counting side.
The following code produces such a list, so you can check your own counting for any base from 2 to 16:
SYMBOLS = "0123456789ABCDEF"
def numeral(n, base):
digits = []
while True:
n, r = divmod(n, base)
digits.append(SYMBOLS[r])
if n == 0:
break
return "".join(reversed(digits))
def count_in_base(start, stop, base):
return [numeral(k, base) for k in range(start, stop + 1)]
print(count_in_base(0, 10, 3))
# ['0', '1', '2', '10', '11', '12', '20', '21', '22', '100', '101']
print(count_in_base(8, 18, 16))
# ['8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '10', '11', '12']The second line shows that after F (fifteen) comes 10, which is sixteen. The loop is the repeated-division method, which the chapter on decimal-to-base conversion will explain in detail. Here it is only a counting tool.
How many numbers fit in k digits
A frequent exam skill is to say how large a k-digit numeral can be, without converting anything.
In base n, a k-digit numeral has these properties:
- The smallest value is 1 followed by k − 1 zeros, which equals nᵏ⁻¹. The leading digit cannot be 0, or the numeral would really have fewer digits.
- The largest value is k digits all equal to n − 1, which equals nᵏ − 1.
- The count of different k-digit numerals is (n − 1) · nᵏ⁻¹. There are n − 1 choices for the leading digit and n choices for each other position.
The largest value is nᵏ − 1 because adding 1 to it makes every digit carry. The result is 1 followed by k zeros, which is nᵏ. In decimal, 999 + 1 = 1000 is the same effect.
The following code checks these formulas for 3-digit base-5 numerals and 4-digit binary numerals:
def k_digit_range(base, k):
low = base ** (k - 1)
high = base ** k - 1
count = (base - 1) * base ** (k - 1)
return low, high, count
print(k_digit_range(5, 3)) # (25, 124, 100)
print(k_digit_range(2, 4)) # (8, 15, 8)For base 5, the range is 25 to 124, and 124 − 25 + 1 = 100 matches the count. For binary with four digits, the values are 8 to 15, which are 1000₂ to 1111₂.
Comparing numerals in different bases
Two numerals written with the same digits can have very different values when the bases differ. The same digit string is worth more in a larger base, as long as it has at least one nonzero digit outside the units place.
| Numeral | Base | Value in decimal |
|---|---|---|
| 21 | 3 | 2·3 + 1 = 7 |
| 21 | 5 | 2·5 + 1 = 11 |
| 21 | 10 | 21 |
When a question asks which of several numerals in different bases is largest, do not compare the strings. Convert each to decimal, or estimate each with its leading term. Take 100₂ against 22₃. The first is 4. The second is 2·3 + 2 = 8. The shorter-looking numeral wins here because its base is larger.
A quicker estimate uses the length. A k-digit numeral in base n lies between nᵏ⁻¹ and nᵏ − 1. If these ranges do not overlap, you can decide the comparison without computing.
A worked example
Question. A numeral is written 4 1 3 in some base b. Which values of b are possible, and what is the largest value the numeral can take if b is at most 8?
Step 1: find the lower bound on b. The largest digit is 4, so b ≥ 5.
Step 2: write the value as a function of b. Place value gives 4b² + 1·b + 3.
Step 3: use the upper limit. The value increases with b, so it is largest at b = 8. That gives 4·64 + 8 + 3 = 267.
The possible bases are 5, 6, 7 and 8. The largest value is 267, and the smallest is 4·25 + 5 + 3 = 108.
Common errors
- Using the digit n. Writing "5" in base 5 is invalid. The number five is written 10 in base 5.
- Reading the subscript as part of the number. In 213₅, the 5 is a label. It is not a digit.
- Dropping the leading-digit rule. A k-digit numeral cannot start with 0, so the count is (n − 1)·nᵏ⁻¹, not nᵏ.
- Assuming the base from the digits. The digits only set a minimum. The base comes from the problem statement.
- Treating 10 as ten. In every base, 10 means "one group of the base". Only in base 10 is that ten.
Exercise
List the first twelve numerals (starting at 0) in base 4. Then say which decimal number the numeral 100 in base 4 stands for.
Show answer
Base 4 uses digits 0 to 3. The count is 0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23. The next numeral is 30, then 31, 32, 33, and then 100. The numeral 100 in base 4 is 4² = 16 in decimal.
Exercise
How many 3-digit numerals exist in base 6, and what are the smallest and largest decimal values they can represent?
Show answer
The count is (6 − 1) · 6² = 5 · 36 = 180. The smallest is 100 in base 6, which is 36. The largest is 555 in base 6, which is 6³ − 1 = 215. Check: 215 − 36 + 1 = 180.
Exercise
A student claims that 7283 is a valid base-8 numeral. Is the claim correct? What is the smallest base in which the numeral could be valid?
Show answer
The claim is wrong. The digit 8 is not allowed in base 8, where digits run from 0 to 7. The largest digit in 7283 is 8, so the base must be at least 9. The smallest base is 9.
Exercise
Which is larger, 1000₂ or 33₄? Justify without computing both values in full.
Show answer
33₄ is larger. A 2-digit base-4 numeral has a maximum of 4² − 1 = 15, and 33₄ is exactly 15 (3·4 + 3). The numeral 1000₂ has four digits, so its value is 2³ = 8. Since 15 > 8, 33₄ is larger.