Sub topic 5
Unit digit of power towers
This chapter assumes you can already find the unit digit of a^n for a large n using cyclicity. That means you know the cycle lengths 1, 2 and 4 and the rule that a remainder of 0 maps to the last position in the cycle. It also assumes you know the laws of exponents and can find remainders mod 4.
What a power tower is
A power tower stacks exponents, as in 3^(4^5). It is evaluated from the top down. The exponent 4^5 is computed first, and then 3 is raised to that result.
Exponentiation is right-associative, so a^b^c means a^(b^c), not (a^b)^c. The two give very different values:
- 2^3^2 = 2^(3^2) = 2^9 = 512.
- (2^3)^2 = 2^6 = 64.
The second form equals a^(bc), so it is an ordinary large-power problem. A true tower needs one extra step.
The core idea
For a base with unit digit 2, 3, 7 or 8, the unit digit of a^N repeats every 4 powers. For 4 and 9 it repeats every 2. For 0, 1, 5 and 6 it never changes. So the unit digit of a^N depends on two things:
- The unit digit of a.
- The exponent N only through its remainder modulo the cycle length.
In a tower, N is itself a power such as 4^5. You never need its full value. You only need N mod 4, and N mod 2 for the cycle-2 digits.
| Unit digit of base | Cycle | Exponent needed |
|---|---|---|
| 0, 1, 5, 6 | one value, never changes | none |
| 4, 9 | length 2 | N mod 2 |
| 2, 3, 7, 8 | length 4 | N mod 4 |
Every cycle length divides 4, so you can always compute N mod 4. For a cycle of length 2, the parity of N is enough.
The position rule
Take the remainder r of N modulo the cycle length L. Then:
- If r is 1, 2 or 3 (below L), use the r-th entry of the cycle.
- If r is 0, use the last entry, entry number L.
The remainder 0 maps to the last entry because a^L has the same unit digit as the last entry of the cycle. Position 0 does not exist, so "r = 0" never means "use 1". It means "use the entry at position L". Here is an example:
- The cycle of 3 is 3, 9, 7, 1.
- An exponent that is a multiple of 4 gives 1, the fourth entry.
- Check: 3^4 = 81 ends in 1.
This rule assumes the exponent is at least 1, which is always true for a tower of positive integers.
Finding N mod 4 when N = b^c
This is where tower problems are won or lost. The remainder of b^c modulo 4 follows a short set of cases:
| Base b | Exponent c | b^c mod 4 |
|---|---|---|
| even | c ≥ 2 | 0 |
| even | c = 1 | b mod 4 (0 or 2) |
| odd, b mod 4 = 1 | any | 1 |
| odd, b mod 4 = 3 | c odd | 3 |
| odd, b mod 4 = 3 | c even | 1 |
The reasons are short:
- An even base raised to a power of 2 or more contains 2^2, so it is a multiple of 4.
- A base congruent to 1 stays congruent to 1 under any power.
- A base congruent to 3 is congruent to -1, so it alternates between -1 and 1 as the exponent goes from odd to even.
For deeper towers, the exponent c may itself be a tower. In that case you only need its parity or its size relative to 2:
- Whether c is odd or even.
- Whether c is at least 2.
The procedure
Worked examples
Example 1: 3^(4^5)
- The digit is 3, so the cycle is 3, 9, 7, 1.
- The exponent 4^5 has an even base and exponent 5 ≥ 2, so it is a multiple of 4. The remainder is 0.
- A remainder of 0 means the last position, which is 1.
The unit digit is 1. This matches pow(3, 4**5, 10) from the code examples.
Example 2: 2^(3^4)
- The cycle of 2 is 2, 4, 8, 6.
- 3 mod 4 = 3 and the exponent 4 is even, so 3^4 ≡ 1 (mod 4). Indeed 3^4 = 81 and 81 = 4 × 20 + 1.
- Position 1 gives 2.
The unit digit is 2.
Example 3: 7^(9^9)
- The cycle of 7 is 7, 9, 3, 1.
- 9 ≡ 1 (mod 4), so 9^9 ≡ 1 (mod 4).
- Position 1 gives 7.
The unit digit is 7.
Example 4: 3^(7^7)
- The cycle of 3 is 3, 9, 7, 1.
- 7 ≡ 3 (mod 4) and the exponent 7 is odd, so 7^7 ≡ 3 (mod 4). Direct check: 7^7 = 823543, and 43 mod 4 = 3.
- Position 3 gives 7.
The unit digit is 7.
Example 5: a three-level tower, 7^(3^(5^2))
- The cycle of 7 is 7, 9, 3, 1.
- We need 3^(5^2) mod 4. The inner value 5^2 = 25 is odd.
- 3 ≡ 3 (mod 4) raised to an odd exponent gives 3 (mod 4).
- Position 3 gives 3.
The unit digit is 3. At each level you only track the property the level above needs. Here that is parity, not the actual value 25.
Example 6: a cycle of length 2, 9^(8^7)
- The cycle of 9 is 9, 1.
- 8^7 is even, so the position is 2.
- Position 2 gives 1.
The unit digit is 1.
Checking with code
The function below applies the cycle rules to a tower a^(b^c). The test loop compares it against Python's three-argument pow over many small cases.
def unit_digit_rule(a, b, c):
"""Unit digit of a^(b^c) for positive integers, using cycle rules."""
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]
length = len(cycle)
r = pow(b, c, length) # exponent b^c modulo the cycle length
return cycle[(r - 1) % length] # r = 0 maps to the last position
for a in range(1, 31):
for b in range(1, 9):
for c in range(1, 7):
assert unit_digit_rule(a, b, c) == pow(a, b**c, 10)
print(unit_digit_rule(3, 4, 5)) # 1
print(unit_digit_rule(2, 3, 4)) # 2
print(unit_digit_rule(7, 9, 9)) # 7
print(unit_digit_rule(3, 7, 7)) # 7The expression (r - 1) % length encodes the position rule. For r = 0 it gives index -1 modulo the length, which is the last entry. For r between 1 and length minus 1 it gives the r-th entry.
Common errors
- Reducing the exponent mod 10. The base's unit digit uses mod 10, but the exponent uses the cycle length. For 3^(4^5) the exponent 1024 should be reduced mod 4, not mod 10.
- Treating remainder 0 as position 0 or position 1. It is the last entry of the cycle. For the cycle 2, 4, 8, 6, a remainder of 0 gives 6, not 2.
- Evaluating bottom-up. Computing (a^b)^c instead of a^(b^c) changes the answer in general.
- Forgetting the c = 1 case. With an even base and exponent 1, the base is not a multiple of 4. For example 6^1 mod 4 = 2, not 0.
- Using the full base instead of its unit digit. The base 17 behaves like 7. The base 28 behaves like 8. The exponent rules, however, use the whole value of b modulo 4, not its unit digit.
Edge cases to know
- Base ending in 0, 1, 5 or 6. The tower collapses immediately and the exponent does not matter. The unit digit of 25^(anything) is 5.
- Bases 4 and 9 with a cycle of length 2. Only parity matters. A tower whose exponent is even gives 6 for base 4 and 1 for base 9.
- A top exponent of 1. If the top value is 1, the layer below is just its base. So 2^(5^1) uses 5 mod 4 = 1, giving position 1, so the unit digit is 2.
- A zero exponent. If an exponent can be 0, then a^0 = 1 for any nonzero a. The cycle rule assumes an exponent of at least 1, so handle zero separately.
Summary of the method
- Reduce the base to its unit digit and look up the cycle.
- If the digit is 0, 1, 5 or 6, stop.
- Find the exponent modulo 4 (or its parity for a length-2 cycle) using the case table.
- Map the remainder to a position, with 0 meaning the last one.
- Confirm with a quick test on a smaller version of the tower if you have time.
Exercise
Find the unit digit of 8^(3^(6^2)).
Show answer
The cycle of 8 is 8, 4, 2, 6. The exponent is 3^(6^2) = 3^36. Since 3 ≡ 3 (mod 4) and 36 is even, 3^36 ≡ 1 (mod 4). Position 1 gives 8. The unit digit is 8.
Exercise
Find the unit digit of 2^(6^(4^3)). Explain why the remainder is 0 and what position that selects.
Show answer
The cycle of 2 is 2, 4, 8, 6. The exponent 6^(4^3) has an even base and an exponent 4^3 = 64 ≥ 2, so it is a multiple of 4 and the remainder is 0. A remainder of 0 selects the last position, which is 6. The unit digit is 6.
Exercise
Find the unit digit of 4^(7^(10^5)). Then state which of the two possible results you would get if you wrongly used the exponent mod 4 as position 0.
Show answer
The cycle of 4 is 4, 6, so only parity matters. The exponent 7^(10^5) is odd because 7 is odd. Position 1 gives 4, so the unit digit is 4. A bad "position 0" reading has no valid entry, and a bad "last position" reading would give 6. The error is treating an odd exponent as if it were even.