Number Theory & Combinatorics
Use gcd, modular arithmetic, primes and counting formulas to solve integer problems exactly and keep large answers within a modulus.
4 objectives 5 steps about 110 min
Check what you know
The lessons are coming later. The knowledge check is ready now.
This skill page is a public outline. Its lessons and knowledge check appear here as subject-matter experts approve them.
Learning objectives
- Compute gcd and lcm with Euclid's algorithm, find Bezout coefficients with the extended algorithm, and explain why Euclid takes O(log min(a, b)) steps.
- Do arithmetic modulo a prime: reduce intermediate results, compute powers by binary exponentiation, and find modular inverses with Fermat's little theorem or the extended Euclidean algorithm.
- List primes up to n with the sieve of Eratosthenes, factorize many numbers with a smallest-prime-factor sieve or one number by trial division up to its square root, and choose by input limits.
- Count arrangements with factorials, binomial coefficients and inclusion-exclusion, computing them modulo a prime when they are large and checking small cases against brute-force enumeration.
Lesson plan
5 steps, about 110 min · 0 of 4 lessons available so far
- Euclid's Algorithm and Its Extension Worked example · 20 min · coming later
- Arithmetic That Wraps: Working Modulo a Prime Worked example · 25 min · coming later
- Lab: Sieves and Factorization at Scale Lab · 25 min · coming later
- Counting Without Listing: Binomials and Inclusion-Exclusion Worked example · 25 min · coming later
- Check: Number Theory and Counting Problems Knowledge check · 8 questions
Practise it
Write a numtheory module (gcd, extended gcd, modular inverse, sieve, smallest-prime-factor factorization, nCr mod p from precomputed factorials) and test it with Hypothesis against math.gcd, pow(a, -1, m), math.comb and brute-force counting; then count the integers up to 10^12 divisible by none of a given set of up to 15 primes with inclusion-exclusion, confirming the method on bounds small enough to enumerate, and answer 10^5 binomial queries modulo 10^9 + 7.
References
Hard prerequisites
Unlocks
Nothing depends on this skill yet.
On these roadmaps
Progress and evidence for this skill are shared across all of them.