Unit 3: GCD and Primality testing
Number-theoretic algorithms rest on divisibility: how integers factor, share common divisors, and behave under the primes. This unit builds from the greatest common divisor (GCD) up to primality-flavoured classifications, treating each as a computational problem with a concrete algorithm and complexity.
I. Foundations and Conventions
Every integer > 1 has a unique prime factorisation (Fundamental Theorem of Arithmetic), and this fact underlies divisor counting, factor-finding, and the number classes below.
- GCD: The largest integer dividing both
aandb. Writtengcd(a, b); by conventiongcd(a, 0) = a. - Prime: An integer
p > 1whose only positive divisors are1andp. Smallest is2, the only even prime. - Prime factorisation:
n = p₁^e₁ · p₂^e₂ · … · p_k^e_k, with distinct primespᵢand exponentseᵢ ≥ 1. - Coprime:
aandbare coprime whengcd(a, b) = 1; they share no prime factor. - √n bound: If
nhas a factor> √n, its co-factor is< √n, so testing divisors up to⌊√n⌋suffices — the key complexity lever throughout.
II. Basic Euclidean Algorithm
A. Statement and principle
Repeatedly replacing the larger number by its remainder against the smaller preserves the GCD until a remainder of zero exposes the answer.
- Invariant:
gcd(a, b) = gcd(b, a mod b), because any common divisor ofaandbalso dividesa − q·b. - Termination: The remainder strictly decreases and stays non-negative, so it reaches
0. - Complexity:
O(log(min(a, b)))divisions; worst case occurs on consecutive Fibonacci numbers.
def gcd(a, b):
while b:
a, b = b, a % b
return a- Symbols:
a,bare inputs;a % bis the remainder ofadivided byb. - Example:
gcd(48, 18) → gcd(18, 12) → gcd(12, 6) → gcd(6, 0) = 6.
III. Extended Euclidean Algorithm
A. Statement and Bézout's identity
Beyond the GCD, this variant produces integer coefficients expressing that GCD as a linear combination of the inputs.
- Bézout's identity: There exist integers
x, ywitha·x + b·y = gcd(a, b). - Recurrence: If
gcd(b, a mod b) = b·x₁ + (a mod b)·y₁, then back-substitution givesx = y₁,y = x₁ − ⌊a/b⌋·y₁. - Use: Computes the modular inverse —
xisa⁻¹ mod bwhengcd(a, b) = 1.
def ext_gcd(a, b):
if b == 0:
return a, 1, 0
g, x1, y1 = ext_gcd(b, a % b)
return g, y1, x1 - (a // b) * y1- Symbols: returns
(g, x, y)satisfyinga·x + b·y = g. - Example:
ext_gcd(30, 12) = (6, 1, −2)since30·1 + 12·(−2) = 6.
IV. Total Number of Divisors of a Number
A. Divisor-count formula
The count of divisors depends only on the exponents in the prime factorisation, not on the primes themselves.
- Formula:
d(n) = (e₁ + 1)(e₂ + 1)…(e_k + 1)forn = ∏ pᵢ^eᵢ. - Reason: Each prime
pᵢcontributes an exponent from0toeᵢ, givingeᵢ + 1independent choices. - Complexity:
O(√n)to factorise, then multiply exponent-plus-ones.
def num_divisors(n):
count, d = 1, 2
while d * d <= n:
e = 0
while n % d == 0:
n //= d; e += 1
count *= (e + 1); d += 1
if n > 1: count *= 2
return count- Example:
72 = 2³·3², sod(72) = (3+1)(2+1) = 12.
V. Finding All Prime Factors of a Number
A. Trial-division factorisation
Dividing out each prime as it is found reduces n and guarantees the remaining factors are still prime.
- Method: Divide by
2, then oddd = 3, 5, 7, …, removing all copies of each before advancing. - Multiplicity: Record each prime as many times as it divides —
n = 2·2·3lists2twice. - Leftover: After the loop, if
n > 1it is itself a prime factor larger than√n₀.
def prime_factors(n):
factors = []
d = 2
while d * d <= n:
while n % d == 0:
factors.append(d); n //= d
d += 1
if n > 1: factors.append(n)
return factors- Example:
84 → [2, 2, 3, 7].
VI. Finding the Prime Factors by Taking the Square Root
A. The √n cutoff
Stopping trial division at ⌊√n⌋ is correct because a composite n must have a factor no greater than its square root.
- Justification: If
n = a·bwitha ≤ b, thena ≤ √n; a divisor above√nalways pairs with one below. - Effect: Reduces naïve
O(n)scanning toO(√n)— forn ≈ 10¹², about10⁶steps instead of10¹². - Composite detection: If no divisor
≤ √nis found,nis prime; this is the basic primality test.
def is_prime(n):
if n < 2: return False
d = 2
while d * d <= n:
if n % d == 0: return False
d += 1
return TrueVII. K-jagged Numbers
A. Definition by factor structure
A k-jagged number is one whose smallest prime factor equals a given bound, characterising numbers "rough" up to k.
- k-rough / k-jagged: An integer all of whose prime factors are
≥ k; equivalently its least prime factor is at leastk. - Construction: Remove any prime factor smaller than
k; if any exists, the number is not k-jagged. - Contrast with smoothness: Roughness bounds factors from below, whereas smoothness (Section X) bounds them from above.
- Example:
35 = 5·7is5-jagged (least prime factor5);30 = 2·3·5is only2-jagged.
VIII. Stormer Numbers
A. Definition via arctangent and largest prime factor
A Størmer number is a positive integer n for which the greatest prime factor of n² + 1 is at least 2n.
- Condition: Let
P(m)be the largest prime factor ofm;nis Størmer ifP(n² + 1) ≥ 2n. - Significance: They mark which
arctan(1/n)terms cannot be decomposed into smaller Machin-like arctangent identities. - Density: Størmer numbers are infinite; the first are
1, 2, 4, 5, 6, 9, 10, …. - Example: For
n = 3,n² + 1 = 10 = 2·5, andP(10) = 5 < 6 = 2n, so3is not a Størmer number.
IX. Frugal Numbers
A. Definition by digit economy
A frugal (economical) number has more digits than its prime factorisation written with exponents.
- Rule:
nis frugal ifdigits(n) > digits of its factorisation(primes plus exponents, exponent1omitted). - Base dependence: The comparison is base-specific; the standard case uses base
10. - Rarity: Frugal numbers thin out but occur infinitely often; the smallest in base 10 is
125. - Example:
125 = 5³— the number has3digits, the factorisation string53has2, so125is frugal.
X. P-smooth Numbers in Given Ranges
A. Definition and enumeration
A number is P-smooth when it has no prime factor exceeding P; counting them in a range is a filtering problem.
- Definition:
nis P-smooth ifP(n) ≤ P, i.e. every prime factor is≤ P. - Range check: For each
nin[L, R], strip all primes≤ P; the number is P-smooth iff the residue is1. - Sieve approach: Mark multiples of each prime
≤ Pto factor a range efficiently rather than factoring eachnalone. - Example: In
[1, 10], the3-smooth numbers are1, 2, 3, 4, 6, 8, 9—5, 7, 10fail because they carry primes> 3.
XI. Lemoine's Conjecture
A. Statement about odd numbers
Lemoine's conjecture asserts a Goldbach-style decomposition for odd integers using one prime and one semiprime component.
- Statement: Every odd integer
n > 5can be written asp + 2qwherepandqare primes. - Status: Unproven but verified computationally to very large bounds; also called the Levy conjecture.
- Relation: A strong-Goldbach refinement — it constrains not just a sum of primes but the specific form
p + 2q. - Example:
11 = 5 + 2·3 = 3 + 2·2(p = 5, q = 3andp = 3, q = 2both work).
XII. Problems Based on GCD and Primality Testing
A. Common problem patterns
Competitive tasks recombine the primitives above; recognising which primitive applies is the core skill.
- GCD-driven problems: Reducing fractions, LCM via
lcm(a, b) = a·b / gcd(a, b), and diophantine solvability using Bézout — a solution toax + by = cexists iffgcd(a, b) | c. - Primality-driven problems: Counting divisors, testing coprimality across arrays, and factor-based classification (jagged, smooth, frugal) all reduce to
O(√n)factorisation or a sieve over a range.
- GCD of an array: Fold pairwise —
gcd(gcd(a, b), c); short-circuit when the running GCD reaches1. - Range primality: Precompute with a Sieve of Eratosthenes in
O(N log log N)instead of testing each number separately. - Modular inverse: Apply the extended Euclidean algorithm when the modulus is not prime; the inverse exists only when the value is coprime to the modulus.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →