Java Program to Count Primes in Ranges Last Updated : 13 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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}. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. A simple solution is to do the following for every query [L, R]. Traverse from L to R, check if current number is prime. If yes, increment the count. Finally, return the count.An efficient solution is to use Sieve of Eratosthenes to find all primes up to the given limit. Then we compute a prefix array to store counts till every value before limit. Once we have a prefix array, we can answer queries in O(1) time. We just need to return prefix[R] - prefix[L-1]. 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. Output: 2 4 Please refer complete article on Count Primes in Ranges for more details! Comment More infoAdvertise with us Next Article Program for sum of primes from 1 to n K kartik Follow Improve Article Tags : Java array-range-queries prefix-sum sieve Prime Number +1 More Practice Tags : Javaprefix-sumPrime Numbersieve Similar Reads Javascript Program for Count Primes in Ranges 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 = 10Output : 4 2Explanation :P 3 min read Count Primes in Ranges Given a 2d array queries[][] of size n, where each query queries[i] contain 2 elements [l, r], your task is to find the count of number of primes in inclusive range [l, r]Examples: Input: queries[][] = [ [1, 10], [5, 10], [11, 20] ]Output: 4 2 4Explanation: For query 1, number of primes in range [1, 12 min read Count Primes in Ranges Given a 2d array queries[][] of size n, where each query queries[i] contain 2 elements [l, r], your task is to find the count of number of primes in inclusive range [l, r]Examples: Input: queries[][] = [ [1, 10], [5, 10], [11, 20] ]Output: 4 2 4Explanation: For query 1, number of primes in range [1, 12 min read Program for sum of primes from 1 to n Given a positive integer n, compute and return the sum of all prime numbers between 1 and n (inclusive).Examples: Input : 10Output : 17Explanation : Primes between 1 to 10 : 2, 3, 5, 7.Input : 11Output : 28Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.[Naive Approach] Trial Division Method - 10 min read Count Full Prime numbers in a given range Given two integers L and R, the task is to count the number of full prime numbers that are present in the given range. A number is said to be Full prime if the number itself is prime and all its digits are also prime. Examples: 53 is Full Prime because it is prime and all its digits (5 and 3) are al 8 min read Queries on the sum of prime factor counts in a range There are Q queries. Each query is of the form of L and R. The task is to output sum of number of prime factors of each number in the given range of each query.Examples: Input : Q = 2 L = 6, R = 10 L = 1, R = 5 Output : 7 4 For query 1, 6 => 2 [Prime factors are 2 and 3] 7 => 1 8 => 1 9 = 7 min read Like