🐐
LeetGoat
Interview Prep
Competitive Programming
Discord
Prime templates
Range queries
2D range queries
Sets
Union find
Strings
Tree queries
Tree helpers
Graphs
Matrices
Math
Primes
Bit ops & Bitset
Geometry
Misc.
Golden References
Primes
Primes: testing, factorizing, listing, counting
Open code →
One big number: no setup, any x < 2^64
isPrime(x) (deterministic Miller-Rabin)
-> True if x is prime
e.g. isPrime(10**18 + 9) -> True, isPrime(561) -> False
exact, not probabilistic: a fixed set of witnesses that is proven to work for every x < 2^64
O(log x)
primeFactorize(x) (Pollard rho)
-> [(prime, exponent), ...] sorted by prime
e.g. primeFactorize(360) -> [(2, 3), (3, 2), (5, 1)]; primeFactorize(1) -> []
randomized: about x^(1/4) expected, fine for any 64-bit x. The largest prime factor is the last pair
O(x^¼)
allFactors(x)
-> every positive divisor of x, UNSORTED
e.g. allFactors(12) -> [1, 2, 4, 3, 6, 12]
factorizes with primeFactorize, then builds the divisors; D = number of divisors; sort it yourself if you need an order (D log D)
O(x^¼ + D)
Many small numbers: build the sieve once, O(N · log(log N))
sieve.isPrime(x)
-> True if x is prime
e.g. sieve.isPrime(97) -> True
needs x <= N
O(1)
sieve.smallestPrimeFactor(x)
-> the smallest prime dividing x
e.g. sieve.smallestPrimeFactor(91) -> 7
needs 2 <= x <= N
O(1)
sieve.primeFactorize(x)
-> [(prime, exponent), ...] sorted by prime
e.g. sieve.primeFactorize(360) -> [(2, 3), (3, 2), (5, 1)]
needs 1 <= x <= N; the largest prime factor is the last pair
O(log x)
sieve.allFactors(x)
-> every positive divisor of x, UNSORTED
e.g. sieve.allFactors(12) -> [1, 2, 4, 3, 6, 12]
needs 1 <= x <= N; D = number of divisors; sort it yourself if you need an order (D log D)
O(log x + D)
sieve.primeFactorizeUpToNSquared(x)
-> [(prime, exponent), ...] sorted by prime
e.g. with N = 10^6: sieve.primeFactorizeUpToNSquared(999962000357) -> [(999979, 1), (999983, 1)]
needs x <= N^2 (sieve to 10^6, factor up to 10^12): trial division by the sieved primes up to sqrt(x); what's left is 1 or one prime
O(√x / log x)
sieve.primes
-> every prime <= N, increasing
e.g. PrimeSieve(20).primes -> [2, 3, 5, 7, 11, 13, 17, 19]
the same list primesUpTo(N) gives, here for free because the sieve is already built (with its smallest-prime-factor table)
O(1)
Listing primes
primesUpTo(N) (C++: segmented sieve)
-> every prime <= N, increasing
e.g. primesUpTo(20) -> [2, 3, 5, 7, 11, 13, 17, 19]
just the list, without the sieve's smallest-prime-factor table, so it needs far less memory: C++ reaches N = 10^9 in ~1s (its 50,847,534 primes ~200 MB; a PrimeSieve that big would need ~4 GB). Python: N = 10^8 in ~2.5s
O(N · log(log N))
primesInRange(lo, hi) (segmented sieve)
-> every prime p with lo <= p <= hi, increasing
e.g. primesInRange(10**12, 10**12 + 100) -> [1000000000039, 1000000000061, 1000000000063, 1000000000091]
segmented sieve: crosses off multiples of the primes up to sqrt(hi) inside just that window, so hi can be ~10^12 as long as hi - lo is ~10^7 or less
O((hi − lo) · log(log hi) + √hi)
Counting primes without listing them
countPrimes(n) (Lucy_Hedgehog)
-> how many primes are <= n
e.g. countPrimes(100) -> 25; countPrimes(10**9) -> 50847534
O(sqrt n) memory. C++: n = 10^12 in ~0.2s, 10^13 in ~1s; Python: 10^10 in ~1s
O(n^¾)
Prime Templates — LeetGoat