Discrete Math Intro
Discrete mathematics studies things you can count one by one: true/false values, integers, sets, graphs, strings, programs. Calculus is about smooth change; discrete math is about separate, distinct objects, which is exactly what a computer stores. It is the mathematics of computer science: logic for conditions and circuits, induction for recursion, number theory for cryptography, graphs for networks, counting and probability for algorithms.
The reference book
The topic order of this course follows the open textbook Mathematics for Computer Science by Eric Lehman, F. Thomson Leighton and Albert R. Meyer, the text of MIT's course 6.042J. The book is published under a Creative Commons Attribution-ShareAlike (CC BY-SA) licence, and the course materials are free on MIT OpenCourseWare. Thanks to the authors for making it open.
The book is used as a map of topics only. The explanations, examples, numbers and exercises in these lessons are written from scratch, with a programmer's angle and runnable Python.
What is in the course
| Part | Topics |
|---|---|
| 1. Proofs | propositions, proving implications, cases, contradiction, the well ordering principle |
| 2. Logical formulas | connectives and truth tables, equivalence and normal forms, SAT, predicates and quantifiers |
| 3. Mathematical data types | sets, sequences, functions, relations, cardinality |
| 4. Induction and infinity | ordinary and strong induction, invariants, structural induction, countable and uncountable sets |
| 5. Number theory | divisibility, GCD, primes, modular arithmetic, Fermat and Euler, RSA |
| 6. Directed graphs and partial orders | walks, DAGs and scheduling, partial orders, equivalence relations |
| 7. Simple graphs | degrees, isomorphism, matching, coloring, connectivity, trees, planar graphs |
| 8. Sums and asymptotics | closed forms, bounds, harmonic numbers, Stirling, Big-O |
| 9. Counting | sum and product rules, permutations, binomial coefficients, inclusion-exclusion, pigeonhole |
| 10. Generating functions | sequences as power series |
| 11. Discrete probability | probability spaces, conditional probability, random variables, expectation, deviation |
| 12. Recurrences | Towers of Hanoi, linear recurrences, divide and conquer |
| Discrete math in computing | circuits, program correctness, hashing, dynamic programming |
Why programmers need discrete math
| Where | What discrete math does there |
|---|---|
Every if statement |
Boolean logic: De Morgan's laws turn not (a and b) into not a or not b. |
| Recursion and loops | Induction proves a recursive function correct; a loop invariant proves a loop correct. |
| Cryptography | RSA and HTTPS rest on modular arithmetic and prime numbers. |
| Networks, maps, dependencies | Graphs model routers, roads, social networks and package dependencies. |
| Algorithm analysis | Sums, recurrences and Big-O tell you whether code scales. |
| Hash tables, randomized algorithms | Counting and probability predict collisions and running times. |
A first taste: the number of ways to choose 3 toppings out of 8 is , and Python can count them in two ways, by listing them and by formula.
from itertools import combinations
from math import comb
toppings = ["ham", "olive", "onion", "pepper", "mushroom", "basil", "corn", "tuna"]
listed = list(combinations(toppings, 3))
print("by listing:", len(listed))
print("by formula:", comb(8, 3))
print("first three:", listed[:3])by listing: 56
by formula: 56
first three: [('ham', 'olive', 'onion'), ('ham', 'olive', 'pepper'), ('ham', 'olive', 'mushroom')]
And a taste of logic: checking De Morgan's law for every possible input, which is a complete proof for a statement about two Boolean variables.
from itertools import product
ok = all((not (a and b)) == ((not a) or (not b)) for a, b in product([False, True], repeat=2))
print("not (a and b) == (not a) or (not b) for all inputs:", ok)not (a and b) == (not a) or (not b) for all inputs: True
How lessons work
Every lesson explains the idea in plain words, typesets formulas in math notation (, , , ), proves the key facts, and then computes them. Python is ideal for discrete math because its integers never overflow, fractions.Fraction keeps probabilities exact, and itertools lists permutations, combinations and products for you. SymPy adds number theory helpers. The next lesson, Discrete Math with Python, shows the toolbox.
Tip: A computer check over small cases is evidence, not a proof. This course uses code to explore and to test conjectures, then proves them with logic, induction or counting arguments.
Related courses: Basic Math for primes and modular arithmetic at school level, Probability Theory for the full theory of chance, and Data Structures & Algorithms for the algorithms that use graphs and recurrences.
Learning paths
| Your goal | Suggested route |
|---|---|
| Algorithms and coding interviews | Basic Math (logs) → this course (parts 1, 4, 7–9, 12) → Data Structures & Algorithms |
| University math | Algebra → this course → Linear Algebra → Probability Theory |
| Security | this course (parts 5 and 9) → Number Theory & Cryptography → Cybersecurity |
| AI / ML | this course (parts 3, 9, 11) → Probability Theory → Math for Machine Learning |
Exercises
Exercise 1: Discrete or continuous?
Which of these quantities are discrete? (a) the number of users online, (b) the exact temperature of a room, (c) the moves of a chess game, (d) the shortest number of hops between two routers.
Answer
(a), (c) and (d) are discrete; (b) is continuous.
Solution
A quantity is discrete when its possible values can be listed one by one with gaps between them. User counts and hop counts are whole numbers, and chess moves form a finite list of choices. A temperature can take any real value in an interval, so it is continuous (a computer only stores a rounded, discrete approximation of it).
Exercise 2: Count by hand
How many 2-letter strings can be made from the letters a, b, c if letters may repeat? List them.
Answer
9: aa, ab, ac, ba, bb, bc, ca, cb, cc.
Solution
There are 3 choices for the first letter and, independently, 3 for the second, so strings. This is the product rule, which you will meet in the counting part.
from itertools import product
words = ["".join(p) for p in product("abc", repeat=2)]
print(len(words), words)9 ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
Exercise 3: A check that is not a proof
Someone claims " is prime for every natural number ". Test in Python. What do you conclude?
Answer
It is prime for , but gives , so the claim is false.
Solution
Forty successful checks did not prove anything: one counterexample is enough to disprove a "for every" claim. At , .
from sympy import isprime
bad = [n for n in range(46) if not isprime(n * n + n + 41)]
print("first failures:", bad)
print("40^2 + 40 + 41 =", 40 * 40 + 40 + 41, "=", 41, "*", 41)first failures: [40, 41, 44]
40^2 + 40 + 41 = 1681 = 41 * 41
Test yourself
Which statement best describes discrete mathematics?
- The study of smooth change and limits
- The study of countable, separate objects such as integers, graphs and logical values
- The study of measurement errors
- The study of continuous probability densities only
A program checks a claim about all natural numbers for and finds no counterexample. What has it shown?
- The claim is proved
- The claim is true for those values only
- The claim is false
- Nothing at all
Which part of discrete math is the basis of RSA encryption?
- Graph coloring
- Number theory (primes and modular arithmetic)
- Planar graphs
- Asymptotic notation