Most elegant way to generate prime numbers
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
When people ask for the most elegant way to generate prime numbers, they usually mean a solution that is both clear and reasonably efficient. For generating all primes up to a limit, the classic answer is the Sieve of Eratosthenes because it is simple to understand and much faster than testing every number independently.
Start with the Sieve of Eratosthenes
The sieve works by assuming every number is prime at first, then crossing out multiples of each prime as you discover it. What remains unmarked at the end are the primes.
This prints:
The elegance comes from the fact that every composite number gets ruled out by one of its smaller prime factors. You do not need to keep checking divisibility from scratch for every candidate.
Why It Beats Repeated Trial Division
A straightforward beginner approach is to test each number by dividing it by every smaller number, or at least by every number up to its square root.
That works, but it repeats too much work. The sieve is better when you want all primes up to n because it marks many composite numbers in one pass rather than rediscovering the same divisibility facts over and over.
For one-off primality checks, trial division can still be fine:
This is readable, but if the task is "generate primes up to a limit," the sieve is usually the better tool.
A Memory-Friendly Generator Approach
Sometimes you do not know the upper bound in advance, or you want a stream of primes rather than a full list. In that case, a generator based on trial division by earlier primes is a nice compromise between elegance and practicality.
This version is attractive when you want the first few primes on demand without allocating a sieve array up to a fixed maximum.
Choosing the Right Definition of Elegant
Elegant does not always mean shortest code. In algorithm discussions, elegant usually means:
- the idea is easy to explain
- the implementation avoids unnecessary work
- the code is still readable when revisited later
By that standard, the Sieve of Eratosthenes is elegant because the core idea matches the implementation directly. You are literally sieving out composites.
If you only need to check one number at a time, the prime-checking function may feel more elegant because it uses less memory and solves the smaller problem directly.
Common Pitfalls
One common mistake is starting to mark multiples at 2 * p instead of p * p. Values below p * p have already been handled by smaller factors, so starting earlier does extra work for no benefit.
Another mistake is forgetting the edge cases for numbers less than 2. Zero and one are not prime, and that should be explicit in the code.
Developers also sometimes choose a clever-looking one-liner that is hard to read and much slower than a standard sieve. Compact code is not automatically elegant if it hides the algorithm.
Finally, use the right approach for the task. If you need all primes up to a million, repeated trial division is the wrong baseline even if the code looks simple.
Summary
- For generating all primes up to a limit, the Sieve of Eratosthenes is the classic elegant solution.
- Trial division is simpler for checking a single number but less efficient for bulk generation.
- A prime generator is useful when you want primes lazily instead of up to a fixed bound.
- Starting the sieve at
p * pavoids unnecessary work. - Elegant prime-generation code should be readable, correct, and appropriate for the problem size.
Related reading
- Move all odd positioned element to left half and even positioned to right half in-place
- Move duplicates to the end of a sorted array
- moving an object from point to point in a linear path
- Multi-Class SVM one versus all
- Multiple parameter optimization with lots of local minima
- Multiple subset sum calculation
- Multi-start and Multi-end shortest path set
- multi-way merge vs 2-way merge

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.