Practice Discrete Math

Algorithms / Primes Sieves Factorization

Least You Need to Know: Primes, Sieves, Trial Division, and Factorization Bounds

Prime-heavy interview questions usually boil down to one of three facts: every composite has a small factor, every integer has a unique prime factorization, and sieve-style preprocessing trades memory for many fast primality or factor queries.

The least you need to know

Key notation

prime integer greater than `1` with exactly two positive divisors
spf(n) smallest prime factor of `n`
sqrt(n) threshold for trial-division checks

Tiny worked example

  • To test whether `91` is prime, try prime divisors up to `sqrt(91)`.
  • Since `sqrt(91)` is less than `10`, checking `2, 3, 5, 7` is enough.
  • Finding `7 * 13 = 91` shows the number is composite without any larger checks.

Common mistakes

How to recognize this kind of problem

Start practice