Sub topic 7
Last One and Two Digits of a Power
This chapter assumes you can already do these things:
- Write a number as divisor × quotient + remainder.
- Use congruence notation (a ≡ b mod n).
- Reduce a product by multiplying the remainders of its factors.
- Spot a repeating cycle in the remainders of successive powers.
Last digits are remainders
The last digit of a positive integer N is N mod 10. The last two digits are N mod 100, written with a leading zero if needed. So "find the last digit of 7^100" means "find 7^100 mod 10". You already have the tools for this. This chapter organises them into a routine for the two cases CAT asks most often.
The last digit: cycles of length 1, 2 or 4
Only the last digit of the base matters, because (10k + d)^n ≡ d^n (mod 10). Printing the first eight powers of each digit shows the cycles.
for d in range(10):
print(d, [pow(d, k, 10) for k in range(1, 9)])
# 0 [0, 0, 0, 0, 0, 0, 0, 0]
# 1 [1, 1, 1, 1, 1, 1, 1, 1]
# 2 [2, 4, 8, 6, 2, 4, 8, 6]
# 3 [3, 9, 7, 1, 3, 9, 7, 1]
# 4 [4, 6, 4, 6, 4, 6, 4, 6]
# 5 [5, 5, 5, 5, 5, 5, 5, 5]
# 6 [6, 6, 6, 6, 6, 6, 6, 6]
# 7 [7, 9, 3, 1, 7, 9, 3, 1]
# 8 [8, 4, 2, 6, 8, 4, 2, 6]
# 9 [9, 1, 9, 1, 9, 1, 9, 1]| Last digit of base | Cycle of last digits (exponent 1, 2, 3, ...) | Cycle length |
|---|---|---|
| 0, 1, 5, 6 | the digit itself | 1 |
| 4, 9 | 4, 6 and 9, 1 | 2 |
| 2, 3, 7, 8 | 2, 4, 8, 6 / 3, 9, 7, 1 / 7, 9, 3, 1 / 8, 4, 2, 6 | 4 |
Every cycle length (1, 2 or 4) divides 4. So you can always reduce the exponent mod 4, which gives one rule for all digits:
- Take the last digit d of the base.
- Compute r = exponent mod 4. If r = 0, use r = 4.
- The answer is the last digit of d^r.
Step 2 matters because the cycle starts at exponent 1, not exponent 0. If the exponent is a multiple of 4, you want the fourth term of the cycle, not the zeroth.
Example 1. Last digit of 7^100. Since 100 mod 4 = 0, use r = 4. 7^4 = 2401, so the answer is 1.
Example 2. Last digit of 13^45 × 17^22 × 8^31.
- 13^45 behaves like 3^45. 45 mod 4 = 1, so the digit is 3.
- 17^22 behaves like 7^22. 22 mod 4 = 2, so the digit is 9.
- 8^31: 31 mod 4 = 3, and 8^3 = 512, so the digit is 2.
Multiply the digits, keeping only the last digit each time: 3 × 9 = 27 gives 7, and 7 × 2 = 14 gives 4. The answer is 4.
When the exponent is itself a power
For the last digit of 2^(3^100), you need the exponent 3^100 mod 4, not mod 10. Since 3 ≡ −1 (mod 4), 3^100 ≡ (−1)^100 = 1. So the expression has the same last digit as 2^1, which is 2.
The modulus for the exponent is the cycle length of the base's digit (4 here). It is not the modulus of the final answer (10).
def last_digit(base, exp):
"""Last digit of base**exp for exp >= 1, using the mod-4 rule."""
r = exp % 4 or 4
return pow(base % 10, r, 10)
for base in range(1, 120):
for exp in range(1, 60):
assert last_digit(base, exp) == pow(base, exp, 10)
print(last_digit(7, 100)) # 1
print(pow(2, 3**100 % 4, 10)) # 2The last two digits: remainders mod 100
For two digits there is no single cycle table. Pick a technique based on the base. The diagram shows the decision.
Technique 1: find a power that gives 01
If you can find k with a^k ≡ 1 (mod 100), reduce the exponent mod k. Useful facts:
- 7^2 = 49 and 7^4 = 2401, so 7^4 ≡ 01.
- 49^2 = 2401 ≡ 01 and 51^2 = 2601 ≡ 01.
- 43^2 = 1849 ≡ 49, so 43^4 ≡ 49^2 ≡ 01.
Example 3. Last two digits of 43^101. Since 43^4 ≡ 1, 43^100 ≡ 1, so 43^101 ≡ 43. The answer is 43.
Example 4. Last two digits of 7^2025. 2025 mod 4 = 1, so 7^2025 ≡ 7^1, which is 07. Keep the leading zero when you report two digits.
Technique 2: use −1
A base near a multiple of 100 can be written with a negative remainder. 99 ≡ −1 (mod 100), so 99^n ≡ (−1)^n. For n = 99 this gives −1 ≡ 99. The same idea works for 101 ≡ 1 and 49^n = (50 − 1)^n.
Technique 3: bases ending in 1, via the binomial theorem
Write the base as 1 + 10k. Expand (1 + 10k)^n. Every term from the third onward contains 10^2 = 100, so it vanishes mod 100:
(1 + 10k)^n ≡ 1 + 10kn (mod 100)
Example 5. Last two digits of 31^45. Here k = 3, so 31^45 ≡ 1 + 30 × 45 = 1351 ≡ 51 (mod 100). Only the last two digits of 10kn matter, which is why you only need kn mod 10.
Technique 4: bases that fix themselves
Some residues mod 100 reproduce themselves when multiplied:
- 76 × 76 = 5776 ≡ 76. Any power of 76 ends in 76.
- 25 × 25 = 625 ≡ 25. Any power of 25 ends in 25.
- 24^2 = 576 ≡ 76, so 24^n ends in 76 for even n and in 24 for odd n (24 × 76 = 1824 ≡ 24).
- Any 10m raised to a power of at least 2 ends in 00.
Technique 5: powers of 2
2^n mod 100 does not return to 2. The sequence for n ≥ 2 repeats with period 20, because 2^20 = 1048576 ≡ 76 and 76 × 4 = 304 ≡ 4. So 2^100 = (2^20)^5 ≡ 76^5 ≡ 76.
def two_digits(base, exp):
return f"{pow(base, exp, 100):02d}"
print(two_digits(43, 101)) # 43
print(two_digits(7, 2025)) # 07
print(two_digits(31, 45)) # 51
print(two_digits(99, 99)) # 99
print(two_digits(24, 25)) # 24
print(two_digits(24, 26)) # 76
print(two_digits(2, 100)) # 76
print(two_digits(20, 5)) # 00
print(pow(3, 20, 100)) # 1The last line shows a general pattern. For any base coprime to 100, a^20 ≡ 1 (mod 100). Euler's theorem, covered in a later chapter, guarantees a^40 ≡ 1, and the true cycle often divides 20. For now, treat 20 as a fallback you can verify, and prefer the shorter cycles above when you can spot them.
Edge cases and common errors
- Exponent remainder 0. Use the last term of the cycle, not exponent 0. 3^8 ends in 1, not in "3^0 = 1 by luck". For 2^8 the same slip would give 1 instead of 6.
- Leading zeros. 7^2025 ends in 07, not 7. A question asking for the sum of the last two digits expects 0 + 7.
- Even bases and mod 100. Because 100 = 4 × 25 and an even base is not coprime to 100, there is no pure cycle from exponent 1. Either check the pre-period (2^n, n ≥ 2) or split the problem mod 4 and mod 25.
- Towers. In a^(b^c), reduce b^c by the cycle length of a, not by 10.
- Base ending in 0. The last digit is 0 for any exponent of at least 1. The last two digits are 00 once the exponent is at least 2.
Exercise
Find the last two digits of 7^2025 × 43^2025. (Hint: 7 × 43 = 301.)
Show answer
7^2025 × 43^2025 = 301^2025 ≡ 1^2025 = 1 (mod 100), since 301 ≡ 1. The last two digits are 01. This agrees with Examples 3 and 4 only in spirit: 7^2025 ≡ 07 and 43^2025 ≡ 43 (because 2025 mod 4 = 1), and 07 × 43 = 301 ≡ 01.
Exercise
Find the last digit of 4^(3^50).
Show answer
The cycle for 4 has length 2: odd exponents give 4, even exponents give 6. 3^50 is odd, so the last digit is 4.
Exercise
Find the last two digits of 51^73 using the binomial shortcut.
Show answer
Write 51 = 1 + 10 × 5, so k = 5. Then 51^73 ≡ 1 + 10 × 5 × 73 = 1 + 3650 = 3651, which ends in 51. The last two digits are 51.