Fastest way to list all primes below N
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
For most practical values of N, the fastest general-purpose way to list all primes below N is a sieve, not repeated primality testing. The exact best sieve depends on scale: an optimized Sieve of Eratosthenes is great for moderate ranges, while a segmented sieve becomes the better practical choice once memory locality matters.
Why Trial Division Is Not the Fastest
Testing every number individually with trial division is simple, but it wastes work. If you test each odd candidate up to its square root, the total cost grows much faster than a sieve.
That is why the interview or practical answer is usually:
- do not test one number at a time
- mark composites in bulk
This is exactly what sieves are good at.
The Standard Answer: Sieve of Eratosthenes
The classic sieve marks multiples of each prime starting from p * p:
This is already very fast and has time complexity about O(n log log n) with O(n) memory.
For many real applications, this is the right answer.
An Important Practical Optimization: Skip Even Numbers
You can cut both work and memory substantially by storing only odd candidates:
This is still conceptually the Sieve of Eratosthenes, just implemented more efficiently.
When N Gets Large: Use a Segmented Sieve
If N is very large, a full O(n) boolean array can become the bottleneck. That is where a segmented sieve helps.
The idea is:
- generate primes up to
sqrt(N) - process the range
[2, N)in manageable blocks - mark composites in each block using the small base primes
This improves cache behavior and reduces peak memory usage. It is usually the practical answer when someone asks for the "fastest" method at larger scales.
The segmented version is more implementation work, but it is the next step after the ordinary sieve, not a completely different idea.
What About the Sieve of Atkin
The Sieve of Atkin has better-looking asymptotic discussion in some contexts, but in practice it is often slower or more complex for everyday use. Unless you are doing specialized prime generation work, the ordinary or segmented Eratosthenes sieve is the better engineering choice.
So the realistic hierarchy is often:
- simple sieve for normal use
- odd-only sieve for better constants
- segmented sieve for huge ranges
That is much more useful than chasing a theoretically fancier algorithm with worse constant factors.
Choose Based on the Goal
Ask what "fastest" means in context:
- fastest to write correctly
- fastest wall-clock time
- fastest under strict memory limits
For a coding interview, an optimized Sieve of Eratosthenes is usually enough. For production prime generation over very large ranges, segmented sieving is the stronger answer.
That distinction matters because the right answer changes with scale.
Common Pitfalls
- Using trial division for every number when the task is to list all primes up to
N. - Forgetting to start crossing off multiples at
p * p. - Storing even numbers even though all even numbers above 2 are composite.
- Assuming the fanciest named sieve is automatically fastest in practice.
- Ignoring memory behavior when
Nbecomes very large.
Summary
- The fastest practical way to list all primes below
Nis usually a sieve, not repeated primality tests. - The Sieve of Eratosthenes is the standard general-purpose answer.
- Odd-only representations improve constants significantly.
- For very large
N, segmented sieves are usually the practical upgrade. - "Fastest" depends on both time and memory, so scale matters.
Related reading
- Fastest way to sample most numbers with minimum difference larger than a value from a Python list
- Fastest way to scan for bit pattern in a stream of bits
- Fastest way to search a number in a list of ranges
- Fastest way to search for an element in unsorted array
- Fill 2D grid with single path
- Fill arbitrary 2D shape with given set of rectangles
- Fastest way to sort 10 numbers? numbers are 32 bit
- Fastest way to sort 32bit signed integer arrays in JavaScript?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.