Sub topic 4
Unit digit of a large power in three steps
This chapter assumes you can divide with remainders, read a number's last digit as its remainder mod 10, and know the exponent laws. The previous chapter showed that unit digits of powers repeat in cycles. Here you turn that observation into a routine that handles any exponent in a few seconds.
The three steps
Take a question of the form "What is the unit digit of a^n?" where n is a large positive integer.
- Reduce the base. Only the unit digit d of a matters, so replace a by d. For example, 47^n behaves like 7^n.
- Reduce the exponent. Find the cycle length L of d, then compute r = n mod L. If r = 0, use r = L.
- Read the digit. Write out the cycle of d and take the r-th entry.
The cycles from the previous chapter are:
| Unit digit d | Cycle of unit digits (n = 1, 2, 3, ...) | Length L |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 1 | 1 |
| 5 | 5 | 1 |
| 6 | 6 | 1 |
| 4 | 4, 6 | 2 |
| 9 | 9, 1 | 2 |
| 2 | 2, 4, 8, 6 | 4 |
| 3 | 3, 9, 7, 1 | 4 |
| 7 | 7, 9, 3, 1 | 4 |
| 8 | 8, 4, 2, 6 | 4 |
Why the remainder works
Each time you multiply by a, the unit digit of the result depends only on the previous unit digit and on d. Once a unit digit repeats, everything after it repeats too. So the sequence of unit digits of a^1, a^2, a^3, ... is periodic with period L, and a^n has the same unit digit as a^(n − L).
Subtracting L repeatedly reduces n to a value between 1 and L. This is the same as taking n mod L, except that a remainder of 0 corresponds to the last position, L, not to position 0. That is why step 2 says "if r = 0, use L".
A single rule for all digits
The cycle lengths 1, 2 and 4 all divide 4. So you can use n mod 4 for every base and skip the table of lengths. For d = 4, the cycle 4, 6 repeated twice gives 4, 6, 4, 6, which has the same entries at positions 1 to 4 as a length-4 cycle. Likewise for d = 9. For d = 0, 1, 5, 6 every position gives the same digit.
The only cost is that you must remember the cycles for 4 and 9 as "odd exponent gives d, even exponent gives the other digit". This is often faster than looking up a table.
Worked examples
Example 1: 7^123. The base is already a single digit, so d = 7. Then 123 = 4 × 30 + 3, so r = 3. The cycle for 7 is 7, 9, 3, 1, and the third entry is 3. The unit digit is 3.
Example 2: 2^2026. Only the last two digits of the exponent matter for a remainder mod 4, because 100 is divisible by 4. Here 26 mod 4 = 2, so r = 2. The cycle for 2 is 2, 4, 8, 6, and the second entry is 4. The unit digit is 4.
Example 3: 8^100. Here 100 mod 4 = 0, so r = 4. The cycle for 8 is 8, 4, 2, 6, and the fourth entry is 6. The unit digit is 6.
Example 4: 1473^57. Step 1 gives d = 3. Then 57 mod 4 = 1, so r = 1. The cycle for 3 is 3, 9, 7, 1, and the first entry is 3. The unit digit is 3.
Example 5: 29^2027. Step 1 gives d = 9, whose cycle is 9, 1. The exponent is odd, so the digit is 9.
Example 6: an exponent given as an expression. Find the unit digit of 3^(2^10). Evaluate the exponent: 2^10 = 1024, and 1024 mod 4 = 0, so r = 4. The fourth entry of 3, 9, 7, 1 is 1. The unit digit is 1. (Exponents that are themselves towers, such as 2^(3^4), get their own chapter.)
Combining powers
Unit digits survive multiplication, addition and subtraction, so you can work out each power separately and then combine only the unit digits.
Product. Find the unit digit of 7^123 × 3^45. From Example 1, 7^123 ends in 3. For 3^45, 45 mod 4 = 1, so it ends in 3. The product of unit digits is 3 × 3 = 9, so the answer is 9.
Sum. Find the unit digit of 2^100 + 3^100. Since 100 mod 4 = 0, r = 4 for both. 2^100 ends in 6 and 3^100 ends in 1, giving 6 + 1 = 7. The answer is 7.
Difference. Find the unit digit of 8^50 − 3^50. Since 50 mod 4 = 2, 8^50 ends in 4 and 3^50 ends in 9. The unit digits give 4 − 9 = −5, which is negative. Because 8^50 is larger than 3^50, the true difference is positive, so borrow 10 and use 14 − 9 = 5. The answer is 5.
Checking your answer with code
Python's three-argument pow(a, n, 10) computes a^n mod 10, which is exactly the unit digit. This is a handy check when you practise.
def unit_digit_power(a, n):
if n == 0:
return 1
d = a % 10
if d in (0, 1, 5, 6):
return d
cycles = {2: [2, 4, 8, 6], 3: [3, 9, 7, 1], 4: [4, 6],
7: [7, 9, 3, 1], 8: [8, 4, 2, 6], 9: [9, 1]}
cycle = cycles[d]
return cycle[(n - 1) % len(cycle)]
print(unit_digit_power(7, 123)) # 3
# Compare the cycle method with direct modular exponentiation.
for a in range(0, 60):, 60):
assert unit_digit_power(a, n) == pow(a, n, 10), (a, n)
print("all checks passed")Theycle[(n - 1) % len(cycle)] is the code version of "take the remainder, treat 0 as the last position". Position r in the text is index r − 1 in the list. Using (n - 1) % L` handles the r = 0 case without a special branch, because n = L maps to index L − 1, the last entry.
Edge cases and common errors
- Exponent 0. For a ≠ 0, a^0 = 1, so the unit digit is 1 whatever the base. The cycle table starts at n = 1 and does not apply. If the exponent is a quantity such as n − 3 or 10 − k, check that it is not zero.
- Forgetting that remainder 0 means the last position. For 8^100 the wrong answer is "position 0 does not exist, so skip" or "use 8". The right answer is the fourth entry, 6.
- Reducing the wrong number. The base is reduced by taking its unit digit, which is a remainder mod 10. The exponent is reduced mod the cycle length, which is mod 4. Mixing these up, for example computing 123 mod 10 for the exponent, gives wrong answers.
- Using n mod L when the cycle was written starting at n = 0. Some people list 1 as the first entry for n = 0. The cycles in this chapter start at n = 1.
- Dividing the exponent by the base. The exponent has no relation to the base's digits. Only n mod 4 matters.
- Treating 0^0. An expression such as 10^n has unit digit 0 only for n ≥ 1.
- Bases ending in 0, 1, 5, 6 with large exponents. These are the easiest cases: the unit digit never changes, so there is nothing to compute.
- Negative bases. For a negative base such as (−7)^123, the unit digit is that of the magnitude 7^123, but the number itself is negative. If a question asks for the last digit of a negative number, be explicit about this.
Exercises
Exercise
Find the unit digit of 38^175 + 27^98.
Show answer
For 38^175 the unit digit of the base is 8, and 175 mod 4 = 3. The cycle of 8 is 8, 4, 2, 6, so the third entry is 2. For 27^98 the unit digit of the base is 7, and 98 mod 4 = 2. The cycle of 7 is 7, 9, 3, 1, so the second entry is 9. The sum of unit digits is 2 + 9 = 11, so the unit digit is 1.
Exercise
For how many values of n from 1 to 100 does 7^n end in the digit 3?
Show answer
The cycle of 7 is 7, 9, 3, 1, and the digit 3 occurs at position 3. So n must satisfy n mod 4 = 3. The values are 3, 7, 11, ..., 99, which is (99 − 3) / 4 + 1 = 25 values.
Exercise
Find the unit digit of 6^45 × 9^(2k+1) × 4^(3k) for any positive integer k, where you may assume 3k is even only when k is even. Then give the answer for k = 2.
Show answer
The unit digit of 6^45 is 6. The exponent 2k + 1 is odd, so 9^(2k+1) ends in 9. For k = 2, the exponent of 4 is 3k = 6, which is even, so 4^6 ends in 6. The product of unit digits is 6 × 9 × 6 = 324, which ends in 4. So for k = 2 the unit digit is 4. For odd k the exponent 3k is odd, 4^(3k) ends in 4, and the product 6 × 9 × 4 = 216 ends in 6.