Number Theory
โน๏ธ Why It Matters
Number theory, once considered the purest branch of mathematics, now powers modern cryptography. RSA encryption secures online transactions, Diffie-Hellman key exchange enables secure communication, and blockchain uses cryptographic hashing. Modular arithmetic underpins hash tables, checksums, and pseudorandom number generation. Understanding number theory is essential for cybersecurity, cryptography, and any system that handles sensitive data.
Definitions
DfDivisibility
An integer divides (written ) if there exists an integer such that . If does not divide , we write .
DfPrime Number
A prime number is an integer whose only positive divisors are and . The Fundamental Theorem of Arithmetic states every integer has a unique prime factorization.
DfGreatest Common Divisor
The greatest common divisor is the largest positive integer dividing both and . Two integers are coprime if .
DfModular Congruence
means . Congruence is an equivalence relation that partitions integers into residue classes.
DfModular Inverse
The modular inverse is the integer such that . It exists if and only if .
DfEuler's Totient Function
counts the integers from to that are coprime to . For : .
DfOrder of an Element
The order of modulo is the smallest positive integer such that .
Formulas
Fermat's Little Theorem
Here,
- =A prime number
- =Integer not divisible by p
Euler's Theorem
Here,
- =Euler's totient function
- =Integer coprime to n
Chinese Remainder Theorem
Here,
- =Pairwise coprime moduli
- =Residues
Euler's Totient Formula
Here,
- =Prime factor of n
Python Implementations
GCD and Extended Euclidean Algorithm
def gcd(a, b):
while b:
a, b = b, a % b
return a
def extended_gcd(a, b):
if a == 0:
return b, 0, 1
g, x, y = extended_gcd(b % a, a)
return g, y - (b // a) * x, x
def mod_inverse(a, m):
g, x, _ = extended_gcd(a % m, m)
if g != 1:
raise ValueError("Modular inverse does not exist")
return x % m
def lcm(a, b):
return a * b // gcd(a, b)
print(f"gcd(48, 18) = {gcd(48, 18)}")
print(f"lcm(4, 6) = {lcm(4, 6)}")
print(f"mod_inverse(3, 7) = {mod_inverse(3, 7)}")
Modular Exponentiation
def mod_pow(base, exp, mod):
result = 1
base %= mod
while exp > 0:
if exp % 2 == 1:
result = (result * base) % mod
exp //= 2
base = (base * base) % mod
return result
def is_prime_miller_rabin(n, k=10):
if n < 2:
return False
if n == 2 or n == 3:
return True
if n % 2 == 0:
return False
d, r = n - 1, 0
while d % 2 == 0:
d //= 2
r += 1
for _ in range(k):
a = 2 + (hash(str(_)) % (n - 4))
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = pow(x, 2, n)
if x == n - 1:
break
else:
return False
return True
print(f"7^222 mod 11 = {mod_pow(7, 222, 11)}")
print(f"Is 97 prime: {is_prime_miller_rabin(97)}")
Sieve of Eratosthenes
def sieve_of_eratosthenes(limit):
is_prime = [True] * (limit + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
for j in range(i*i, limit + 1, i):
is_prime[j] = False
return [i for i in range(2, limit + 1) if is_prime[i]]
primes = sieve_of_eratosthenes(100)
print(f"Primes below 100: {primes}")
print(f"Count: {len(primes)}")
Euler's Totient Function
def euler_totient(n):
result = n
p = 2
temp = n
while p * p <= temp:
if temp % p == 0:
while temp % p == 0:
temp //= p
result -= result // p
p += 1
if temp > 1:
result -= result // temp
return result
for n in range(1, 21):
print(f"phi({n}) = {euler_totient(n)}")
Chinese Remainder Theorem
def chinese_remainder_theorem(remainders, moduli):
M = 1
for m in moduli:
M *= m
x = 0
for i in range(len(moduli)):
Mi = M // moduli[i]
yi = mod_inverse(Mi, moduli[i])
x += remainders[i] * Mi * yi
return x % M
# Solve: x โก 2 (mod 3), x โก 3 (mod 5), x โก 2 (mod 7)
remainders = [2, 3, 2]
moduli = [3, 5, 7]
print(f"Solution: x = {chinese_remainder_theorem(remainders, moduli)}")
Applications in AI/ML
โน๏ธ Applications in AI and Machine Learning
- Cryptography: RSA, Diffie-Hellman, Elliptic Curve Cryptography all use modular arithmetic
- Hash Functions: Modular hashing for hash tables, Bloom filters, and checksums
- Pseudorandom Number Generators: Linear congruential generators use modular arithmetic
- Coding Theory: Error-correcting codes use finite field arithmetic
- Blockchain: Proof-of-work uses modular exponentiation; digital signatures use number theory
- Machine Learning: Feature hashing uses modular operations for dimensionality reduction
- Secure Multi-Party Computation: Protocols based on number-theoretic assumptions
Common Mistakes
| Mistake | Correction |
|---|---|
| Assuming implies or is prime | Coprime numbers are not necessarily prime (e.g., 8 and 15) |
| Forgetting modular inverse requires | Inverse does not exist if and share a factor |
| Applying Fermat's theorem to composite moduli | Fermat's theorem requires to be prime; use Euler's theorem for composite |
| Miscomputing | For : , not |
| Confusing with | Congruence means ; there can be many values of |
| Assuming modular arithmetic preserves division | does NOT imply unless |
| Forgetting Chinese Remainder Theorem requires coprime moduli | CRT only works when moduli are pairwise coprime |
| Using small primes for cryptographic applications | RSA requires primes with hundreds of digits for security |
Interview Questions
-
What is the Euclidean algorithm and its time complexity? It computes by repeated division: . Time complexity: .
-
How do you compute modular inverse? Use the extended Euclidean algorithm: find such that . Then is the inverse.
-
Explain Fermat's Little Theorem with an example. If and , then . This means .
-
How does RSA encryption work? Choose primes , compute and . Pick coprime to . Private key . Encrypt: . Decrypt: .
-
What is the Chinese Remainder Theorem? A system of congruences with pairwise coprime has a unique solution modulo .
-
How do you check if a number is prime efficiently? For small numbers, trial division up to . For large numbers, use Miller-Rabin probabilistic test (polynomial time).
-
What is Euler's totient function? counts integers from 1 to coprime to . For : .
-
Why is modular arithmetic important in computer science? It provides efficient computation with bounded integers, underpins cryptography and hashing, and enables arithmetic in finite fields.
Practice Problems
๐Practice: Fermat's Little Theorem
Problem: Find .
๐กSolution: Fermat's Little Theorem
By Fermat's Little Theorem: . Write . Then .
๐Practice: Extended GCD
Problem: Find integers such that .
๐กSolution: Extended GCD
. Back-substitution: , so . Thus .
๐Practice: Euler's Totient
Problem: Compute .
๐กSolution: Euler's Totient
. .
๐Practice: Modular Inverse
Problem: Find .
๐กSolution: Modular Inverse
Using extended Euclidean algorithm: , , . Back-substituting: . So .
Primality Testing Deep Dive
โน๏ธ Primality Testing Methods
There are several approaches to testing if a number is prime:
Deterministic Methods:
- Trial division: โ check all divisors up to
- Sieve of Eratosthenes: โ find all primes up to
Probabilistic Methods:
- Fermat test: โ fast but fails for Carmichael numbers
- Miller-Rabin: โ reliable with rounds; error probability
- AKS: โ first deterministic polynomial-time algorithm (2002)
Practical Choice: Miller-Rabin with 10-20 rounds gives error probability , sufficient for cryptographic applications.
def trial_division(n):
if n < 2:
return False
if n < 4:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def fermat_test(n, k=10):
if n < 2:
return False
if n == 2 or n == 3:
return True
for _ in range(k):
a = 2 + (hash(str(_)) % (n - 3))
if pow(a, n - 1, n) != 1:
return False
return True
# Compare methods
test_numbers = [97, 561, 1105, 1729, 2147483647]
for n in test_numbers:
td = trial_division(n)
fr = fermat_test(n)
print(f"{n}: trial={td}, fermat={fr}")
Quadratic Residues
DfQuadratic Residue
An integer is a quadratic residue modulo if there exists such that . If no such exists, is a quadratic non-residue.
ThLaw of Quadratic Reciprocity
For distinct odd primes and : , where is the Legendre symbol.
def legendre_symbol(a, p):
if p < 2 or p % 2 == 0:
raise ValueError("p must be an odd prime")
a = a % p
if a == 0:
return 0
ls = pow(a, (p - 1) // 2, p)
return -1 if ls == p - 1 else ls
def quadratic_residues(p):
return sorted(set(pow(x, 2, p) for x in range(p)))
for p in [7, 11, 13]:
qr = quadratic_residues(p)
print(f"p={p}: QR={qr}, QNR={[x for x in range(1, p) if x not in qr]}")
Quick Reference
| Concept | Formula |
|---|---|
| Euclidean algorithm: | |
| Extended Euclidean algorithm | |
| Modular exponentiation (square-and-multiply) | |
| for prime | |
| general | |
| Fermat's theorem | |
| Euler's theorem | |
| CRT | Unique solution mod for coprime moduli |
| Sieve complexity | |
| Euclidean complexity | |
| Miller-Rabin | , error |
Cross-References
- Graph Theory รขโฌโ Discrete Graphs: Graph coloring uses modular arithmetic
- Trees รขโฌโ Discrete Trees: Cayley's formula involves exponentiation
- Recurrence Relations รขโฌโ Discrete Recurrence: Modular exponentiation is recursive
- Boolean Algebra รขโฌโ Discrete Boolean: Finite field arithmetic for error-correcting codes
- Automata Theory รขโฌโ Discrete Automata: Finite state machines over modular states
- Applications รขโฌโ Discrete Applications: Cryptography and coding theory use number theory