Divisibility, GCD, and the Euclidean Algorithm
The arithmetic of the integers: division with remainder, greatest common divisors, and Bézout’s identity - the bedrock every later lesson stands on.
Leads to: 19.2 factorizes; 19.3 inverts mod n using extended Euclid; the Euler Lab’s Number Theory path drills these directly.
Learning Objectives
Click a status chip to cycle: Not started → In progress → Studied → Practiced → Needs review → Mastered.
- State and prove the division algorithm and use it to define the greatest common divisor.
- Derive the Euclidean algorithm from the invariant \(\gcd(a,b)=\gcd(b,\,a\bmod b)\) and prove it terminates.
- Compute Bézout coefficients with the extended Euclidean algorithm and interpret them.
- Implement iterative extended Euclid in Python and verify Bézout’s identity on test inputs.
- Explain why Euclid runs in \(O(\log\min(a,b))\) steps and why that makes it usable inside inner loops.
Key Vocabulary
- Divides
- \(d\mid n\) means \(n=dk\) for some integer \(k\). Note \(d\mid 0\) for every \(d\), and only \(0\mid 0\).
- Division algorithm
- For \(a\in\Z,\ b\gt 0\) there exist unique \(q,r\) with \(a=qb+r\) and \(0\le r\lt b\).
- Greatest common divisor
- The largest \(d\) dividing both \(a\) and \(b\); equivalently the non-negative generator of the ideal \(a\Z+b\Z\).
- Bézout’s identity
- There exist integers \(x,y\) with \(ax+by=\gcd(a,b)\); the gcd is the smallest positive integer combination of \(a\) and \(b\).
- Coprime
- \(\gcd(a,b)=1\). Coprimality is what makes modular inverses, CRT, and Farey/Pell arguments work.
- Least common multiple
- \(\operatorname{lcm}(a,b)=|ab|/\gcd(a,b)\) for nonzero \(a,b\) - compute the gcd first to avoid overflow in fixed-width languages.
Intuition & Motivation
Division with remainder
In Python, a % b already returns the mathematician’s non-negative remainder for \(b\gt 0\) (unlike C, where -7 % 3 is -1). This is a small gift you should not take for granted when porting code.
The gcd and Euclid’s invariant
Extended Euclid and Bézout
Run the same recursion but carry the coefficients. Maintain the two invariants \(r_i = a x_i + b y_i\) for the running remainders \(r_i\); the last nonzero remainder is \(g\) and its coefficients are Bézout’s.
Coding workbench
Where this shows up
The Euler Lab’s Number Theory path is full of problems that reduce to a gcd in disguise: reduced fractions and Farey sequences, lattice-point counting on lines, cycle lengths of maps like \(x\mapsto x+k \bmod n\) (which visit \(n/\gcd(n,k)\) states), and totient computations. When a problem smells like “how many of these are in lowest terms?”, reach for gcd first.
- Assuming \(\gcd(0,0)\) is defined as a positive number - it is 0 by convention; guard it.
- Recursive Euclid on adversarial inputs in languages with small stacks. The iterative form has no such risk.
- Computing \(\operatorname{lcm}(a,b)=ab/\gcd\) as a*b//g in a fixed-width language: \(ab\) can overflow. Do a//g*b instead.
- Reading Bézout coefficients as unique. They are only unique modulo \((b/g,\,a/g)\): \((x+kb/g,\ y-ka/g)\) works for every \(k\).
- Python’s math.gcd is C-fast and handles arbitrary precision - use it in production and write your own only to understand it (and when you need the Bézout coefficients, which math.gcd does not give you).
- The gcd is the cheapest primitive that lets you decide ‘are these two structures aligned?’ - periods, rotations, and step sizes all synchronise after \(\operatorname{lcm}\) steps.
- Binary (Stein’s) gcd replaces division with shifts and subtraction; worth knowing when division is expensive, but in Python the built-in wins.
Practice this in the Euler Lab
Computational problems that exercise exactly this technique. Each opens in the Euler Lab with a Python workbench, a progressive hint ladder, and answer checking. Tier A/B run at full scale in the browser.
Warm-up:
#1 Multiples of 3 or 5 (1%, tier A) #2 Even Fibonacci Numbers (1%, tier A) #7 10 001st Prime (1%, tier A) #20 Factorial Digit Sum (1%, tier A)
Applied:
#293 Pseudo-Fortunate Numbers (16%, tier B) #313 Sliding Game (16%, tier B) #357 Prime Generating Integers (16%, tier B)
Challenge:
#161 Triominoes (41%, tier C) #182 RSA Encryption (41%, tier C)
481 Project Euler problems in total are mapped to this lesson. Open the Euler Lab to filter them all.
Knowledge Check
Practical Exercise
(a) Prove that \(\gcd(a,b)\) is the smallest positive element of the set \(S=\{ax+by: x,y\in\Z\}\cap\Z_{\gt 0}\). (b) Use it to show that the linear Diophantine equation \(ax+by=c\) has an integer solution if and only if \(\gcd(a,b)\mid c\), and describe all solutions.
(a) Let \(d\) be the smallest positive element of \(S\), say \(d=ax_0+by_0\). Divide: \(a=qd+r\) with \(0\le r\lt d\). Then \(r=a-qd=a(1-qx_0)+b(-qy_0)\in a\Z+b\Z\). If \(r\gt 0\) it would be a positive element of \(S\) smaller than \(d\), contradiction; so \(r=0\) and \(d\mid a\). Symmetrically \(d\mid b\), so \(d\) is a common divisor. And any common divisor \(e\) of \(a,b\) divides \(ax_0+by_0=d\), hence \(e\le d\). Therefore \(d=\gcd(a,b)\).
(b) (\(\Rightarrow\)) If \(ax+by=c\) then \(g=\gcd(a,b)\) divides the left side, so \(g\mid c\). (\(\Leftarrow\)) If \(c=kg\), take Bézout \(ax_0+by_0=g\) and scale: \(a(kx_0)+b(ky_0)=c\).
All solutions: given one particular \((x_0,y_0)\) the general solution is
Lesson Summary
Retrieval Practice
Close the lesson and answer from memory before checking. This is deliberate, effortful recall - the single highest-yield study action.
A: \(\gcd(a,b)=\gcd(b,a\bmod b)\) because both pairs share the same common divisors. It terminates because the second argument strictly decreases in \(\Z_{\ge0}\), and it is fast because the larger argument at least halves every two steps.
A: The Bézout coefficients \(x,y\) with \(ax+by=\gcd(a,b)\). When \(\gcd(a,b)=1\) this \(x\) is \(a^{-1}\bmod b\), and scaling it solves any solvable \(ax+by=c\).
Completion Checklist
- I can explain the core ideas in my own words
- I worked the derivations/examples by hand
- I completed the interactive workbench(es)
- I passed the knowledge check
Source References
This lesson synthesizes and paraphrases concepts from the sources below. No copyrighted text, problem sets, or solutions are reproduced. Return to the originals for full depth.
- Calculus, Vol. I (Tom Apostol, 2nd ed., 1967) foundational - Ch. I.4 & Ch. 4 (induction) - Induction, well-ordering and the least-integer principle - the proof machinery behind the division algorithm and Euclid’s termination argument.
- The Pragmatic Programmer (Thomas & Hunt, 2nd/20th Anniv. ed., 2019) current - Ch. 5 (Bend or Break) & Ch. 7 - Algorithmic craft: pick the primitive with the right complexity and test it against an invariant (here, Bézout’s identity) rather than a table of outputs.