Number Theory [00D3]
- April 6, 2026
Introduction [00D4]
- April 6, 2026
We’re going to embark on a study of basic number theory, which can be thought of as the study of the sets \mathbb {N} and \Z . These sets may seem straightforward—and they are, if all you care about is addition. But as soon as we throw multiplication into the mix, things get very interesting indeed. Number theory—and in particular the interaction between addition and multiplication on the integers—is the basis for, e.g. all of modern cryptography, as well as one of the most famous open problems in mathematics, the Riemann hypothesis. Questions about prime numbers, factoring, and solving equations over the integers have fascinated humans for thousands of years.
Divisibility [00D5]
- April 6, 2026
With that in mind, recall the notion of one number being a “multiple” of another, or (equivalently) one number “evenly dividing” another number. We’ll begin by making this notion precise.
Definition The divisibility relation [00D6]
- April 6, 2026
If a, b \in \Z , we say a divides b, denoted a \mid b, iff there exists k \in \Z such that ka = b.
Remark Terminology and notation for divisibility [00D7]
- April 6, 2026
When a divides b, we also say a is a divisor or factor of b, and that b is a multiple of a. We write a \nmid b as an abbreviation for \neg (a \mid b).
Example Divisibility examples [00D8]
- April 6, 2026
- 3 \mid 6 (pick k = 2).
- 5 \nmid 16, since there is no k \in \Z we can pick such that 5k = 16. (Of course k = 16/5 works but that’s not an integer.)
- 5 \mid -15 (pick k = -3).
- 4 \mid 4 (pick k = 1).
- 4 \mid 8 (pick k = 2).
- 8 \nmid 4. Note this illustrates the very important point that \mid is not symmetric—in general a \mid b and b \mid a are not the same. (In fact, the only case in which a \mid b and b \mid a is when a = \pm b.)
- 0 \nmid 3 since there is no k \in \Z for which 0k = 3.
- 3 \mid 0 since we can pick k = 0.
- 0 \mid 0 since we can pick, e.g. k = 671 to make 0k = 0 (of course, any k \in \Z will do).
Visualizing divisibility [00HE]
- July 11, 2026
We can illustrate the situation as follows:
In the situation illustrated at the top, b can be covered exactly with three copies of a, so a \mid b. On the other hand, in the bottom illustration, attempting to cover b with copies of a leaves a bit left over; hence a \nmid b. Thinking about pictures like this can help us build the right intuition. However, keep in mind that the formal definition is a bit more general than these pictures, since the formal definition allows a and b to be negative, or zero, which the pictures can’t really show.
Remark [00D9]
- April 6, 2026
Notice that there is no need to limit the definition of a \mid b to a \neq 0. The definition does not actually mention division at all so everything is perfectly well-defined even if a = 0.
Theorem Divisibility properties [00DA]
- April 6, 2026
For all a, b, c \in \Z ,
- a \mid a (the divisibility relation is reflexive).
- if a \mid b and b \mid c, then a \mid c (divisibility is transitive).
- if a \mid b and a \mid c, then a \mid (b + c).
- if a \mid b, then a \mid bc.
Proof.
- April 6, 2026
Remark Divisibility and subtraction [00HL]
- July 14, 2026
If a \mid b and a \mid c, does a \mid (b - c)? Actually, this already follows from the previous theorem: Since a \mid c, by property (4) we have a \mid c \cdot (-1), i.e. a \mid (-c); then by property (3), a \mid (b + (-c)), that is, a \mid (b - c).
Remark Divisibility vs \leq [00DB]
- April 6, 2026
By definition we have (a \mid b) \leftrightarrow (\exists k \in \Z .\; k \cdot a = b). But consider that we can characterize \leq similarly: (a \leq b) \leftrightarrow (\exists k \in \mathbb {N} .\; k + a = b). (There is a slight discrepancy because of the need to use \mathbb {N} in the characterization of \leq rather than \Z , but if we forget about negative numbers the discrepancy goes away.) That is, \leq is to addition as \mid is to multiplication. The two relations actually share many important properties in common (both are reflexive and transitive, and antisymmetric when restricted to the natural numbers).
Lemma Divisibility is antisymmetric [00F6]
- April 16, 2026
For all a,b \in \mathbb {N}, if a \mid b and b \mid a, then a = b.
Remark Antisymmetry [00F7]
- April 16, 2026
This property is called antisymmetry. Notice \leq has this property too: if a \leq b and b \leq a, then a = b.
Proof.
- April 16, 2026
Theorem ★ Equality characterized by divisibility [00HO]
- July 14, 2026
Natural numbers are equal if and only if they have all the same divisors. That is, for any natural numbers a and b, (a = b) \leftrightarrow (\forall {d}: {\mathbb {N}}.\; (d \mid a) \leftrightarrow (d \mid b)).
Proof.
- July 14, 2026
Subproof. ★ Equality characterized by divisibility ›
- July 14, 2026
Subproof. ★ Equality characterized by divisibility ›
- July 14, 2026
The Division Algorithm [00DC]
- April 6, 2026
Intuitively, if a \mid b it means that some number of copies of a fit precisely into b with nothing left over; conversely, if a \nmid b, then there is a small amount left over. We can make this idea more precise.
Theorem The Division Algorithm [00DD]
- April 6, 2026
Let a, d \in \Z and d \in \Z ^+. Then there exist unique integers q and r such that 0 \leq r < d and a = dq + r.
Remark [00DM]
- April 8, 2026
This is commonly called the "Division Algorithm"—even though, technically, it is a theorem and not an algorithm—because the proof does actually correspond to an algorithm we could use to compute the quotient and remainder (although as we will see, that algorithm is incredibly inefficient!).
Terminology for the Division Algorithm [00DE]
- April 6, 2026
In this definition, q is called the quotient and r the remainder. We write \begin {align*}
q &= a \mathbin {\mathbf {div}} d \\
r &= a \mathbin {\mathbf {mod}} d
\end {align*} The \mathbin {\mathbf {div}} operator is written / in Java and C/C++, and // in Python and Disco; the \mathbin {\mathbf {mod}} operator is written % in many programming languages, and mod in Disco.
Note, however, that the \mathbin {\mathbf {div}} and \mathbin {\mathbf {mod}} operators in Java, C/C++, and Rust do not work in the same way as specified above. In particular, in those languages, the result of a \mathbin {\mathbf {mod}} d is negative when a is negative, rather than always returning a result in the range 0 \leq r < d. Languages such as Python, Disco, and Haskell implement \mathbin {\mathbf {mod}} correctly.
Division Algorithm examples [00DF]
- April 7, 2026
Example [00DG]
- April 7, 2026
What is the quotient and remainder when 101 is divided by 11?
According to the division algorithm, there exist q and r such that 101 = 11q + r and 0 \leq r < 11. We find that 101 = 11 \cdot 9 + 2, so q = 9 and r = 2. We could also express this as \begin {align*} 101 \mathbin {\mathbf {div}} 11 &= 9 \\ 101 \mathbin {\mathbf {mod}} 11 &= 2 \end {align*}
Example [00DH]
- April 7, 2026
What about when 55 is divided by 11? In this case we have 55 \mathbin {\mathbf {div}} 11 = 5 and 55 \mathbin {\mathbf {mod}} 11 = 0.
Example [00DI]
- April 7, 2026
What is 7 \mathbin {\mathbf {mod}} 11? This situation is sometimes confusing for students just learning about the % operator in a programming language, but applying the definition, we seek (q and) r such that 0 \leq r < 11 and 11 = 7q + r. The only values that fit the bill are q = 0 and r = 7. In other words, if we try to divide 7 by 11, we find that 0 copies of 11 fit into 7, and we are left with the same 7 we started with.
Example [00DJ]
- April 7, 2026
What is the result of dividing -24 by 11? If we think purely in terms of “how many copies of 11 will fit” we might think the quotient is -2, but that would result in a remainder of -2, which is not in the required range! In fact, -24 = 11 \cdot (-3) + 9, so the quotient is -3 with a remainder of 9.
Remark Negative divisors [00DK]
- April 8, 2026
We can easily generalize the Division Algorithm to work when d < 0: if a = qd + r then a = (-q)(-d) + r. In other words, to divide by -d we can just divide by d and then negate the resulting quotient q.
Even and odd [00DN]
- April 8, 2026
Note that we can finally use the Division Algorithm to tidy up our notions of even and odd. We defined being even as \mathrm {Even}(n) \equiv \exists {k}: {\Z }.\;{n = 2k} and we defined being odd as \mathrm {Odd}(n) \equiv \exists {k}: {\Z }.\;{n = 2k+1}. Intuitively, we know that if a number is not even, then it has to be odd, and vice versa, but we haven't proved that until now.
Theorem Every integer is even or odd [00DO]
- April 8, 2026
Every integer is either even or odd. That is, \forall {n}: {\Z }.\;{\mathrm {Even}(n) \lor \mathrm {Odd}(n)}.
Proof.
- April 8, 2026
Theorem Not even is odd [00DQ]
- April 8, 2026
This in turn allows us to conclude that \forall {n}: {\Z }.\;{\neg \mathrm {Even}(n) \leftrightarrow \mathrm {Odd}(n)}.
Proof.
- April 8, 2026
Subproof. Not even is odd ›
- April 8, 2026
Subproof. Not even is odd ›
- April 8, 2026
Subproof. Not even is odd ›
- April 8, 2026
Subproof. Not even is odd ›
- April 8, 2026
Proof of the Division Algorithm [00DL]
- April 8, 2026
Informally, the Division Algorithm is true because we can start with q = 0 and r = a; then, as long as r \geq d we can add one to q and subtract d from r (and similarly if r < 0).
Proof.
- April 8, 2026
Subproof. Proof of the Division Algorithm ›
- April 8, 2026
Subproof. Proof of the Division Algorithm ›
- April 8, 2026
Now, if a < 0, the above proof tells us there must exist q and r such that -a = qd + r with 0 \leq r < d. If r = 0, then a = (-q)d + r. Otherwise, a = -(q+1) d + (d - r); the remainder d - r is in the right range, since if 0 < r < d then 0 < d - r < d.
Finally, we will show that q and r are unique. Suppose there are two sets of q and r that satisfy the theorem, that is, a = q_1 d + r_1 = q_2 d + r_2 with 0 \leq r_1 < d and 0 \leq r_2 < d. We will show that they must in fact be equal. Rearranging, we have r_1 - r_2 = (q_2 - q_1)d, so r_1 - r_2 is a multiple of d. Since 0 \leq r_1, r_2 < d, we must have -d < r_1 - r_2 < d. The only multiple of d in this range is 0, so in fact r_1 - r_2 = 0 and thus r_1 = r_2. This, in turn, means q_1 d = q_2 d and hence q_1 = q_2.
Modular Equivalence [00DR]
- April 10, 2026
Motivation [00DS]
- April 10, 2026
In many situations, we care only about the remainder when some number(s) are divided by a particular divisor; for example, when modelling things that repeat cyclically, i.e. “loop around” (times on a clock; storing items cyclically in an array; orbital periods; etc.). In such cases we might not care exactly how many times something has looped, but only where it currently is in the cycle. This notion is also important when studying divisibility itself: the remainder in some sense measures “how far away” a particular number is from being divisible by the divisor, and keeping track of this for various numbers is often important.
Intuition: wrapping up the number line [00DT]
- April 10, 2026
Consider the number line below where we’ve marked all the multiples of m. If a and b are “at the same place in the cycle” that means they have the same offset from some multiple of m; it also means, intuitively, that their difference is a multiple of m. Since we want to treat every multiple of m as being the same and only care about the remainder or offset between multiples of m, we can think of “curling up” the number line onto itself into an infinite spiral, so that all the multiples of m line up. Then we want to consider all the numbers lined up along any radial line as being “the same”.
These ideas motivate the definition below.
Definition Modular equivalence [00DU]
- April 10, 2026
If a, b \in \Z and m \in \Z ^+, then a is equivalent (or congruent) to b modulo m, written a \equiv _m b, if and only if m \mid (a - b).
Remark Notation for modular equivalence [00DV]
- April 10, 2026
People typically use the notation a \equiv b \pmod m instead of a \equiv _m b. But this notation is so bad that we will not use it. This \pmod m is not the same thing as the \mathbin {\mathbf {mod}} operator we introduced earlier. It is not doing anything to b. This is just the way that we indicate that a and b are equivalent “in the \pmod m world”, that is, if we only care about the remainder after dividing by m. It is also difficult to read when we want to write down a long sequence of equivalences, since the m only comes at the very end: a \equiv b \equiv c \equiv d \equiv e \equiv f \pmod {m}.
I prefer \equiv _m because it makes it more clear that we are talking about a specific relation.
Let’s continue studying equivalence modulo m, that is, \equiv _m. First, let’s prove a couple theorems giving us alternative ways to characterize it.
Theorem Modular equivalence \leftrightarrow offset by a multiple of m [00DW]
- April 10, 2026
Let a, b \in \Z and m \in \Z ^+. Then a \equiv _m b if and only if there exists k \in \Z such that a = b + km.
Proof.
- April 10, 2026
Theorem Modular equivalence \leftrightarrow same mod [00DY]
- April 10, 2026
For all a, b \in \Z and m \in \Z ^+, (a \equiv _m b) \leftrightarrow (a \mathbin {\mathbf {mod}} m = b \mathbin {\mathbf {mod}} m).
Proof.
- April 10, 2026
This is an “if and only if”, so we have to prove both directions.
Subproof. Modular equivalence \leftrightarrow same mod ›
- April 10, 2026
Subproof. Modular equivalence \leftrightarrow same mod ›
- April 10, 2026
Remark Equivalence classes and canonical representatives [00E4]
- April 10, 2026
The previous theorem was a bit more work to prove, but it is an extremely useful characterization of modular equivalence.
In general, suppose we have a relation \sim on a set A, and a function c : A \to B such that a \sim b if and only if c(a) = c(b). This means that \sim is automatically an equivalence relation, and we say that the function c computes a canonical representative for each equivalence class of \sim . (Technically, the equivalence relation \sim is known as the kernel of the function c.) From a computational point of view, this is fantastic: given any two elements a,b \in A, we can easily check whether they are equivalent, simply by computing c(a) and c(b) and checking whether the resulting representatives are equal.
Many of the equivalence relations we have studied can be characterized in this way.
- For example, the relation on words by length (where two English words are related if they have the same number of letters) is of course characterized by the \mathit {length} function.
- Case-insensitive equality can be characterized by the function that returns the all-lowercase version of a string (or, alternatively, all-uppercase): two strings are equivalent iff their all-lowercase versions are equal. Of course, this is exactly how case-insensitive comparison is usually implemented.
- The equivalence that relates lists with the same elements but different orders can be characterized by sorting: two lists are equivalent iff their sorted versions are equal.
- Finally, the previous theorem shows that \equiv _m is likewise characterized by canonical representatives for each equivalence class: given some a \in Z, the canonical representative for a is a \mathbin {\mathbf {mod}} m, and the theorem indeed says that two numbers a and b are equivalent modulo m if and only if a \mathbin {\mathbf {mod}} m = b \mathbin {\mathbf {mod}} m. For example, one of the equivalence classes of \equiv _4 is \{\dotsm -7, -3, 1, 5, 9, 13, \dots \}, and 1 is the canonical representative of this equivalence class: any two numbers in this equivalence class result in 1 when we compute their remainder modulo 4. For example, -3 \mathbin {\mathbf {mod}} 4 = 1, 5 \mathbin {\mathbf {mod}} 4 = 1, 9 \mathbin {\mathbf {mod}} 4 = 1, and so on.
Note, however, that not all equivalence relations can be characterized in this way! For example, \leftrightarrow is an equivalence relation on propositions, but there is no function f we can use to compute whether P \leftrightarrow Q just by checking whether f(P) = f(Q); in general it can be difficult to figure out whether P \leftrightarrow Q.
Now let’s verify that \equiv _m is in fact an equivalence relation.
Theorem \equiv _m is an equivalence relation [00E5]
- April 10, 2026
\equiv _m is an equivalence relation for any m \in \Z ^+, that is, it is reflexive, symmetric, and transitive.
Proof.
- April 10, 2026
- Since equality is reflexive, f(a) = f(a) for any a \in A and thus a \sim a.
- If a \sim b, then f(a) = f(b); since equality is symmetric this means f(b) = f(a) and thus b \sim a.
- If a \sim b and b \sim c, then f(a) = f(b) and f(b) = f(c); by transitivity of equality, this means f(a) = f(c), and thus a \sim c.
But there's more: besides being an equivalence relation, \equiv _m interacts nicely with addition, subtraction, and multiplication.
Theorem \equiv _m is a congruence [00E2]
- April 10, 2026
For any a,b,c,d \in \Z and m \in \Z ^+, if a \equiv _m b and c \equiv _m d, then
- a + c \equiv _m b + d, and
- a - c \equiv _m b - d, and
- ac \equiv _m bd.
Proof.
- April 10, 2026
- Considering a+c, we have a + c = (b + jm) + (c + km) = (b + c) + (j+k)m, and thus by the same theorem in reverse, a + c \equiv _m b + d.
- The proof for subtraction is very similar: a - c = (b + jm) - (c + km) = (b - c) + (j-k)m, so a - c \equiv _m b - d.
- For multiplication, we can reason as follows: ac = (b + jm)(c + km) = bc + m(\text {stuff}), that is, when we expand the product, we get one bc term and several other terms that all have an m we can factor out. We don’t actually need to do all the algebra—we can see that ac will be bd plus m times some integer, so ac \equiv _m bd.
Remark Operations on modular equivalences [00E3]
- April 10, 2026
Note that when we have an equality a = b, we can do whatever we want to it: as long as we do the same thing to both sides they will remain equal. With \equiv _m, however, this only works for certain operations! The things we do to an equivalence have to “play nice” with the operation of taking remainders modulo m.
The previous theorem shows that we can add, subtract, or multiply both sides of a modular equivalence by the same thing. However, it turns out that we cannot divide—at least, not in a naïve way! For example, notice that 2 \equiv _4 6, but if we try dividing both sides by 2, we get 1 \equiv _4 3, which is false!
Solving modular equivalences (part I) [00E6]
- April 13, 2026
Example [00E7]
- April 13, 2026
Solve for x: x + 7 \equiv _3 12. Normally, if we were solving the equation x + 7 = 12, we would solve for x by subtracting 7 from both sides. Can we do that here? Yes, we can, because \equiv _3 is an equivalence relation and a congruence with respect to subtraction. Specifically, because it is reflexive, we know that 7 \equiv _3 7; then, from x + 7 \equiv _3 12 and 7 \equiv _3 7, we can conclude that x + 7 - 7 \equiv _3 12 - 7. Simplifying yields x \equiv _3 5. This is a valid solution for x; since 5 \equiv _3 2 (and \equiv _3 is transitive) we can also simplify this to x \equiv _3 2, which is a bit simpler; to mirror the Division Algorithm, we sometimes prefer our solutions to be specifically of the form x \equiv _m k where 0 \leq k < m.
Note that x \equiv _3 2 is really a way of expressing infinitely many valid solutions for x: it means that x \in \{\dots , -4, -1, 2, 5, 8, 11, \dots \}. We can check our solution by picking one or more values for x, substituting into the original equivalence, and checking that it is satisfied. For example, if x = 2 then the original equivalence says 2 + 7 \equiv _3 12, which is true since both sides are a multiple of 3.
We do not usually need to be this pedantic when solving modular equivalences. It would be acceptable, for example, to just write the following: “Starting from x + 7 \equiv _3 12, we can subtract 7 from both sides, yielding x \equiv _3 5 \equiv _3 2.”
Example [00E8]
- April 13, 2026
Solve for x: 101x + 52 \equiv _{10} 68
We can first subtract 52 from both sides, yielding 101x \equiv _{10} 16 \equiv _{10} 6. It might look like we are stuck at this point: we are not allowed to divide both sides by 101. However, notice that 101 \equiv _{10} 1, and so 101x \equiv _{10} x (since \equiv _{10} is a congruence with respect to multiplication). Hence, we have x \equiv _{10} 6.
Example [00E9]
- April 13, 2026
Solve for x: 3x + 19 \equiv _7 2(x - 73) We can solve this as follows:
It is worth taking a moment to spell out the reasons we are allowed to, say, replace 3x + 19 with 3x + 5 above. Since 3x \equiv _7 3x and 19 \equiv _7 5, we can add these to conclude 3x + 19 \equiv _7 3x + 7. In fact, given any expression made up of only addition, subtraction, and multiplication, we can replace any part of the expression with something else equivalent. For example, 3x + y(1 + 19(x - 2)) \equiv _7 3x + y(1 + 5(x - 2)).
Example [00EA]
- April 13, 2026
Solve for x: x + 22 \equiv _{19} 20x + 3 Subtracting x + 3 from both sides yields 19 \equiv _{19} 19x. But since 19 \equiv _{19} 0, this reduces to 0 \equiv _{19} 0, which is always true for any value of x. Hence every integer is a solution for x.
Example [00EB]
- April 13, 2026
Solve for x: x + 22 \equiv _{19} 20x - 7 This is similar to the previous example, but we end up with 10 \equiv _{19} 0, which is impossible. Hence in this example there are no solutions for x.
Primes [00EC]
- April 13, 2026
You're probably familiar with prime numbers and prime factorization from elementary school. Let's study these concepts formally, using the framework that we've built so far.
Definition Trivial divisors [00EN]
- April 14, 2026
Notice that for any natural number n we always have 1 \mid n (since 1 \times n = n) and n \mid n (since n \times 1 = n). 1 and n are called trivial divisors of n.
Definition Prime [00ED]
- April 13, 2026
An integer p > 1 is prime iff it has no nontrivial divisors. That is, \mathrm {Prime}(p) \equiv (p > 1) \land (\forall {d}: {\mathbb {N}}.\;{(1 < d < p) \to d \nmid p}).
Example [00EF]
- April 13, 2026
2, 3, 5, and 7 are prime, which we can easily check by trying all possibilities:
- 2 is prime because there aren't even any numbers between 1 and 2 which could be divisors.
- 3 is prime because 2 \nmid 3.
- 4 is not prime, because 2 \mid 4.
- 5 is prime, because 2 \nmid 5, 3 \nmid 5, and 4 \nmid 5.
- 6 is not prime, because 2 \mid 6 (and 3 \mid 6).
- 7 is prime, because 2 \nmid 7, 3 \nmid 7, 4 \nmid 7, 5 \nmid 7, and 6 \nmid 7.
Definition Composite [00EH]
- April 13, 2026
Integers > 1 that are not prime are called composite.
That is, a composite number n is one which has at least one divisor d \mid n with 1 < d < n. Note that if d \mid n, then by definition dk = n for some integer k, which means k is also a divisor of n; divisors come in pairs (except for the special case when n = d^2).
Remark 0 and 1 are neither prime nor composite [00EE]
- April 13, 2026
Remark Additive and multiplicative building blocks [00HD]
We can now state one of the most fundamental results in number theory, which several ancient civilizations discovered independently, and you probably learned in elementary school.
It's easy to see that the number 1 is the "fundamental building block" of the natural numbers under addition: that is, we can create any natural number we want by starting with 0 and adding 1 any number of times. It turns out that under multiplication, the primes are the building blocks: any natural number besides 0 can be formed by starting with 1 and multiplying primes.
Theorem Fundamental Theorem of Arithmetic [00EG]
- April 13, 2026
Every positive integer n \geq 1 is equal to a product of zero or more primes, known as its prime factorization. The product is unique up to reordering; say, if the primes are listed in order from smallest to largest.
There are two things to prove: first, that it is always possible to write any positive integer as a product of primes, and second, that the product is always unique. At this point, we will only prove that the product exists.
Proof.
- April 13, 2026
Subproof. Fundamental Theorem of Arithmetic ›
- April 13, 2026
Subproof. Fundamental Theorem of Arithmetic ›
- April 13, 2026
Subproof. Fundamental Theorem of Arithmetic ›
- April 13, 2026
Subproof. Fundamental Theorem of Arithmetic ›
- April 13, 2026
For completeness, a proof of uniqueness is included below, but it uses some material we have not covered yet.
★ Uniqueness of prime factorizations [00HP]
- July 16, 2026
The Fundamental Theorem of Arithmetic claims not only that every positive integer can be written as a product of primes, but that there is essentially only one way to do it—as long as we do not care about the order of the prime factors. That is, we consider 2 \times 3 and 3 \times 2 to be the same factorization of 6, since the only difference is the order of the factors.
First we need a key lemma.
Lemma Euclid's Lemma [00HQ]
- July 16, 2026
If p is a prime number and p \mid ab (where a and b are arbitrary natural numbers), then either p \mid a or p \mid b.
Proof.
- July 16, 2026
Now we can prove uniqueness of prime factorizations.
Proof.
- July 16, 2026
Subproof. ★ Uniqueness of prime factorizations ›
- July 16, 2026
Subproof. ★ Uniqueness of prime factorizations ›
- July 16, 2026
Now let n be arbitrary, and suppose we already know that the prime factorizations for every number less than n are unique; we must show this is true for n as well. So suppose n = p_1 p_2 \cdots p_j = q_1 q_2 \cdots q_k.
Consider p_1. Since p_1 \mid n, then p_1 \mid q_1 q_2 \cdots q_k. By Euclid's Lemma, either p_1 \mid q_1 or p_1 \mid q_2 \cdots q_k. If p_1 \mid q_1 then in fact p_1 = q_1, since p_1 and q_1 are both prime. Otherwise, again by Euclid's Lemma either p_1 \mid q_2 (and hence p_1 = q_2) or p_1 \mid q_3 \cdots q_k, and so on. In the end, p_1 must be equal to one of the q's. We can therefore cancel p_1 from both sides, resulting in two prime factorizations for the smaller number n/p_1. By the induction hypothesis, the two prime factorizations must be the same—hence the two prime factorizations for n were the same in the first place.
Theorem Composites have a divisor \leq \sqrt {n} [00EI]
- April 13, 2026
If n is composite, it has a prime divisor d \leq \sqrt n.
Remark [00EJ]
- April 13, 2026
It’s worth spelling out how to write this in formal predicate logic: \forall n \in \Z ^+ .\; \mathrm {Composite}(n) \rightarrow \exists p \in \Z ^+ .\; \mathrm {Prime}(p) \land (p \mid n) \land p \leq \sqrt n. The phrase “it has a prime divisor \leq \sqrt n” actually expands into an \exists with a conjunction of three propositions! Each of “prime”, “divisor”, and “\leq \sqrt n” corresponds to a separate property of the number that is claimed to exist.
Proof.
- April 13, 2026
Example Sieve of Eratosthenes [00EK]
- April 13, 2026
The sieve of Eratosthenes is an ancient Greek method for finding all the primes up to some limit; consider as an example finding all the primes up to 100. We start by circling 2 as the first prime, then crossing out all multiples of 2. Since the 3 is not crossed out, it must be prime too. We circle it and cross out all multiples of 3. The 4 is already crossed out now, but 5 is not, which means it must be prime—if it were composite it would have some smaller prime divisor, but we have already crossed out all multiples of smaller primes. So we circle 5 and cross out all its multiples; finally we circle 7 and cross out all its multiples. But now we can stop! We have found all the primes up to \sqrt {100} = 10. The rest of the numbers up to 10 are already crossed out; by the previous theorem, composite numbers up to 100 must have a prime divisor less than or equal to 10, but we have already crossed out all multiples of primes less than 10. So all the remaining numbers must be prime and we can simply circle them.
Here is another classic piece of mathematics from ancient Greece:
Theorem Infinitude of primes [00EL]
- April 13, 2026
There are infinitely many primes.
Proof.
- April 13, 2026
The largest known prime [00EM]
- April 13, 2026
So there is no largest prime; but the currently (as of April 13, 2026) largest known prime—that is, the largest number we can specifically point to and say for certain it is prime—is 2^{136,279,841} - 1. This number has 41,024,320 decimal digits (pop quiz: how many binary digits does it have?), and was found by the Great Internet Mersenne Prime Search. If it were printed in a book, with, say, 100 digits per line and 50 lines of digits per page, it would require around 8204 pages—probably a 20-volume set of books taking up a whole shelf!
Divisibility tests [00EO]
- April 16, 2026
Example [00EP]
- April 16, 2026
Is 7431 prime? No, it’s divisible by 3 since the sum of its digits is. Many of you perhaps know this divisibility rule, but do you know why it works?
Theorem Testing for divisibility by 3 [00EQ]
- April 16, 2026
A number is divisible by 3 iff the sum of its digits in base 10 is also divisible by 3.
Note this process can be iterated: for example, 7431 is divisible by 3 if and only if 7 + 4 + 3 + 1 = 15 is; and 15 in turn is divisible by 3 if and only if 1 + 5 = 6 is.
Proof.
- April 16, 2026
Remark Testing for divisibility by 2 and 5 [00ER]
- April 16, 2026
This also shows why for 2 and 5 we can just look at the last digit: since 2 and 5 are both divisors of 10, we have 10 \equiv _2 0 and 10 \equiv _5 0. So replacing 10 with 0 in the expansion for n tells us that n \equiv _2 d_0 and n \equiv _5 d_0.
Testing for divisibility by 7 [00ES]
- April 16, 2026
What about 7? Let n be a positive integer and write n = 10m + d_0. For example, if n = 7451 we have m = 745 and d_0 = 1. We reason as follows:
Thus, n is divisible by 7 if and only if m - 2d_0 is. That is, we take the last digit of n, double it, and subtract from the number formed by the rest of the digits. For example, doing this process on 7451 yields 745 - 2\cdot 1 = 743; doing it again yields 74 - 2 \cdot 3 = 68, and 68 is not divisible by 7 (it is 7\cdot 10 - 2). On the other hand, 7441 yields 742, which yields 70, which is divisible by 7, and indeed, 163 \cdot 7 = 7441.
Note, there is actually a key piece missing from our proof above: although we proved that we can always multiply both sides of a modular equivalence by the same number (-2 in this case), we don’t yet know that we can divide both sides by the same number, which we need for that step to be an if and only if. In fact, sometimes we can’t! But it turns out this step is allowed since 2 and 7 share no common factors. We will prove this later in the week.
GCD and the Euclidean Algorithm [00ET]
- April 16, 2026
Warmup GCD [00FC]
- April 17, 2026
What is the greatest common divisor (greatest common factor) of 60 and 18? Of 7169 and 7811?
The divisibility lattice [00EU]
- April 16, 2026
We are used to thinking of the natural numbers like this:
They are arranged along a number line, in order according to the \leq relation. Here we’ve drawn an arrow from each number to the next. Notice that because \leq is transitive, there is a path following the arrows from one number to another iff the first is less than or equal to the second. We’ve seen before that \leq is intimately related to addition, so we can think of this chain of arrows as a picture of the additive structure of \mathbb {N}.
The divisibility relation a \mid b, on the other hand, gives us a way to think about the multiplicative structure of the natural numbers. Let’s draw a similar kind of diagram of the natural numbers where a and b are connected by an arrow whenever a \mid b. (However, if a \mid b \mid c then we won’t bother drawing an arrow a \to c: since divisibility is transitive, we can just follow the path a \to b \to c to see that a \mid c. Abstractly, the arrow is still “there”, we just won’t draw it.)
Of course, this is just a tiny part of the entire (infinite) diagram. Here are a few observations:
This diagram is much more complicated than the diagram for \leq ! This is, in some sense, the reason why studying the multiplicative structure of \mathbb {N} is much more interesting than studying the additive structure.
All the primes are at the bottom, right above 1. Composite numbers come in “layers” above that depending on how many prime divisors they have.
The greatest common divisor of two numbers a and b is the highest number in the diagram from which we can travel up to both a and b. For example, we can see that the GCD of 60 and 18 is 6, since from 6 there is a path along arrows upwards to both 60 and 18, and there is no higher node with this property.
Likewise, the least common multiple of a and b is the lowest number we can reach from both a and b. In this example, the LCM of 60 and 18 is 180.
Where should 0 go in this diagram? In fact, it has to go at the “top”, since every number divides 0, hence there is an arrow from every natural number up to 0.
Generally speaking, numbers get bigger as we go up the diagram; but it is not true that higher numbers are always greater than lower ones. For example, 35 would go in the second layer from the bottom, but 35 < 30 which is in the third layer. And this is dramatically false in the case of 0, which is less than all other natural numbers, but is at the top of our divisibility diagram.
GCD and LCM [00EV]
- April 16, 2026
Definition GCD [00EW]
- April 16, 2026
Let a, b \in \mathbb {N}. Then the greatest common divisor of a and b, written \gcd (a,b), is the unique natural number such that for all d \in \mathbb {N}, (d \mid \gcd (a,b)) \leftrightarrow (d \mid a \land d \mid b).
In the left-to-right direction, this says that anything which divides \gcd (a,b) must be a common divisor of a and b. Because divisibility is transitive, this will clearly be the case when \gcd (a,b) is a divisor of both a and b. And in fact, picking d = \gcd (a,b) shows that this must be the case. In the right-to-left direction, this says that any common divisor of a and b must divide their GCD. This is what makes it the “greatest” common divisor: there can’t be any common divisor d which is “above” the GCD.
Remark [00EX]
- April 16, 2026
Note that this definition doesn’t say anything about \leq ! The word greatest in the phrase greatest common divisor should really be taken to mean highest in the divisibility diagram, not “greater than” in the usual \geq sense.
Example GCD examples [00EY]
- April 16, 2026
\begin {align*} \gcd (6,10) &= 2 \\ \gcd (2,3) &= 1 \\ \gcd (2,4) &= 2 \\ \gcd (3,3) &= 3 \\ \gcd (0,7) &= 7 \\ \gcd (0,0) &= 0 \end {align*} If we took the word “greatest” in GCD to refer to the \leq relation, then we would have to say \gcd (0,0) is undefined; every natural number is a common divisor of 0 and 0, and there is no greatest natural number. However, if “greatest” means “highest in the divisibility diagram” then clearly the answer should be 0. And indeed, we can easily check that 0 satisfies the definition: for every d \in \mathbb {N}, (d \mid 0) \leftrightarrow (d \mid 0 \land d \mid 0) is trivially true.
Definition LCM [00EZ]
- April 16, 2026
The least common multiple of a, b \in \mathbb {N}, written \mathrm {lcm}(a,b), is the unique natural number such that for all m \in \mathbb {N}, (\mathrm {lcm}(a,b) \mid m) \leftrightarrow (a \mid m \land b \mid m).
We won’t talk much more about \mathrm {lcm}—it has very similar properties to \gcd but with everything flipped.
Computing GCD and LCM via factoring [00F0]
- April 16, 2026
One way to compute the GCD of a and b (and the way you’re probably familiar with from grade school) is to factor both, and pick the biggest power of each prime that divides both. For example, \gcd (18,60) = \gcd (2^1 \cdot 3^2 \cdot 5^0, 2^2 \cdot 3^1 \cdot 5^1) = 2^1 \cdot 3^1 \cdot 5^0 = 6. In general, consider writing a natural number n as an infinite product of all the primes raised to powers (if n does not have a certain prime p as a divisor, we just set p’s exponent to 0): a = 2^{a_2} 3^{a_3} 5^{a_5} 7^{a_7} \dots Then in general, \gcd (a,b) = \gcd (2^{a_2} 3^{a_3} 5^{a_5} \dots , 2^{b_2} 3^{b_3} 5^{b_5} \dots ) = 2^{\min (a_2,b_2)} 3^{\min (a_3,b_3)} 5^{\min (a_5,b_5)} \dots That is, we take the minimum power of each prime. This works even when some of the powers are 0. The proof is not too hard and left as an exercise.
Likewise, the LCM can be found by taking the max of each prime power: \mathrm {lcm}(a,b) = \mathrm {lcm}(2^{a_2} 3^{a_3} 5^{a_5} \dots , 2^{b_2} 3^{b_3} 5^{b_5} \dots ) = 2^{\max (a_2,b_2)} 3^{\max (a_3,b_3)} 5^{\max (a_5,b_5)} \dots Incidentally, this shows us that:
Theorem LCM-GCD [00F1]
- April 16, 2026
For every a, b \in \mathbb {N}, \gcd (a,b) \cdot \mathrm {lcm}(a,b) = ab.
Proof.
- April 16, 2026
Computing GCD via the Euclidean algorithm [00F2]
- April 16, 2026
Computing the GCD via factoring is not very practical for larger numbers, because factoring takes a long time. It turns out there is a much better way that is actually used in practice. To see why it works, we’ll first need a few lemmas and theorems about \gcd .
Theorem GCD arguments can be subtracted from the other [00GQ]
- May 29, 2026
For all a,b \in \mathbb {N}, if a \geq b, then \gcd (a,b) = \gcd (a-b, b).
Intuitively, if a and b can be chopped into pieces of a common size, then so can their difference.
Proof.
- May 29, 2026
Subproof. GCD arguments can be subtracted from the other ›
- May 29, 2026
Subproof. GCD arguments can be subtracted from the other ›
- May 29, 2026
Lemma GCD is commutative [00F3]
- April 16, 2026
\gcd (a,b) = \gcd (b,a).
Proof.
- April 16, 2026
Definition Euclidean Algorithm (simple version) [00HM]
- July 14, 2026
We can put these observations about \gcd together to write down a procedure for computing it. This is one of the oldest recorded algorithms, and is named the Euclidean Algorithm since it is in Euclid's Elements, written in about 300 BC. It was also independently invented in both India and China. This simple version is essentially equivalent to the version found in the Elements, although we will also describe a slightly more efficient version later.
- \gcd (a,0) = a.
- If a \geq b, then \gcd (a,b) = \gcd (a-b,b).
- Otherwise, \gcd (a,b) = \gcd (b,a).
Each of these steps is justified by one of our previous theorems, and a and b keep getting smaller and smaller so b must eventually reach 0.
Example [00HN]
\begin {align*} &\phantom {=} \gcd (60, 18) \\ &= \gcd (42, 18) \\ &= \gcd (24, 18) \\ &= \gcd (6, 18) \\ &= \gcd (18, 6) \\ &= \gcd (12, 6) \\ &= \gcd (6, 6) \\ &= \gcd (0, 6) \\ &= \gcd (6, 0) \\ &= 6 \end {align*}
This seems a bit tedious—and indeed, we can do slightly better. Notice that we end up repeatedly subtracting b from a: \gcd (a,b) = \gcd (a-b,b) = \gcd (a-2b,b) = \gcd (a-3b,b) = \dots And we keep doing this until a is finally smaller than b. But that's exactly what \mathbin {\mathbf {mod}} does! This leads to the following observation:
Theorem GCD reduction via mod [00F8]
- April 16, 2026
For all a,b \in \mathbb {N}, \gcd (a,b) = \gcd (a \mathbin {\mathbf {mod}} b, b).
Proof.
- April 16, 2026
By the division algorithm write a = bq + r. Then
Notice that by the division algorithm, a \mathbin {\mathbf {mod}} b is guaranteed to be strictly smaller than b. So if we replace \gcd (a,b) with \gcd (a \mathbin {\mathbf {mod}} b, b), the very next thing we would do is to switch the two arguments; hence, we might as well combine these two steps into one. This finally leads to the most elegant, modern formulation of the algorithm.
Definition Euclidean Algorithm [00F9]
- April 16, 2026
Let a,b \in \mathbb {N}. To find \gcd (a,b), repeatedly apply these rules: \begin {align*} \gcd (a,0) &= a \\ \gcd (a,b) &= \gcd (b, a \mathbin {\mathbf {mod}} b) \end {align*} That is, just keep repeating the second case until reaching the first case. This is guaranteed to stop in a finite amount of time since the second input to \gcd gets smaller with each step; it must hit zero eventually.
Example [00FA]
- April 16, 2026
\begin {align*} &\phantom {=} \gcd (60, 18) \\ &= \gcd (18, 6) \\ &= \gcd (6, 0) \\ &= 6 \end {align*}
Example [00FB]
- April 16, 2026
\begin {align*} &\phantom {=} \gcd (7169, 7811) \\ &= \gcd (7811, 7169) \\ &= \gcd (7169, 642) \\ &= \gcd (642, 107) \\ &= \gcd (107, 0) \\ &= 107 \end {align*}
We can confirm that 107 is indeed a common divisor of 7169 = 107 \times 67 and 7811 = 107 \times 73. Since 67 and 73 are both prime there is obviously no bigger common divisor.
Bézout's Theorem and the Extended Euclidean Algorithm [00FE]
- April 21, 2026
Motivating example: frog hops and jumps [00FF]
- April 21, 2026
A mathematical frog starts at zero on the number line. He can jump 30 units in either direction, or hop 18 units in either direction. Which positions on the number line can he reach? Can he reach 12? 6? 3? 1? 8? 42252? 42254?
The frog can reach 12 by jumping right then hopping left. He can reach 6 by hopping right twice then jumping left. He can’t reach 3 or 1, since jumps and hops are both even and 3 is odd. 8 is even, but he can’t reach that either since in fact both 12 and 30 are multiples of 6, but 8 is not. We can check that 4+2+2+5+2 = 15 is divisible by 3, hence 42252 is divisible by 6, so we conjecture he can reach that, but not 42254.
In fact, it turns out that the frog can reach position n if and only if 6 \mid n. What’s special about 6?
Theorem Bézout's Theorem [00FG]
- April 21, 2026
Let a, b \in \mathbb {N}. Then there exist s, t \in \Z such that sa + tb = \gcd (a,b).
This is like saying we can reach \gcd (a,b) by some combination of jumps and hops.
Proof.
- April 21, 2026
Subproof. Bézout's Theorem ›
- April 21, 2026
Subproof. Bézout's Theorem ›
- April 21, 2026
We have shown that d divides both a and b, and that conversely any common divisor of a and b must also divide d; hence, by definition, d = \gcd (a,b).
The Extended Euclidean Algorithm [00FH]
- April 21, 2026
Given some a and b, how do we actually find s and t? The above proof is not much help: it merely shows s and t must exist, but does not actually show us how to find them. Fortunately, there is an efficient procedure for doing so, known as the Extended Euclidean Algorithm.
Let’s take a = 60, b = 18 as an example. We begin by making a table with three columns labelled s, t, 60s + 18t. In the first row we set s = 1 and t = 0; in the second row we do the reverse. The value in the third column should always be the corresponding value of the expression 60s + 18t.
\begin {array}{ccc} s & t & 60s + 18t \\ \hline 1 & 0 & 60 \\ 0 & 1 & 18 \end {array}Of course, 60 \cdot 1 + 18 \cdot 0 = 60, and 60 \cdot 0 + 18 \cdot 1 = 18, so we fill in these values in the third column.
The idea is that we are going to run the Euclidean Algorithm on the values in the third column, but along the way, we are going to keep track of the values of s and t that generate each. If we were running the Euclidean Algorithm to compute \gcd (60,18), our first step would be to replace (60, 18) by (18, 6) (since 60 \mathbin {\mathbf {mod}} 18 = 6). So we place a 6 in the third column under the 18:
\begin {array}{ccc} s & t & 60s + 18t \\ \hline 1 & 0 & 60 \\ 0 & 1 & 18 \\ & & 6 \end {array}But what should we put in the first two columns? The key is to think about how we get 6 from 60 and 18. We get the remainder 6 by subtracting some number of copies of 18 from 60: in fact, we subtract 60 \mathbin {\mathbf {div}} 18 = 3 copies of 18. The big insight is that we can do exactly the same operation in each of the first two columns, namely, subtract three times the bottom value from the top value. In general, if 60s_1 + 18t_1 = x and 60s_2 + 18t_2 = y, then x - qy = (60s_1 + 18t_1) - q(60s_2 + 18t_2) = 60(s_1 - qs_2) + 18(t_1 - qt_2). So we put 1 - 3\cdot 0 = 1 in the first column and 0 - 3 \cdot 1 = -3 in the second column: \begin {array}{ccc} s & t & 60s + 18t \\ \hline 1 & 0 & 60 \\ 0 & 1 & 18 \\ 1 & -3 & 6 \end {array} And indeed, we can verify that 60 \cdot 1 + 18 \cdot (-3) = 6.
If we proceed one more step, we find that 18 \mathbin {\mathbf {mod}} 6 = 0, which means that 6 is actually the \gcd of 60 and 18, and we have found the coefficients s and t we were looking for.
\begin {array}{ccc} s & t & 60s + 18t \\ \hline 1 & 0 & 60 \\ 0 & 1 & 18 \\ 1 & -3 & 6 \\ & & 0 \end {array}Example [00FI]
- April 21, 2026
Let’s do one more example. This time, let’s make a fourth column where we record the quotient q. That is, at each step we look at the two last values in the third column and divide the upper value by the lower; we record the result q in the fourth column next to the lower value; then we subtract q times the bottom row from the one above it to generate the next row. This time let’s take a = 39 and b = 16.
\begin {array}{ccc} s & t & 39s + 16t & q \\ \hline 1 & 0 & 39 & \\ 0 & 1 & 16 & 2 \\ 1 & -2 & 7 & 2 \\ -2 & 5 & 2 & 3 \\ 7 & -17 & 1 & 2 \\ && 0 & \end {array}So we have found that \gcd (39,16) = 1, and that 39 \cdot 7 + 16 \cdot (-17) = 1 (check this!). In other words, if there is a frog that can jump 39 units or hop 16 units, it could reach 1 starting from the origin by jumping 7 times to the right and 17 times to the left.
Modular Inverses and Solving Modular Equivalences [00FJ]
- April 21, 2026
Example [00FM]
- April 21, 2026
Solve for x: 3x \equiv _7 5. We can't divide both sides by 3. But the point of dividing by 3 is that 3/3 = 1, so dividing by 3 "cancels" the 3 on the left. But what if we could cancel the 3 by multiplying both sides by something? In particular, could we find some number s such that 3s \equiv _7 1?
There are only a few possible values to try, since e.g. multiplying by 9 or 16 would be the same as multiplying by 2; we only need to try the values from 0 to 6 (and actually, it would be pointless to try 0 or 1). It turns out that 5 works: 3 \cdot 5 = 15 \equiv _7 1. So if we multiply both sides of the equivalence by 5, we get 15x \equiv _7 25, or, reducing modulo 7, x \equiv _7 4.
When will this work in general? And how can we find s besides just trying different values?
It turns out that Bézout’s Theorem can answer these questions!
Theorem Modular inverses [00FK]
- April 21, 2026
Let a \in \Z and m \in \Z ^+. If \gcd (a,m) = 1, then there exists some b \in \Z such that ab \equiv _m 1. We say that b is the modular inverse of a modulo m.
Proof.
- April 21, 2026
Remark [00FL]
- April 21, 2026
This theorem can actually be stated as an “if and only if”. Can you prove the other direction? i.e. prove that if ab \equiv _m 1, then \gcd (a,m) = 1.
Of course, this proof shows that not only do modular inverses exist, but we can compute them easily using the extended Euclidean algorithm!
Example [00FN]
- April 21, 2026
Solve for x: 3x \equiv _9 5 - x. We can’t just start by finding a modular inverse of 3: for one thing, we would still have an x on the other side of the equation; for another thing, 3 and 9 share a common factor so 3 actually doesn’t have a modular inverse modulo 9. We should add x to both sides of the equation first, which yields 4x \equiv _9 5. We find that the modular inverse of 4 modulo 9 is -2 \equiv _9 7. Multiplying both sides of the equation by -2 (we could also multiply both sides by 7) we get x \equiv _9 -10 \equiv _9 -1 \equiv _9 8. Checking, if we take x = -1 we get 3(-1) = -3 \equiv _9 5 - (-1) = 6.
Example [00FO]
- April 24, 2026
Solve for x: 523x \equiv _{17405} 77. We need to cancel the 523, so we can run the Extended Euclidean Algorithm on 523 and 17405, both to make sure that the GCD is 1, and if so, to find the inverse of 523.
\begin {array}{ccc} s & t & 523s + 17405t & q \\ \hline 0 & 1 & 17405 & \\ 1 & 0 & 523 & 33 \\ -33 & 1 & 146 & 3 \\ 100 & -3 & 85 & 1 \\ -133 & 4 & 61 & 1 \\ 233 & -7 & 24 & 2 \\ -599 & 18 & 13 & 1 \\ 832 & -25 & 11 & 1 \\ -1431 & 43 & 2 & 5 \\ 7987 & -240 & 1 & \\ \end {array}Sure enough, the GCD is 1, and 7987 is the inverse of 523 modulo 17405 (it's the value of s we want, since that is the one being multiplied by 523). Therefore, multiplying both sides of the equivalence by 7987 will cancel the 523 and allow us to solve for x: x \equiv _{17405} 77 \cdot 7987 = 614999 \equiv _{17405} 5824.
★ The Chinese Remainder Theorem [00I2]
- July 16, 2026
Warm-up [00I3]
- July 16, 2026
Chinese Remainder Theorem [00I4]
- July 16, 2026
For all a,b \in \Z , if \gcd (m,n) = 1 then the system of equivalences \begin {align*} x &\equiv _m a \\ x &\equiv _n b \end {align*} has a unique solution modulo mn.
Proof.
- July 16, 2026
Remark Chinese Remainder Theorem with more than two equivalences [00I5]
- July 16, 2026
One way to understand the Chinese Remainder Theorem is that it gives us a way to replace any two equations x \equiv _m a and x \equiv _n b with a single equation x \equiv _{mn} c (as long as m and n share no common factors). If we have more than two equations, we can simply iterate this process.
Example [00I6]
- July 16, 2026
Solve the system of equivalences \begin {align*} x &\equiv _5 1 \\ x &\equiv _6 2 \\ x &\equiv _7 3. \end {align*}
The first step is to solve the first two equivalences, combining them into a single equivalence. We can verify that 5 and 6 are indeed relatively prime, and 5(-1) + 6(1) = 1. Then according to the Chinese Remainder Theorem proof, we can combine the first two equivalences into a single equivalence x \equiv _{5 \cdot 6} 2 \cdot 5 \cdot (-1) + 1 \cdot 6 \cdot 1 = -4 \equiv _{30} 26.
At this point we can verify that 26 \equiv _5 1 and 26 \equiv _6 2, as required. So now we are down to a system of two equations: \begin {align*} x &\equiv _{30} 26 \\ x &\equiv _7 3. \end {align*} We can repeat the process to combine this into a single solution. First, we can use the extended Euclidean algorithm to compute that 30(-3) + 7(13) = 1. The solution for x is thus x \equiv _{30 \cdot 7} 3 \cdot 30 \cdot (-3) + 26 \cdot 7 \cdot 13 = 2096 \equiv _{210} 206. Hence the final solution for x is x \equiv _{210} 206, and we can verify that this satisfies each of the original three equivalences.
★ Fermat's Little Theorem [00I7]
- July 16, 2026
If you play around a bit with arithmetic modulo p, one natural question you might end up asking is: if I start with some number a and keep multiplying a by itself (reducing modulo p every time), will I ever reach 1? If so, how many times do I have to multiply by a before reaching 1? If you did this when p is a prime, you would quickly notice that you always seem to reach 1 eventually, and that the number of iterations needed is always a divisor of p-1. This leads us to:
Theorem Fermat's Little Theorem [00I8]
- July 16, 2026
If p is prime and a is any integer such that p \nmid a, then a^{p-1} \equiv _p 1.
Remark [00I9]
- July 16, 2026
This seems kind of weird and random, but in fact this theorem expresses something really fundamental about the way multiplication works modulo p. As we will see, this theorem forms an important basis for a lot of modern cryptography. It’s also the basis for how tests for primality work. Consider the contrapositive: if a^{p-1} \not \equiv _p 1, then p is not prime. So given a number p we can pick some a that shares no common factors with p, and try computing a^{p-1} \bmod p. If we get 1, we can’t conclude anything; but if we get any result other than 1, we know for sure that p is not prime (though it doesn’t tell us what the factors of p are!). This is not a great test by itself, it turns out, but it is the basis for more sophisticated tests.
Proof of Fermat's Little Theorem [00IA]
- July 16, 2026
Proof.
- July 16, 2026
Consider the set S = \{a \mathbin {\mathbf {mod}} p, 2a \mathbin {\mathbf {mod}} p, 3a \mathbin {\mathbf {mod}} p, \dots , (p-1)a \mathbin {\mathbf {mod}} p\}. For example, if p = 7 and a = 3, we get the set \{3, 6, 2, 5, 1, 4\}. Notice how this contains every integer between 1 and 6.
We can make this precise: we claim that if p \nmid a then S contains every number in \{1, \dots , (p-1)\} exactly once. For suppose there are two multiples of a in S which are equivalent modulo p, that is, ja \equiv _p ka. Then by definition, p \mid (ja - ka) and so p \mid (j-k)a. Since p does not divide a and p is prime, it must divide (j-k). But j and k are both greater than 0 and less than p, so their difference cannot be as small as -p or as big as p. The only way j-k could be divisible by p is if j - k = 0, that is, j=k. This shows that the two “different” multiples of a with the same remainder modulo p actually have to be the same—or, put another way, any two truly different multiples of a must also have different remainders modulo p.
Now consider taking the product of all the elements of S: a \cdot 2a \cdot 3a \cdot \dots \cdot (p-1)a = a^{p-1}(p-1)! If we consider this modulo p, we know that every possible remainder from 1 up to (p-1) occurs exactly once, so a \cdot 2a \cdot 3a \cdot \dots \cdot (p-1)a \equiv _p 1 \cdot 2 \cdot 3 \cdot \dots \cdot (p-1). Putting these together, a^{p-1}(p-1)! \equiv _p (p-1)!. Since (p-1)! shares no common factors with p (p is prime, and (p-1)! is the product of a bunch of numbers less than p, so p cannot possibly be a divisor of (p-1)!), it has an inverse modulo p, and we are justified in canceling it from both sides of the equation. This leaves us with a^{p-1} \equiv _p 1, which is what we wanted to show.
★ Public-key cryptography and RSA [00IB]
- July 17, 2026
Warm-up [00IC]
- July 17, 2026
Can you decrypt this coded message?
J GPS POF XFMDPNF PVS OFX VOJDPSO PWFSMPSET
Symmetric ciphers [00ID]
- July 17, 2026
The code used in the warm-up is a Caesar cipher—shift every letter by the same amount through the alphabet. It’s not a very secure cipher, but there are (slightly) more secure variants, such as a Vigenère cipher, where instead of a single shift amount we choose a sequence of shift amounts and cycle through them. Typically one would choose a key word and use the letters of the key word to determine the shift amounts.
These are all known as symmetric ciphers: in order to communicate, sender and receiver must have a shared, secret key. The secret key must be known by the people communicating (and no one else), and the same key is used for both encrypting and decrypting. This works fine in many situations, but it has some drawbacks:
- The two parties must somehow agree on a secret key before they can communicate. In order to keep the key a secret, typically this must be done in person.
- Because of this need to agree on a secret key, it is impossible for people who have never met to communicate securely.
Public-key cryptography [00IE]
- July 17, 2026
There is a different idea known as a public-key cryptosystem which does not have these drawbacks. We’ll start by outlining the general properties of such a system.
- Everyone who wants to communicate picks their own personal pair of keys: a private key and a public key.
- The private key should be kept absolutely, 100% secret.
- The public key should be published as widely as possible. You should put your public key on your website, in a public directory of public keys (like a phone book), and so on.
- In order to send a message, the sender looks up the public key of the recipient and uses it to encode the message.
- The recipient decodes the message using their private key.
In order for this to make sense the system should have a few properties:
- A public and private key work together so that messages encrypted using a public key can only be decrypted using the corresponding private key.
- It should be impossible to figure out someone's corresponding private key if you only know their public key.
It is not at all obvious that such a scheme is even possible! But it is possible using some clever mathematics.
Some key facts [00IF]
- July 17, 2026
Here are a few mathematical and computational facts that you'll have to take on faith for now.
- Multiplying two large integers can be done very quickly. By large we have in mind something like 500 digits. This may sound like a lot, but a modern computer can multiply two 500-digit numbers in a miniscule fraction of a second. If you were abducted by a sadistic math nerd, locked in a room, and told you will not be let out until you correctly multiply two 500-digit numbers, it would be tedious but you could do it. You could probably do it in an afternoon if you were focused and determined.
- On the other hand, the opposite operation—factoring a large integer into its prime factors—is believed to be very difficult. Given a 1000-digit number n, there are approximately 8 \times 10^{496} primes less than \sqrt {n}. Even if you could test a trillion trillion trillion trillion trillion primes per second to see whether they are a divisor of n, it would still take many lifetimes of the universe to try them all. We do know of better methods than that—but not anywhere near good enough to make it in any way feasible.
- You might think that this means testing whether a number is prime is difficult too—surely, the only way to test whether a number is prime is to try factoring it? Surprisingly, this is false—it is possible to quickly test whether a number is prime without factoring it! (In fact, most such tests rest fundamentally upon Fermat's Little Theorem, which tells us that if p is prime and p does not divide a, then a^{p-1} \equiv _p 1—so we can take a number n we want to test and some a < n and compute a^{n-1} \mathbin {\mathbf {mod}} n. If we get something other than 1, then n is definitely not prime. This isn’t a great test, but more sophisticated tests are based on similar ideas.)
These ingredients come together to give us RSA.
RSA [00IG]
- July 17, 2026
RSA was discovered in 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman (the name "RSA" just comes from the initials of their last names). It was actually independently discovered four years earlier, by Clifford Cocks, while he was working for the UK’s GCHQ—basically the equivalent of the NSA—but this wasn’t known until 1997 when it became declassified.
Generating public/private key pairs with RSA [00IH]
- July 17, 2026
Here’s how generating a public/private key pair works in RSA:
- Pick two large (say, 500-digit) primes p and q. This can be done efficiently: just pick a random 500-digit number and test it to see if it is prime (which can be done efficiently), if not, pick another one and repeat. For 500-digit numbers about one in every 2300 integers is prime, so you might have to repeat this procedure a few thousand times before finding a prime, but that’s not a big deal. Even if this process were to take a few seconds or even minutes, you only have to do it once when creating a public and private key.
- Compute n = pq. As mentioned previously, this can be done very quickly.
- Choose some e such that 1 < e < (p-1)(q-1) and e shares no common factors with (p-1)(q-1). For example, we could randomly pick an e and compute the \gcd (which is very efficient using the Euclidean Algorithm) to make sure it is 1. If not, pick a different e and repeat. In practice, it can be beneficial to use specific values of e to make encoding more efficient, so one could instead fix the value of e and keep picking p and q until \gcd (e, (p-1)(q-1)) = 1.
- Find d such that de \equiv _{(p-1)(q-1)} 1, that is, the modular inverse of e modulo (p-1)(q-1). Since e shares no common factors with (p-1)(q-1), a modular inverse is guaranteed to exist, and we can find it easily with the extended Euclidean algorithm.
- p, q, and d are the private key and should be kept absolutely secret.
- n and e are the public key and should be published widely.
Remark [00II]
- July 17, 2026
As far as we know, of course, it is very difficult to find p and q by factoring n. In turn this means it is difficult to find d from e. We can easily compute d from e if we know (p-1)(q-1), but there’s no way to find (p-1)(q-1) from n without first factoring n to find out what p and q are. This means that no one can figure out d, p, or q (the private key) if they only know n and e (the public key).
Encrypting messages with RSA [00IJ]
- July 17, 2026
To encrypt a message:
- Find or look up the public key values n and e for the desired recipient.
- Turn the message into an integer M < n. It does not matter how, as long as the sender and receiver agree on how this will be done. The method can even be public—knowing how the message is encoded as an integer will not help anyone read it once it is encrypted. In practice, most information that people want to encrypt is already stored on a computer as a sequence of bits, which can readily be interpreted as an integer. If the message is too long to fit into an integer which is less than n, it can simply be broken into multiple parts, and each part encoded separately.
- Compute C = M^e \mathbin {\mathbf {mod}} n. This can be done efficiently due to two key observations:
- It is not necessary to first compute M^e (which might be truly enormous) and then reduce modulo n at the end. Because (a\cdot b) \mathbin {\mathbf {mod}} n = ((a \mathbin {\mathbf {mod}} n) \cdot (b \mathbin {\mathbf {mod}} n)) \mathbin {\mathbf {mod}} n, we can keep reducing modulo n along the way, after every multiplication. That way, we never have to deal with numbers bigger than n^2.
- We don’t have to simply multiply M by itself e times. Instead we can use something called repeated squaring. For example, if we want to compute M^{16}, we can first compute M \cdot M = M^2; then we can square that to get (M^2) \cdot (M^2) = M^4; squaring that yields (M^4) \cdot (M^4) = M^8; and one final squaring yields M^{16}. If we want an exponent which is not a power of two we can simply throw in some extra multiplications by M (for example, if we want M^9 we can get M^8 as described above and then multiply by M). In this way we can compute M raised to very large powers with only a few multiplications (it takes about 2 \log _2 e multiplications in the worst case for exponent e).
- Send C.
Remark [00IK]
- July 17, 2026
It turns out that given only C, e, and n, it is (as far as we know) very difficult to find an M such that M^e \equiv _n C. So no one can read the encrypted message C even if they know the public key (e,n) that was used to encrypt it.
Decrypting messages with RSA [00IL]
- July 17, 2026
To decrypt the encrypted message C, the recipient simply does the same thing but using their private key d as the exponent instead of e! That is, they compute C^d \mathbin {\mathbf {mod}} n.
Theorem RSA works [00IM]
- July 17, 2026
Encrypting and then decrypting in this way results in recovering the original message, that is, (M^e)^d \equiv _n M.
Proof.
- July 17, 2026
Since de \equiv _{(p-1)(q-1)} 1, there exists some integer k such that de = 1 + k(p-1)(q-1). Also, by Fermat's Little Theorem, if p \nmid M, we have M^{p-1} \equiv _p 1, so (M^e)^d = M^{1 + k(p-1)(q-1)} = M \cdot (M^{p-1})^{k(q-1)} \equiv _p M \cdot 1^{k(q-1)} = M. Likewise, by a similar argument, (M^e)^d \equiv _q M.
Now, M is obviously a solution to the system of equations \begin {align*}x &\equiv _p M \\ x &\equiv _q M \end {align*} But since p and q are primes and hence share no common factors, by the Chinese Remainder Theorem we know that the solution to these equations is unique modulo pq = n. Hence (M^e)^d \equiv _n M.
Digital signatures [00IN]
- July 17, 2026
There is still a problem with this scheme. Under a symmetric cipher, where sender and receiver share a secret key, when the receiver gets a message encrypted using the secret key they can be sure it came from the sender (unless someone has stolen the secret key). However, in a public key system, anyone in the world can encrypt a message to the receiver, so the receiver has no way of knowing who the message is actually from. There is no a priori reason to trust who the message says it is from, because anyone can write a message that says “Hi, this is Alice” whether or not they are actually Alice.
However, this problem can be solved very elegantly by noticing that the operations of encryption and decryption are symmetric, that is, (M^e)^d = (M^d)^e. That is, a private key can actually be used to encrypt a message, and then the corresponding public key will work to decrypt it. So, suppose A wants to send a secure message to B, and they also want B to be able to verify that the message actually came from them. Instead of directly encrypting the message for B, first A encrypts the message using A’s private key. This is known as a digital signature. Then, A takes the resulting signed message and encrypts it using B’s public key as usual. When B receives the encrypted message, they first decrypt it using their private key as usual. The result will not be readable since it has been encrypted using A’s private key. But B can now recover the original message by decrypting using A’s public key, which will undo the effects of encrypting with A’s private key. B can now be sure the message came from A, because the only way to make a message that can be successfully decrypted using A’s public key would be to use A’s private key; and the only person who can do that (assuming A’s private key is secret) is A.