All courses › Discrete Mathematics
Discrete Mathematics: free practice, theory and problems
Computers are built on logic: true and false, and and or. Sets are how mathematics collects things. Both show up everywhere in programming, databases and digital circuits.
Contents
1. Logic and sets
What is it about?
Computers are built on logic: true and false, and and or. Sets are how mathematics collects things. Both show up everywhere in programming, databases and digital circuits.
In short
- Propositional logic: (not), (and), (or), (if … then).
- is only false when is true and is false.
- De Morgan's laws:
- Contrapositive: is equivalent to .
- A truth table with variables has rows.
- Sets: union (in A or B), intersection (in both), complement.
- Inclusion–exclusion:
- A set with elements has subsets (the power set).
Propositional logic in practice
A proposition is a statement that is either true or false. With connectives we build compound statements: not (), and (), or (), if–then () and if and only if (). A truth table with rows for variables always gives the answer.
- The implication is false only when is true and false. "If it rains, the ground is wet" is broken only by rain and dry ground.
- The contrapositive is equivalent to , while the converse is not. Many proofs go via the contrapositive.
- De Morgan's laws: and . They are used daily in programming to simplify conditions.
- A tautology is always true, a contradiction always false.
Quantifiers
"For all" () and "there exists" (). Negation swaps the quantifier: . To disprove "all primes are odd" a single counterexample suffices: 2.
Sets
A set is a collection of distinct elements. Key operations are union , intersection , difference and complement . Venn diagrams make them concrete. Inclusion–exclusion: . The power set of a set with elements has subsets – each element is either in or out. The Cartesian product is all ordered pairs, with elements.
Why engineers need this
Logic underlies digital circuits (AND, OR, NOT gates), database queries (WHERE conditions) and program verification. Sets are used in databases (relations are sets of rows), probability (events are sets of outcomes) and algorithms.
How to solve the problems
- Logic: make a truth table, or use De Morgan and the contrapositive.
- Counting with overlap: add and subtract what was counted twice.
- "Neither": total minus the union.
Example
40 students: 25 take maths, 18 take physics and 10 take both. How many take neither?
- .
- Neither: .
Common mistakes
- Thinking is false when is false. A promise is not broken if the condition doesn't happen.
- Forgetting to subtract the intersection and so counting some twice.
- Confusing the converse () with the contrapositive.
Concepts in this part
2. Combinatorics
What is it about?
How many passwords exist? In how many ways can a team be chosen? Combinatorics is systematic counting. It is used in probability, security (how long does it take to guess a password?) and algorithms (how many cases must be checked?).
In short
- Multiplication principle: choices followed by choices give possibilities.
- Ordered selections with repetition: (for example a PIN code).
- Permutations: things in order: .
- Ordered selection without repetition (order matters):
- Combinations (order doesn't matter):
- Handshakes between people: .
Four basic situations
Most combinatorics comes down to two questions: does order matter, and can the same item be chosen more than once?
- Order, with repetition: – PIN codes, passwords, bit strings.
- Order, without repetition: – podium places, order of tasks.
- No order, no repetition: – teams, committees, lottery rows.
- No order, with repetition: – choosing scoops of ice cream from flavours (stars and bars).
The sum and product rules
When choices happen one after another, multiply. When counting separate cases that do not overlap, add. "Passwords with 4 or 5 digits" gives . If the cases overlap, subtract what is counted twice (inclusion–exclusion).
Counting the complement
It is often easier to count what you don't want. "At least one" → all minus "none". The number of bit strings of length 8 with at least one 1 is .
Binomial coefficients and Pascal's triangle
are the numbers in Pascal's triangle, where each number is the sum of the two above: . The binomial theorem explains the name. The symmetry says that choosing who is in is the same as choosing who is out.
The pigeonhole principle
Put letters into holes and at least one hole gets two. Simple but powerful: among 367 people at least two share a birthday, and any hash function mapping many inputs to fewer values must have collisions.
Applications
- Security: a 12-character password from 70 possible characters gives possibilities – length matters more than special characters.
- Algorithms: checking all subsets is and all orderings – this explodes quickly, which is why smarter methods are needed.
- Probability: favourable over possible outcomes when all outcomes are equally likely.
How to solve the problems
- Ask: does order matter? Can the same thing be chosen more than once?
- Order and repetition: . Order without repetition: . No order: .
Example
8 runners compete for gold, silver and bronze. How many possible podiums are there?
- The order matters, and nobody can get two medals.
- .
Common mistakes
- Using combinations when the order matters (or the other way round).
- Forgetting that .
- Adding when the choices should be multiplied.
Concepts in this part
3. Graphs and modular arithmetic
What is it about?
A graph is nodes (points) connected by edges (lines). Road networks, computer networks, friends on social media and dependencies between program modules are all graphs. Modular arithmetic is "clock arithmetic" with remainders, used in hashing, encryption and check digits.
In short
- The degree of a node: the number of edges leaving it. Handshake lemma:
- The complete graph (everyone connected to everyone) has edges.
- A tree is a connected graph without cycles. A tree with nodes has exactly edges.
- Euler circuit (use every edge exactly once and return): exists when the graph is connected and every node has even degree.
- Shortest path in a weighted graph: Dijkstra's algorithm. Always expand from the nearest node you haven't finished.
- Modular arithmetic: is the remainder when is divided by . For example .
Graphs
A graph consists of vertices (nodes) and edges between them. It can be directed (one-way streets, web links) or undirected, and edges can have weights (distance, cost, capacity).
- The degree of a vertex is the number of edges it has. The handshake lemma: the sum of all degrees is the number of edges – so the number of odd-degree vertices is always even.
- A path follows edges without repeating vertices; a cycle returns to the start. A tree is a connected graph without cycles; with vertices it always has edges.
- An Euler path (every edge exactly once) exists if the graph is connected and has 0 or 2 vertices of odd degree – the answer to the Königsberg bridges.
- Graphs are stored as an adjacency matrix () or adjacency lists (best for sparse graphs).
Key algorithms
- Breadth-first search (BFS) finds the shortest path in number of edges; depth-first search (DFS) explores and finds cycles and connected components.
- Dijkstra's algorithm finds shortest paths with positive weights – the basis of route planning and network routing.
- Minimum spanning tree (Kruskal, Prim): the cheapest way to connect all vertices – cables, pipes, roads.
Modular arithmetic
is the remainder when is divided by . We write when and leave the same remainder. Clock arithmetic: 10 hours after 20:00 is .
- You can reduce as you go: and can be computed from the remainders. This makes large powers manageable – fast exponentiation by repeated squaring.
- The greatest common divisor is found with Euclid's algorithm: .
- An inverse of modulo exists if and only if .
Applications
Modular arithmetic is everywhere in computing: hash tables (index = key mod table size), check digits (national ID numbers, ISBN, bank account numbers with mod 11), circular buffers, random numbers and cryptography – RSA relies on it being easy to compute powers modulo a large number but very hard to factor it.
How to solve the problems
- Count edges via the degrees: add up and divide by 2.
- For trees and complete graphs: use the formulas.
- Shortest path: try all reasonable routes in small graphs, or follow Dijkstra.
Example
Edges: A–B (4), A–C (1), C–B (2), B–D (5), C–D (8). Shortest path from A to D?
- A–C–B costs , which is cheaper than A–B directly (4).
- Then B–D: . The alternative A–C–D costs .
- The shortest path is A–C–B–D with length 8.
Common mistakes
- Forgetting to divide the degree sum by 2.
- Thinking the direct edge is always shortest.
- Using negative remainders: in mathematics is always between 0 and .
Concepts in this part
Example problems with solutions
Here are some of the problems in discrete Mathematics. In the app, calculation problems get new numbers every time, so you can practise until it sticks – and take a practice exam with an indicative grade as you prepare for the exam.
Logic and sets: When is the implication false?
Answer: When is true and is false
The implication promises that follows when holds. That promise is only broken when happens and does not.
Combinatorics: You have 3 shirts and 4 pairs of trousers. How many outfits can you make?
Answer: 12
Multiplication principle: .
Graphs and modular arithmetic: A graph has 7 edges. What is the sum of the degrees of all the nodes?
Answer: 14
Each edge adds 2 to the degree sum: .
Logic and sets: , and . What is ?
Answer: 30
.
Matches these university courses
The content follows the syllabus found in, for example:
- TMA4140 (NTNU)