โ† Math|77 of 100
Discrete Mathematics

Number Theory

Master modular arithmetic, GCD, primes, Fermat's theorem, and cryptography applications.

๐Ÿ“‚ Numbers๐Ÿ“– Lesson 77 of 100๐ŸŽ“ Free Course

Advertisement

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 aa divides bb (written aโˆฃba \mid b) if there exists an integer kk such that b=akb = ak. If aa does not divide bb, we write aโˆคba \nmid b.

DfPrime Number

A prime number is an integer p>1p > 1 whose only positive divisors are 11 and pp. The Fundamental Theorem of Arithmetic states every integer >1> 1 has a unique prime factorization.

DfGreatest Common Divisor

The greatest common divisor gcdโก(a,b)\gcd(a, b) is the largest positive integer dividing both aa and bb. Two integers are coprime if gcdโก(a,b)=1\gcd(a, b) = 1.

DfModular Congruence

aโ‰กb(modm)a \equiv b \pmod{m} means mโˆฃ(aโˆ’b)m \mid (a - b). Congruence is an equivalence relation that partitions integers into residue classes.

DfModular Inverse

The modular inverse aโˆ’1(modm)a^{-1} \pmod{m} is the integer xx such that axโ‰ก1(modm)ax \equiv 1 \pmod{m}. It exists if and only if gcdโก(a,m)=1\gcd(a, m) = 1.

DfEuler's Totient Function

ฯ•(n)\phi(n) counts the integers from 11 to nn that are coprime to nn. For n=p1e1p2e2โ‹ฏpkekn = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}: ฯ•(n)=nโˆi=1k(1โˆ’1/pi)\phi(n) = n \prod_{i=1}^{k} (1 - 1/p_i).

DfOrder of an Element

The order of aa modulo nn is the smallest positive integer kk such that akโ‰ก1(modn)a^k \equiv 1 \pmod{n}.


Formulas

Fermat's Little Theorem

apโˆ’1โ‰ก1(modp)a^{p-1} \equiv 1 \pmod{p}

Here,

  • pp=A prime number
  • aa=Integer not divisible by p

Euler's Theorem

aฯ•(n)โ‰ก1(modn)a^{\phi(n)} \equiv 1 \pmod{n}

Here,

  • ฯ•(n)\phi(n)=Euler's totient function
  • aa=Integer coprime to n

Chinese Remainder Theorem

xโ‰กa1(modm1),โ€ฆ,xโ‰กak(modmk)x \equiv a_1 \pmod{m_1}, \ldots, x \equiv a_k \pmod{m_k}

Here,

  • m1,โ€ฆ,mkm_1, \ldots, m_k=Pairwise coprime moduli
  • a1,โ€ฆ,aka_1, \ldots, a_k=Residues

Euler's Totient Formula

ฯ•(n)=nโˆpโˆฃn(1โˆ’1p)\phi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right)

Here,

  • pp=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

MistakeCorrection
Assuming gcdโก(a,b)=1\gcd(a, b) = 1 implies aa or bb is primeCoprime numbers are not necessarily prime (e.g., 8 and 15)
Forgetting modular inverse requires gcdโก(a,m)=1\gcd(a, m) = 1Inverse does not exist if aa and mm share a factor
Applying Fermat's theorem to composite moduliFermat's theorem requires pp to be prime; use Euler's theorem for composite
Miscomputing ฯ•(n)\phi(n)For n=pkn = p^k: ฯ•(n)=pkโˆ’pkโˆ’1=pkโˆ’1(pโˆ’1)\phi(n) = p^k - p^{k-1} = p^{k-1}(p-1), not pkโˆ’1p^k - 1
Confusing aโ‰กb(modm)a \equiv b \pmod{m} with a=b+ma = b + mCongruence means mโˆฃ(aโˆ’b)m \mid (a-b); there can be many values of aa
Assuming modular arithmetic preserves divisionaโ‰กb(modm)a \equiv b \pmod{m} does NOT imply acโ‰กbc(modm)ac \equiv bc \pmod{m} unless gcdโก(c,m)=1\gcd(c, m) = 1
Forgetting Chinese Remainder Theorem requires coprime moduliCRT only works when moduli are pairwise coprime
Using small primes for cryptographic applicationsRSA requires primes with hundreds of digits for security

Interview Questions

  1. What is the Euclidean algorithm and its time complexity? It computes gcdโก(a,b)\gcd(a, b) by repeated division: gcdโก(a,b)=gcdโก(b,aโ€Šmodโ€Šb)\gcd(a, b) = \gcd(b, a \bmod b). Time complexity: O(logโก(minโก(a,b)))O(\log(\min(a, b))).

  2. How do you compute modular inverse? Use the extended Euclidean algorithm: find x,yx, y such that ax+my=gcdโก(a,m)=1ax + my = \gcd(a, m) = 1. Then x(modm)x \pmod{m} is the inverse.

  3. Explain Fermat's Little Theorem with an example. If p=7p = 7 and a=3a = 3, then 36โ‰ก1(mod7)3^6 \equiv 1 \pmod{7}. This means 36=729=104ร—7+13^6 = 729 = 104 \times 7 + 1.

  4. How does RSA encryption work? Choose primes p,qp, q, compute n=pqn = pq and ฯ•(n)=(pโˆ’1)(qโˆ’1)\phi(n) = (p-1)(q-1). Pick ee coprime to ฯ•(n)\phi(n). Private key d=eโˆ’1(modฯ•(n))d = e^{-1} \pmod{\phi(n)}. Encrypt: c=me(modn)c = m^e \pmod{n}. Decrypt: m=cd(modn)m = c^d \pmod{n}.

  5. What is the Chinese Remainder Theorem? A system of congruences xโ‰กai(modmi)x \equiv a_i \pmod{m_i} with pairwise coprime mim_i has a unique solution modulo M=โˆmiM = \prod m_i.

  6. How do you check if a number is prime efficiently? For small numbers, trial division up to n\sqrt{n}. For large numbers, use Miller-Rabin probabilistic test (polynomial time).

  7. What is Euler's totient function? ฯ•(n)\phi(n) counts integers from 1 to nn coprime to nn. For n=p1e1โ‹ฏpkekn = p_1^{e_1} \cdots p_k^{e_k}: ฯ•(n)=nโˆ(1โˆ’1/pi)\phi(n) = n \prod (1 - 1/p_i).

  8. 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 7222(mod11)7^{222} \pmod{11}.

๐Ÿ’กSolution: Fermat's Little Theorem

By Fermat's Little Theorem: 710โ‰ก1(mod11)7^{10} \equiv 1 \pmod{11}. Write 222=22ร—10+2222 = 22 \times 10 + 2. Then 7222=(710)22โ‹…72โ‰ก122โ‹…49โ‰ก5(mod11)7^{222} = (7^{10})^{22} \cdot 7^2 \equiv 1^{22} \cdot 49 \equiv 5 \pmod{11}.

๐Ÿ“Practice: Extended GCD

Problem: Find integers x,yx, y such that 35x+15y=gcdโก(35,15)35x + 15y = \gcd(35, 15).

๐Ÿ’กSolution: Extended GCD

gcdโก(35,15)=5\gcd(35, 15) = 5. Back-substitution: 35=2ร—15+535 = 2 \times 15 + 5, so 5=35โˆ’2ร—155 = 35 - 2 \times 15. Thus x=1,y=โˆ’2x = 1, y = -2.

๐Ÿ“Practice: Euler's Totient

Problem: Compute ฯ•(36)\phi(36).

๐Ÿ’กSolution: Euler's Totient

36=22ร—3236 = 2^2 \times 3^2. ฯ•(36)=36(1โˆ’1/2)(1โˆ’1/3)=36ร—1/2ร—2/3=12\phi(36) = 36(1 - 1/2)(1 - 1/3) = 36 \times 1/2 \times 2/3 = 12.

๐Ÿ“Practice: Modular Inverse

Problem: Find 17โˆ’1(mod43)17^{-1} \pmod{43}.

๐Ÿ’กSolution: Modular Inverse

Using extended Euclidean algorithm: 43=2ร—17+943 = 2 \times 17 + 9, 17=1ร—9+817 = 1 \times 9 + 8, 9=1ร—8+19 = 1 \times 8 + 1. Back-substituting: 1=9โˆ’8=9โˆ’(17โˆ’9)=2ร—9โˆ’17=2(43โˆ’2ร—17)โˆ’17=2ร—43โˆ’5ร—171 = 9 - 8 = 9 - (17 - 9) = 2 \times 9 - 17 = 2(43 - 2 \times 17) - 17 = 2 \times 43 - 5 \times 17. So 17โˆ’1โ‰กโˆ’5โ‰ก38(mod43)17^{-1} \equiv -5 \equiv 38 \pmod{43}.


Primality Testing Deep Dive

โ„น๏ธ Primality Testing Methods

There are several approaches to testing if a number is prime:

Deterministic Methods:

  • Trial division: O(n)O(\sqrt{n}) โ€” check all divisors up to n\sqrt{n}
  • Sieve of Eratosthenes: O(nlogโกlogโกn)O(n \log \log n) โ€” find all primes up to nn

Probabilistic Methods:

  • Fermat test: anโˆ’1โ‰ก1(modn)a^{n-1} \equiv 1 \pmod{n} โ€” fast but fails for Carmichael numbers
  • Miller-Rabin: O(klogโก2n)O(k \log^2 n) โ€” reliable with kk rounds; error probability 4โˆ’k4^{-k}
  • AKS: O(logโก6n)O(\log^{6} n) โ€” first deterministic polynomial-time algorithm (2002)

Practical Choice: Miller-Rabin with 10-20 rounds gives error probability <10โˆ’12< 10^{-12}, 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 aa is a quadratic residue modulo pp if there exists xx such that x2โ‰กa(modp)x^2 \equiv a \pmod{p}. If no such xx exists, aa is a quadratic non-residue.

ThLaw of Quadratic Reciprocity

For distinct odd primes pp and qq: (pq)(qp)=(โˆ’1)pโˆ’12โ‹…qโˆ’12\left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{p-1}{2} \cdot \frac{q-1}{2}}, where (ap)\left(\frac{a}{p}\right) 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

ConceptFormula
gcdโก(a,b)\gcd(a, b)Euclidean algorithm: gcdโก(a,b)=gcdโก(b,aโ€Šmodโ€Šb)\gcd(a, b) = \gcd(b, a \bmod b)
aโˆ’1(modm)a^{-1} \pmod{m}Extended Euclidean algorithm
ak(modn)a^k \pmod{n}Modular exponentiation (square-and-multiply)
ฯ•(p)\phi(p)pโˆ’1p - 1 for prime pp
ฯ•(pk)\phi(p^k)pkโˆ’pkโˆ’1p^k - p^{k-1}
ฯ•(n)\phi(n) generalnโˆpโˆฃn(1โˆ’1/p)n \prod_{p \mid n}(1 - 1/p)
Fermat's theoremapโˆ’1โ‰ก1(modp)a^{p-1} \equiv 1 \pmod{p}
Euler's theoremaฯ•(n)โ‰ก1(modn)a^{\phi(n)} \equiv 1 \pmod{n}
CRTUnique solution mod โˆmi\prod m_i for coprime moduli
Sieve complexityO(nlogโกlogโกn)O(n \log \log n)
Euclidean complexityO(logโกminโก(a,b))O(\log \min(a, b))
Miller-RabinO(klogโก2n)O(k \log^2 n), error 4โˆ’k4^{-k}

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
Lesson Progress77 / 100