Javascript Program for Count Primes in Ranges Last Updated : 23 Jul, 2025 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 = 10Output : 4 2Explanation :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 the following for every query [L, R]. Traverse from L to R, check if the 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]. JavaScript // Javascript program to answer queries for // count of primes in given range. let MAX = 10000; // prefix[i] is going to store count // of primes till i (including i). let prefix = []; function 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. let prime = []; for (let p = 1; p <= MAX + 1; p++) { prime[p] = true; } for (let 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 (let i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (let 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) { return prefix[R] - prefix[L - 1]; } // driver program buildPrefix(); let L = 5, R = 10; console.log(query(L, R)); L = 1; R = 10; console.log(query(L, R)); Output2 4 Please refer complete article on Count Primes in Ranges for more details! Comment More infoAdvertise with us Next Article C++ Program to Count Primes in Ranges K kartik Follow Improve Article Tags : JavaScript array-range-queries prefix-sum sieve Prime Number +1 More Practice Tags : prefix-sumPrime Numbersieve Similar Reads C++ Program to 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 = 10 Output : 4 2 Explanation 3 min read Javascript Program for Prime Numbers What are prime numbers?A prime number is a natural number greater than 1, which is only divisible by 1 and itself. The first few prime numbers are 2 3 5 7 11 13 17 19 23...Prime numbersIn other words, the prime number is a positive integer greater than 1 that has exactly two factors, 1 and the numbe 5 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 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 JavaScript Program to Print Prime Numbers from 1 to N A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we'll explore how to create a JavaScript program to print all prime numbers from 1 to a given number N. To find prime numbers from 1 to N, we need to: Iterate through all numbers 3 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 Like