Fast Algorithm to find number of primes between two numbers
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Finding the number of prime numbers between two given numbers is a classic problem in computer science and mathematics. A brute-force solution might not be efficient, especially when dealing with large numbers. Therefore, utilizing a fast algorithm can prove immensely beneficial. This article explores efficient methods to count primes between two numbers, focusing on optimized algorithms that can significantly reduce computation time.
Prime Number Basics
A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. Key characteristics include:
• Only divisible by 1 and itself. • The first prime numbers are: 2, 3, 5, 7, 11, 13, ...
Common Algorithms
Several algorithms can be used to identify and count prime numbers, ranging from simple to complex:
1. The Sieve of Eratosthenes
One of the oldest and most efficient algorithms for finding all primes up to a specified integer . This method works by iteratively marking the multiples of each prime starting from 2. However, its direct application between two limits needs modifications.
Steps:
- Create a list of integers from 2 to .
- Let equal 2, the first prime number.
- Strike out all multiples of from the list greater than .
- Find the next integer that is not struck out, set to this number, and repeat step 3.
- Repeat steps 3 and 4 until .
Complexity:
2. Segmented Sieve Algorithm
To find primes between two numbers and , the Segmented Sieve is an optimized version of the Sieve of Eratosthenes, allowing us to handle large ranges efficiently.
Process:
- Use the Sieve of Eratosthenes to find all prime numbers up to .
- Create a boolean array for the range .
- Use the list of found primes to mark their multiples in the range.
- Numbers that remain unmarked in the array are prime.
Advantages: Works well with large ranges as it uses memory efficiently.
Complexity:
3. Counting Primes in Ranges - Using Precomputation
For multiple queries asking for the number of primes between different ranges, precomputation combined with prefix sums can prove invaluable.
Method:
- Precompute the number of primes up to each number using the Sieve of Eratosthenes.
- Store these counts in an array, `prime_count`.
- For any range , compute the number of primes as:
Efficiency: Ideal for scenarios with multiple range queries after a single preprocessing step.
Complexity: precomputation, per query.
Practical Example
Let's find the number of primes between 10 and 30 using the Segmented Sieve Algorithm:
- First, find primes up to using the Sieve of Eratosthenes: Primes are [2, 3, 5].
- Create a boolean array for 10 to 30, initialized to `true`.
- For each prime , mark its multiples in [10, 30]. • For , mark 10, 12, 14, ..., 30. • For , mark 12, 15, 18, ..., 30. • For , mark 10, 15, 20, ..., 30.
- Remaining `true` values represent prime numbers: [11, 13, 17, 19, 23, 29].
Summary Table
| Algorithm | Complexity | Use Case |
| Sieve of Eratosthenes | Finding all primes up to a small/moderate | |
| Segmented Sieve | Efficient for large ranges | |
| Precomputation + Prefix | precomputation, per query | Suitable for multiple queries on different ranges |
Final Thoughts
Counting the number of prime numbers between two given numbers is a foundational problem with applicability in various fields, including cryptography and number theory. The efficiency of the selected algorithm can greatly impact performance, especially when working with significant data sets or numerous queries. From the simple Sieve of Eratosthenes to the advanced Segmented Sieve, each algorithm offers unique benefits tailored to specific needs.

