Divisibility and Congruence
Divisibility is the fundamental relation of number theory. We say divides () if there exists an integer such that .
1. The Division Algorithm
For any integers and (with ), there exist unique integers (quotient) and (remainder) such that:
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 to signify that and have the same remainder when divided by . This is an equivalence relation, meaning it is reflexive, symmetric, and transitive.
Properties of Congruence:
- If and , then .
- If and , then .
- for any .
If x ≡ 3 (mod 7), what is x^2 mod 7?
3. Fast Modular Exponentiation
In cryptography, we often calculate where 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 is prime and is not divisible by , then . This provides a fast way to check if a number is probably prime (the Fermat Primality Test).