Given a range [L, R], we need to find the count of total numbers of prime numbers in the range [L, R] where 0 <= L <= R < 10000. Consider that there are a large number of queries for different ranges.
Examples:
Input : Query 1 : L = 1, R = 10
Query 2 : L = 5, R = 10
Output : 4
2
Explanation
Primes in the range L = 1 to R = 10 are
{2, 3, 5, 7}. Therefore for query, answer
is 4 {2, 3, 5, 7}.
For the second query, answer is 2 {5, 7}.
A simple solution is to do following for every query [L, R]. Traverse from L to R, check if current number is prime. If yes, increment count. Finally return the count.
An efficient solution is to use Sieve of Eratosthenes to find all primes up to given limit. Then we compute a prefix array to store counts till every value before limit. Once we have prefix array, we can answer queries in O(1) time. We just need to return prefix[R] – prefix[L-1].
C++
// CPP program to answer queries for count of // primes in given range. #include <bits/stdc++.h> using namespace std; const int MAX = 10000; // prefix[i] is going to store count of primes // till i (including i). int prefix[MAX + 1]; void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. bool prime[MAX + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p]++; } } // Returns count of primes in range from L to // R (both inclusive). int query(int L, int R) { return prefix[R] - prefix[L - 1]; } // Driver code int main() { buildPrefix(); int L = 5, R = 10; cout << query(L, R) << endl; L = 1, R = 10; cout << query(L, R) << endl; return 0; } |
Java
// Java program to answer queries for // count of primes in given range. import java.util.*; class GFG { static final int MAX = 10000; // prefix[i] is going to store count // of primes till i (including i). static int prefix[] = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p]++; } } // Returns count of primes in range // from L to R (both inclusive). static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } // Driver code public static void main(String[] args) { buildPrefix(); int L = 5, R = 10; System.out.println(query(L, R)); L = 1; R = 10; System.out.println(query(L, R)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to answer queries for # count of primes in given range. MAX = 10000 # prefix[i] is going to # store count of primes # till i (including i). prefix =[0]*(MAX + 1) def buildPrefix(): # Create a boolean array value in # prime[i] will "prime[0..n]". A # finally be false if i is Not a # prime, else true. prime = [1]*(MAX + 1) p = 2 while(p * p <= MAX): # If prime[p] is not changed, # then it is a prime if (prime[p] == 1): # Update all multiples of p i = p * 2 while(i <= MAX): prime[i] = 0 i += p p+=1 # Build prefix array # prefix[0] = prefix[1] = 0; for p in range(2,MAX+1): prefix[p] = prefix[p - 1] if (prime[p]==1): prefix[p]+=1 # Returns count of primes # in range from L to # R (both inclusive). def query(L, R): return prefix[R]-prefix[L - 1] # Driver code if __name__=='__main__': buildPrefix() L = 5 R = 10 print(query(L, R)) L = 1 R = 10 print(query(L, R)) # This code is contributed by mits. |
C#
// C# program to answer // queries for count of // primes in given range. using System; class GFG { static int MAX = 10000; // prefix[i] is going // to store count of // primes till i (including i). static int[] prefix = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array // "prime[0..n]". A value // in prime[i] will finally // be false if i is Not a // prime, else true. bool[] prime = new bool[MAX + 1]; for (int p = 2; p * p <= MAX; p++) { // If prime[p] is // not changed, then // it is a prime if (prime[p] == false) { // Update all // multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = true; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p] == false) prefix[p]++; } } // Returns count of primes // in range from L to R // (both inclusive). static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } // Driver code public static void Main() { buildPrefix(); int L = 5, R = 10; Console.WriteLine(query(L, R)); L = 1; R = 10; Console.WriteLine(query(L, R)); } } // This code is contributed // by mits. |
PHP
<?php // PHP program to answer queries for // count of primes in given range. $MAX = 10000; // prefix[i] is going to // store count of primes // till i (including i). $prefix = array_fill(0, ($MAX + 1), 0); function buildPrefix() { global $MAX, $prefix; // Create a boolean array value in // prime[i] will "prime[0..n]". A // finally be false if i is Not a // prime, else true. $prime = array_fill(0, ($MAX + 1), true); for ($p = 2; $p * $p <= $MAX; $p++) { // If prime[p] is not changed, // then it is a prime if ($prime[$p] == true) { // Update all multiples of p for ($i = $p * 2; $i <= $MAX; $i += $p) $prime[$i] = false; } } // Build prefix array // $prefix[0] = $prefix[1] = 0; for ($p = 2; $p <= $MAX; $p++) { $prefix[$p] = $prefix[$p - 1]; if ($prime[$p]) $prefix[$p]++; } } // Returns count of primes // in range from L to // R (both inclusive). function query($L, $R) { global $prefix; return $prefix[$R] - $prefix[$L - 1]; } // Driver code buildPrefix(); $L = 5; $R = 10; echo query($L, $R) . "\n"; $L = 1; $R = 10; echo query($L, $R) . "\n"; // This code is contributed by mits. ?> |
Output:
2 4
This article is contributed by ShivamKD. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Choose points from two ranges such that no point lies in both the ranges
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Length of largest sub-array having primes strictly greater than non-primes
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Count distinct median possible for an Array using given ranges of elements
- Count numbers which can be represented as sum of same parity primes
- Count number of primes in an array
- Count the number of primes in the prefix sum array of the given array
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Count of interesting primes upto N
- Count of primes after converting given binary number in base between L to R
- Count of primes in a given range that can be expressed as sum of perfect squares
- Count smaller primes on the right of each array element
- GCDs of given index ranges in an array
- Maximum occurred integer in n ranges
- Print matrix after applying increment operations in M ranges
- Queries on probability of even or odd number in given ranges

