Discrete Mathematics Spring 2026 lecture notes [0021]
- November 24, 2025
- Brent A. Yorgey
These are my lecture notes for MATH 240, Discrete Mathematics for the Spring 2026 semester.
See here for a hyperlinked list of learning goals.
Advanced/optional material [advanced]
Some sections of the notes may be marked with a star (★). This indicates optional, more advanced material which is included for the sake of those who are interested. Such material may or may not be presented in class and will not be formally assessed.
Introduction to MATH 240, Discrete Mathematics [0026]
- November 28, 2025
What is Discrete Mathematics? [0022]
- January 21, 2026
You have taken a lot of math classes throughout your elementary, middle school, and high school years. Unless you've been exposed to math from other sources, you may have developed the ideas that math is (1) linear, that is, a sequence of topics each more advanced than the last; and (2) focused on reaching Calculus, the mathematics of things that are continuous, i.e. things that change smoothly. But both are false! Math is not a linear chain of ideas, but rather an interconnected web. And there are whole areas of math that study not continuous, but discrete (or algebraic) things, i.e. things that don't vary smoothly. Things that jump from one to the next, collections of distinct items, and so on. Broadly speaking, this is exactly the kind of mathematics that is needed for computing: computers do not deal with continuous values, but with discrete ones and zeros. This course will serve as an introduction to all the cool math you missed in high school; the kind of math that gets used every day in computing.
Themes [0025]
- November 28, 2025
In one sense, this course is something of a grab bag of topics: we will cover introductions to logic and proof, set theory, relations and functions, induction, number theory, and a bit of combinatorics. Each topic could be an entire course in and of itself. But there will be several themes running throughout the semester that will unify our study:
- Types
- Formal notation is a superpower
- Don't be afraid of zero
- Algebraic structures
Disco [0027]
- November 28, 2025
Throughout the semester, we will be using a programming language called Disco, which I have been developing over the last ten years. A number of students have worked on Disco as well, including:
- Callahan Hirrel '19
- Bosco Ndemeye '18
- Sanjit Kalapatapu
- Jacob Hines '19
- Eric Pinter '21
- Daniel Burnett '23
- Katherine Reid '25
- Ted Bjurlin '25
- Colin Phillips '26
- Jacob Collier-Tenison '26
Perhaps someday your name could be on this list as well!
MATH 240 syllabus [0028]
- November 28, 2025
The syllabus for MATH 240, Discrete Mathematics can be found here. Some important highlights:
- Your final grade in the course is determined by the number of learning goals completed, and the number of engagement points earned.
- Learning goals can be completed by completing problems on weekly in-class quizzes.
- Engagement points are earned by e.g. attending class, completing homework assignments, and attending office hours.
- Homework is graded only for completion/effort on a credit/no credit basis.
Types in Disco and mathematics [0024]
- November 28, 2025
Everything has a type [002B]
- November 28, 2025
Everything in Disco has a type which tells us what kind of value it is (or will be). Types tell us how we are allowed to put things together in ways that make sense. For example, it makes sense to add two numbers, but it does not make sense to add a number and a list. In that way, types, and the ways they are allowed to match up, are kind of like "grammar rules" for writing down programs—or mathematics. In fact, most mathematicians think about types informally in their head all the time. One of the main reasons we will be using a language with a so-called strong type system—where everything has a type, and all the types must match up properly—is to give you practice with this important way of thinking, in a way that gives you immediate feedback.
You can read more about types in the Gentle Introduction to Disco.
Asking Disco for types [002A]
We can ask Disco what type something has with the :type command. For example, if we ask for the type of a number like 3, Disco tells us it has type \mathbb {N} (which stands for the Natural numbers):
Disco> :type 3 3 : ℕ
Remark. Blackboard bold [0029]
Note this fancy style of writing capital letters with a double line in the middle is called blackboard bold, and is the standard way mathematicians write \mathbb {N} and other similar types we will meet soon. However, you do not have to use fancy symbols when writing Disco code. For example, \mathbb {N} can instead be written N.
Natural numbers, integers, and closure [002C]
- November 29, 2025
You can read more about these topics in the Gentle Introduction to Disco here.
Definition. Natural numbers [002D]
- November 29, 2025
We write \mathbb {N} (or N, or Nat, or Natural) for the type of natural numbers. Natural numbers include all of the non-negative counting numbers 0, 1, 2, 3, \dots and so on.
Remark. Is zero a natural number? [002G]
- November 29, 2025
Note that mathematicians disagree on whether 0 counts as a natural number. However, among computer scientists, having 0 count as a natural number is fairly universal. The important point, which we will explore in more depth later, is that the collection of non-negative counting numbers 0, 1, 2, 3, \dots has much better mathematical properties than the collection 1, 2, 3, \dots without zero, so it is much more elegant to take the former collection as "fundamental" and define the latter in terms of it (e.g. "nonzero natural numbers"). But what we call these collections doesn't matter in an ultimate sense.
In any case, Disco agrees that zero is a natural number, as we can check:
Disco> :type 0 0 : ℕ
Definition. Closure [002F]
- November 29, 2025
A collection of numbers (or other items) is closed under a certain operation if doing that operation on two of the numbers always gives another such number. For example, the natural numbers are closed under addition (that is, adding any two natural numbers gives another natural number) and under multiplication (multipltying any two natural numbers gives another natural number).
Disco> :type 3 + 5 3 + 5 : ℕ Disco> :type 3 * 5 3 * 5 : ℕHowever, the natural numbers are not closed under subtraction, because subtracting two natural numbers might not always result in another natural number. For example, 2 - 5 is not a natural number.
Definition. Integers [002H]
- November 29, 2025
Since the natural numbers are not closed under subtraction, if we want to be able to do subtraction we must include all negative numbers along with positive. This bigger set of numbers \dots , -3, -2, -1, 0, 1, 2, 3, \dots is called the integers. We write \Z (or Z, or Int, or Integer) to denote the type of integers (using the letter Z to stand for the integers is mathematically standard, and comes from the German word for "numbers", Zahlen).
Integers in Disco [002I]
- November 29, 2025
Disco knows about integers in addition to natural numbers:
Disco> :type -2 -2 : ℤ Disco> :type 3 - 5 3 - 5 : ℤ Disco> :type 5 - 1 5 - 1 : ℤThe last example might be surprising: we know that 5 - 1 = 4, which is actually a natural number, so why does disco say \Z instead of \mathbb {N}? The answer is very simple: because we used subtraction. Disco does not actually simplify/evaluate things to figure out what type they are. In fact, the whole point is to be able to tell what kind of value we will get—to make sure everything matches up properly—before actually trying to evaluate an expression.
Similarly, \mathbb {N} is not closed under division, and Disco has other types to handle that, just like it has \Z to handle subtraction, but we'll talk about them later (in the meantime you might enjoy playing around to see if you can figure it out for yourself).
Functions and tests in Disco [002K]
- November 29, 2025
Defining functions in Disco [002J]
- November 29, 2025
We can define our own functions in Disco. First, we give the name of
the function and its type, which will be something like
A -> B where A is the type of the function's inputs,
and B is the type of the outputs. Then we define what output
the function should give for each input.
double : N -> N double(n) = 2nIf we write the above definition of
double in a
.disco file, we can load it at the Disco prompt using the
:load command, then try it on some inputs:
Disco> :load double.disco Loading double.disco... Loaded. Disco> double(2) 4 Disco> double(7) 14
Tests in Disco [002L]
- November 29, 2025
We can also attach tests to our functions in Disco. Each test goes on a line by itself beginning with !!!.
!!! double(0) == 0 !!! double(2) == 4 !!! double(7) == 14 !!! forall n : N. double(n) >= 0 double : N -> N double(n) = 2nTests can either be simple true or false tests, or they can have a
forall to say that something should be true for every value
of a certain type. When we :load this file, Disco will run
the tests and report whether they succeed.
Disco> :load double.disco Loading double.disco... Running tests... double: OK Loaded.If we try changing the
double(n) >= 0 test to
double(n) > 0, we can see that the test fails:
Disco> :load double.disco
Loading double.disco...
Running tests...
double:
- Certainly false: ∀n. double(n) > 0
Found counterexample:
n = 0
- Left side: 0
- Right side: 0
Loaded.
However, another way to express a correct test would be as follows:
!!! forall n : N. (n == 0) or double(n) > 0
Example: factorial [002M]
- November 29, 2025
As another example, let's implement factorial (although it is already built in). Recall that n! = n \cdot (n-1) \cdot (n-2) \cdot \dots \cdot 1. Another way to write this is n! = n \cdot (n-1)! If we add a "base case" 0! = 1, this becomes a perfectly viable way to define factorial. We could transcribe it into Disco as follows:
fac : N -> N fac(0) = 1 fac(n) = n * fac (n-1)Unfortunately, this is a type error. The reason is that since the input to
fac is supposed to be a natural number, that means
n-1 must be a natural number, but it cannot be since it uses
subtraction. We can use the .- operator instead, which works
on natural numbers (you will explore what this operator does on a
homework assignment).
fac : N -> N fac(0) = 1 fac(n) = n * fac (n .- 1)
Propositional logic [00BH]
- March 14, 2026
Introduction to Propositional Logic [002R]
- December 15, 2025
Motivation [002N]
- December 14, 2025
Consider the following two problems:
- 30 people each buy a ticket to see the heffalump. The owner pays $12 for food and ends up with $33 in profit. How much does one ticket cost?
- In what circumstances does the following loop stop?
while (tickets < 30 or (!heffalumps.isEmpty() and not(count > 5))): ...
How would you solve these? Different students give different answers, but if you are like many, you would solve the first one by writing down an algebraic equation and solving it, and the second one by talking about it and thinking hard. The first problem definitely seems easier.
It turns out that people used to solve algebra problems like the first using natural language and thinking really hard. Formal notation is a superpower! Our goal is to allow you to solve the second problem using a formal language too.
As another motivation for our study: how do we rigorously prove a mathematical statement, i.e. how can we completely convince ourselves and others of its truth? How do we know what details we need to include, and what counts as a correct argument? As we will see, the language of propositional logic can help with this, too!
Propositions [002O]
- December 14, 2025
Definition Proposition [002P]
- December 14, 2025
A proposition is a declarative statement that can be true or false.
Remark [002T]
- December 15, 2025
This is not really a formal definition, but just an intuitive idea of what propositions should be. Later, we will define more formal ways to create propositions.
Example [002Q]
- December 14, 2025
Some example propositions:
- Washington, D.C. is the capital city of the United States.
- 9 is evenly divisible by 3.
- Toronto is the capital of Canada.
- 9 < 3.
- Every even natural number greater than 2 can be written as the sum of two prime numbers.
The first two propositions are true. The next two are false. The last one is a famous proposition called the Goldbach Conjecture; we do not know if it is true or false. However, all of these are valid propositions.
Example [002S]
- December 15, 2025
Some examples of things which are not propositions:
- 3 + 7
- Please hold this.
- Is the moon made of cheese?
The first is an expression with a numeric value, not a declarative true or false statement. The second is a command, and the third is a question. The question could be answered by a proposition (e.g. "The moon is made of cheese."), but it is not itself a proposition.
Remark Propositional variables [002U]
- December 15, 2025
We will tend to use the letters p, q, r, s or P, Q, R, S to stand for arbitrary propositions.
Booleans in Disco [003O]
- January 28, 2026
In Disco, the type Bool has two values, T and F (which can also be written True and False, or true and false):
Disco> :type T T : Bool Disco> :type F F : Bool Disco> true T Disco> False F
One way we can create Bool values in Disco is using comparison operators:
Disco> 3 < 9 T Disco> 3 > 9 F Disco> 6 <= 7 T Disco> 6 >= 7 F Disco> 5 == 5 T Disco> 6 /= 5 T
Propositional connectives [002V]
- December 16, 2025
Now we will see how to put propositions together to build up more complex propositions.
Negation (NOT) [003D]
- January 21, 2026
The first and simplest connective we will consider is negation, which flips a proposition from true to false and vice versa.
Definition Negation [002W]
- December 16, 2025
If P is any proposition, the negation of P is written \neg P (or sometimes \overline {P}) and read as "not P" or "It is not the case that P." \neg P is true whenever P is false, and vice versa.
Remark Truth table for negation [002Y]
- December 16, 2025
We can summarize the definition of negation with a truth table showing the truth value of \neg P for each possible value of P: \begin {array}{cc}P & \neg P \\ \hline \mathsf {T} & \mathsf {F} \\ \mathsf {F} & \mathsf {T} \end {array}
In general, a truth table is a table showing the value of some expression for every possible combination of values for some variables. In this case, there is only one variable, so the table only has two rows—one for P = \mathsf {T} and one for P = \mathsf {F}.
Remark Disco :table command [002X]
- December 16, 2025
We can also get Disco to show us the truth table for
negation, using the :table command:
Disco> :table not F T T F
Conjunction (AND) [003E]
- January 21, 2026
The next connective we consider is conjunction, commonly known as AND.
Definition Conjunction [002Z]
- December 16, 2025
The conjunction of propositions P and Q is written P \land Q, and pronounced "P and Q". It is true when both P and Q are true, and false otherwise.
Remark [0030]
- December 16, 2025
One way you can remember the symbol \land is by noticing that it looks a bit like a capital letter A (for "And") without the crossbar.
Remark Truth table for conjunction [0031]
- December 16, 2025
Let's make a truth table. Notice that the truth table for \neg P needed two rows, one for \mathsf {T} and one for \mathsf {F}. The truth table for P \land Q will need four rows, one for each possible combination of truth values for P and Q. \begin {array}{ccc} P & Q & P \land Q \\ \hline \mathsf {T} & \mathsf {T} & \mathsf {T} \\ \mathsf {T} & \mathsf {F} & \mathsf {F} \\ \mathsf {F} & \mathsf {T} & \mathsf {F} \\ \mathsf {F} & \mathsf {F} & \mathsf {F} \\ \end {array}
Conjunction in Disco [003P]
- January 28, 2026
In Disco, conjunction can be written /\, or and, or &&:
Disco> T and F F Disco> (1 < 5) && (6 <= 6) T Disco> true /\ (99 == 990) F
As before, we can also get Disco to show us a truth table:
Disco> :table /\ F F F F T F T F F T T TThe rows are in a different order than the table shown previously, but the order of the rows doesn't really matter.
Example [003G]
- January 21, 2026
Conjunction works just like we expect in natural language. Consider: "It is not raining today and I had eggs for breakfast."
This is true if both parts are true. If it's raining, the statement is false. If I ate something other than eggs, it's false. Of course it's definitely false when it's raining and I ate something other than eggs.
If we let r = "It is raining today" and e = "I had eggs for breakfast", then we can translate this sentence into propositional logic as \neg r \land e. Note that negation has "higher precedence" than conjunction (it has "stickier glue") so this is unambiguous. It does not mean \neg (r \land e), which would be different. If in doubt, just use parentheses: (\neg r) \land e.
Disjunction (OR) [003F]
- January 21, 2026
We next consider disjunction, commonly known as OR.
Definition Disjunction [003H]
- January 21, 2026
The disjunction of P and Q is written P \lor Q, and pronounced "P or Q". It is true when at least one of P and Q are true, and false when both are false.
Remark Truth table for disjunction [003I]
- January 21, 2026
We can make a truth table for disjunction as follows: \begin {array}{ccc} P & Q & P \lor Q \\ \hline \mathsf {T} & \mathsf {T} & \mathsf {T} \\ \mathsf {T} & \mathsf {F} & \mathsf {T} \\ \mathsf {F} & \mathsf {T} & \mathsf {T} \\ \mathsf {F} & \mathsf {F} & \mathsf {F} \\ \end {array}
Remark Notation for conjunction vs disjunction [003J]
- January 21, 2026
The symbols \land and \lor are easy to mix up. Just remember that \land looks like a capital "A" for "And", \lor looks like a capital "V" for "Vote" (a vote is when you choose between options).
Remark Duality of conjunction and disjunction [003K]
- January 21, 2026
There's a good reason the symbols for conjunction and disjunction are upside down versions of each
other: they are "opposite" (the mathy word is "dual") in the
sense that if we consider an opposite world where everything false
becomes true and vice versa, then \land becomes \lor and vice
versa. Consider the following alternative definition of disjunction:
The disjunction of propositions P and Q is false when both P
and Q are false, and true otherwise.Definition Disjunction (alternative) [003L]
Disjunction in Disco [003Q]
- January 28, 2026
In Disco, disjunction can be written \/, or or, or ||:
Disco> T or F T Disco> (1 < 5) || (6 <= 6) T Disco> false \/ (99 == 990) F
Remark Disjunction in English: inclusive and exclusive OR [003M]
- January 21, 2026
Unlike the word "and", whose English meaning corresponds pretty exactly with its meaning in logic, we use the English word "or" in at least two logically distinct ways. Consider the following English sentences:
- The prerequisite for Algorithms is MATH 240 or permission of the instructor.
- Each entree is served with your choice of soup or a salad.
In the first example, it's OK for both things to be true: it is fine if you pass MATH 240 and also (for some reason) get the instructor's permission to take Algorithms. This is called inclusive or, and corresponds to disjunction (\lor ).
In the other example, you can choose soup, or a salad, but not both (you would be charged extra, at the very least). This is called exclusive or, sometimes written P \oplus Q. It is true when exactly one of P, Q is true, and false otherwise. (Put another way, it is true when P and Q are different, and false when they are the same.) Exclusive or is very important in computer science, but rarely comes up in mathematical logic.
Example [003N]
- January 21, 2026
Let r = "It is raining today", e = "I had eggs for breakfast", and c = "I had cereal for breakfast." Consider the sentence "Either it is raining, or I ate cereal and not eggs for breakfast." We can translate this sentence into propositional logic as follows: r \lor (c \land \neg e) Note that it's not necessarily clear which kind of or to use here; it doesn't matter too much. Also, the parenthesis may technically not be required, but it's probably best to include them.
Implication (IF-THEN) [003R]
- January 28, 2026
The last major connective we will consider is implication, which may be less familiar but is no less important.
Definition Implication [003S]
- January 28, 2026
Let P and Q be propositions. The conditional or implication "if P, then Q" is written P \to Q. It is true if Q is true whenever P is true.
Remark Terminology for the parts of an implication [003T]
- January 28, 2026
In an implication P \to Q, the left-hand proposition P is called the premise or hypothesis. Q is called the conclusion.
You do not need to memorize these terms, but you should be familiar with them.
Truth table for implication [003U]
- January 28, 2026
Let's figure out what the truth table for implication should be. I like to think of it this way: if someone makes you a promise of the form "if ... then ...", in what scenarios have they lied to you?
Consider the propositions
- P = "You fulfill all the requirements for an A."
- Q = "You get an A."
- You complete all the requirements and get an A. This is great; I have kept my promise.
- You complete all the requirements but don't get an A. Obviously in this scenario I lied and you should be super mad at me, and probably complain to my department chair or the Provost.
- Let's next consider the scenario where you don't complete the requirements for an A, and don't get an A. You might be mad at yourself but you certainly can't be mad at me. I still made a true statement.
- The last case is the one that sometimes trips people up: what if you don't complete the requirements, but you get an A anyway? You obviously won't be upset in this scenario (though other students might!), but did I lie? Actually, I didn't. I only said what would happen if you DO complete the requirements. I am free to do whatever I want (including giving you an A anyway) if you don't. (In fact, I occasionally do this in special circumstances!)
So overall we have the following truth table: \begin {array}{ccc} P & Q & P \to Q \\ \hline \mathsf {T} & \mathsf {T} & \mathsf {T} \\ \mathsf {T} & \mathsf {F} & \mathsf {F} \\ \mathsf {F} & \mathsf {T} & \mathsf {T} \\ \mathsf {F} & \mathsf {F} & \mathsf {T} \\ \end {array}
In other words, the only scenario in which an implication P \to Q is false is when P is true but Q is false. It is true in all other cases. "If the premise is false, anything goes!"
Implication in English [003V]
- January 28, 2026
There are lots of ways to express conditionals in English besides just "if P then Q". For example:
- if P, Q
- P implies Q
- Q if P
- P only if Q
- Q whenever P
Again, you need not memorize this list, but it's good to be aware of these.
Definition Related conditionals [003W]
- January 28, 2026
Given an implication P \to Q, we have:
- the converse is Q \to P
- the inverse is \neg P \to \neg Q
- the contrapositive is \neg Q \to \neg P (the contrapositive is the inverse of the converse or vice versa)
Theorem Relationship of an implication to its variants [003X]
- January 28, 2026
An implication and its contrapositive always have the same truth value. On the other hand, an implication does not always have the same truth value as its inverse or converse.
Definition Biconditional [003Y]
- January 28, 2026
Let P and Q be propositions. The biconditional P \leftrightarrow Q (pronounced "P if and only if Q") is true when P and Q have the same truth value, and false otherwise.
Remark If and only if [003Z]
- January 28, 2026
We often abbreviate "if and only if" as "iff".
Tautologies and equivalence [0040]
- January 28, 2026
Definition Tautologies and contradictions [0041]
- January 28, 2026
A proposition that is always true, no matter the values of the propositional variables it contains, is a tautology. One that is always false is a contradiction.
Definition Logical equivalence [0042]
- January 28, 2026
Two propositions that always have the same truth value are logically equivalent, written P \equiv Q. Alternatively, P \equiv Q when P \leftrightarrow Q is a tautology.
Example [0043]
- January 28, 2026
Show that P \to Q \equiv \neg P \lor Q.
One way we could do this is by making a truth table: \begin {array}{ccccc} P & Q & P \to Q & \neg P & \neg P \lor Q \\ \hline \mathsf {T} & \mathsf {T} & \mathsf {T} & \mathsf {F} & \mathsf {T} \\ \mathsf {T} & \mathsf {F} & \mathsf {F} & \mathsf {F} & \mathsf {F} \\ \mathsf {F} & \mathsf {T} & \mathsf {T} & \mathsf {T} & \mathsf {T} \\ \mathsf {F} & \mathsf {F} & \mathsf {T} & \mathsf {T} & \mathsf {T} \\ \end {array} Notice that the columns for P \rightarrow Q and \neg P \lor Q are exactly the same, which means that these two propositions always have the same truth value. Hence they are logically equivalent.
We could also use Disco to check this automatically, like so:
Disco> :test forall p: Bool, q:Bool. (p -> q) == (not p \/ q)
- Certainly true: ∀p, q. (p -> q) == (not p \/ q)
No counterexamples exist; all possible values were checked.Example [0044]
- January 28, 2026
Show that P \to Q \equiv \neg Q \to \neg P.
Algebraic laws for propositional logic [0045]
- January 31, 2026
In algebra, you learned a number of equational laws that allow you to manipulate algebraic expressions. For example, the distributive law tells us that x \cdot (y + z) = x \cdot y + x \cdot z. Once you have internalized these laws, you can manipulate symbolic algebraic expressions without having to think hard about what they mean. There are algebraic laws that allow us to work similarly with propositional logic: Formal notation is a superpower!
Mnemonic principles for propositional logic laws [0046]
- January 31, 2026
There are two important organizing/mnemonic principles:
- Most equivalences have an "opposite world" version where we switch \land / \lor and \mathsf {T} / \mathsf {F}.
- We can get some intuition by thinking in terms of multiplication
and addition:
- \mathsf {T} is kind of like 1
- \mathsf {F} is kind of like 0
- \neg is kind of like subtracting from 1
- \land is kind of like \times
- \lor is kind of like +, but with a maximum result of 1
Propositional logic laws [0047]
- January 31, 2026
Remark [0048]
- January 31, 2026
Idempotence, "opposite world distributivity", and the De Morgan laws are the surprising ones that don't have a nice counterpart in terms of multiplication and addition.
Remark [004A]
- January 31, 2026
Note that associativity means we are justified in writing things like p \land q \land r \land s \land t \land \dots with no parentheses, since where we put parentheses doesn't matter. (But if we mix \land and \lor we definitely need parentheses!!)
Sporadic laws [0049]
- January 31, 2026
Then there are three special equivalences which don't have opposite world versions:
- Double negation elimination: \neg (\neg p) \equiv p
- Implication: p \rightarrow q \equiv \neg p \lor q
- Iff: p \leftrightarrow q \equiv (p \rightarrow q) \land (q \rightarrow p)
You should convince yourself of all these, using either a truth table or thinking about a logical argument.
From now on, we don't have to use truth tables anymore, we can prove any equivalence we want using these core equivalences. Here are a couple examples.
Example [004B]
- January 31, 2026
Prove (p \rightarrow q) \land (p \rightarrow r) \equiv p \rightarrow (q \land r).
\begin {align*} &(p \rightarrow q) \land (p \rightarrow r) \\ &\equiv \quad \quad \quad \{\text {Implication, twice}\} \\ & (\neg p \lor q) \land (\neg p \lor r) \\ & \equiv \quad \quad \quad \{\text {Factor out $\neg p$ (distributivity, backwards)}\} \\ & \neg p \lor (q \land r) \\ & \equiv \quad \quad \quad \{\text {Implication}\} \\ & p \rightarrow (q \land r) \end {align*}Example [004D]
- February 2, 2026
Simplify: \neg (p \rightarrow \neg (\neg q \lor (r \land q))).
Predicates and Quantifiers [004E]
- February 3, 2026
By this point we’ve caught up with the ancient Greeks. However, it turns out that propositional logic isn’t expressive enough to talk about everything we want to reason about mathematically. In particular we can’t make general sorts of statements involving words like “all” or “some”. These are called quantifiers since they allow us to "quantify" (in a very limited way) how many things a certain proposition is true for. We've seen that we are able to write tests with forall in Disco; we will now see more formally what this means.
Predicates [004F]
- February 3, 2026
Before we can formally define quantifiers, we must come to terms with parameterized propositions, which will allow us to talk about more than one thing. For example, consider the statement x + 2 = 5. By itself, this is not a proposition because we can’t say whether it is true or false; it depends on x.
Disco> :test x + 2 == 5 Error: there is nothing named x. https://disco-lang.readthedocs.io/en/latest/reference/unbound.html
However, it’s almost a proposition: it’s “waiting for” a value of x to be filled in, at which point it will be a proposition. We can make this explicit by defining P(x) to be the statement “x + 2 = 5”; then P(x) is a predicate, that is, a function that takes x as input and outputs a proposition.
P : N -> Bool P(x) = x + 2 == 5
- P(2) represents the proposition 2 + 2 = 5 (which happens to be false).
- P(3) represents the proposition 3 + 2 = 5 (which happens to be true).
We can also make multi-argument predicates, just like we can have functions of multiple variables. For example, let J(x,y) mean “x is less than the square of y”.
J : N * N -> Bool J(x,y) = x < y^2
Universal quantifier (\forall ) [004G]
- February 3, 2026
The first quantifier we will learn about is the universal quantifier, which lets us encode the idea behind English words like "all", "every", and "each", that is, universal statements that are supposed to be true for every member of some domain.
Definition Universal quantifier [004H]
- February 3, 2026
If P(x) is a predicate and D is some “domain” (i.e. set of values, i.e. type) then \forall x: D .\; P(x) (pronounced “for all x in D, P(x)”) is a proposition which is true iff P(x) is true for every x in the domain D.
Remark [004I]
- February 3, 2026
You also often see the notation \forall x \in D. P(x).
Sometimes mathematicians omit writing the domain D as part of the notation, and simply require that it be mentioned off to the side somehwere what the "domain of discourse" is, but this is silly and dangerous: specifying the domain of discourse is not optional (it can determine whether the proposition is true or false), so we should force ourselves to put it in the notation. Otherwise, the notation is ambiguous.
Example [004J]
- February 3, 2026
- \forall {n}: {\mathbb {N}}.\; P(n): assuming P is defined as above, this is a false proposition: it is not the case that n + 2 = 5 for every natural number n.
- \forall {n}: {\mathbb {N}}.\; n \geq 0: this is a true proposition. Every natural number is greater than or equal to zero.
- \forall {x}: {\Z }.\; x \geq 0: this is false. For example, -2 is less than zero.
- \forall {x}: {\Z }.\; x^2 \geq 0: this is true.
We can explore the same examples with Disco:
Disco> :test forall n:N. P(n)
- Certainly false: ∀n. P(n)
Counterexample:
n = 0
Disco> :test forall n:N. n >= 0
- Possibly true: ∀n. n >= 0
Checked 100 possibilities without finding a counterexample.
Disco> :test forall x:Z. x >= 0
- Certainly false: ∀x. x >= 0
Found counterexample:
x = -1
- Left side: -1
- Right side: 0
Disco> :test forall x:Z. x^2 >= 0
- Possibly true: ∀x. x ^ 2 >= 0
Checked 100 possibilities without finding a counterexample.
Disco> :test forall b:Bool. (b /\ b) == b
- Certainly true: ∀b. (b /\ b) == b
No counterexamples exist.Remark [004K]
- February 3, 2026
We should think of \forall as being like a big “and”. That is, \forall x:D.\; P(x) \equiv P(x_1) \land P(x_2) \land P(x_3) \land \dots where x_1, x_2, x_3, ... are all the elements of the domain D. But writing out such giant lists of propositions would be tedious; the domain could even be infinite, as in the last example! But even though this is not literally true it will help us think about what properties \forall should have.
\forall with an empty domain [004N]
- February 4, 2026
Consider this example: suppose H is the domain of "Hendrix students who are over 11 feet tall". Is the following proposition true or false? \forall {h}: {H}.\; h \text { plays varsity pickleball} The strange thing here, of course, is that the domain H is empty. What should it mean to say that "every" element of a domain has some property, when the domain is in fact empty?
Although we don't even have a varsity pickleball team, the perhaps surprising answer is that this is true, and in fact any \forall with an empty domain should be true. On one level, you can just take my word for it: this is the standard, commonly accepted definition within mathematics. But there are also good reasons for defining it this way: things work out more elegantly and consistently this way. Here are a couple ways to think about it:
- In order for the proposition \forall {x}: {D}.\; P(x) to be false, there should be a counterexample, that is, a specific value of x in the domain D for which P(x) is false. If the domain D is empty, then clearly no counterexamples can exist.
- Recall that we can think of \forall like a big \land : \forall {x}: {D}.\; P(x) \equiv P(x_1) \land P(x_2) \land P(x_3) \land \dots So what do we get when we take the \land of zero things? The answer is that we should get the identity element for \land —which is \mathsf {T}; just like the sum of zero things is zero (the identity element for addition), the product of zero things is one (the identity element for multiplication), and so on.
This is our first example of the theme Don't be afraid of zero! For example, we could require any \forall to have a non-empty domain, and say that it is undefined when the domain has size zero. But this is unnecessary: there is a perfectly elegant way to define it.
Existential quantifier (\exists ) [004O]
- February 4, 2026
Next, we consider the existential quantifier, which lets us encode existential statements using words like "some" or "exists". An existential statement is more specific than a universal statement: rather than saying that a proposition is true for every member of some domain, it just says that it is true for some members.
Definition Existential quantifier [004L]
- February 3, 2026
If P(x) is a predicate and D any domain, then \exists x:D.\; P(x) (“there exists an x in D such that P(x)”) is a proposition which is true iff there is at least one x in D such that P(x) holds.
Example [004M]
- February 3, 2026
- \exists {n}: {\mathbb {N}}.\; P(n): This is a true proposition, since, for example, P(2) is true.
- \exists {x}: {\Z }.\; x^2 = 16: This is a true proposition. Both 4 and -4 are integers which satisfy x^2 = 16.
- \exists {x}: {\Z }.\; x^2 = 17: This is a false proposition. There is no integer whose square is 17.
- \forall {x}: {\mathbb {N}}.\; \exists {y}: {\mathbb {N}}.\; J(x,y): This is true: for every natural number x, there is some other natural number y such that x < y^2.
- \forall {y}: {\mathbb {N}}.\; \exists {x}: {\mathbb {N}}.\; J(x,y): This is false, since when y = 0, there is no natural number x such that J(x,y), that is, x < 0^2.
Disco> :test exists x:N. P(x)
- Certainly true: ∃x. P(x)
Found example:
x = 3
Disco> :test exists x:Z. x^2 == 17
- Possibly false: ∃x. x ^ 2 == 17
No example was found; checked 100 possibilities.
Disco> :test exists x:Z. x^2 == 16
- Certainly true: ∃x. x ^ 2 == 16
Found example:
x = 4
- Left side: 16
- Right side: 16
Disco> :test forall x:N. exists y:N. J(x,y)
- Possibly true: ∀x. ∃y. J(x, y)
Checked 100 possibilities without finding a counterexample.
Disco> :test forall y:N. exists x:N. J(x,y)
- Possibly false: ∀y. ∃x. J(x, y)
No example was found; checked 100 possibilities.
Counterexample:
y = 0Remark [004Q]
- February 5, 2026
Dually to \forall , we can think of \exists as being like a big “or”. That is, \exists {x}: {D}.\; P(x) \equiv P(x_1) \lor P(x_2) \lor P(x_3) \lor \dots where x_1, x_2, x_3, ... are all the elements of the domain D.
\exists with an empty domain [004P]
- February 4, 2026
Is the following proposition true or false? \exists {h}: {H}.\; h \text { plays on the varsity pickleball team} The proposition is clearly false. Even something like \exists {h}: {H}.\; h \text { has taken MATH 240} is false: there are certainly Hendrix students who have taken MATH 240, but none in the domain H! And in general, \exists {x}: {D}.\; P(x) is always false when the domain D is empty.
This is more intuitive than \forall with an empty domain, but we can justify it in similar ways. For example, if \exists corresponds to a big \lor , an \exists with an empty domain corresponds to the question: what do we get when we take the \lor of zero things? The identity element for \lor , of course, namely \mathsf {F}.
Example propositions with nested quantifiers [0050]
- February 5, 2026
Decide whether each proposition is true or false.
Example [0052]
- February 5, 2026
\forall {n}: {\mathbb {N}}.\; \exists {z}: {\Z }.\; n < z
This is true. For any natural number n we pick, we can always find an integer which is larger, for example, n+1. Notice that for each n, we are allowed to choose a different z.
Example [0053]
- February 5, 2026
\forall {z}: {\Z }.\; \exists {n}: {\mathbb {N}}.\; z < n
This is also true. We cannot always pick z+1 since it might not be a natural number. But, for example, we could pick 0 when z is negative and z+1 when z is nonnegative.
Example [0054]
- February 5, 2026
\forall {z}: {\Z }.\; \exists {n}: {\mathbb {N}}.\; n < z
This is false. For example, when z = -1, there is no natural number which is less than z.
Example [0055]
- February 5, 2026
\exists {n}: {\mathbb {N}}.\; \forall {z}: {\Z }.\; n < z
This is false. There is no natural number which is less than all integers.
Example [0056]
- February 5, 2026
\exists {z}: {\Z }.\; \forall {n}: {\mathbb {N}}.\; z < n
This is true: for example, we could pick z = -1 again, which is less than all natural numbers.
Translating English sentences into propositional logic with quantifiers [004R]
- February 5, 2026
Let's look at some examples of expressing natural language statements in formal propositional logic.
Example [004S]
- February 5, 2026
“Every natural number is less than or equal to its own square.” \forall n:\mathbb {N}. n \leq n^2 (This is a true proposition.)
Example [004T]
- February 5, 2026
“The square of any integer is nonnegative.” \forall x:\Z . x^2 \geq 0 (This is also true.)
Example [004U]
- February 5, 2026
“1369 is a perfect square.” \exists n:\mathbb {N}. n^2 = 1369 This also happens to be true. Note that this is much nicer than trying to write something like \mathrm {IsInteger}(\sqrt {n}). \sqrt {-} is complicated, and we don’t know how to define \mathrm {IsInteger}.
Example [004V]
- February 5, 2026
“n is even.” \mathrm {Even}(n) \equiv (\exists {k}: {\Z }.\; 2k = n).
Example [004W]
- February 5, 2026
“n is odd.” \mathrm {Odd}(n) = (\exists {k}: {\Z }.\; 2k + 1 = n). Or we could define it as \neg \mathrm {Even}(n). (It is actually slightly nontrivial to prove formally that \mathrm {Odd}(n) \equiv \neg \mathrm {Even}(n)—we won’t be able to prove it until we go over the Division Algorithm later in the semester!)
Example [004X]
- February 5, 2026
“If n is even, then n + 2 is even.” \forall {n}: {\Z }.\; \mathrm {Even}(n) \to \mathrm {Even}(n+2) Note that bare variables in mathematical statements like this tend to hide an implicit \forall !
De Morgan laws for quantifiers [004Y]
- February 5, 2026
The quantifiers \forall and \exists have de Morgan-like equivalences: \begin {align*} \neg (\forall x:D.\; P(x)) &\equiv \exists x:D.\; \neg P(x) \\ \neg (\exists x:D.\; P(x)) &\equiv \forall x:D.\; \neg P(x) \end {align*} Intuitively, if P(x) is not true for all x, then it must be false for at least one; likewise, if it is not the case that there is some x for which P(x) is true, then P(x) is false for all x.
Alternatively, if we remember that \forall is like a big AND and \exists is like a big OR, then we can see these as actual instances of the de Morgan laws for \land and \lor .
Example [004Z]
- February 5, 2026
What is the negation of \forall {a}: {\mathbb {N}}.\; \exists {b}: {\mathbb {N}}.\; (a = 0) \lor (a + b = 0)?
\begin {align*} & \neg ( \forall {a}: {\mathbb {N}}.\; \exists {b}: {\mathbb {N}}.\; (a = 0) \lor (a + b = 0)) \\ & \equiv \quad \quad \quad \{ \text {de Morgan} \} \\ & \exists {a}: {\mathbb {N}}.\; \neg (\exists {b}: {\mathbb {N}}.\; (a = 0) \lor (a + b = 0)) \\ & \equiv \quad \quad \quad \{ \text {de Morgan} \} \\ & \exists {a}: {\mathbb {N}}.\; \forall {b}: {\mathbb {N}}.\; \neg ((a = 0) \lor (a + b = 0)) \\ & \equiv \quad \quad \quad \{ \text {de Morgan} \} \\ & \exists {a}: {\mathbb {N}}.\; \forall {b}: {\mathbb {N}}.\; (a \neq 0) \land (a + b \neq 0) \end {align*}Quantifier restriction [0051]
- February 5, 2026
We naturally make "for all" and "exists" statements in English about adjectives applied to sets, like "all even numbers" or "there exists a prime, happy number greater than 17". The most natural way to encode such statements is by making up a domain to represent the adjectives, like "\forall {n}: {\mathrm {Even}}.\; \dots " where \mathrm {Even} represents the set of even numbers, or "\exists {n}: {H}.\;" where H is the set of prime, happy numbers greater than 17. However, when we hide all the conditions inside the definition of a domain, we cannot work with those conditions using the tools of propositional logic we have developed. Therefore, we are going to learn how to translate statements involving complicated domains into logically equivalent statements with simpler, standard domains.
Expanding restricted forall [00HR]
- July 16, 2026
How would we encode the (false) proposition “every even integer is equal to 2”? The most straightforward way is to write \forall {x}: {\mathrm {Even}}.\; x = 2. However, there is a logically equivalent way that uses a more primitive domain: \forall {x}: {\Z }.\; \mathrm {Even}(x) \to (x = 2). Why does this work? In the case of \forall {x}: {\mathrm {Even}}.\; x = 2, we are directly making a statement about every element of the set of even numbers. In the case of \forall {x}: {\Z }.\; \mathrm {Even}(x) \to (x = 2), it looks like we are making a broader statement about all integers—but the implication \mathrm {Even}(x) \to \dots restricts it again to only saying something interesting about even numbers. For odd numbers, we will end up with \mathsf {F} \to \dots which is equivalent to \mathsf {T}.
More generally, suppose D is some domain and S is a subset of D, and let S(x) be a predicate meaning “x is a member of S”. Then \forall {x}: {S}.\; P(x) \equiv \forall {x}: {D}.\; S(x) \to P(x). In other words, we can translate \forall {x}: {S}.\; \dots into \forall {x}: {D}.\; S(x) \to \dots .
Expanding restricted exists [00HS]
- July 16, 2026
We can do something similar with "exists" statements. For example, how would we encode the proposition "there exists an even integer which is equal to 2"? Again, one way would be \exists {x}: {\mathrm {Even}}.\; x = 2, but I claim this is equivalent to \exists {x}: {\Z }.\;{\mathrm {Even}(x) \land (x = 2)}. In the original proposition, we are claiming that an element of the smaller set \mathrm {Even} exists with a certain property. In the expanded version, we are claiming that an element of \Z exists with a certain property—but in order to re-focus on just the even integers, the property we claim is that the integer is a member of \mathrm {Even} and is equal to 2.
In general, suppose D is some domain and S is a subset of D, and let S(x) be a predicate meaning “x is a member of S”. Then \exists {x}: {S}.\; P(x) \equiv \exists {x}: {D}.\; S(x) \land P(x). In other words, we can translate \exists {x}: {S}.\; \dots into \exists {x}: {D}.\; S(x) \land \dots .
Remark [00HT]
- July 16, 2026
Example [00HU]
- July 16, 2026
As a final example, let’s encode the statement “Every even natural number can be written as the sum of two primes” in formal propositional logic. Assume we have a predicate \mathrm {Prime}(n) meaning “n is prime” (we will see how to define this later). First, we can write it directly as \forall {n}: {\mathrm {Even}}.\; \exists {p}: {\mathrm {Prime}}.\; \exists {q}: {\mathrm {Prime}}.\; n = p + q. We can now progress from left to right, expanding each quantifier into one that uses a more primitive domain:
Incidentally, this proposition is definitely false as written, since 0 and 2 cannot be written as a sum of primes. However, if we add a restriction that n > 2, this is the famous Goldbach conjecture, which we think is probably true, but no one has been able to prove!
Proofs [0057]
- February 9, 2026
We can formally establish the truth of a proposition by way of a proof. Proofs can be written in a more or less formal style, but the underlying structure is always the same. We will consider each connective of propositional logic, and see for each (1) how to prove it, and (2) how to use it if you know it. (How to prove and use a given connective are also known as introduction and elimination rules, respectively.)
You need to memorize these; but they should make some intuitive sense, so memorizing them is not difficult.
Once you understand how to prove and use each connective, writing a proof for a mathematical statement looks something like this:
- Translate the statement into propositional logic (using \land , \lor , \neg , \rightarrow , \forall , and \exists ).
- Write a proof outline corresponding to the propositional logic formula, using the rules for proof and use of the relevant connectives.
- Use your intuition/insight/ingenuity/specific content knowledge to fill in the missing bits.
Why proofs? [005T]
- February 13, 2026
Incidentally, why do we care about writing proofs? There are several reasons:
- Writing formal arguments to establish the truth of propositions is part of a great human intellectual tradition going back thousands of years to ancient India, China, and Greece. Learning about this tradition is an important part of becoming a well-educated world citizen!
- Knowing how to reason formally is important both for discovering for ourselves what is true and communicating it to others. For example, suppose you write a function that is supposed to accomplish a task but are not sure whether it works correctly. You can try formally proving that it does: this can either convince you and others that it does, or reveal why it does not.
- Formal verification is now used in many industries (for example, Intel formally verifies many parts of their CPUs). Learning how to write proofs is the basis for understanding how formal verification technologies work.
\mathsf {T} [005G]
- February 9, 2026
To prove \mathsf {T}, do nothing.
If you know \mathsf {T}, you know nothing.
\mathsf {F} [005H]
- February 9, 2026
There is no way to prove \mathsf {F}.
Remark [005I]
- February 9, 2026
Note that it may be possible to prove \mathsf {F} given some extra assumptions. This is just saying there is no way to prove it "from scratch".
If you know \mathsf {F}, then you know anything you want. (This principle is sometimes called ex falso quodlibet, or just ex falso for short. This is Latin for "from false, whatever you want".)
P \land Q [0058]
- February 9, 2026
To prove a proposition of the form P \land Q:
- Prove P
- Prove Q
If you know a proposition of the form P \land Q, then you know P, and you know Q.
P \rightarrow Q [0059]
- February 9, 2026
To prove a proposition of the form P \rightarrow Q, temporarily suppose you know P, and prove Q.
If you know P \rightarrow Q, and you know P, then you know Q.
Example [005F]
- February 9, 2026
Write an outline for a proof of P \rightarrow (Q \land R).
Proof.
- February 9, 2026
Subproof. http://ozark.hendrix.edu/~yorgey/forest/005F/ ›
- February 9, 2026
Subproof. http://ozark.hendrix.edu/~yorgey/forest/005F/ ›
- February 9, 2026
Subproof. http://ozark.hendrix.edu/~yorgey/forest/005F/ ›
- February 9, 2026
Example [005K]
- February 11, 2026
Prove that (2n + 1 > 6) \to (n > 2).
Proof.
- February 11, 2026
Subproof. http://ozark.hendrix.edu/~yorgey/forest/005K/ ›
- February 11, 2026
P \lor Q [005B]
- February 9, 2026
To prove a proposition of the form P \lor Q:
- Prove P, OR
- Prove Q
If you know P \lor Q, and you want to prove R, prove (P \rightarrow R), and prove (Q \rightarrow R). This is known as a "proof by cases". No matter which of P or Q is true, R follows.
\forall {x}: {D}.\; P(x) [005C]
- February 9, 2026
To prove a proposition of the form \forall {x}: {D}.\; P(x), suppose d is an arbitrary element of D, and prove P(d). By arbitrary, we mean that we assume nothing about d except that it is an element of the domain D. Note that you must make up a new (unused) variable name for d!
Remark [005J]
- February 9, 2026
Sometimes people reuse the variable from the \forall , but in any case there can't already be another variable with the same name!
(Later we will see that for certain domains D we can also use a proof by induction.)
If you know \forall {x}: {D}.\; P(x), then you know P(d) for any d in the domain D.
\exists {x}: {D}.\;P(x) [005D]
- February 9, 2026
To prove a proposition of the form \exists {x}: {D}.\; P(x), give a specific example d of an element in the domain D, and prove P(d).
If you know \exists {x}: {D}.\; P(x), then you know P(d), where d is a arbitrary element of the domain D (with a newly made up variable name).
Example Odd squares [005L]
- February 11, 2026
Prove: if n is an odd integer, then n^2 is also odd.
The first step is to translate this statement into propositional logic! Notice that it’s talking about a variable n. It is really making a statement about all possible values of n. This is common in mathematics, to make a “for all” statement just by mentioning some variables without explicitly saying a word like “all” or “every”. So the correct translation is \forall {n}: {\Z }.\; \mathrm {Odd}(n) \rightarrow \mathrm {Odd}(n^2).
Now, what does \mathrm {Odd}(n) mean?
Definition Odd integers [005M]
- February 11, 2026
An integer n is odd if there is some integer k such that n = 2k + 1. That is, \mathrm {Odd}(n) \equiv \exists {k}: {\Z }.\; n = 2k + 1.
So let’s prove \forall {n}: {\Z }.\; \mathrm {Odd}(n) \rightarrow \mathrm {Odd}(n^2). We start by introducing an arbitrary integer n; then we have to prove \mathrm {Odd}(n) \rightarrow \mathrm {Odd}(n^2). This is an implication, so we prove it by supposing that \mathrm {Odd}(n) is true, and then proving in that case \mathrm {Odd}(n^2) is true as well. We can expand the definition of \mathrm {Odd} to figure out what our assumption \mathrm {Odd}(n) means and what we have to show for \mathrm {Odd}(n^2). The whole thing goes like this:
Proof.
- February 11, 2026
Subproof. Odd squares ›
- February 11, 2026
Subproof. Odd squares ›
- February 11, 2026
The above proof has a lot of detail to help us keep track of what is going on. A more experienced mathematician or computer scientist might write something more like this:
Proof.
- February 11, 2026
But you are welcome and encouraged to include a lot of detail about what you are doing especially as you are starting out.
Remark Other connectives and logical equivalence [005N]
- February 11, 2026
What about \neg P and P \leftrightarrow Q? They are logically equivalent to other, more primitive things:
- \neg P \equiv P \to \mathsf {F}
- P \leftrightarrow Q \equiv (P \to Q) \land (Q \to P)
In general, if you are trying to prove P and P \equiv Q, it is always OK to prove Q instead of P. Sometimes it is easier to prove a proposition in one form rather than another, even when the two are logically equivalent. For example:
- When proving P \to Q, sometimes we can prove the contrapositive \neg Q \to \neg P instead.
- When proving a negation of a complex proposition, it often makes sense to first simplify it using de Morgan laws. For example, to prove \neg (P \lor Q), it can make sense to prove \neg P \land \neg Q instead.
Example Odd squares, again [005O]
- February 11, 2026
Prove: \forall {n}: {\Z }.\; \mathrm {Odd}(n) \leftrightarrow \mathrm {Odd}(n^2).
Proof.
- February 11, 2026
Subproof. Odd squares, again ›
- February 11, 2026
Subproof. Odd squares, again ›
- February 11, 2026
Suppose \mathrm {Odd}(k^2), that is, suppose there is an integer j such that k^2 = 2j+1. Then we must show \mathrm {Odd}(k), that is, we must pick an integer p such that k = 2p + 1.
Subproof. Odd squares, again ›
Let's try again. Instead of directly proving \mathrm {Odd}(k^2) \rightarrow \mathrm {Odd}(k), let's prove its contrapositive, which we know is logically equivalent. The contrapositive is \neg \mathrm {Odd}(k) \rightarrow \neg \mathrm {Odd}(k^2), but we will assume the following theorem for now:
For any integer n, \neg \mathrm {Odd}(n) \leftrightarrow \mathrm {Even}(n).Theorem [005S]
Proof.
- February 11, 2026
Subproof. Odd squares, again ›
- February 11, 2026
Example Irrationality of \sqrt {2} [005P]
- February 11, 2026
Definition Rational number [005Q]
- February 11, 2026
A rational number is a number that is a ratio of two integers. That is, formally, \mathrm {Rational}(r) \equiv \exists {p}: {\Z }.\; \exists {q}: {\Z }.\; r = \frac {p}{q}.
Remark [009E]
- March 6, 2026
Actually, the definition of the rational numbers is a bit more subtle than this; the above definition does not take into account the fact that, say, 1/2 = 2/4. We will see how to deal with this when we learn about equivalence relations.
Definition Irrational number [009D]
- March 6, 2026
A number is irrational if it is not rational, that is, \mathrm {Irrational}(x) \equiv \neg \mathrm {Rational}(x).
Prove that \sqrt {2} is irrational.
Proof.
- February 11, 2026
Subproof. Irrationality of \sqrt {2} ›
- February 11, 2026
Since we know \mathrm {Rational}(\sqrt {2}), by definition that means there exist some integers p and q such that \sqrt {2} = \frac {p}{q}. We may further assume that p and q have no common factors, that is, p/q is reduced to lowest terms (if it were not, we could just cancel common factors until none are left, which would not affect equality with \sqrt 2). We can reason as follows:
Since p is even, there must be an integer r such that p = 2r. Continuing,
But this implies—using identical reasoning as for p—that q is even as well. Hence p and q are both even and have a factor of 2 in common. But we started out by assuming that p and q share no common factors! Thus we have a contradiction, that is, we have proved \mathsf {F}: at this point we know that p and q both do and do not have a common factor, and P \land \neg P \equiv \mathsf {F}.
Set theory [0060]
- February 16, 2026
Introduction to set theory [0061]
- February 16, 2026
The theory of sets is of both practical and historic significance. It is of practical significance because working with collections of values is commonplace in computing and mathematics. It is also of historical significance as one of the first attempts to put mathematics as a whole on a rigorous logical foundation. Even today, if you ask most mathematicians what the foundation of mathematics is, they will give set theory as the answer. On this view, set theory is sort of like the "assembly language" of mathematics: everything is ultimately "compiled down" into the language of set theory, even though no one thinks about it on a day-to-day basis. I would argue that something called type theory provides a much better foundation for mathematics as it is actually carried out—but that is a topic for a different course!
Definition Set [0062]
- February 16, 2026
A set is an unordered, finite or infinite collection of objects, called elements or members of the set. Sets cannot contain a given element more than once, and the order of elements in a set does not matter. Put another way, the only thing that matters about a given element is whether it is in the set or not.
Remark Set variables [0063]
- February 16, 2026
We typically use capital letter variables to stand for sets, like A, B, C, S, T, ... and lowercase letters to stand for elements of sets.
Definition Element-of relation [0064]
- February 16, 2026
The notation x \in S is a proposition that means “x is an element of set S”; x \notin S is an abbreviation for \neg (x \in S).
Writing sets [0065]
- February 16, 2026
There are two basic ways to write down a set.
Literal set notation [0068]
- February 16, 2026
We can write a (finite) set by listing its elements, separated by commans, within curly braces.
Example [0066]
- February 16, 2026
S = \{1,3,5,7,9\}.
We can also sometimes abbreviate using an ellipsis (three dots), which generally means "fill in the obvious pattern". One must be careful, however, since what counts as "obvious" depends a lot on context and audience. When in doubt, and in very formal situations, it's best to avoid the use of ellipsis; but it can be convenient in an informal setting.
Example [0067]
- February 16, 2026
S = \{1,2,3, \dots , 100\}.
Example [006E]
- February 18, 2026
We can have sets with things other than numbers, such as \{\text {cow}, \text {horse}, \text {dog}\}.
Example [006F]
- February 18, 2026
\{53\} is a set with one element.
Definition Empty set [006G]
- February 18, 2026
The empty set, written \varnothing or \{\}, is the set with no elements.
Remark [006H]
- February 18, 2026
The definition of sets technically allows for sets with a mix of different kinds of elements, like \{2, \mathsf {T}, \text {cow}, \text {``hello''}\}, but we will never do this; our sets will always be collections of elements of the same type.
Set builder notation [0069]
- February 16, 2026
We can also use set builder (or set comprehension) notation, whose basic form is \{ \text {expression} \mid \text {variable definition}, \text {conditions} \}
This means that for each value of the variable(s) defined on the right side of the vertical bar which satisfy all the conditions, the set will contain the value of the expression on the left side of the vertical bar.
Note that the expression on the left of the \mid must evaluate to the value of the set; the conditions on the right of the \mid must be propositions.
Example [006B]
- February 16, 2026
This is the set of all positive integers which are less than 5, that is, the set \{1,2,3,4\}.
Example [006C]
- February 16, 2026
We can stack multiple conditions; for a given x, all the conditions must be true for it to be considered. This is the set of all odd integers between 0 and 100.
Example [006D]
- February 16, 2026
The expression to the left of the vertical bar does not have to be just a variable; it can be any expression. For each value of x that satisfies the conditions on the right side, we put that value of x into the expression to compute a value that will be in the set.
In this example, when x = 0, we get 2x+5 = 2 \cdot 0 + 5 = 5; when x = 1, we get 2x+5 = 7, and so on. All told, this defines the set \{5, 7, 9, \dots , 25\}.
Example [006I]
- February 18, 2026
This is the (infinite) set of all possible ordered pairs of integers which sum to 6. That is, \{(2,4), (3,3), (-10, 16), \dots \}.
Special sets [006J]
- February 18, 2026
There are various sets that are so fundamental and frequently useful that we give them special names.
- \varnothing , the empty set
- \mathbb {N} = \{0, 1, 2, \dots \}, the natural numbers
- \Z = \{\dots , -2, -1, 0, 1, 2, \dots \}, the integers
- \Z ^+ = \{1, 2, \dots \}, the positive integers
- \mathbb {Q} = \{ p/q \mid p \in \Z , q \in \Z , q \neq 0 \}, the rational numbers
Actually, the definition of the rational numbers is a bit more subtle than this; the above definition does not take into account the fact that, say, 1/2 = 2/4. We will see how to deal with this when we learn about equivalence relations in a couple weeks.
- \mathbb {R}, the real numbers
- \mathbb {C}, the complex numbers
We will not use \mathbb {C} at all in this class, and we will only mention \mathbb {R} occasionally.
Subsets [006K]
- February 18, 2026
Definition Subset [006L]
- February 18, 2026
A is a subset of B, written A \subseteq B, iff every element of A is also an element of B. That is, formally: A \subseteq B \equiv \forall {a}: {A}.\; a \in B.
Example [006S]
- February 18, 2026
- \{1,3,6\} \subseteq \{2,3,7,6,1\}: each element of the set \{1,3,6\} is also an element of \{2,3,7,6,1\}.
- \{1,3,6\} \subseteq \mathbb {N}.
- \{1,3,6\} \not \subseteq \{2,7,6,1\}, since 3 is an element of the first set but not the second.
Remark Is a set a subset of itself? [006M]
- February 18, 2026
According to the definition, given a set A, is A \subseteq A? That is, is a set a subset of itself?
Intuitively, given the way we typically use the word “subset” in English, we might expect the answer to be “no”. However, given the definition above, A \subseteq A is true, since every element of A is an element of A.
(Note there is a notion of being a strict subset: A \subset B, “A is a strict subset of B”, means that A \subseteq B but A \neq B; however, we will not use it.)
Why do we define subset this way? It would actually make things more complicated, with more weird special cases, if we disallowed a set from being a subset of itself.
Remark Is the empty set a subset? [006R]
- February 18, 2026
Recall the definition of the empty set:
Definition Empty set [006G]
- February 18, 2026
The empty set, written \varnothing or \{\}, is the set with no elements.
Question: is \varnothing \subseteq \mathbb {N}?
Answer: yes! By definition, this means \forall {a}: {\varnothing }.\; a \in \mathbb {N}, so it really comes down to what we do with a \forall whose domain is empty; but as we discussed previously, such a \forall should be considered true.
In fact, the empty set is a subset of every set, even itself.
Equality of sets [006N]
- February 18, 2026
Now that we have defined subset, we can define what it means for two sets to be equal.
Definition Set equality [006O]
- February 18, 2026
Two sets are equal if each is a subset of the other; that is, (A = B) \equiv (A \subseteq B) \land (B \subseteq A).
Remark [006P]
- February 18, 2026
This is typically how we prove that two sets are equal: show that each is a subset of the other. We might want to do this, for example, in a situation where we have two sets which are described in very different ways, and it is an interesting and nontrivial fact that these two different descriptions actually result in the same set.
Example [006Q]
- February 18, 2026
Let A = \{ 2x+3 \mid x \in \Z \} and B = \{ x \mid x \in \Z , \mathrm {Odd}(x) \}. Prove that A = B.
Proof.
- February 18, 2026
Subproof. http://ozark.hendrix.edu/~yorgey/forest/006Q/ ›
- February 18, 2026
Subproof. http://ozark.hendrix.edu/~yorgey/forest/006Q/ ›
- February 18, 2026
Subproof. http://ozark.hendrix.edu/~yorgey/forest/006Q/ ›
- February 18, 2026
Subproof. http://ozark.hendrix.edu/~yorgey/forest/006Q/ ›
- February 18, 2026
Finite sets in Disco [006T]
- February 18, 2026
Disco has built-in support for sets—though, for reasons we will explore, only finite sets.
Syntax for sets in Disco [006U]
- February 19, 2026
Disco gives us a few different ways to write sets.
Set literals in Disco [006V]
- February 19, 2026
We can write literal sets using curly braces and commas in between elements, just as in standard mathematical notation. Notice that the order in which we write the elements, or the number of copies we write of each element, does not matter.
Disco> {1,2,3}
{1, 2, 3}
Disco> {3,1,1,2}
{1, 2, 3}
Disco> {} -- this is how we write the empty set
{}We can also use ellipses to indicate an omitted sequence of elements. Notice that the number of dots in the ellipses, or whether you use commas and/or spaces before and after the ellipses, does not matter.
Disco> {1..5}
{1, 2, 3, 4, 5}
Disco> {1, ......., 5}
{1, 2, 3, 4, 5}
Disco> {1, 3, 5 ... 15}
{1, 3, 5, 7, 9, 11, 13, 15}
Disco> {8 ... 3}
{3, 4, 5, 6, 7, 8}
Disco> {5, 10, 15, ... 40}
{5, 10, 15, 20, 25, 30, 35, 40}
Disco> {1, 4, 9, ... 36}
{1, 4, 9, 16, 25, 36}The last few examples show that Disco can follow basic patterns when using ellipses. For example, {1, 3, 5 ...} will generate odd numbers, {8 ... 3} counts down instead of up, and {5, 10, ...} counts by fives. In the last example, we can even generate square numbers! See the Disco language reference documentation if you are interested to understand exactly what Disco is doing here: basically it is fitting a polynomial to the given points.
Set comprehensions in Disco [006W]
- February 19, 2026
Disco also supports set comprehensions, i.e. set builder notation. For example:
Disco> {x | x in {1..5}} -- same as {1..5}
{1, 2, 3, 4, 5}
Disco> {3x | x in {1..5}} -- multiply each element of {1..5} by 3
{3, 6, 9, 12, 15}
-- Pick out the elements of {1..10} that satisfy the condition
Disco> {x | x in {1 .. 10}, x^2 + 20 == 9x}
{4, 5}
-- Pick out the elements of {1..100} that satisfy all the conditions
Disco> {x | x in {1 .. 100}, x =< 10 \/ x >= 90, x mod 2 == 0}
{2, 4, 6, 8, 10, 90, 92, 94, 96, 98, 100}
-- Products of all combinations of elements from {1..4} and {1, 10, 100}
Disco> {x * y | x in {1 .. 4}, y in {1, 10, 100}}
{1, 2, 3, 4, 10, 20, 30, 40, 100, 200, 300, 400}
-- Pairs of elements from {1..4} where the first is >= the second
Disco> {(x,y) | x in {1 .. 4}, y in {1 .. x}}
{(1, 1), (2, 1), (2, 2), (3, 1), (3, 2), (3, 3), (4, 1), (4, 2), (4, 3), (4, 4)}Set types in Disco [006X]
- February 19, 2026
The type of a finite set with elements of type T is written Set(T). For example:
Disco> :type {1,2,3}
{1, 2, 3} : Set(ℕ)
Disco> :type {T, F}
{T, F} : Set(Bool)All the elements in a set have to have the same type. For example, this is an error:
Disco> {1, T}
Error: typechecking failed.However, we can mix and match different numeric types, and Disco will just give them the smallest type that works for all of them.
Disco> :type {1,2}
{1, 2} : Set(ℕ)
Disco> :type {3, 4, -5}
{3, 4, -5} : Set(ℤ)
Disco> :type {2, 1/2}
{2, 1 / 2} : Set(𝔽)
Disco> :type {2, -1, 3, 5/2}
{2, -1, 3, 5 / 2} : Set(ℚ)If we ask for the type of the empty set, Disco responds with Set(a): the a here is a variable that can stand for any type. This is our first example of polymorphism.
Disco> :type {}
{} : Set(a)Types vs sets [006Y]
- February 19, 2026
There is something quite subtle going on with the distinction between sets and types. In math, \mathbb {N} and \{1,2,3\} are both sets, but in Disco, \mathbb {N} is a type and \{1,2,3\} is a set. In a math context, it makes sense to say \{1,2,3\} \subseteq \mathbb {N}, but in Disco this is a syntax error!
Part of the difference is explained by the fact that Disco only supports finite sets. If infinite sets were allowed, Disco's implementation of sets would have to be much more complicated, and it would in general be difficult to know whether a given expression involving sets will run forever or not.
Another part of the difference lies in the distinction between type theory and set theory. Disco does not take sets as primitive, but starts with a collection of primitive types, corresponding to collections of objects with particularly nice closure properties—such as the natural numbers, which we know are closed under addition and multiplication. Finite sets are then something we can carve out of these existing primitive collections.
subset and elem operators [006Z]
- February 19, 2026
We can check whether one set is a subset of another using the subset operator (which can also be written ⊆):
Disco> {1,3,2} subset {4,6,2,9,1,3}
T
Disco> {3,4,5} ⊆ {4,5,6}
FWe can also check whether a value is an element of a set using the elem operator (which can also be written ∈):
Disco> 1 elem {3,6,1}
T
Disco> 3 ∈ {4,5,6}
FOperations on sets [0071]
- February 20, 2026
Just as with propositions, there are many algebraic operations we can use to combine and manipulate sets.
Cardinality [0072]
- February 20, 2026
The cardinality of a finite set is the number of distinct elements it contains, i.e. a natural number. The cardinality of a set A is written |A|.
Example [0073]
- February 20, 2026
- |\{1,3,5\}| = 3.
- |\{2, 4, \dots , 100\}| = 50.
- |\varnothing | = 0.
Notice that we can’t (yet!) say anything about the cardinality of an infinite set!
Cardinality in Disco [0076]
- February 23, 2026
In Disco, we can also use vertical bars to ask for the cardinality of a set:
Disco> |{1,2,3}|
3
Disco> |{1, 3, ... 100}|
50
Disco> |{}|
0Cartesian product [0077]
- February 23, 2026
Definition Cartesian product [0078]
- February 23, 2026
If A and B are sets, the (Cartesian) product of A and B, written A \times B, is the set of all possible ordered pairs of elements from A and B. That is, A \times B = \{ (a, b) \mid a \in A, b \in B \}.
Example [0079]
- February 23, 2026
Let A = \{1, 2, 3\} and B = \{ \triangle , \square \}. Then A \times B = \{(1,\triangle ), (1,\square ), (2,\triangle ), (2,\square ), (3,\triangle ), (3,\square )\}. We can think of this in terms of filling out a 2D grid where the rows are labeled with elements from A and the columns with elements from B:
\begin {array}{ccc} & \triangle & \square \\ 1 & (1,\triangle ) & (1, \square ) \\ 2 & (2,\triangle ) & (2, \square ) \\ 3 & (3,\triangle ) & (3, \square ) \\ \end {array}Each cell in the grid corresponds to one possible combination of an element from A with an element from B.
From this we can also see that for finite sets, the cardinality of a Cartesian product is the product of the cardinalities: |A \times B| = |A| \times |B|.
Cartesian product in Disco [007A]
- February 23, 2026
We can use the >< operator in Disco for Cartesian product:
Disco> {1,2,3} >< {T, F}
{(1, F), (1, T), (2, F), (2, T), (3, F), (3, T)}Cartesian product of types [007X]
- February 26, 2026
In Disco, we can take the Cartesian product of types as well. For example, if we take the Cartesian product of a set of natural numbers and a set of Booleans, the type of the resulting elements is ℕ × Bool:
Disco> {1,2,3} >< {T,F}
{(1, F), (1, T), (2, F), (2, T), (3, F), (3, T)}
Disco> :type {1,2,3} >< {T,F}
{1, 2, 3} >< {T, F} : Set(ℕ × Bool)In this example, ℕ × Bool is the type of the elements, which look like pairs such as (1, F). In general, the type A × B (also written A >< B or A * B) has values that look like ordered pairs (a,b).
Power set [007B]
- February 23, 2026
Definition Power set [007C]
- February 23, 2026
The power set of a set A, written \mathcal {P}(A), is the set of all possible subsets of A.
Example [007D]
- February 23, 2026
Let A = \{1,2,3\}. Then \mathcal {P}(A) = \{ \varnothing , \{1\}, \{2\}, \{3\}, \{1,2\}, \{1,3\}, \{2,3\}, \{1,2,3\}\}.
Example [007E]
- February 23, 2026
\mathcal {P}(\varnothing ) = \{ \varnothing \}, since the only subset of the empty set is itself.
Listing all elements of a power set [007F]
- February 23, 2026
How do we know if we’ve listed all possible subsets? Notice that to make a particular subset, we get to independently choose whether each element will be in the subset or not. So we can associate a Boolean variable to each element of the set, and list all possible combinations of \mathsf {T} and \mathsf {F}, just like we would when making a truth table:
\begin {array}{cccc} \text {1?} & \text {2?} & \text {3?} & \text {subset} \\ \hline \mathsf {T} & \mathsf {T} & \mathsf {T} & \{1,2,3\} \\ \mathsf {T} & \mathsf {T} & \mathsf {F} & \{1,2\} \\ \mathsf {T} & \mathsf {F} & \mathsf {T} & \{1,3\} \\ \mathsf {T} & \mathsf {F} & \mathsf {F} & \{1\} \\ \mathsf {F} & \mathsf {T} & \mathsf {T} & \{2,3\} \\ \mathsf {F} & \mathsf {T} & \mathsf {F} & \{2\} \\ \mathsf {F} & \mathsf {F} & \mathsf {T} & \{3\} \\ \mathsf {F} & \mathsf {F} & \mathsf {F} & \varnothing \\ \end {array}Or we could just dispense with the booleans and make a table where we either list or don’t list each element in all possible combinations:
\begin {array}{rcccl} & \text {1?} & \text {2?} & \text {3?} & \\ \hline \{ & 1 & 2 & 3 & \} \\ \{ & 1 & 2 & & \} \\ \{ & 1 & & 3 & \} \\ \{ & 1 & & & \} \\ \{ & & 2 & 3 & \} \\ \{ & & 2 & & \} \\ \{ & & & 3 & \} \\ \{ & & & & \} \\ \end {array}This line of reasoning shows us that in general, if A is a finite set and |A| = n, then |\mathcal {P}(A)| = 2^n. Note this even works when A is empty: we have \mathcal {P}(\varnothing ) = \{\varnothing \} (since the empty set is the only subset of itself), and 2^0 = 1.
Notice how this is all pointing to the fact that we should allow both A \subseteq A and \varnothing \subseteq A. The entire set and the empty set both show up naturally when enumerating every possible combination of true/false values, and we get an elegant formula, 2^n, for the number of subsets including the entire set and the empty set. If we were to disallow these from being subsets, we would end up with a less elegant formula like 2^n - 2 for the number of subsets, and would have to use the word “except” when describing how to generate all possible subsets. We would essentially be fighting against the universe. The entire set and the empty set want to be subsets.
Power sets in Disco [007G]
- February 23, 2026
We can compute power sets in Disco using the power function:
Disco> power({1,2,3})
{{}, {1}, {1, 2}, {1, 2, 3}, {1, 3}, {2}, {2, 3}, {3}}
Disco> power({})
{{}}
Disco> power({2..5})
{{}, {2}, {2, 3}, {2, 3, 4}, {2, 3, 4, 5}, {2, 3, 5}, {2, 4}, {2, 4, 5}, {2, 5}, {3}, {3, 4}, {3, 4, 5}, {3, 5}, {4}, {4, 5}, {5}}
Disco> { |power({1..n})| | n in {1 .. 8} }
{2, 4, 8, 16, 32, 64, 128, 256}When given a set of type Set(T), the power function produces a result of type Set(Set(T)); that is, given a set of T, it produces a set of sets of T.
Disco> :type {1,2,3}
{1, 2, 3} : Set(ℕ)
Disco> :type power({1,2,3})
power({1, 2, 3}) : Set(Set(ℕ))Intersection [007I]
- February 24, 2026
Definition Intersection [007J]
- February 24, 2026
The intersection of sets A and B is the set of elements common to both: A \cap B = \{ x \mid x \in A \land x \in B \}.
Intersection in Disco [0080]
- February 26, 2026
We can compute the intersection of finite sets in Disco with the intersect operator (also written ∩):
Disco> {1,2,3} intersect {2,4,6}
{2}Union [007L]
- February 24, 2026
Definition Union [007M]
- February 24, 2026
The union of sets A and B is the set of elements which are in either: A \cup B = \{ x \mid x \in A \lor x \in B \}.
Cardinality of union [007N]
- February 24, 2026
What can we say about the cardinality of A \cup B? First, we can give it some lower and upper bounds: \max (|A|, |B|) \leq |A \cup B| \leq |A| + |B|. But we can also say something exact: |A \cup B| = |A| + |B| - |A \cap B|. If we add the cardinality of A and B, any elements common to both are counted twice, so we have to subtract them in order to find the cardinality of their union (which has no duplicates). This is (a simple special case of) the principle of inclusion-exclusion, or "PIE".
Union in Disco [007Y]
- February 26, 2026
We can compute the union of finite sets in Disco with the union operator (also written ∪):
Disco> {1,2,3} union {2,4,6}
{1, 2, 3, 4, 6}Remark Union and intersection notation [007O]
- February 24, 2026
The notation for intersection and union is not an accident. Notice that \cap is defined in terms of \land and \cup is defined in terms of \lor ; intersection is kind of like “AND for sets” and union is “OR for sets”. (Also \cup looks like a “U” for “union”.)
Difference [007Q]
- February 24, 2026
Definition Set difference [007P]
- February 24, 2026
The difference of two sets A and B, written A - B or A \mathbin {\backslash } B, consists of the elements in A but not B: A - B = \{ x \mid x \in A \land x \notin B \}.
Set difference in Disco [007Z]
- February 26, 2026
Note that set difference in Disco must be written with the \ operator. The subtraction symbol cannot be used, since it would introduce too much ambiguity with subtraction on numbers.
Disco> {3,4,5} \ {1,2,3}
{4, 5}Complement [007R]
- February 24, 2026
Finally, we want to define the complement of a set as the set of things which are not in the set. However, we have to be careful: this is a bit nonsensical on its own. For example, if A = \{1,2,3\}, what elements are in its complement? Surely the number 4 is, but what about my sister? Or Beethoven? Or the small brown rock I picked up and threw into a pond last Tuesday? We have to restrict ourselves to some universal set, a “universe of discourse” that contains all the potential elements we are considering; in a given context, all the sets we work with will be subsets of the universal set. (If we are working with typed sets, where the elements are all taken from a particular type, then the type itself serves as the universal set.)
Definition Set complement [007S]
- February 24, 2026
Given a universal set U, the of a set A is all the elements of U which are not in A: \overline {A} = U - A = \{ x \mid x \in U \land x \notin A \}.
Note that there is no way to take the complement of a set in Disco, since Disco can only work with finite sets. The complement of a finite set is often infinite, in the general case where the universe of discourse U is infinite. For example, \{1,2,3\} is a finite set of natural numbers, but its complement is the infinite set \{0, 4, 5, 6, \dots \}.
Remark Algebraic laws for set operations [007T]
- February 24, 2026
Set union, intersection, and complement follow laws which are exactly analogous to the algebraic laws for \lor , \land , and \neg . For example, \cup and \cap are associative, commutative, and idempotent, and have \varnothing and the universal set U as their identity and/or annihilator; \cap distributes over \cup and vice versa, that is, A \cap (B \cup C) = (A \cap B) \cup (A \cap C); there are also analogues of De Morgan’s laws, for example, \overline {A \cup B} = \overline {A} \cap \overline {B}. These follow from the fact that each set operation is defined in terms of a corresponding logical operation.
Example proofs with sets [0082]
- February 26, 2026
Theorem Union is idempotent [0086]
- February 27, 2026
Set union is idempotent, that is, A \cup A = A for all sets A.
Proof.
- February 27, 2026
Subproof. Union is idempotent ›
- February 27, 2026
Subproof. Union is idempotent ›
- February 27, 2026
Theorem De Morgan for union [0083]
- February 27, 2026
For all sets S and T, \overline {S \cup T} = \overline {S} \cap \overline {T}.
Proof.
- February 27, 2026
Subproof. De Morgan for union ›
- February 27, 2026
Subproof. De Morgan for union ›
- February 27, 2026
Subproof. De Morgan for union ›
- February 27, 2026
Note that (\forall {x}: {U}.\; x \in S \leftrightarrow x \in T) \leftrightarrow (S = T). So we could alternatively phrase the above proof like this:
Proof.
- February 27, 2026
Theorem Distributivity of intersection over union [0084]
- February 27, 2026
For all sets A, B, and C, A \cap (B \cup C) = (A \cap B) \cup (A \cap C).
Proof.
- February 27, 2026
Theorem Characterization of subset via union [0085]
- February 27, 2026
For all sets A and B, (A \subseteq B) \leftrightarrow (A \cup B = B).
Proof.
- February 27, 2026
Subproof. Characterization of subset via union ›
- February 27, 2026
Subproof. Characterization of subset via union ›
- February 27, 2026
Subproof. Characterization of subset via union ›
- February 27, 2026
Subproof. Characterization of subset via union ›
- February 27, 2026
Subproof. Characterization of subset via union ›
- February 27, 2026
Subproof. Characterization of subset via union ›
- February 27, 2026
Subproof. Characterization of subset via union ›
- February 27, 2026
Subproof. Characterization of subset via union ›
- February 27, 2026
Relations [0089]
- February 28, 2026
Relations [008A]
- February 28, 2026
Definition Relation [008B]
- February 28, 2026
Let A and B be sets. A relation from A to B is a subset of the Cartesian product A \times B.
We write a\; R \; b to denote (a,b) \in R (“a is related to b by R”).
Example [008C]
- February 28, 2026
Let S be set of students at Hendrix, and C the set of courses offered next year. Let R be the relation \{ (s,c) \mid \text {$s$ is preregistered for $c$} \}. We might visualize this relation by drawing dots for students on the left, and courses on the right, with a line connecting students to courses they are preregistered for.
Most relations we care about are actually from a set to itself.
Definition Relation on a set [008D]
- February 28, 2026
A relation on a set A is a relation from A to A (that is, a subset of A \times A).
Example Example relations [008E]
- February 28, 2026
Here are some example relations on \mathbb {N}:
R_1 = \{(a,b) \mid a \leq b\}
R_2 = \{(a,b) \mid a > b\}
R_3 = \{(a,b) \mid a = b\}
R_4 = \{(a,b) \mid a = b + 1\}
R_5 = \{(a,b) \mid a + b \leq 10\}
R_6 = \{(a,b) \mid a \text { evenly divides } b\}
R_7 = \{(a,b) \mid a, b \text { end with the same digit in base 10} \}
And a few more example relations over other sets:
R_8 = \{(P,Q) \mid P, Q \text { propositions }, P \to Q\}
R_9 = \{(P,Q) \mid P, Q \text { propositions }, P \leftrightarrow Q\}
R_{10} = \{(S,T) \mid S \subseteq T\}
Properties of relations [008S]
- March 2, 2026
Many relations we care about have one or more nice properties.
Reflexivity [008I]
- February 28, 2026
Definition Reflexive relation [008G]
- February 28, 2026
A relation R on a set A is reflexive if \forall a \in A.\, a\;R\;a, that is, every element is related to itself.
Example relations which are reflexive [008H]
- February 28, 2026
Of our example relations, the \leq (R_1), equality (R_3), divisibility (R_6), last digit (R_7), implication (R_8), biconditional (R_9) and subset (R_{10}) relations are reflexive, whereas the others are not. Note that there exist natural numbers which are related to themselves under R_5; for example, 2\;R_5\;2 since 2 + 2 \leq 10. However, it is not the case that every natural number is related to itself under R_5 (for example, 7 + 7 \nleq 10), so R_5 is not reflexive.
Symmetry [008J]
- February 28, 2026
Definition Symmetric relation [008K]
- February 28, 2026
A relation R on a set A is symmetric if \forall a,b \in A.\, a\;R\;b \rightarrow b\;R\;a, that is, whenever a and b are related, then b and a are also related.
Example relations which are symmetric [008L]
- February 28, 2026
The only symmetric relations among our examples are R_3 (equality), R_5, R_7, and R_9.
Antisymmetry [008M]
- February 28, 2026
Definition Antisymmetric relation [008N]
- February 28, 2026
A relation R on a set A is antisymmetric if \forall a,b \in A.\, (a\;R\;b) \land (b\;R\;a) \rightarrow a = b, that is, the only way we can possibly have both a\;R\;b and b\;R\;a is when a and b are actually the same. In other words, whenever a and b are different, at most one of a\;R\;b or b\;R\;a can hold. Intuitively, this says that the relation R is as far from being symmetric as possible: there’s not even a single pair of values that exhibits symmetry.
Example relations which are antisymmetric [008O]
- February 28, 2026
Let's consider which of our example relations are antisymmetric.
- R_1 (\leq ) is clearly antisymmetric: if a \leq b and b \leq a then we definitely have a = b.
- R_{10} (subset) is similarly antisymmetric.
- R_6 (divisibility) is also antisymmetric on the natural numbers: the only way to have both a divisible by b and b divisible by a is if a = b; we will be able to prove this later in the semester.
- R_3 (equality) is antisymmetric, though for a boring reason.
- It may require a bit more headscratching to see, but R_2 and R_4 are also antisymmetric. For example, take R_2 (>). For this to be antisymmetric, we must have \forall a,b \in \mathbb {N}.\, (a > b) \land (b > a) \rightarrow a = b. Is this true? In fact, it is, since it is impossible for (a > b) \land (b > a) to ever be true, and an implication is always trivially true when its premise is false.
- Note finally that R_8 is not antisymmetric: if P \to Q and Q \to P then they are logically equivalent, but it does not mean they are exactly the same.
Transitivity [008P]
- February 28, 2026
Definition Transitive relation [008Q]
- February 28, 2026
A relation R on a set A is transitive if \forall a,b,c \in A.\, (a\;R\;b) \land (b\;R\;c) \rightarrow (a\;R\;c).
Example relations which are transitive [008R]
- February 28, 2026
The only relations among our examples which are not transitive are R_4 and R_5. All the rest are transitive. We use the transitivity of these relations all the time! For example, transitivity of = is what allows us to prove X = Y by showing a chain of equalities X = X_1 = X_2 = \dots = Y.
Equivalence relations [008Y]
- March 4, 2026
A very important class of relations is equivalence relations, which in some suitable sense act like “a more general sort of equality”. We have used several of them this semester already!
Definition Equivalence relation [008Z]
- March 4, 2026
An equivalence relation (sometimes just an equivalence) on a set A is a reflexive, symmetric, transitive relation.
I don’t know of a good way to explain why these particular properties are the “right” ones to capture “equality-like” things. But hopefully as we see some examples you will be convinced.
Equivalence relation examples [0094]
- March 4, 2026
Example Equality [0090]
- March 4, 2026
Of course, equality itself is an equivalence relation. We can check that it is indeed reflexive, symmetric, and transitive.
Example Words by length [0091]
- March 4, 2026
Consider the relation L on the set of all English words, where two words are related if and only if they have the same number of letters. This is an equivalence relation:
Reflexive? Check, each word has the same number of letters as itself.
Symmetric? Check, if a has the same number of letters as b, then b has the same number of letters as a.
Transitive? Check, if a has the same number of letters as b, and b has the same number of letters as c, then a has the same number of letters as c.
Example Last digit in base 10 [0092]
- March 4, 2026
What about the example from last time where two numbers were related when they had the same last digit in base 10? This is an equivalence relation too. Reflexivity, symmetry, and transitivity follow straightforwardly from the definition. Later in the semester, we will explore a more general version of this relation.
Example If and only if [0093]
- March 4, 2026
If and only if, aka biconditional (\leftrightarrow ), is an equivalence relation on propositions.
Example Case-insensitive equality [009M]
- March 7, 2026
We can define a relation on strings where two strings are related if they have the same characters in the same order, except that letters may differ in whether they are upper- or lowercase. For example, Straw! would be related to sTRaW! but not to strawberry, nor to warts.
Example Lists up to reordering [009N]
- March 7, 2026
As another example, consider the relation on lists of elements, where two lists are related if they have exactly the same elements, but possibly in a different order. For example, [1,2,3,1] is related to [3,1,1,2], but not to [3,2,1].
Many equivalence relations that arise naturally in mathematics and computer science are like this: we start out with a bunch of things that are all different, but there is some aspect of them we don't care about. We want to consider things "the same" if the only difference bewteen them has to do with the part we don't care about.
Example Divisibility [0095]
- March 4, 2026
What about the divisibility relation R_6? It is reflexive and transitive; however, it is not symmetric, so it is not an equivalence relation.
Example Close integers [0096]
- March 4, 2026
Consider the relation on integers given by \{(a,b) \mid |a - b| \leq 1 \}. In other words, a and b are related when they are “very close”, that is, the distance between them is at most 1. This is reflexive and symmetric, but not transitive, so it is not an equivalence relation.
Equivalence classes [0098]
- March 4, 2026
So, what kinds of things can we say about equivalence relations in general? First, let’s define the important concept of an equivalence class.
Definition Equivalence class [0097]
- March 4, 2026
Let \sim be an equivalence relation on A, and let a \in A. The equivalence class of a, denoted [a], is the set of all elements of A which are related to a, that is, [a] = \{ b \in A \mid a \sim b \}.
Example Words by length [0099]
- March 4, 2026
Under the equivalence relation L (words are related if they have the same length), the equivalence class of “dog” is the set of all three-letter English words: [\text {dog}] = \{ \text {dog}, \text {cow}, \text {hat}, \text {eat}, \dots \}. This is a finite (but large) set.
Example Last digit in base 10 [009A]
- March 4, 2026
Under the equivalence relation R_7, where natural numbers are equivalent if they have the same last digit in base 10, the equivalence class of 1 is the infinite set [1] = \{ 1, 11, 21, 31, \dots \}.
Suppose we have two elements a, b \in A. What can we say about the potential relationship between their equivalence classes, [a] and [b]? It turns out there are only two possibilities: either they are exactly the same, or they do not overlap at all.
Theorem Equivalence classes are disjoint [009B]
- March 4, 2026
Let \sim be an equivalence relation on a set A. For any a, b \in A, the following three propositions are logically equivalent:
- a \sim b
- [a] = [b]
- [a] \cap [b] \neq \varnothing
Proof.
- March 4, 2026
Subproof. Equivalence classes are disjoint ›
- March 4, 2026
Subproof. Equivalence classes are disjoint ›
- March 4, 2026
Subproof. Equivalence classes are disjoint ›
- March 4, 2026
Subproof. Equivalence classes are disjoint ›
- March 4, 2026
Subproof. Equivalence classes are disjoint ›
- March 4, 2026
Example ★ The rational numbers as equivalence classes [009C]
- March 6, 2026
Why do we care about equivalence relations so much? It turns out that they show up all over mathematics and computer science. Here is just one example. Recall that we defined the set of rational numbers \mathbb {Q} like so:
Definition Rational number [005Q]
- February 11, 2026
A rational number is a number that is a ratio of two integers. That is, formally, \mathrm {Rational}(r) \equiv \exists {p}: {\Z }.\; \exists {q}: {\Z }.\; r = \frac {p}{q}.
However, we noted that this definition is not really complete, since it does not take into account the fact that, say, 1/2 = 2/4, or 6/10 = 9/15. Now we finally have the tools to see the real definition.
Definition \sim _{\mathbb {Q}} [009F]
- March 6, 2026
Define a relation \sim _{\mathbb {Q}} on \Z \times (\Z - \{0\}) as follows: (a,b) \sim _{\mathbb {Q}} (c,d) \leftrightarrow ad = cb.
Theorem \sim _{\mathbb {Q}} is an equivalence [009G]
- March 6, 2026
Proof.
- March 6, 2026
- Reflexivity: (a,b) \sim _{\mathbb {Q}} (a,b) for any (a,b) \in \Z \times (\Z - \{0\}), since ab = ab.
- Symmetry: If (a,b) \sim _{\mathbb {Q}} (c,d), then by definition ad = cb. Hence cb = ad, which means (c,d) \sim _{\mathbb {Q}} (a,b) also.
- Transitivity: Suppose (a,b) \sim _{\mathbb {Q}} (c,d), and (c,d) \sim _{\mathbb {Q}} (e,f). Then by definition ad = cb and cf = ed, and we know b \neq 0, d \neq 0, and f \neq 0. We wish to show (a,b) \sim _{\mathbb {Q}} (e,f), that is, af = eb. We have two cases to consider: either c = 0 or c \neq 0.
Subproof. \sim _{\mathbb {Q}} is an equivalence ›
- March 6, 2026
If c = 0, then ad = cb = 0, and the fact that d \neq 0 means we must have a = 0. Likewise, cf = ed implies e = 0. Hence, af = eb = 0 as desired.Subproof. \sim _{\mathbb {Q}} is an equivalence ›
- March 6, 2026
If c \neq 0, multiplying equals by equals yields adcf = cbed; we can cancel cd from both sides (since c \neq 0 and d \neq 0) to yield af = eb.
Equivalence classes of \sim _{\mathbb {Q}} [009H]
- March 6, 2026
What do the equivalence classes of \sim _{\mathbb {Q}} look like? Well, to pick an example, (1,2) \sim _{\mathbb {Q}} (2,4) since 1 \cdot 4 = 2 \cdot 2. Or again, (1,2) \sim _{\mathbb {Q}} (3,6) since 1 \cdot 6 = 3 \cdot 2. In general, if we start with the equation ad = cb and divide both sides by b and d, we get \frac {a}{b} = \frac {c}{d} That is, (a,b) \sim _{\mathbb {Q}} (c,d) exactly when \frac {a}{b} = \frac {c}{d} as fractions! So, intuitively, each equivalence class is the set of all the different pairs of numerators and denominators that correspond to a single rational number.
Using this insight we can finally give a completely sound, formal definition of the rational numbers:
Definition Rational number [009I]
- March 6, 2026
A rational number is an equivalence class of the \sim _{\mathbb {Q}} relation on \Z \times (\Z - \{0\}).
Functions [009O]
- March 9, 2026
Definition Function [009P]
- March 9, 2026
Let A and B be sets. (Note: some books say nonempty sets, but there’s no particular reason to make this restriction!) A function f from A to B, written f : A \to B, is a relation from A to B such that every a \in A is related to exactly one b \in B.
A is called the domain of f and B is the codomain. We write f(a) = b to denote the unique element b \in B that is assigned to a.
Remark Visualizing functions [009Q]
- March 9, 2026
Definition Range of a function [009R]
- March 9, 2026
The range of a function f : A \to B is the set \{ b \in B \mid \exists a \in A .\; f(a) = b \}, that is, the subset of B consisting of all those b’s which are actually the image of (at least one) a \in A.
Remark Range vs codomain [009S]
- March 9, 2026
It's quite possible you have never before seen a careful distinction made between the range and the codomain of a function. The codomain is the set of potential outputs, whereas the range is the set of actual outputs.
Why bother making this distinction? There are several reasons. For one thing, it's often useful to be able to talk about the set of potential outputs and which elements in that set are or are not "covered" by a specific function. For another thing, the codomain is part of the definition of a function, but the actual range of a function can be quite complicated to describe, or even impossible to describe fully. We want to be able to define functions in a simple way, without getting bogged down in which specific values are or are not in the range.
Some example functions [009T]
- March 9, 2026
Example [009U]
- March 9, 2026
Let \mathrm {Shp} = \{ \diamond , \triangle , \square \}. Now define a function g : \mathrm {Shp} \to \mathbb {N} by \begin {align*} g(\diamond ) &= 6 \\ g(\triangle ) &= 6 \\ g(\square ) &= 32. \end {align*} This is a valid function since every element of \mathrm {Shp} is assigned to exactly one element of \mathbb {N}. The domain of g is \mathrm {Shp}, the codomain is \mathbb {N}, and the range of g is the set \{6,32\}.
Example [009V]
- March 9, 2026
Let h : \mathbb {N} \to \mathbb {N} be defined by h(n) = 2n. The domain and codomain of h are both \mathbb {N}; the range is the set of all even natural numbers.
Example [009W]
- March 9, 2026
Let \ell : \mathbb {N} \to \mathrm {Shp} be the function which sends all multiples of 3 to \diamond , all numbers which are one more than a multiple of 3 to \triangle , and all which are two more than a multiple of 3 to \square . That is, \begin {align*} \ell (3n) &= \diamond \\ \ell (3n+1) &= \triangle \\ \ell (3n+2) &= \square \end {align*} This is a valid function since every natural number falls into exactly one of these three cases.
Defining functions in Disco [009X]
- March 9, 2026
We have previously seen how to define functions in Disco. Here are a few more examples.
g : Bool -> N g(T) = 5 g(F) = 17 h : N -> N h(n) = 2n l : N -> Bool l(3n) = False l(3n + 1) = True l(3n + 2) = True j : N -> N j(3n) = 2n+5 j(3n+1) = n^2 j(3n+2) = 17
And we can still use the :table command to get Disco to show a table of example input-output values for a function.
One-to-one, onto, and invertible functions [009Y]
- March 10, 2026
Motivation [009Z]
- March 10, 2026
What do we need to be true in order to be able to turn a function around into a valid function that goes the opposite direction?
If we take any old function and reverse the direction of all the arrows, the result might not be a valid function. There are two potential problems:
- Different elements of the domain can map to the same element of the codomain. If we reverse the arrows, that element of the codomain will have multiple arrows coming out of it, which is not allowed.
- Some elements of the codomain might be left out. If we turn the arrows around, these will not be assigned to anything.
We can define some special types of functions that don’t have these problems.
One-to-one (injective) functions [00A0]
- March 10, 2026
Definition One-to-one / injective [00A1]
- March 10, 2026
A function f : A \to B is one-to-one (injective, an injection) iff no two different elements of the domain map to the same element of the codomain. That is, \forall a_1, a_2 \in A .\; a_1 \neq a_2 \rightarrow f(a_1) \neq f(a_2), or equivalently, taking the contrapositive, \forall a_1, a_2 \in A .\; f(a_1) = f(a_2) \rightarrow a_1 = a_2.
Remark Proving that a function is injective [00A2]
- March 10, 2026
If we want to prove that a function is one-to-one / injective, we typically use the first formulation: for arbitrary a_1, a_2 \in A, we must prove f(a_1) = f(a_2) \rightarrow a_1 = a_2, which we can do by supposing f(a_1) = f(a_2) and proving that a_1 = a_2.
Remark Proving that a function is not injective [00A3]
- March 10, 2026
To prove that a function is not one-to-one / injective, we need to prove the negation:
That is, we pick a_1 and a_2 which are not equal, and show that f(a_1) = f(a_2).
Which of our example functions g, h, and \ell are injective? For those that are not, could we make another function with the same domain and codomain which is injective?
Example [00A4]
- March 10, 2026
g is not not injective, since \diamond \neq \square but g(\diamond ) = g(\square ) = 6. We could easily make a different function with the same domain and codomain which is injective. For example, the function which is the same as g on \triangle and \square but sends \diamond to 349.
Example [00A5]
- March 10, 2026
h is injective, which we can prove as follows.
Proof.
- March 10, 2026
Example [00A6]
- March 10, 2026
\ell is very much not injective (for example, every single multiple of 3 maps to \diamond ). Moreover, there is no way to make an injective function with the same domain and codomain.
Checking injectivity with Disco [00A7]
- March 10, 2026
Let's use Disco to check the injectivity of our example functions from before.
Disco can tell us for sure that g is injective:
Disco> :test forall x:Bool, y:Bool. g(x) == g(y) -> x == y
- Certainly true: ∀x, y. g(x) == g(y) -> x == y
No counterexamples exist; all possible values were checked.With h, on the other hand, Disco cannot tell for sure, but reports that it tried a bunch of examples without finding a counterexample. This makes sense: since the domain of h is the natural numbers, there are infinitely many inputs to try, so Disco cannot try them all. (Of course, we can prove that h is injective, but Disco is not doing anything that sophisticated.)
Disco> :test forall x:N, y:N. h(x) == h(y) -> x == y
- Possibly true: ∀x, y. h(x) == h(y) -> x == y
Checked 100 possibilities without finding a counterexample.The function l is not injective, and Disco finds a counterexample immediately:
Disco> :test forall x:N, y:N. l(x) == l(y) -> x == y
- Certainly false: ∀x, y. l(x) == l(y) -> x == y
Found counterexample:
x = 0
y = 3
- Certainly true: l(x) == l(y)
- Left side: F
- Right side: F
- Certainly false: x == y
- Left side: 0
- Right side: 3Disco also easily finds a counterexample showing that j is not injective (try it yourself!).
As one last example, we can try making a function which is "almost" injective, which fools Disco:
k : N -> N
k(193) = 0
k(n) = 2n
Disco> :test forall x:N, y:N. k(x) == k(y) -> x == y
- Possibly true: ∀x, y. k(x) == k(y) -> x == y
Checked 100 possibilities without finding a counterexample.We can easily see that k is not injective, since k(0) = k(193) = 0, but Disco happened not to try those particular inputs. When Disco says that something is "possibly" true or false, we must take it with a grain of salt!
Onto (surjective) functions [00A8]
- March 10, 2026
Definition Onto / surjective [00A9]
- March 10, 2026
Remark Proving that a function is surjective [00AA]
- March 10, 2026
If we want to prove that a function is onto / surjective, we let b be an arbitrary element of the codomain, and show how to find a specific element a of the domain such that f(a) = b.
Remark Proving that a function is not surjective [00AB]
- March 10, 2026
To show something is not onto / surjective, we just have to show the negation:
That is, “there is some b \in B which is not the output of f for any a \in A”.
Which of our example functions g, h, and \ell are surjective? For those that are not, could we make another function with the same domain and codomain which is surjective?
Example [00AC]
- March 10, 2026
g is not onto. There are many, many elements of the domain (that is, natural numbers) which are not the output of g for any input. There is no function with the same domain and codomain we could make that would be onto—the domain is too small to cover everything in the codomain!
Example [00AD]
- March 10, 2026
h : \mathbb {N} \to \mathbb {N}; h(n) = 2n is not onto either. The range of h is only the even natural numbers; all the odd natural numbers are not covered. However, we could make an onto function from \mathbb {N} to \mathbb {N}, for example, the function f(n) = n.
Example [00AE]
- March 10, 2026
\ell is onto: every shape is the output of the function for some (actually, many) inputs.
Invertible (bijective) functions [00AF]
- March 10, 2026
Definition Invertible / bijective [00AG]
- March 10, 2026
A function f : A \to B which is both one-to-one and onto (injective and surjective) is called invertible (bijective, a bijection). We write f^{-1} : B \to A for the inverse of f, that is, the function defined by f^{-1}(b) = a if and only if f(a) = b.
Remark [00AH]
- March 10, 2026
Careful: there is an unfortunate notation overlap here. Usually x^{-1} means 1/x. But f^{-1} is not the same as 1/f! It just denotes the inverse function of f.
Remark Proving a function is a bijection [00AI]
- March 10, 2026
Normally, to prove a function is a bijection, we can show it is both 1-1 and onto. Alternatively, it turns out we can show it has an inverse. However, we must be careful: to prove g is the inverse of f, we have to prove both g(f(a)) = a for all a in the domain of f, and f(g(b)) = b for all b in the codomain. (Exercise: prove that if there is a function g such that g(f(a)) = a for all a in the domain of f, then f is injective; likewise, if there is g such that f(g(b)) = b for all b in the codomain, then f is surjective.)
Example [00AJ]
- March 10, 2026
Let f : \Z \to \Z be defined by f(x) = x+1. Is f invertible?
- Check: is it one-to-one? Yes; for any x, y \in \Z , if x+1 = y+1 then x = y.
- Onto? Yes, for any y \in \Z , y-1 \in \Z and f(y-1) = y.
Example [00AK]
- March 10, 2026
Is f : \mathbb {N} \to \mathbb {N} defined by f(n) = \lfloor n/2 \rfloor invertible? No, it is not one-to-one, since, for example, f(3) = f(2).
Example [00AL]
- March 10, 2026
Is f : \Z \to \Z defined by f(x) = 3x+2 invertible? No, although it is indeed one-to-one, it is not onto. For example, there is no x \in \Z such that f(x) = 6; the output of f will never be a multiple of 3.
Example [00AM]
- March 10, 2026
What about f : \mathbb {Q} \to \mathbb {Q} defined by f(x) = 3x+2? This looks similar to the previous example, but it is a different function! In fact this f is both one-to-one and onto; f^{-1}(s) = \frac {s-2}{3}.
★ Countable and uncountable sets [00AN]
- March 12, 2026
Motivation: matching vs counting [00AO]
- March 12, 2026
You are in a large room with thousands of people. Each person is wearing either a red shirt or a blue shirt. You want to know whether there are more people wearing red shirts, more with blue shirts, or an equal number. You have only five minutes and a microphone. What should you do?
There are many outside-the-box answers to this riddle, but one idea that works well is to tell everyone to pair up: if you have a blue shirt, look for someone with a red shirt, and vice versa. Once you have paired up with an opposite-color shirt, sit down. Pretty soon it should be clear whether the people still standing up have blue shirts or red shirts.
Another similar puzzle is the following: you have a bunch of sheep and want to make sure you still have all of your sheep at the end of the day. However, your language has no words for numbers. What should you do?
One idea is to use a bunch of pebbles. As your sheep leave to pasture at the start of the day, place one pebble in a pile for each sheep. When the sheep return, move one pebble from the pile to a different pile for each sheep.
These examples illustrate that in some sense, matching things up is more fundamental than counting.
Motivation: infinite cardinality is weird [00AP]
- March 12, 2026
Previously we considered the function h : \mathbb {N} \to \mathbb {N}, h(n) = 2n; it is not onto. However, if we define 2\mathbb {N} as the set of all even natural numbers, then h : \mathbb {N} \to 2\mathbb {N} defined in the same way is indeed a bijection. This is a bit strange though, because 2\mathbb {N} \subseteq \mathbb {N}, yet there can be a bijection between them, i.e. their elements can be matched up.
Infinity really messes with our intuition! With finite sets, if A \subseteq B and they are not equal, then |A| < |B| and their elements can’t be matched up. But infinite sets don’t work that way. In fact, we have been using the notation |A| for the cardinality of sets, but we don’t even know what that should mean for infinite sets.
However, matching things up is even more fundamental than counting. It’s probably how people started counting in the first place. Let’s see if this can guide us to some ideas about the cardinality of infinite sets.
Countable sets [00AQ]
- March 12, 2026
Definition Matching set cardinalities [00AR]
- March 12, 2026
Let A, B be sets. If there exists a bijection f : A \to B then we say A and B have the same cardinality, and write |A| = |B|.
If there is an injection f : A \to B then we say |A| \leq |B|.
Remark [00AS]
- March 12, 2026
Note that this definition makes sense for finite sets and the intuition we already have for what the cardinality of a finite set is. If there is a bijection between two finite sets, then they definitely have the same (natural number) size; if there is an injection from A to B, then B definitely has to be at least as big as A.
Definition Countable and countably infinite sets [00AT]
- March 12, 2026
A set A is countable if it has the same cardinality as (i.e. can be put into bijection with) a subset of \mathbb {N}. Note that \mathbb {N} is a subset of itself: in particular, we say A is countably infinite if |A| = |\mathbb {N}|.
Remark Finite sets are countable [00AU]
- March 12, 2026
Clearly every finite set is countable, since we can match up the elements of a finite set with, say, some finite prefix of the natural numbers \{0, 1, 2, 3, \dots , n\}.
\begin {array}{ccccccc} A: & a_0 & a_1 & a_2 & a_3 & \dots & a_n \\ & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow \\ \mathbb {N}: & 0 & 1 & 2 & 3 & \dots & n \end {array}Example Shapes are countable [00AW]
- March 12, 2026
The set of shapes \{\triangle , \square , \diamond \} is countable since we can make a bijection to the set \{0,1,2\}: \begin {array}{ccc} \triangle & \square & \diamond \\ \updownarrow & \updownarrow & \updownarrow \\ 0 & 1 & 2 \end {array}
Remark Countably infinite sets [00AV]
- March 12, 2026
For countably infinite sets, the situation looks the same, but there is no endpoint:
\begin {array}{cccccc} A: & a_0 & a_1 & a_2 & a_3 & \dots \\ & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow \\ \mathbb {N}: & 0 & 1 & 2 & 3 & \dots \end {array}We can think of this scenario in several equivalent ways:
A is countable if it has a bijection to (a subset of) \mathbb {N}. (definition)
A is countable if all its elements can be put in a list. (We can always number the elements of a list 0, 1, 2, \dots .)
A is countable if we can write (or imagine) a program that prints the elements of A one after another, in such a way that any given element of A will eventually be printed after some finite amount of time (if we are willing to wait long enough).
Example Even integers are countable [00AX]
- March 12, 2026
The set 2\mathbb {N} of all even natural numbers is countably infinite, since f(n) = 2n is a bijection between \mathbb {N} and 2\mathbb {N}: \begin {array}{cccccc} \mathbb {N}: & 0 & 1 & 2 & 3 & \dots \\ & \updownarrow & \updownarrow & \updownarrow & \updownarrow \\ 2\mathbb {N}: & 0 & 2 & 4 & 6 & \dots \end {array}
Example The integers are countable [00AY]
- March 12, 2026
Is the set of integers \Z countable? At first glance it might seem like it’s not, since it is infinite in two directions and \mathbb {N} is only infinite in one direction. And indeed, if we try to list all the elements of \Z in a naive way, it fails: “First list 0, then all the positive integers, then all the negative integers” is not a valid list, since it would take infinitely long to list all the positive integers and we would never get around to any of the negative integers.
But \Z is countable, which we can show by reordering the elements in a clever way, for example: \begin {array}{ccccccccc} \mathbb {N}: & 0 & 1 & 2 & 3 & 4 & 5 & 6 & \dots \\ & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \\ \Z : & 0 & 1 & -1 & 2 & -2 & 3 & -3 & \dots \end {array} This will eventually get to any integer we want after some finite amount of time, whether positive or negative. We could figure out an explicit formula for a bijection following this pattern, something like f(n) = \begin {cases} -(n/2) & \text {if $n$ is even} \\ (n+1)/2 & \text {if $n$ is odd} \end {cases} but we don’t necessarily need to; just demonstrating the pattern and arguing why it will reach every integer is enough.
Example \N \times \N is countable [00AZ]
- March 12, 2026
Is \mathbb {N} \times \mathbb {N} countable?
The elements of \mathbb {N} \times \mathbb {N} are ordered pairs of two natural numbers, for example, (3,5). We can imagine them in a 2D grid: \begin {array}{cccc} \vdots & & & ⋰ \\ (2,0) & (2,1) & (2,2) & \\ (1,0) & (1,1) & (1,2) & \\ (0,0) & (0,1) & (0,2) & \dots \end {array}
At first it seems like the answer should be no. Each row of the 2D grid is the same size as a complete copy of \mathbb {N}; and we have infinitely many rows! How could infinitely many copies of \mathbb {N} be the same size as \mathbb {N}?
And indeed, if we try listing the elements by row or column it definitely doesn’t work. “First, list all the elements in row 0; then, list all the elements in row 1; then row 2; and so on”—we will spend an infinitely long time just listing elements of the form (0,n) and never even get around to pairs starting with anything other than 0.
However, surprisingly, \mathbb {N} \times \mathbb {N} is countable! Again, we can show this by listing the pairs in a clever order that guarantees we will reach any specific pair in a finite amount of time. The key idea is to list them by diagonals, that is, we list the pairs (x,y) in order of the sum x+y. First we list all the pairs with sum 0 (there’s only one, (0,0)), then all the pairs with sum 1 (there are two of them, (1,0) and (0,1)), then the pairs with sum 2, and so on. We end up with a list that looks like this: \begin {array}{ccccccccc} \mathbb {N}: & 0 & 1 & 2 & 3 & 4 & 5 & 6 & \dots \\ & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \updownarrow & \\ \mathbb {N} \times \mathbb {N}: & (0,0) & (1,0) & (0,1) & (2,0) & (1,1) & (0,2) & (3,0) & \dots \end {array}
\N \times \N \times \N is countable [00HV]
- July 16, 2026
Once we have this idea, we can see that \mathbb {N} \times \mathbb {N} \times \mathbb {N} must also be countable: we can either explicitly list them by “diagonal slices” through the corner of the infinite 3D cube; or we can just observe that we can do the \mathbb {N} \times \mathbb {N} \to \mathbb {N} bijection twice to get a bijection (\mathbb {N} \times \mathbb {N}) \times \mathbb {N} \to \mathbb {N} \times \mathbb {N} \to \mathbb {N}.
Challenge [00HW]
- July 16, 2026
Work out the details of showing that this is valid. For example, we need to know that if f : A \to B is a bijection then we can use it to make a bijection between A \times C and B \times C; we also need to know that chaining two bijections together will give another bijection.
Likewise, \mathbb {N}^4, \mathbb {N}^5, and any finite Cartesian product of \mathbb {N} with itself is also countable.
Uncountable sets [00HX]
- July 16, 2026
Is everything countable? [00HY]
- July 16, 2026
At this point you might wonder if every infinite set is countable. (But if so, why is there a name for it?) In fact, there are some infinite sets that are not countable!
The rationals are countable! [00HZ]
- July 16, 2026
Let’s think about \mathbb {Q}, the set of rational numbers. There are a lot, right? In between any two integers there are infinitely many rational numbers. If you pick two rational numbers that are really close together, there are infinitely many rational numbers between those. No matter how far you zoom in there will always be infinitely many rational numbers in the part of the number line you are looking at! It is like infinitely fuzzy fuzz. Surely there are more rational numbers than there are natural numbers, right?
Wrong! A rational number is just a pair of integers: a numerator and a denominator. The function f : \mathbb {Q} \to \Z \times \Z that sends the rational number p/q to the pair (p,q) is injective: if two rational numbers are not the same, p/q \neq r/s, then certainly the pairs (p,q) and (r,s) are not the same. So |\mathbb {Q}| \leq |\Z \times \Z | = |\mathbb {N}|! If you weren’t weirded out by the fact that |\mathbb {N} \times \mathbb {N}| = |\mathbb {N}| before, you should be now.
Proving a set uncountable [00I0]
- July 16, 2026
Are there any non-countable sets? How would we prove it if there were? Let’s use our superpower:
\begin {align*} &\phantom {\equiv } \neg \mathrm {Countable}(A) \\ &\equiv \neg \exists f:\mathbb {N} \to A .\; \mathrm {Bijective}(f) \\ &\equiv \neg \exists f:\mathbb {N} \to A .\; \mathrm {Injective}(f) \land \mathrm {Surjective}(f) \\ &\equiv \forall f:\mathbb {N} \to A .\; \neg \mathrm {Injective}(f) \lor \neg \mathrm {Surjective}(f) \end {align*}So to prove that a set A is not countable, we must show that for every possible function f from \mathbb {N} to A, either f is not injective or it is not surjective. Note that a function from \mathbb {N} to A is the same as an infinite list of values from the set A. So we have to show that every possible infinite list of A either has repeated elements, or leaves some elements out.
Our first uncountable set [00I1]
- July 16, 2026
Consider the set I of real numbers between 0 (inclusive) and 1 (exclusive). Each such real number can be written as an infinite decimal, 0.d_1 d_2 d_3 d_4 \dots (Note this even works for things with finite decimal expansions because we can stick an infinite number of zeros on the end, like 1/2 = 0.500000000\dots . There is also a finicky issue with some real numbers having multiple representations as infinite decimals, for example, 0.5000000000\dots = 0.4999999999\dots , but we’re not going to worry about this for now. It’s annoying but not too hard to deal with this.)
Now, suppose we have a list of real numbers. It might start something like this: \begin {align*} r_1 &= 0.d_{11}d_{12}d_{13}d_{14}\dots \\ r_2 &= 0.d_{21}d_{22}d_{23}d_{24}\dots \\ r_3 &= 0.d_{31}d_{32}d_{33}d_{34}\dots \\ r_4 &= 0.d_{41}d_{42}d_{43}d_{44}\dots \\ &\vdots \end {align*} Remember that this list represents a function \mathbb {N} \to I. We claim that no matter what this function is, it is not surjective, that is, there is at least one real number in the interval I that gets left out. We’re going to build a real number 0.c_1 c_2 c_3 c_4 \dots by specifying it digit-by-digit.
For the first digit, c_1, pick any digit which is not equal to d_{11}, the first digit of the first number in the list.
For the second digit, c_2, pick any digit which is not equal to d_{22}, the second digit of the second number in the list.
…and so on: for digit c_i pick any digit which is not equal to d_{ii}, the ith digit of the ith number in the list.
We now claim that the resulting number c = 0.c_1c_2c_3c_4\dots is nowhere on the list! It cannot be equal to r_1, because c and r_1 have different first digits. It cannot be equal to r_2, because they differ in their second digits. It cannot be equal to any r_i, because the ith digit of c was chosen in such a way that it is different than the corresponding digit of r_i.
We assumed nothing about our list of real numbers other than the fact that it was a list; we have therefore shown that any list of real numbers in the interval I must necessarily be incomplete. Therefore, it is not possible to create a bijection between \mathbb {N} and I, so the set of real numbers in the interval I is uncountable. (Of course, this means the entire set of real numbers \mathbb {R} is uncountable too.)
Sequences, recurrences, and sums [00B0]
- March 13, 2026
Sequences [00B1]
- March 13, 2026
Informally, a sequence is just a list of things (typically numbers). For example, 2, 5, 8, 11, 14, \dots . Formally, we can define a sequence as follows.
Definition Sequence [00B2]
- March 13, 2026
A sequence is a function from \mathbb {N} (or a subset of \mathbb {N}) to a set A. We use subscript notation like a_n, b_n to denote the image of n, the nth term of the sequence.
Remark [00B3]
- March 13, 2026
In other words, a sequence can also be thought of as a list where each element has a number: a_0, a_1, a_2, a_3, \dots The subset of \mathbb {N} serves as a set of indices; we often use \mathbb {N} itself, or sometimes \Z ^+ = \{1, 2, 3, \dots \}, or a finite set like \{0, 1, 2, \dots , n-1\}.
Remark [00B4]
- March 13, 2026
You sometimes see the notation \{a_n\} to refer to a sequence, but it’s terrible notation (because it looks like a set, but isn’t) and I won’t use it, even though it is (somewhat) standard.
Example [00B5]
- March 13, 2026
Consider the sequence of rational numbers with a_n = 1/n, starting with a_1. The sequence starts 1, \frac 1 2, \frac 1 3, \frac 1 4, \dots
Example [00B6]
- March 13, 2026
The sequence defined by a_n = 5 begins 5, 5, 5, 5, 5, \dots
Example [00B7]
- March 13, 2026
The string “hello” can be thought of as a finite sequence of letters, with a_0 = h, a_1 = e, a_2 = l, a_3 = l, a_4 = o.
Example Arithmetic sequence [00B8]
- March 13, 2026
Consider the sequence 2, 5, 8, 11, 14, 17, \dots Evidently each term in the sequence is three more than the previous term. This sequence can also be described by a_n = 2 + 3n, starting at a_0.
This type of sequence, in which there is a common difference between consecutive terms, is known as an arithmetic sequence.
Example Geometric sequence [00B9]
- March 13, 2026
Consider the sequence 2, 6, 18, 54, 162, \dots In this case, a_n = 2 \cdot 3^n for n \geq 0. This type of sequence, with a common ratio between consecutive terms, is known as a geometric sequence.
Recurrences [00BA]
- March 13, 2026
When we define a sequence via a formula for a_n in terms of n, we say it is in closed form. Another way to define a sequence is via a recurrence.
Definition Recurrence [00BB]
- March 13, 2026
A recurrence for a sequence is a rule expressing a_n using one or more previous terms of the sequence.
Remark [00BD]
- March 13, 2026
You will sometimes hear the term recurrence relation used in place of recurrence. This phrase is confusing, since a "recurrence relation" is not a relation (it just relates terms of a sequence to previous terms). I will try to consistently use the term recurrence.
Example [00BC]
- March 13, 2026
What sequences are described by the recurrence a_n = 3a_{n-1}? The solutions to this recurrence are all geometric sequences with a common ratio of 3. For example, one such sequence would be 1, 3, 9, 27, 81, \dots Of course, another is the example 2, 6, 18, \dots that we saw previously.
If we specify a value for a_0, say, a_0 = 2, then the sequence is determined completely: from a_0 we can compute a value for a_1; then from a_1 we can compute a_2, and so on.
Example [00BE]
- March 13, 2026
Let a_0, a_1, \dots be a sequence satisfying the recurrence a_n = 2a_{n-1} + 1 with a_0 = 0.
We have a_1 = 2a_0 + 1 = 1; then a_2 = 2 \cdot 1 + 1 = 3; then a_3 = 7; and so on. The sequence starts 0, 1, 3, 7, 15, 31, 63, \dots
Looking at this we notice that every term of the sequence seems to be one less than a power of two, so we conjecture the closed form a_n = 2^n - 1. First, let’s check that this works for a_0, which it does: 2^0 - 1 = 1 - 1 = 0. Next, let’s see if a_n satisfies the recurrence when defined in this way. We can do this by substituting our conjectured closed form for a_n into the recurrence and seeing if the two sides are equal: 2a_{n-1} + 1 = 2(2^{n-1} - 1) + 1 = 2^n - 2 + 1 = 2^n - 1 = a_n. Yay! This is in fact a valid proof that this closed form and recurrence define the same sequence.
Example [00BF]
- March 13, 2026
a_n = n \cdot a_{n-1}, a_0 = 1. These are the factorial numbers, a_n = n!.
Example [00BG]
- March 13, 2026
Consider the recurrence a_n = 2a_{n-1} - a_{n-2}. In general, there are many sequences that satisfy this recurrence. For example, let’s pick a_0 = 3 and a_1 = 13 and see what we get: 3, 13, 23, 33, 43, 53, \dots We could conjecture that a_n = 10n + 3 is a closed form for this sequence, and prove it by substituting it for a_n in the recurrence to see if it checks out.
Some example sequences [00BI]
- March 14, 2026
Can you describe the following sequences either with a closed form or a recurrence?
Example Odd numbers [00BJ]
- March 14, 2026
These are of course the odd numbers. The simplest way to describe them is to notice that each term is two more than the previous term, leading to the recurrence \begin {align*} a_0 &= 1 \\ a_n &= a_{n-1} + 2 \end {align*} Alternatively, we can describe them by the closed form a_n = 2n+1 (if we start at a_0) or a_n = 2n-1 (if we start at a_1).
Triangular numbers [00BK]
- March 14, 2026
These are the triangular numbers, because a_n is the number of dots in an equilateral triangle with n dots on a side.
Notice how the gap between successive terms increases by 1 every time. This satisfies the recurrence \begin {align*} \triangle _0 &= 0 \\ \triangle _n &= \triangle _{n-1} + n \end {align*} There is also a closed form for \triangle _n which we’ll derive shortly.
Fibonacci numbers [00BL]
- March 14, 2026
These are the famous Fibonacci numbers:
Definition Fibonacci numbers [00OZ]
- October 13, 2026
The Fibonacci numbers are the sequence F_n defined by the recurrence \begin {align*} F_0 &= 0 \\ F_1 &= 1 \\ F_n &= F_{n-1} + F_{n-2}. \end {align*}
Remark Fibonacci closed form [00P0]
- October 13, 2026
There is a closed form for F_n although we won’t derive it in this class. In fact, F_n = \frac 1 {\sqrt 5} \left ( \varphi ^n - \hat \varphi ^n \right ) where \varphi and \hat \varphi are respectively the positive and negative solutions of x^2 - x - 1 = 0, that is, (1 \pm \sqrt 5)/2. If you’re interested in learning where this comes from, check out this exercise, or just ask!
Recurrences in Disco [00BO]
- March 18, 2026
Since a sequence is formally defined as a function, we can easily define sequences in Disco.
Example Odd number sequence [00BP]
- March 18, 2026
odds : N -> N odds(n) = 2n + 1
Disco> :table odds 0 1 1 3 2 5 3 7 4 9 5 11 6 13 7 15 8 17 9 19 10 21 11 23 12 25 13 27 14 29 15 31 16 33 17 35 18 37 19 39 20 41 21 43 22 45 23 47 24 49 ...
We can also define sequences via recurrence, simply by defining a Disco function that calls itself.
Example Triangular numbers [00BQ]
- March 18, 2026
tri : N -> N tri(0) = 0 tri(n) = n + tri(n .- 1)
Instead of using :table we can also create a list to see exactly the part of the sequence we want. We can use the each function to apply tri to each index in a list.
Disco> each(tri, [0 .. 10]) [0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
Example Fibonacci numbers [00BR]
- March 18, 2026
fib : N -> N fib(0) = 0 fib(1) = 1 fib(n) = fib(n .- 1) + fib(n .- 2)
Disco> each(fib, [0..20]) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
Summation and \Sigma -notation [00BS]
- March 18, 2026
Motivation [00BT]
- March 18, 2026
Often, what we want to do with a sequence is add the terms of the sequence. This is such a common idea that there is special notation for it, and algebraic rules for manipulating such sums.
Definition \Sigma notation [00BU]
- March 18, 2026
We use the notation \sum _{n=i}^j a_n to denote the sum of the sequence a_i + a_{i+1} + a_{i+2} + \dots + a_j. That is, we add up each a_n with n taking on the values i, i+1, and so on up to j. n is the , i is the and j is the . \Sigma is a capital Greek letter Sigma, and stands for \Sigma um.
Notation [00BV]
- March 18, 2026
I actually prefer the notation \sum _{i \leq n \leq j} a_n because we get to reuse familiar notation and we can apply a lot of our intuition and knowledge about dealing with inequalities to successfully manipulating sums.
Example [00BW]
- March 18, 2026
Write 1+3+5+7+ \dots + 99 using \Sigma -notation.
If we call the first term a_0, this is a sum of terms with closed form a_n = 2n+1, and the last term is a_{49}, so we have \sum _{0 \leq n \leq 49} (2n+1).
Now, how could we actually evaluate this sum? Let's develop some general techniques and then come back to attack it later.
Algebraic rules for sums [00BX]
- March 18, 2026
Sum of a constant [00BY]
- March 18, 2026
First, notice that \sum _{a \leq n \leq b} 1 = 1 + 1 + 1 + \dots + 1 = b-a+1. Why b-a+1 and not b-a, you ask? This is the “fence post problem”: if we number the posts of a fence, how many fence posts are there between post a and post b? If there is one post every meter, then the distance from post a to post b is certainly b-a meters. But if we try to match up a post to each meter-long section of fence, we find that we have one post left over. For example, if we match up each meter-long section of fence with the post on its left, then the post at the very end is not matched with any section. Hence, the number of posts from a to b is one more than the number of sections.
Factoring out constants [00BZ]
- March 18, 2026
\sum _n ka_n = k \sum _n a_n This works as long as k is a constant with respect to n, that is, k does not contain or depend on n at all. k does not have to be a number; it could be anything at all that does not depend on n. This rule is telling us that if we are adding up a bunch of terms, and every term is k times something, we can factor out all the k’s and simply multiply once by k at the end. If we write out what the \Sigma -notation means, this should become clear: ka_0 + ka_1 + ka_2 + \dots = k(a_0 + a_1 + a_2 + \dots )
Notice, the fact that k does not depend on n is what guarantees that all the k’s will all be the same, unlike the a_n’s which may all be different.
Summing a sum [00C0]
- March 18, 2026
\sum _n (a_n + b_n) = \left (\sum _n a_n\right ) + \left (\sum _n b_n\right ) This follows because addition is commutative and associative. Written out, the above law is really just saying: (a_0 + b_0) + (a_1 + b_1) + (a_2 + b_2) + \dots = (a_0 + a_1 + a_2 + \dots ) + (b_0 + b_1 + b_2 + \dots ) i.e. we are just putting the a_n’s and b_n’s in a different order.
Triangular numbers [00C1]
- March 18, 2026
What is the nth triangular number, \triangle _n = \sum _{j = 1}^n j ?
We can use a cute trick to compute it. First, write out the sum twice, but reversed the second time. Then add the two equations. \begin {array}{ccccccccccc} \triangle _n & = & 1 & + & 2 & + & \dots & + & (n-1) & + & n \\ +\triangle _n & = & n & + & (n-1) & + & \dots & + & 2 & + & 1 \\ \hline 2\triangle _n & = & (n+1) & + & (n+1) & + & \dots & + & (n+1) & + & (n+1) \end {array} We get n copies of n+1, so 2\triangle _n = n(n+1), and hence \triangle _n = \frac {n(n+1)}{2}.
Example Sum of an arithmetic sequence [00C2]
- March 18, 2026
We can now compute the result of our previous example.
Compute \sum _{0 \leq n \leq 49} (2n+1). We can now compute as follows:
Example Another arithmetic sequence [00C3]
- March 18, 2026
Evaluate: \sum _{5 \leq n \leq 22} 3n.
Sum of a geometric sequence [00C4]
- March 19, 2026
What about a geometric sequence such as a_n = r^n, that is, 1 + r + r^2 + r^3 + \dots + r^n? We can use a similar kind of trick as the one we used to find a closed form for triangular numbers. This time, we multiply by r and then subtract: \begin {array}{ccccccccccccc} S & = & 1 & + & r & + & r^2 & + & \dots & + & r^n && \\ rS & = & & & r & + & r^2 & + & \dots & + & r^n & + & r^{n+1} \\ \hline S - rS & = & 1 & & & & & & & & & - & r^{n+1} \end {array} Solving for S yields S = \frac {1 - r^{n+1}}{1-r} = \frac {r^{n+1} - 1}{r - 1}.
Example Powers of two [00C5]
- March 19, 2026
Consider the sum 1 + 2 + 2^2 + 2^3 + \dots + 2^n. This is a geometric series with r = 2. Hence the sum is \frac {2^{n+1} - 1}{2 - 1} = 2^{n+1} - 1.
★ Solving recurrences [00C6]
- March 19, 2026
In many situations, it is easy to write down a recurrence relation, but what we really want is a closed form. How can we figure out a closed form for a given recurrence? There are many techniques, but the simplest is to “unfold” the recurrence and look for a pattern.
Example Odd numbers, again [00C7]
- March 19, 2026
Let’s start by looking at a recurrence for the odd numbers: \begin {align*} g_0 &= 1 \\ g_n &= g_{n-1} + 2 & (n \geq 1) \end {align*}
We can start with g_n and keep unfolding it via the recurrence. For example, after unfolding to g_{n-1} + 2, the recurrence tells us that g_{n-1} = g_{n-2} + 2, so we can unfold again, and so on:
\begin {align*} g_n &= g_{n-1} + 2 \\ &= (g_{n-2} + 2) + 2 \\ &= ((g_{n-3} + 2) + 2) + 2 \\ &= \dots \end {align*}
We see a pattern: g_{n-k} is going to be followed by adding k copies of 2. So in general, we have g_n = g_{n-k} + 2k. Since we know the value of g_0, substituting k = n is helpful: g_n = g_{n-n} + 2n = g_0 + 2n = 1 + 2n. So we have derived the closed form g_n = 2n + 1 which is what we knew it should be.
Example [00C8]
- March 19, 2026
As another example, consider the recurrence \begin {align*} p_0 &= 0 \\ p_n &= 2p_{n-1} + 1 \end {align*} We have seen this recurrence before, and found a closed form just by looking at terms of the sequence and noticing a pattern. But we can derive the closed form more directly by unfolding: \begin {align*} p_n &= 2p_{n-1} + 1 \\ &= 2(2p_{n-2} + 1) + 1 \\ &= 2^2 p_{n-2} + 2 + 1 \\ &= 2^2 (2p_{n-3} + 1) + 2 + 1 \\ &= 2^3 p_{n-3} + 2^2 + 2 + 1 \\ &= \dots \\ &= 2^k p_{n-k} + 2^{k-1} + 2^{k-2} + \dots + 2 + 1 \\ &= 2^k p_{n-k} + 2^k - 1 \end {align*} The last step follows because 2^{k-1} + 2^{k-2} + \dots + 1 is the sum of a geometric sequence with ratio r = 2. When k = n, this becomes p_n = 2^n p_{n-n} + 2^n - 1 = 2^n - 1 since p_0 = 0.
Example [00C9]
- March 19, 2026
As another example, consider \begin {align*} q_0 &= 1 \\ q_n &= 3q_{n-1} + 2 \end {align*} The first few terms of this sequence are 1, 5, 17, 53, 161, \dots Unfolding: \begin {align*} q_n &= 3q_{n-1} + 2 \\ &= 3(3q_{n-2} + 2) + 2 \\ &= 3^2 q_{n-2} + 3\cdot 2 + 2 \\ &= 3^2 (3q_{n-3} + 2) + 3\cdot 2 + 2 \\ &= 3^3 q_{n-3} + 3^2 \cdot 2 + 3\cdot 2 + 2 \\ &= \dots \\ &= 3^k q_{n-k} + 3^{k-1} \cdot 2 + 3^{k-2} \cdot 2 + \dots + 2 \\ &= 3^k q_{n-k} + 2 (3^{k-1} + 3^{k-2} + \dots + 1) \\ &= 3^k q_{n-k} + 2 \frac {3^k - 1}{2} \\ &= 3^k q_{n-k} + 3^k - 1 \\ \end {align*} When k = n, we obtain q_n = 3^n q_0 + 3^k - 1 = 2 \cdot 3^n - 1. We can check that this is correct by (1) computing a few terms of the sequence and checking that our closed form generates the same terms, and/or (2) substituting our closed form into the recurrence to make sure it checks out.
Induction [00CA]
- March 26, 2026
Introduction [00CB]
- March 26, 2026
When discussing propositional logic, we talked about proof rules for different types of statements. When proving a forall statement of the form \forall {x}: {D}.\;{P(x)}, the proof strategy we discussed was to let d be an arbitrary element of D and prove P(d). However, for certain domains D there is another way to prove forall statements: induction. In particular, today we will discuss how to prove something for all natural numbers.
Suppose we have a proposition P(n) and we want to prove that P(n) holds for all n \in \mathbb {N}. That is, we want to prove all the propositions in the list: P(0) \quad P(1) \quad P(2) \quad P(3) \quad P(4) \quad P(5) \quad \dots We cannot literally prove each one separately, because there are infinitely many. One way we can think about proving them is to arrange them in a chain, like this: P(0) \to P(1) \to P(2) \to P(3) \to P(4) \to P(5) \to \dots The idea is to start by proving P(0) (the “base case”) and then “prove the arrows” by proving that each proposition implies the next in the chain—that is, for every k \geq 0, P(k) always implies P(k+1) (this is the “inductive step”). We can prove the inductive step by supposing k to be an arbitrary natural number, supposing P(k) is true (this is called the “induction hypothesis”, abbreviated IH), and proving that P(k+1) must therefore be true as well.
Another way to think of this is as an infinite chain of dominos:
Proving the base case corresponds to knocking over the first domino;
The induction step corresponds to showing that each domino, if it falls down, will knock over the next domino in the line.
The principle of (weak) (natural number) induction [00CC]
- March 26, 2026
In formal propositional logic notation, the principle of induction says P(0) \land (\forall {k}: {\mathbb {N}}.\;{P(k)} \rightarrow P(k+1)) \rightarrow (\forall {n}: {\mathbb {N}}.\;{P(n)}).
Remark [00CD]
- March 26, 2026
Note that this involves a \forall inside an \land inside a \to ! Students often find this confusing. Rely on your skill in manipulating formal propositional logic!
Example Sum of first n natural numbers [00CE]
- March 26, 2026
The other day we explored two different proofs of a formula for the nth triangular number, 1 + 2 + 3 + \dots + n = \frac {n(n+1)}{2}, one algebraic proof and one visual/geometric proof. Both are convincing, but rely on our ability as humans to recognize and generalize patterns; in particular, they both requires us to see intuitively that the same pattern would work for any n. Let's now see how to use induction to give a more formal proof.
Proof.
- March 26, 2026
Subproof. Sum of first n natural numbers ›
- March 26, 2026
(If you're not completely comfortable with this "empty sum" business, note that we could instead prove this for only n \geq 1, by starting with P(1) as our base case. You are welcome to check the P(1) case yourself.)
Subproof. Sum of first n natural numbers ›
- March 26, 2026
Subproof. Sum of first n natural numbers ›
- March 26, 2026
Subproof. Sum of first n natural numbers ›
- March 26, 2026
And here is the same proof again, but much more compressed/concisely:
Proof.
- March 26, 2026
Subproof. Sum of first n natural numbers ›
- March 26, 2026
Subproof. Sum of first n natural numbers ›
- March 26, 2026
Example Growth of 2^n and n! [00CJ]
- March 30, 2026
As a second example, consider the predicate P(n) defined as 2^n < n!. If we try to prove this for all n \in \mathbb {N}, we quickly discover that it isn’t: P(0) is not true (2^0 = 1 which is not less than 0! = 1); neither is P(1) (2^1 is not less than 1!), nor P(2), nor P(3). However, all is not lost: P(4) is true (2^4 = 16 < 4! = 24), and we suspect that P(n) is true from then on, that is, for all n \geq 4. We can still use induction to prove this; we just need to use a different base case. Formally, as stated, the principle of induction does not allow this; but it still works in the same way to prove that something is true for all natural numbers n \geq 4 instead of n \geq 0. If we wanted to be completely formal, we could define Q(n) = P(n+4) and then prove Q(n) for all n \geq 0, which amounts to the same thing.
Theorem [00CK]
- March 30, 2026
2^n < n! for all natural numbers n \geq 4.
Proof.
- March 30, 2026
Subproof. Growth of 2^n and n! ›
- March 30, 2026
Subproof. Growth of 2^n and n! ›
- March 30, 2026
Strong induction [00CM]
- April 1, 2026
Sometimes it’s difficult—or even impossible—to directly prove P(k+1) from the assumption of P(k). For example, what if we can’t prove P(4) from P(3), but we can prove it from P(2)? Is that OK? What if we can only prove P(3) by assuming both P(0) and P(1)? Is that OK?
In general, yes, these are OK: as long as we can prove each proposition from assuming only previous propositions, we will eventually be able to prove any of them. This leads to the principle of strong induction. Instead of assuming only P(k) as our induction hypothesis and then proving P(k+1), like this: P(k) \rightarrow P(k+1) we will instaed assume all the previous propositions and use them to prove P(k+1), like this: \big (P(0) \land P(1) \land P(2) \land \dots \land P(k) \big ) \rightarrow P(k+1). Written out, a first take on the principle of strong induction thus looks like this: P(0) \land (\forall k \in \mathbb {N} .\; \big (P(0) \land P(1) \land \dots \land P(k)\big ) \rightarrow P(k+1)) \rightarrow (\forall n \in \mathbb {N} .\; P(n)). Alternatively, we could write it without the \dots like this: P(0) \land (\forall k \in \mathbb {N} .\; [\forall {j}: {\mathbb {N}}.\;(j \leq k) \rightarrow P(j)] \rightarrow P(k+1)) \rightarrow (\forall n \in \mathbb {N}. P(n)).
We can also make this a bit more general, in two ways:
Just like in the example proving 2^n < n!, the base case need not actually be 0.
Sometimes the inductive step might need a certain number of previous cases to “get off the ground”. In this case we need more base cases to start.
★ More general form of strong induction [00CN]
- April 1, 2026
We can actually get rid of the separate P(0) case by writing the principle of strong induction this way: (\forall {k}: {\mathbb {N}}.\; [\forall {j}: {\mathbb {N}}.\;{(j < k) \rightarrow P(j)}] \rightarrow P(k)) \rightarrow (\forall {n}: {\mathbb {N}}.\;{P(n)}) In other words, this says that we must always be able to show P(k) when assuming P(j) is true for all smaller j < k. Notice that when k = 0, the condition \forall {j}: {\mathbb {N}}.\;{(j < k) \to P(j)} is vacuously true: there are no j < 0. Hence P(0) is required to be true without getting to use any induction hypothesis at all. This also deals with cases where we need several base cases to "get off the ground": for example, we are required to be able to prove P(0) \rightarrow P(1); but if knowing P(0) does not help, then tough luck, we just have to prove P(1) on its own—essentially another base case.
Remark Strong vs weak induction [00CO]
- April 1, 2026
Note that despite their names, “strong” induction is not really any “stronger” than normal (“weak”) induction. In fact, they are logically equivalent, in the sense that either one can be used to prove the other. This is unsurprising in the case of strong induction implying weak induction: if you get to use strong induction, when you have your induction hypotheses that says P(0) \land \dots \land P(k), you may always choose to ignore everything except P(k). What is perhaps more surprising is the fact that weak induction implies strong induction.
In the end, then, we can just say “induction” and in some sense it does not really matter whether we are using weak or strong induction. However, it is often helpful to signal to our readers what kind of argument they can expect to see.
★ Weak induction implies strong induction [00CP]
- April 1, 2026
We can actually use weak induction to prove strong induction. The trick is to define Q(n) \equiv \forall {j}: {\mathbb {N}}.\;{(j \leq n) \to P(n)}, that is, informally, Q(n) \equiv P(0) \land P(1) \land \dots \land P(n). Now suppose \forall {k}: {\mathbb {N}}.\;{[\forall {j}: {\mathbb {N}}.\;{(j < k) \rightarrow P(j)}] \rightarrow P(k)}, or in other words, \forall {k}: {\mathbb {N}}.\;{Q(k-1) \rightarrow P(k)}. Then we can show \forall {n}: {\mathbb {N}}.\;{Q(n)} by weak induction: in the base case, Q(0) \equiv P(0) is true. In the inductive step, Q(k) \rightarrow P(k+1) and hence Q(k) \rightarrow (Q(k) \land P(k+1)) which is equivalent to Q(k+1).
Example Growth of Fibonacci numbers [00CQ]
- April 1, 2026
Recall the definition of the Fibonacci numbers:
Definition Fibonacci numbers [00OZ]
- October 13, 2026
The Fibonacci numbers are the sequence F_n defined by the recurrence \begin {align*} F_0 &= 0 \\ F_1 &= 1 \\ F_n &= F_{n-1} + F_{n-2}. \end {align*}
The first few Fibonacci numbers are thus 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, \dots The Fibonacci numbers grow very quickly (for example, the 100th Fibonacci number is already 354224848179261915075). How quickly do they grow? Here’s one result that starts to answer this question:
Theorem Fibonacci number growth [00CR]
- April 1, 2026
F_n \leq 2^{n-1} for all n \in \mathbb {N}.
So, although the Fibonacci numbers seem to grow very quickly, they do not grow quite as fast as powers of two. Again, let's try weak induction and see what goes wrong.
Fibonacci number growth (failed proof attempt) [00CS]
- April 1, 2026
Proof.
- April 1, 2026
Subproof. Fibonacci number growth (failed proof attempt) ›
- April 1, 2026
Subproof. Fibonacci number growth (failed proof attempt) ›
- April 1, 2026
Here is where we get stuck: the induction hypothesis let us conclude something about F_k but it says nothing about F_{k-1}. And actually, it is worse than that: one of the steps above was actually incorrect! The step marked with an asterisk was not allowed: we wrote F_{k+1} as the sum of two previous Fibonacci numbers, but if k=0 there is no such thing! There is only one Fibonacci number before F_1. Since the induction step needs two previous cases, we need to use strong induction, and we also need to start with two base cases so the first induction step has something to get started with.
Fibonacci number growth (real proof) [00CT]
- April 1, 2026
Proof.
- April 1, 2026
Subproof. Fibonacci number growth (real proof) ›
- April 1, 2026
Subproof. Fibonacci number growth (real proof) ›
- April 1, 2026
More induction examples [00CU]
- April 2, 2026
Example Proving a closed form for a recurrence [00CV]
- April 2, 2026
Let a_0 = 0, a_n = 2a_{n-1} + 1. Prove that a_n = 2^n - 1 for all n \geq 0.
Proof.
- April 2, 2026
Subproof. Proving a closed form for a recurrence ›
- April 2, 2026
Subproof. Proving a closed form for a recurrence ›
- April 2, 2026
Example Sum of Fibonacci numbers [00CW]
- April 3, 2026
Prove: \forall {n}: {\mathbb {N}}.\;{\sum _{0 \leq j \leq n} F_j = F_{n+2} - 1}, that is, the sum of the first n Fibonacci numbers is F_{n+2} - 1.
Proof.
- April 3, 2026
Subproof. Sum of Fibonacci numbers ›
- April 3, 2026
Subproof. Sum of Fibonacci numbers ›
- April 3, 2026
Example Binary expansions [00CX]
- April 3, 2026
Prove: every natural number can be written as the sum of distinct powers of two. That is, every natural number n can be written in the form n = 2^a + 2^b + 2^c + \dots where a, b, c, \dots are all different natural numbers. This is called the binary expansion of n, and—as you probably know—is how natural numbers are represented in a modern computer.
(Just for fun, if we wanted to be completely formal about it, we could write the proposition like this: \forall {n}: {\mathbb {N}}.\;{\exists {E}: {\mathcal {P}(\mathbb {N})}.\;{n = \sum _{e \in E} 2^e}} but we need not use quite so formal notation.)Proof.
- April 3, 2026
Subproof. Binary expansions ›
- April 3, 2026
Subproof. Binary expansions ›
- April 3, 2026
Subproof. Binary expansions ›
- April 3, 2026
Subproof. Binary expansions ›
- April 3, 2026
Remark [00CY]
- April 3, 2026
In fact, more is true: the binary expansion for each n is unique in the sense that there is exactly one binary expansion that works for each n. In other words, if n = 2^a + 2^b + 2^c + \dots = 2^x + 2^y + 2^z + \dots , then \{a, b, c, \dots \} = \{x, y, z, \dots \}. (Put yet another way: the function that maps each finite set of natural number exponents \{a,b,c, \dots \} to the sum 2^a + 2^b + 2^c + \dots is injective!)
Computing binary expansions [00D1]
- April 3, 2026
In fact, the induction proof above can be thought of as a recursive algorithm for computing the binary expansion of a given natural number, and we can implement that algorithm as a Disco function!
!!! binary(1) == {0}
!!! binary(2) == {1}
!!! binary(3) == {0,1}
!!! binary(5) == {0,2}
!!! binary(63) == {0 .. 5}
!!! forall n:N. fromBinary(binary(n)) == n
!!! forall es:Set(N). binary(fromBinary(es)) == es
binary : N -> Set(N)
binary(0) = {}
binary(2k) = {e+1 | e in binary(k)}
binary(2k+1) = {0} union {e+1 | e in binary(k)}
fromBinary : Set(N) -> N
fromBinary(es) = reduce(~+~, 0, {2^e | e in es})Example Every third Fibonacci number is even [00CZ]
- April 3, 2026
Prove that F_{3n} is always even.
Proof.
- April 3, 2026
Subproof. Every third Fibonacci number is even ›
- April 3, 2026
Subproof. Every third Fibonacci number is even ›
- April 3, 2026
Remark [00D0]
- April 3, 2026
It turns out that the converse is also true: if F_n is even, then n must be a multiple of three. Showing this is left as a challenge for the reader! (Hint: prove the contrapositive; if n is not a multiple of three then it must be of the form 3k+1 or 3k+2 for some natural number k.)
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.
Combinatorics [00FP]
- April 24, 2026
Introduction to combinatorics [00FQ]
- April 24, 2026
Combinatorics is the area of mathematics about counting, that is, determining the sizes of sets. This sounds like it would be trivial or uninteresting, but it turns out to be deep and fascinating, with many connections to other areas of mathematics.
You may have memorized a few combinatorics formulas in high school (permutations, combinations, …) but we are going to learn some basic underlying principles—if you understand the principles involved, you can easily re-derive the formulas as needed.
The product rule [00FR]
- April 24, 2026
The first principle we will consider is the product rule.
Remark Counting vs choosing [00GP]
- May 25, 2026
Note, instead of just talking about the sizes of sets we will often talk in terms of choosing elements of a set. The number of elements in a set is the same as the number of ways to choose an element of the set, so in some sense this makes no difference; but for some reason it is often helpful to think in terms of actively choosing elements rather than just passively counting them.
Principle The Product Rule [00FS]
- April 24, 2026
Suppose choosing an item can be decomposed into two independent choices, with c_1 ways to make the first choice, and c_2 ways to make the second choice. Then the total number of ways to choose overall is the product c_1 \times c_2.
Definition Independence [00FT]
- April 24, 2026
Here, independent means that you have the same set of options for both choices no matter what you pick. In other words, choosing a certain option for one of the choices cannot affect which options are available for the other choice.
Remark [00FU]
- April 24, 2026
We can visualize the situation by drawing a rectangular grid, where each row corresponds to one option for the first choice, and each column corresponds to an option for the second choice. Then each grid square corresponds to a unique combination of choices. The fact that the choices are independent corresponds to the fact that the picture is perfectly rectangular.
This really boils down to the fact, which we saw before when learning about sets, that |A \times B| = |A| \times |B|.
Example Counting breakfasts [00FV]
- April 24, 2026
There are three kinds of cereal and seven kinds of fruit. For breakfast, you must choose one cereal and one fruit. How many breakfast choices do you have?
The product rule clearly applies here: the answer is 3 \times 7 = 21 choices.
Example Counting breakfasts (non-example) [00FW]
- April 24, 2026
This is a non-example. Suppose we have the same scenario as the previous problem, except that when you choose oatmeal, you are limited to having only strawberries or blueberries.
In this case the product rule does not apply (at least not directly) since the choices are no longer independent.
Example Counting strings [00FX]
- April 24, 2026
How many 3-letter strings are there using the letters A–Z? Examples of such strings include AAA, HDX, CAR, and so on.
The number of two-letter strings is 26 \times 26 since there are two independent choices: which letter to choose as the first letter, and which to choose as the second. Then the number of 3-letter strings is the number of ways to pick a two-letter string, times the number of ways to pick a third letter, for a total of (26 \times 26) \times 26 = 26^3 = 17576.
Similarly, there are 26^4 four-letter strings, 26^5 five-letter strings, and in general there are 26^k k-letter strings. This reasoning shows that we can generalize the product rule to a product of n choices instead of just two. If we have n independent choices to make, we just multiply all of them to compute the total number of choices.
Example Counting subsets [00FZ]
- April 27, 2026
How many subsets are there of an n-element set?
In other words, what is the cardinality of a power set? We already know the answer to this question, but let's see how the product rule applies. When choosing a subset, for each of the n elements we have two choices: whether to include it in the subset or not. All these choices are independent, because each element is allowed to be in the subset or not, regardless of which other elements are or are not members of the subset. Hence, by the product rule, the total number of choices is 2 \times 2 \times \dots \times 2 = 2^n.
If we identify each subset with a bit string of length n indicating which elements are in the subset and which are out, we can see a connection to the previous example: there are 2^n bit strings of length n, since we can independently choose each bit to be either 0 or 1.
Example Counting soup schedules [00G0]
- April 27, 2026
Suppose we have 10 cans of soup (all different) and we want to eat them all, one per day over the next 10 days. In how many ways could we do this? Note this is equivalent to asking how many ways we could order the cans of soup; imagine we first put them in a particular order and then each day just eat the next can in the line.
We might at first think the answer is 10^{10}, since there are 10 days and 10 choices of soup on each day. However, the choices are not independent: once I choose a particular can of soup I can’t choose it again on a subsequent day. (10^{10} is the answer to the related problem of how many soup-eating schedules we can make, if each day we go to the store to buy some soup, and the store carries 10 different kinds. Assuming the store has enough stock, the choices are now independent, since getting a particular soup on one day does not prevent us from picking the same soup again another day.)
However, we can conceive of this problem in terms of independent choices if we change our point of view a little bit. On the first day, I have 10 choices. On the second day, I can no longer choose the soup I ate on the first day, but I can freely choose any soup I want out of the 9 that are left. On the third day, I can choose any of the 8 that remain, and so on. In total, then, I have 10 \cdot 9 \cdot 8 \cdot \dots \cdot 1 = 10! = 3628800 ways to do this.
More generally, there are n(n-1)(n-2) \cdots 1 = n! ways to put n things in a particular order, that is, there are n! permutations of n things.
Example Counting truncated soup schedules [00G1]
- April 27, 2026
What if I have 10 different cans of soup, as in the previous scenario, but now I only want to eat soup for the next four days?
As before, I have 10 choices on the first day, 9 choices on the second day, and so on, for a total of 10 \cdot 9 \cdot 8 \cdot 7 = 5040 ways. Note this can also be written as 10 \cdot 9 \cdot 8 \cdot 7 = \frac {10 \cdot 9 \cdot 8 \cdot 7 \cdot 6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}{6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1} = \frac {10!}{6!}. In general, if I have n things and want to pick out k of them in a particular order, I have n \cdot (n-1) \cdot \dots \cdot (n-k+1) = \frac {n!}{(n-k)!} ways to do it.
Example Counting functions [00G2]
- April 27, 2026
Given finite sets A and B, how many functions f : A \to B are there?
Consider how we might go about choosing a particular function f : A \to B. A function from A to B must specify a particular element of B for each element of A. So for the first element a_1 \in A, we can choose any element b \in B to which f should send a_1. Likewise, for a_2 \in A, we can independently choose any element of B to which f should send a_2, and so on for every element of A. (These choices are all independent because for any old function, we don’t care if it sends multiple a_i to the same b, or misses some b’s, or whatever.) So for each element of a, we get to make an independent choice of size |B|; hence, by the product rule, the total number of ways we could choose a function is \underbrace {|B| \times |B| \times \dots \times |B|}_{|A|} = |B|^{|A|}.
Bonus: can you count the number of one-to-one functions f : A \to B? (Counting onto functions is much harder!)
The addition rule [00G3]
- April 28, 2026
The next principle we will consider I like to call the addition rule.
Principle The Addition Rule [00G4]
- April 28, 2026
Suppose choosing something means either making one choice in c_1 ways or making another choice in c_2 ways, and the choices do not overlap at all. Then the total number of choices is c_1 + c_2.
In other words, when A and B are disjoint, |A \cup B| = |A| + |B|.
Example Counting lunches [00G5]
- April 28, 2026
You want to order a single item for lunch. Restaurant A has 13 things on its menu, and Restaurant B has 24 on its menu. How many different choices do you have for lunch, if the two restuarants have completely different menus?
Assuming the restaurant menus do not overlap at all, the answer is simply 13 + 24 = 37.
(What if the menus do overlap? We will come back to this question.)
Example Counting soup schedules of varying length [00G7]
- April 29, 2026
If we have ten different soups, and want to make a schedule of which to eat for at most four days, how many ways can we do it?
In this case we can simply note that we can either choose to eat soup for one day, or for two days, or three, or four. These choices do not overlap, since schedules with different numbers of days are obviously different. Hence, the total is 10 + 10 \cdot 9 + 10 \cdot 9 \cdot 8 + 10 \cdot 9 \cdot 8 \cdot 7.
There is another way we could think about this. On the first day, we have ten choices of what soup to pick. For the remaining three days, we can either choose to stop eating soup (in exactly one way), or we can choose from among the nine remaining soups, then decide on a schedule for the last two days; and so on. So the total is 10 \cdot (1 + 9 \cdot (1 + 8 \cdot (1 + 7))).
Counting binary trees [00G6]
- April 29, 2026
Counting the number of binary trees with a certain number of branch nodes provides a great example putting together many things we have seen so far (the product and addition rules, and recursively defined sets and functions).
Here are all the binary trees with zero, one, two, and three branch nodes:
We can see that there are five different binary trees with three branch nodes. How many will there be with four branch nodes? We are quickly approaching the limit of what we will be able to count while being confident that we have not missed any.
Consider the set of five trees with three branch nodes shown above. Of course, every tree has one branch node at its root; then we have two more branch nodes that we have to put somewhere. We can break the trees into three subsets, depending on how many branch nodes we put to the left and right of the root:
The trees on the left have two branch nodes on the left side of the root and none on the right; the tree in the middle has one branch node on either side; and the trees on the right have none on the left and two on the right. It’s clear that these are the only possibilities, and every tree has to fall into exactly one of these categories. If we put two branch nodes on the left, we have two ways to do it (since there are two different trees with two branch nodes), and only one way to put zero on the right (there is only one tree with zero branch nodes). Similarly, we have one way to make a tree with one branch node to either side, and two ways to put two on the right. In general, we can compute the number of ways we have to make a tree with j nodes on the left and k nodes on the right using the product rule: since the left and right trees can be chosen independently, it is just the number of trees with j branch nodes times the number of trees with k branch nodes.
Put together, this tells us that if T(n) represents the number of binary trees with n branch nodes, then T(3) = T(2)T(0) + T(1)T(1) + T(0)T(2). And sure enough, T(0) = T(1) = 1 and T(2) = 2, so this equals 2 \cdot 1 + 1 \cdot 1 + 1 \cdot 2 = 5, as it should.
Using the same pattern we can compute the number of binary trees with four branch nodes: T(4) = T(3) T(0) + T(2) T(1) + T(1) T(2) + T(0) T(3) = 5 + 2 + 2 + 5 = 14.
In general, we can define T(n) recursively as \begin {align*} T(0) &= 1 \\ T(n) &= \sum _{k=0}^{n-1} T(n-k-1)T(k) \end {align*} The resulting sequence, known as the Catalan numbers, is extremely famous: 1, 1, 2, 5, 14, 42, 132, 429, 1430, \dots
The subtraction rule [00G8]
- April 29, 2026
The next principle to consider, I like to call the subtraction rule. In a sense, though, it is just a more sophisticated version of the addition rule.
Principle The Subtraction Rule [00G9]
- April 29, 2026
If choosing something means making one choice in c_1 ways OR another in c_2 ways, but the choices overlap, the total number of choices is c_1 + c_2 - (\text {the number of choices in common}).
Another way to write this, which we have seen before, is |A \cup B| = |A| + |B| - |A \cap B|.
Example Counting lunches, again [00GA]
- April 29, 2026
You again want to order a single item for lunch; restaurant A has 13 things on its menu, and Restaurant B has 24 on its menu. However, there are 9 items which the two restaurants have in common. How many items can you order for your lunch?
Adding 13 + 24 gives the total number of items on both menus, but it has counted the shared items twice. Subtracting the number of shared items gives the correct total: 13 + 24 - 9 = 28.
Example Counting bit strings [00GB]
- April 29, 2026
How many strings of 8 bits either start with a 1 or end with 00?
There are 2^7 strings of 8 bits that start with a 1: the first bit is fixed, and we can independently choose each remaining bit to be 0 or 1.
Similarly, there are 2^6 strings of 8 bits that end with 00.
We cannot simply add these numbers, because of overlap: there are some strings of bits that both start with 1 and end with 00. In fact there are 2^5 such strings, since three bits are fixed and we can freely choose the other 5. Thus, the total number of bit strings that start with 1 or end with 00 is 2^7 + 2^6 - 2^5 = 160.
Remark ★ Principle of inclusion-exclusion [00GC]
- April 29, 2026
What if we have three (or more) overlapping sets of choices? It is actually possible to come up with a general formula for this situation, known as the Principle of Inclusion-Exclusion.
For 3 sets of choices, the formula looks like this: |A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C| That is, to count the size of the union A \cup B \cup C, we can add their sizes; but this counts the overlapping regions more than once, so we can subtract each overlap. However, the overlap between all three sets got counted three times, then subtracted three times, so we have to finally add it back once.
In general, the cardinality of an arbitrary union of sets is equal to the sum of all the individual cardinalities, minus the cardinalities of all intersections of exactly two of the sets, plus the cardinalities of all intersections of exactly three sets, minus the intersections of four sets, ... and so on. Proving this is outside the scope of this class, but it's actually not a difficult proof—it requires use of some set operation properties such as associativity of union, distributivity of intersection over union, as well as induction on the number of sets.
The division rule and binomial coefficients [00GD]
- April 29, 2026
Example Counting soup schedules, again [00GF]
- April 29, 2026
Recall the problem with soup, where we have a bunch of different kinds of soup and are trying to count the number of different schedules we could make for ourselves. For example, if we have 10 different kinds of soup and want to eat soup for 4 days, there are 10 \cdot 9 \cdot 8 \cdot 7 different soup schedules we can make.
Now consider a slightly different question: in how many ways can we choose which four soups to eat, if we don’t care about the order? In this case 10 \cdot 9 \cdot 8 \cdot 7 is clearly too big, since there are many different schedules which have the same four soups in different orders.
In fact, for any set of four soups, we know there will always be exactly 4! = 24 schedules containing those four soups in some order. So 10 \cdot 9 \cdot 8 \cdot 7 is too big by exactly a factor of 24. Hence the number of different sets of four soups we can choose is \frac {10 \cdot 9 \cdot 8 \cdot 7}{24} = 210.
Principle The Division Rule [00GE]
- April 29, 2026
Suppose we have n total choices, but we really want to think of some of them as being equivalent. If the choices always come in groups of exactly k choices which are all equivalent (i.e. each equivalence class is of size exactly k), then the number of “really different” choices (the number of equivalence classes) is n/k.
Example Counting seating arrangements [00GG]
- April 29, 2026
How many ways are there to seat 5 people around a table, if we only care about who is sitting to the left and right of who, and not about the specific seats they are in?
One way to answer this question is by first considering that there are 5! ways to assign each person to a specific seat. However, given a specific assignment of each person to a seat, if we have everyone stand up and move one seat to the right, we get a different seat assignment, but as a seating arrangement it should be considered the same, since we only care who is sitting next to who. In fact, for every specific seating arrangement there are four other arrangements which are equivalent, for a total of five equivalent arrangements (corresponding to the five possible rotations around the table). Thus, by the division rule, there are 5! / 5 = 24 different seating arrangements.
Note we can also see this by first insisting that person A must always sit in a specific chair; since specific chairs don’t matter, they might as well. Then we have four choices for who to seat the right of person A, then three choices for who to seat to the right of them, and so on, for a total of 4! choices.
Example Counting seating arrangements again [00GI]
What if we again want to seat 5 people around a table, but this time we only care who is sitting next to who, but left vs right doesn't matter?
As we know from the previous example, there are 24 ways to seat people if we consider seating arrangements equivalent that are rotations of one another. Now, for any seating arrangement we also consider it equivalent to its mirror image. Each arrangement always has exactly one mirror image, so we just need to divide by 2: the number of different arrangements is 12.
Definition Binomial coefficients [00GH]
- April 29, 2026
Let's return to our example with soup to generalize it. In that case, we were concerned with picking a subset of 4 soups out of a total of 10. More generally, if we want to choose k things out of n—that is, we want to count the number of size-k subsets of a set of size n—we first of all have n \cdot (n-1) \cdot \dots \cdot (n-k+1) = \frac {n!}{(n-k)!} ways to choose k things in a specific order. But rearranging the k things into a different order is not supposed to make any difference, so for each different subset of k things there are k! orderings which should all be considered equivalent. Thus, by the division rule, there are \frac {n!}{k!(n-k)!} size-k subsets of a set of size n. This is such a common idea that it has a special notation: \binom {n}{k} = \frac {n!}{k!(n-k)!} is called a binomial coefficient, and we pronounce it “n choose k”. There is a special notation for them because they show up all over the place in mathematics (not just in combinatorics).
Example Counting bit strings with exactly 3 ones [00GJ]
- April 29, 2026
How many 8-bit binary strings are there with exactly three 1’s and five 0’s?
This is equivalent to picking three out of the eight bit positions to set to 1, so it is \binom {8}{3} = \frac {8!}{3! 5!} = \frac {8 \cdot 7 \cdot 6}{3 \cdot 2 \cdot 1} = 56.
Alternatively, we could pick five out of the eight bit positions to set to 0: \binom {8}{5} = \frac {8!}{5! 3!} = 56 as well.
Example Counting bit strings with at least 3 ones [00GK]
- April 29, 2026
How many 8-bit binary strings have at least 3 bits set to 1?
It is easiest to compute this by subtracting the strings we don’t want to count from the total: 2^8 - \binom 8 2 - \binom 8 1 - \binom 8 0 = 256 - 28 - 8 - 1 = 219.
Alternatively, we could just add up all the possibilities: \binom {8}{3} + \binom {8}{4} + \binom {8}{5} + \binom {8}{6} + \binom {8}{7} + \binom {8}{8} = 56 + 70 + 56 + 28 + 8 + 1 = 219.
Binomial coefficient properties [00GL]
Here is a table of the first few binomial coefficients, with n along the left side, and k along the top.
\begin {array}{c|cccccc} & 0 & 1 & 2 & 3 & 4 & 5 \\ \hline 0 & 1 & & & & & \\ 1 & 1 & 1 & & & & \\ 2 & 1 & 2 & 1 & & & \\ 3 & 1 & 3 & 3 & 1 & & \\ 4 & 1 & 4 & 6 & 4 & 1 & \\ 5 & 1 & 5 & 10& 10& 5 & 1 \\ \end {array}
(The blank entries are 0, since they represent clearly impossible things like “the number of ways to pick 4 things out of a set of size 2”.) This is called Pascal's triangle and is rather well-known (you may very well have seen it before). Some properties of binomial coefficients:
- \displaystyle \binom {n}{0} = \binom {n}{n} = 1. We can prove this either by looking at the formula (\frac {n!}{0!n!} = 1) or by thinking about what it means combinatorially: there is exactly one way to choose nothing, or everything, from a set of size n.
- \displaystyle \binom {n}{1} = n. Again, we can see this from the formula, or by reasoning that if we want to pick exactly one thing out of a set of size n, we have n ways to do it, one for each element.
- \displaystyle \binom {n}{k} = \binom {n}{n-k} (each row of Pascal's triangle is a palindrome). In the formula, k! and (n-k)! just switch places; combinatorially, we have the same number of ways to choose k things as we do to choose which n-k things we are not going to choose. In other words, instead of thinking in terms of “choosing k things” we can think of it in terms of “splitting a set of size n into two subsets of sizes k and n-k”.
- \displaystyle \binom {n}{k} = \binom {n-1}{k} + \binom {n-1}{k-1} (each entry in Pascal's triangle is the sum of the entry above it and the entry above and to the left). This is the rule for constructing Pascal’s Triangle: each entry is the sum of the entry above it and the one above and to the left. I leave it as a challenge for you to prove this either from the formula or by thinking about it combinatorially.
- \displaystyle \sum _{k=0}^n \binom {n}{k} = 2^n (the sum of the nth row of Pascal's triangle is 2^n). These are just two different ways to count all the subsets of a set of size n.