πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Number Theory

Discrete MathematicsNumbers🟒 Free Lesson

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

⭐

Premium Content

Number Theory

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert Mathematics Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement