Sub topic 6
Remainders of Large Powers by Finding the Cycle
This chapter assumes you can compute a remainder with the division algorithm and use congruence notation. You should also be able to reduce sums and products term by term, and to use negative remainders. Here those tools are applied to expressions like 7^100 or 13^2025 divided by a small number.
Why powers repeat
Take a base a and a modulus n. List the remainders of a^1, a^2, a^3, and so on. Each term is the previous remainder times a, reduced mod n:
a^(k+1) ≡ a^k · a (mod n)
The next remainder therefore depends only on the current remainder. There are at most n possible remainders, so a remainder must eventually repeat. From that point the sequence repeats forever. The repeating block is the cycle, and its length is the period.
Example: powers of 3 modulo 7.
- 3^1 = 3
- 3^2 = 9 ≡ 2
- 3^3 ≡ 2 · 3 = 6
- 3^4 ≡ 6 · 3 = 18 ≡ 4
- 3^5 ≡ 4 · 3 = 12 ≡ 5
- 3^6 ≡ 5 · 3 = 15 ≡ 1
- 3^7 ≡ 1 · 3 = 3
The remainders are 3, 2, 6, 4, 5, 1, then 3 again. The period is 6.
The procedure
- Reduce the base mod n first, so you work with a small number.
- Multiply repeatedly, reducing at every step, until the remainder returns to 1 (or to a value already seen).
- Record the period p.
- Divide the exponent by p and keep the remainder r.
- The answer is the remainder of a^r. If r = 0, use a^p.
Step 4 works because, when a^p ≡ 1, writing the exponent as m = p·q + r gives:
a^m = (a^p)^q · a^r ≡ 1^q · a^r = a^r (mod n)
Worked example: a cycle that returns to 1
Find the remainder when 7^100 is divided by 5.
- 7 ≡ 2 (mod 5), so work with powers of 2.
- 2^1 = 2, 2^2 = 4, 2^3 = 8 ≡ 3, 2^4 = 6 ≡ 1.
- The period is 4.
- 100 = 4 · 25 + 0, so r = 0 and we use the 4th term.
- The remainder is 1.
print([pow(2, k, 5) for k in range(1, 9)]) # [2, 4, 3, 1, 2, 4, 3, 1]
print(pow(7, 100, 5)) # 1When the cycle starts at 1
For a cycle that returns to 1, the first power is a^p ≡ 1. That happens whenever a and n share no common factor, which is called being coprime. In that case the sequence is purely periodic from the start.
If a and n share a factor, the cycle may never reach 1. Powers of 2 modulo 10 give 2, 4, 8, 6, 2, 4, 8, 6. The remainder 1 never appears, but the sequence still repeats with period 4. When no 1 appears, the remainders repeat as a block. Then match the exponent to the position inside the block.
Worked example: the last digit
The last digit of a number is its remainder mod 10. Find the last digit of 7^2025.
- Powers of 7 mod 10 are 7, 9, 3, 1, then repeat. The period is 4.
- 2025 = 4 · 506 + 1, so r = 1.
- The last digit is the first term of the cycle, which is 7.
print([pow(7, k, 10) for k in range(1, 9)]) # [7, 9, 3, 1, 7, 9, 3, 1]
print(pow(7, 2025, 10)) # 7Cycles for the last digit
Every last digit has a cycle with period 1, 2 or 4. Memorising them saves time.
| Last digit of base | Cycle of last digits | Period |
|---|---|---|
| 0, 1, 5, 6 | the digit itself | 1 |
| 4, 9 | 4, 6 or 9, 1 | 2 |
| 2, 3, 7, 8 | 2, 4, 8, 6 or 3, 9, 7, 1 or 7, 9, 3, 1 or 8, 4, 2, 6 | 4 |
Because 4 is a multiple of 1 and 2, you can always divide the exponent by 4. The remainder tells you the position. A remainder of 0 means the 4th position.
Using negative remainders to shorten the cycle
Negative remainders often give a period of 2, which is easier than finding a long cycle.
Find the remainder when 8^77 is divided by 9.
- 8 ≡ −1 (mod 9).
- So 8^77 ≡ (−1)^77 = −1 ≡ 8 (mod 9).
A base congruent to −1 has period 2: the powers alternate between −1 and 1. A base congruent to 1 has period 1.
Another case: the remainder of 10^50 divided by 11. Since 10 ≡ −1 (mod 11), 10^50 ≡ (−1)^50 = 1.
print(pow(8, 77, 9)) # 8
print(pow(10, 50, 11)) # 1Worked example: a composite modulus
Find the remainder when 3^200 is divided by 8.
- 3^1 = 3, 3^2 = 9 ≡ 1.
- The period is 2.
- 200 is even, so r = 0 and the answer is the 2nd term: 1.
Check with 3^201: the exponent is odd, so the remainder is 3.
print(pow(3, 200, 8), pow(3, 201, 8)) # 1 3Reducing an exponent that is itself a power
CAT questions sometimes use a tower such as 5^(3^4). You then need the exponent mod the period, not the full exponent.
Find the remainder when 7^(9^9) is divided by 10.
- The period of 7 mod 10 is 4, so we need 9^9 mod 4.
- 9 ≡ 1 (mod 4), so 9^9 ≡ 1.
- The remainder is that of 7^1, which is 7.
print(pow(7, 9**9, 10)) # 7Be careful when the exponent mod p is 0. In that case use the last term of the cycle, as in the warning above.
Diagram: the cycle as a loop
The remainders of 3^k mod 7 form a closed loop. Moving one step along the loop multiplies by 3.
Common errors
- Dividing the base instead of the exponent. The base is reduced mod n. The exponent is reduced mod the period.
- Using the wrong remainder when the exponent divides evenly. A remainder of 0 means the last term in the cycle.
- Assuming every cycle returns to 1. It only does so when the base and modulus are coprime.
- Mixing up the period and the modulus. For 3 mod 7 the period is 6, not 7.
- Stopping the cycle too early. Keep going until a remainder repeats, not until you have a convenient-looking one.
- Forgetting to reduce at every step. Compute 2^5 = 32 only if needed. Reduce 2^4 ≡ 1 first, then work with small numbers.
A quick decision guide
- If the base is ≡ ±1 mod n, the answer is immediate.
- If the base is small and n is small, list the cycle.
- If n is composite and the base shares a factor with n, list the block and match positions.
- If the exponent is a tower, reduce the top exponent mod the period of the base.
Later chapters give a way to find the period in advance, using Fermat's and Euler's theorems. The listing method here is the fallback and the check.
Exercise
Find the remainder when 13^2025 is divided by 7.
Show answer
13 ≡ −1 (mod 7), so 13^2025 ≡ (−1)^2025 = −1 ≡ 6. The remainder is 6.
Exercise
Find the last digit of 2^1001 + 3^1001.
Show answer
Powers of 2 have last digits 2, 4, 8, 6 with period 4. 1001 = 4 · 250 + 1, so the last digit of 2^1001 is 2. Powers of 3 have last digits 3, 9, 7, 1 with period 4, so the last digit of 3^1001 is 3. The sum ends in 2 + 3 = 5.
Exercise
Find the remainder when 5^123 is divided by 13.
Show answer
5^2 = 25 ≡ −1 (mod 13). So 5^4 ≡ 1 and the period is 4. 123 = 4 · 30 + 3, so 5^123 ≡ 5^3 = 125 = 13 · 9 + 8, giving remainder 8.
Exercise
Find the remainder when 4^(5^6) is divided by 9.
Show answer
Powers of 4 mod 9 are 4, 7, 1, so the period is 3. We need 5^6 mod 3. Since 5 ≡ 2 ≡ −1 (mod 3), 5^6 ≡ 1. The remainder is that of 4^1, which is 4.