Search Knowledge

© 2026 LIBREUNI PROJECT

Divisibility and Congruence

Divisibility and Congruence

Divisibility is the fundamental relation of number theory. We say aa divides bb (a∣ba \mid b) if there exists an integer kk such that b=akb = ak.

1. The Division Algorithm

For any integers aa and bb (with b>0b > 0), there exist unique integers qq (quotient) and rr (remainder) such that: a=bq+r,0≤r<ba = bq + r, \quad 0 \le r < b

python
1def division_demo(a, b):
2 q = a // b
3 r = a % b
4 print(f"{a} = {b} * {q} + {r}")
5 return q, r
6 
7division_demo(103, 7)

2. Congruence Relations

Gauss introduced the notation a≡b(modn)a \equiv b \pmod{n} to signify that aa and bb have the same remainder when divided by nn. This is an equivalence relation, meaning it is reflexive, symmetric, and transitive.

Properties of Congruence:

  1. If a≡ba \equiv b and c≡dc \equiv d, then a+c≡b+d(modn)a + c \equiv b + d \pmod{n}.
  2. If a≡ba \equiv b and c≡dc \equiv d, then ac≡bd(modn)ac \equiv bd \pmod{n}.
  3. ak≡bk(modn)a^k \equiv b^k \pmod{n} for any k≥0k \ge 0.

If x ≡ 3 (mod 7), what is x^2 mod 7?

3. Fast Modular Exponentiation

In cryptography, we often calculate ab mod na^b \bmod n where bb is a massive number. We use the “Square and Multiply” algorithm to do this in logarithmic time.

python
1def fast_pow(base, power, mod):
2 result = 1
3 while power > 0:
4 if power % 2 == 1:
5 result = (result * base) % mod
6 base = (base * base) % mod
7 power //= 2
8 return result
9 
10# Calculate 7^100 mod 13
11print(f"7^100 mod 13: {fast_pow(7, 100, 13)}")
12# Compare with Python's built-in pow()
13print(f"Verify with pow(7, 100, 13): {pow(7, 100, 13)}")

4. Fermat’s Little Theorem

If pp is prime and aa is not divisible by pp, then ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}. This provides a fast way to check if a number is probably prime (the Fermat Primality Test).

Using Fermat's Little Theorem, what is 2^10 mod 11?

5. Summary Check

If a ≡ b (mod n), what can we say about (a - b)?