Sub topic 2
Divisibility tests seen as remainders
Prerequisites
This chapter assumes you know integer division with quotient and remainder, prime factorisation, and the exponent rules for positive integer powers. Every test here asks one question: does the remainder equal zero?
Divisibility tests as remainder statements
A number is divisible by $d$ when it leaves remainder 0 on division by $d$. Each common test works because of how powers of 10 leave remainders.
- 2 and 5: $10$ is divisible by 2 and 5, so only the last digit matters.
- 4 and 8: $100$ is divisible by 4, and $1000$ is divisible by 8. Only the last two (or three) digits matter.
- 3 and 9: $10 \equiv 1 \pmod 9$, so a number leaves the same remainder as its digit sum.
- 11: $10 \equiv -1 \pmod{11}$, so the alternating digit sum leaves the same remainder.
n = 918082
print(n % 11) # 0
digits = [int(c) for c in str(n)][::-1]
alt = sum(d if i % 2 == 0 else -d for i, d in enumerate(digits))
print(alt, alt % 11) # -22 0Remainders of large powers by finding the cycle
To find $7^{100} \bmod 5$, reduce the base first: $7 \equiv 2 \pmod 5$. Then list the powers of 2 mod 5: 2, 4, 3, 1. The remainders repeat every 4 steps. Since $100 \bmod 4 = 0$, the remainder is 1.
print(pow(7, 100, 5)) # 1Last one and two digits of a power
The last digit of a number is its remainder mod 10. The last two digits are its remainder mod 100.
Powers of 7 mod 100 go 07, 49, 43, 01, then repeat every 4 steps. Since $100 \bmod 4 = 0$, $7^{100}$ ends in 01.
print(pow(7, 100, 100)) # 1, so the last two digits are 01> Note: check the cycle by listing a few powers before you rely on it. The cycle length can be shorter than you expect.
Using $a^n \pm b^n$ factors and the remainder theorem
Two identities speed up remainder work:
- $a^n - b^n$ is divisible by $a - b$ for every positive integer $n$.
- $a^n + b^n$ is divisible by $a + b$ when $n$ is odd.
For example, $2^{35} + 3^{35}$ is divisible by $2 + 3 = 5$, so its remainder mod 5 is 0.
The remainder theorem gives a second shortcut. Dividing a polynomial $P(x)$ by $x - a$ leaves remainder $P(a)$. So $x^{100}$ divided by $x - 2$ leaves remainder $2^{100}$.
flowchart TD
A[Remainder question] --> B{Is the base small mod n?}
B -->|Yes| C[List powers until the remainder repeats]
B -->|No| D{Does a plus or minus b factor apply?}
D -->|Yes| E[Use a minus b or a plus b divisibility]
D -->|No| F[Reduce base mod n first]Exercise
Exercise
What is the remainder when $3^{20}$ is divided by 100?
Show answer
Powers of 3 mod 100 repeat every 20 steps, and $3^{20} \equiv 01 \pmod{100}$. So the remainder is 1. You can check with pow(3, 20, 100), which returns 1.