PHP Program to Count Primes in Ranges Last Updated : 22 Jul, 2024 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}.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]. 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); ?> Output2 4Please refer complete article on Count Primes in Ranges for more details! Comment More infoAdvertise with us Next Article PHP Program to Count Primes in Ranges kartik Follow Improve Article Tags : Mathematical Web Technologies PHP PHP Programs DSA array-range-queries prefix-sum sieve Prime Number +5 More Practice Tags : Mathematicalprefix-sumPrime Numbersieve Similar Reads PHP Program to Print Prime Number from 1 to N Given a number N, the task is to print all prime numbers in a range from 1 to N in PHP. Examples: Input: N = 10Output: 2, 3, 5, 7Input: N = 50Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47Print Prime Numbers using PHP for LoopFirst, take the number N as input.Then use a for loop to i 2 min read PHP Program to Print All Prime Factors of a Given Number Prime factors of a number are the prime numbers that divide the given number exactly. Finding prime factors is a common task in number theory and has applications in cryptography and algorithm design. In this article, we will discuss efficient ways to print all prime factors of a given number in PHP 3 min read PHP Program to Find All Prime Numbers in a Given Interval Prime numbers are fundamental in the field of mathematics and computer science. A prime number is defined as a natural number greater than 1 and is divisible by only 1 and itself. In this article, we will explore how to write a PHP program to find all prime numbers within a given interval. Using Tri 2 min read How to Count of Repeating Digits in a Given Number in PHP ? Counting the number of repeating digits in a given number is a common task in programming. This can be useful in various scenarios, such as data analysis or validation. In this article, we will explore different approaches to counting the repeating digits in a given number using PHP.Table of Content 5 min read PHP Program for Range Queries for Frequencies of Array Elements Given an array of n non-negative integers. The task is to find the frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; 2 min read How to Find Prime Number using Sieve of Eratosthenes in PHP ? A prime number is defined as a number that only can be divided by 1 and that number. The number N is prime if it is only divisible by 1 and number N. If the number is divisible by any other number then it is a composite number. For example, number 7 is a prime number and number 12 is a composite num 3 min read Print All Numbers Divisible by 3 and 5 for a Given Number in PHP This article will show you how to print all the numbers less than N, that are divisible by 3 and 5 in PHP. When working with numbers, it's common to need a program that can identify and print all numbers divisible by specific values. In this article, we will explore different approaches in PHP to pr 2 min read Java 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 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, 3 min read Python3 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 = 10Output : 4 2ExplanationPri 2 min read 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 Like