Knowledge / Mathematics / Number theory basics

Number theory basics

Mathematics (B.Sc) · More core mathematics · free study notes for exams, placements and interviews

In short

Number theory is the study of the integers — divisibility, primes, factorisation and arithmetic "modulo n" — and it is the one branch of pure mathematics that runs modern computing. A B.Sc syllabus (UGC CBCS "Number Theory" / "Discrete Mathematics" papers) covers the division algorithm, GCD by Euclid's algorithm, unique factorisation, congruences, the classical theorems of Fermat, Euler and Wilson, the Chinese Remainder Theorem and multiplicative functions. It is examined in GATE (CS and MA), NET/JAM/GATE-MA, CSIR-NET, ISI/CMI entrance and bank/PSU aptitude papers, and appears directly in placement coding rounds at TCS, Infosys, Wipro, Amazon, Microsoft and payment firms. On the job it underlies RSA and Diffie-Hellman in UPI, Aadhaar e-KYC and digital-signature systems, hashing, checksum digits in Aadhaar and ISBN, and competitive-programming problem sets.

Explained

Divisibility and the division algorithm

GCD, LCM and the Euclidean algorithm

Primes and the fundamental theorem of arithmetic

Congruences and modular arithmetic

Arithmetic (number-theoretic) functions

Applications: cryptography and coding

Key points

Formulas and examples

a = bq + r, 0 ≤ r < b — division algorithm (a dividend, b > 0 divisor, q quotient, r remainder; all integers)
gcd(a, b) = gcd(b, a mod b); gcd(a, 0) = |a| — Euclidean algorithm
gcd(a, b) × lcm(a, b) = |a × b| — two integers only
gcd(a, b) = ax + by — Bézout; x, y integers from the extended Euclidean algorithm
n = p₁a₁ … p_ka_k; d(n) = Π(aᵢ + 1); σ(n) = Π (pᵢaᵢ+1 − 1)/(pᵢ − 1); product of divisors = nd(n)/2
φ(n) = n Π_{p|n} (1 − 1/p); φ(pk) = pk − pk−1; Σ_{d|n} φ(d) = n
ap−1 ≡ 1 (mod p), p prime, gcd(a,p) = 1 — Fermat; aφ(n) ≡ 1 (mod n), gcd(a,n) = 1 — Euler
(p − 1)! ≡ −1 (mod p) ⟺ p prime — Wilson
x ≡ Σ aᵢ Mᵢ yᵢ (mod N), N = Πnᵢ, Mᵢ = N/nᵢ, yᵢ = Mᵢ⁻¹ mod nᵢ — CRT
Exponent of prime p in n! = ⌊n/p⌋ + ⌊n/p²⌋ + ⌊n/p³⌋ + … — Legendre
RSA: c = me mod n, m = cd mod n, ed ≡ 1 (mod φ(n))

Example 1 — Extended Euclid and a modular inverse. Find gcd(1071, 462) and express it as a combination; then find 11⁻¹ mod 26.

Example 2 — Divisor functions and φ. For n = 360.

Example 3 — Chinese Remainder Theorem. Solve x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7).

Example 4 — Reducing a huge power (Euler + fast exponentiation). Find 7222 mod 100.

Example 5 — Trailing zeros and a small RSA.

TheoremConditionStatementTypical use
Fermat's littlep prime, p ∤ aap−1 ≡ 1 (mod p)Inverse mod p, primality screening
Eulergcd(a, n) = 1aφ(n) ≡ 1 (mod n)Reduce exponents, RSA correctness
Wilsonp prime(p−1)! ≡ −1 (mod p)Factorial congruences, primality proof
CRTmoduli pairwise coprimeunique x mod ΠnᵢRemainder systems, RSA speed-up
OperationVia factorisationVia Euclid
gcdmin of exponentsrepeated remainders, O(log n)
lcmmax of exponentsab/gcd(a,b)
Practical for 200-digit numbersNo (factoring is hard)Yes (fast)
def egcd(a, b):                 # returns (g, x, y) with ax + by = g
    if b == 0:
        return (a, 1, 0)
    g, x1, y1 = egcd(b, a % b)
    return (g, y1, x1 - (a // b) * y1)

def power_mod(a, b, n):         # square and multiply, O(log b)
    r = 1
    a %= n
    while b:
        if b & 1:
            r = r * a % n
        a = a * a % n
        b >>= 1
    return r

Where it is used

Interview questions

Common mistakes

Related topics

Group theory and rings (ℤ_n as a group and field) · Cryptography and network security · Discrete mathematics and combinatorics · Algorithms and complexity · Graph theory basics · Linear algebra over finite fields

Open in the Knowledge reader →Find jobs →