Java implementation of Sieve of Eratosthenes that can go past n 232?
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
A classic Sieve of Eratosthenes implementation usually uses a boolean array indexed by every number up to n. That breaks down once n grows past 2^32, because the problem is no longer just algorithmic complexity; it is also integer overflow and memory pressure.
Why the Naive Sieve Stops Scaling
Two limits matter immediately.
First, Java int cannot represent values above about 2.1 billion, so code using int for loop bounds or array indexing cannot even address numbers near 2^32, which is about 4.29 billion.
Second, a flat sieve array of size n + 1 is too large for most machines. Even if you use one byte per entry, a sieve up to 2^32 would require several gigabytes of memory before accounting for JVM overhead.
So to go past 2^32, you need two structural changes:
- use
longfor numeric ranges and arithmetic - use a segmented sieve so memory stays bounded
The Segmented Sieve Idea
A segmented sieve works in two phases.
First, compute all primes up to sqrt(n) with a normal sieve. That base set is small enough to fit comfortably in memory.
Second, process the range from 2 to n in chunks, such as one million numbers at a time. For each chunk, mark multiples of the base primes, then count or emit the numbers that remain unmarked.
This keeps memory proportional to the segment size instead of proportional to n.
A Runnable Java Example
The code below counts primes up to a long limit using a segmented sieve. It is written to be clear and correct rather than micro-optimized.
The important point is that n is a long, while each segment remains small enough to index with an int array.
Why This Can Go Past 2^32
This design can handle limits larger than 2^32 because it never allocates an array of length n. Only the current segment and the base primes are stored in memory.
That said, "can go past 2^32" does not mean "will be fast enough for arbitrarily huge limits." Runtime still grows with the size of the range, and JVM tuning, CPU cache effects, and disk or output overhead all matter if you print every prime.
For counting primes, segmented sieves are very practical. For storing every prime up to enormous limits, output size itself becomes a serious constraint.
Useful Optimizations
Once the segmented approach is working, common optimizations include:
- skipping even numbers entirely
- using a
BitSetor manual bit packing instead ofboolean[] - parallelizing independent segments carefully
- counting only, instead of storing every prime
Skipping evens roughly halves memory and work. Bit packing can reduce memory further, which helps cache efficiency.
Common Pitfalls
The biggest mistake is using int for arithmetic such as p * p or loop bounds. That overflows long before your intended limit.
Another mistake is trying to allocate one giant array anyway. Even if the code compiles, the JVM is likely to run out of heap.
A third issue is starting the marking loop at the wrong multiple inside a segment. If the first multiple is computed incorrectly, the sieve silently produces wrong results.
Finally, printing every prime for very large limits can dominate runtime. Benchmark counting and output separately.
Summary
- A naive sieve does not scale to
2^32because of both memory usage andintoverflow. - Use
longfor numeric ranges above theintlimit. - A segmented sieve keeps memory bounded by processing fixed-size chunks.
- The base primes only need to be generated up to
sqrt(n). - Further optimizations include skipping evens and bit packing.
- For very large limits, correctness and memory discipline matter more than clever syntax.
Related reading
- java indexofString str method complexity
- Java recursive Fibonacci sequence
- Java Sorting an array based on another array with indexOf method
- javac option to compile all java files under a given directory recursively
- Java in RETURN statements?
- Java Instanceof and Generics
- Javascript algorithm to find elements in array that are not in another array
- Javascript Array.sort implementation?

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.