Kth smallest Prime Number in range L to R for Q queries
Given three variables L, R and Q which denotes the range [L, R] and total number of queries. For each query there will be a variable K. The task is to find Kth smallest prime number in range [L, R]. If K is greater than count of prime numbers in the range [L, R] then return -1.
Examples:
Input: Q = 3, L = 5, R = 20
queries: K = 4,
K = 3,
K = 9
Output: 13 11 -1
Explanation: The prime numbers in the given range are 5, 7, 11, 13, 17, 19 and
the 4th and 3rd smallest prime numbers are 13 and 11.Input: Q = 1, L = 15, R = 32
queries: K = 3
Output: 23
Approach: The given problem can be solved with the help of Sieve of Eratosthenes method:
When the algorithm terminates, all the numbers in the list that are not marked are prime. Follow the steps mentioned below:
- Run Sieve of Eratosthenes for number in range [L, R]
- For each query find the Kth smallest prime from the calculated primes.
- If K exceeds the number of primes in that range, return -1.
See the illustration below for better understanding of Sieve of Eratosthenes.
Illustration: Take range [20, 40].
Sieve of Eratosthenes will help in finding the primes in this range.
Create a list of all numbers from 1 to 40. According to the algorithm mark all the non-prime numbers in the given range.
So the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37.
The primes in range [20, 40] are: 23, 29, 31 and 37
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; vector< int > primes; // Function to implement // Sieve Of Eratosthenes void sieveOfEratosthenes( int R) { primes.assign(R + 1, 0); primes[1] = 1; for ( int i = 2; i * i <= R; i++) { if (primes[i] == 0) { for ( int j = i + i; j <= R; j += i) primes[j] = 1; } } } // Function to return Kth smallest // prime number if it exists int kthSmallest( int L, int R, int K) { // To count the prime number int count = 0; // Loop to iterate the from L to R for ( int i = L; i <= R; i++) { // Calculate the count Of // primes numbers if (primes[i] == 0) { count++; } // if count equals K, then // Kth smallest prime number is // found then return the number if (count == K) { return i; } } // Kth smallest prime number is not in // this range then return -1 return -1; } // Driver Code int main() { // No of Queries int Q = 3; int L, R, K; L = 5, R = 20; sieveOfEratosthenes(R); // First Query K = 4; cout << kthSmallest(L, R, K) << endl; // Second Query K = 3; cout << kthSmallest(L, R, K) << endl; // Third Query K = 5; cout << kthSmallest(L, R, K) << endl; return 0; } |
Java
// Java code to implement the above approach import java.util.*; public class GFG { static int primes[] = new int [ 1000 ]; // Function to implement // Sieve Of Eratosthenes static void sieveOfEratosthenes( int R) { for ( int i = 0 ; i < R + 1 ; i++) { primes[i] = 0 ; } primes[ 1 ] = 1 ; for ( int i = 2 ; i * i <= R; i++) { if (primes[i] == 0 ) { for ( int j = i + i; j <= R; j += i) primes[j] = 1 ; } } } // Function to return Kth smallest // prime number if it exists static int kthSmallest( int L, int R, int K) { // To count the prime number int count = 0 ; // Loop to iterate the from L to R for ( int i = L; i <= R; i++) { // Calculate the count Of // primes numbers if (primes[i] == 0 ) { count++; } // if count equals K, then // Kth smallest prime number is // found then return the number if (count == K) { return i; } } // Kth smallest prime number is not in // this range then return -1 return - 1 ; } // Driver code public static void main(String args[]) { // No of Queries int Q = 3 ; int L = 5 , R = 20 ; sieveOfEratosthenes(R); // First Query int K = 4 ; System.out.println(kthSmallest(L, R, K)); // Second Query K = 3 ; System.out.println(kthSmallest(L, R, K)); // Third Query K = 5 ; System.out.println(kthSmallest(L, R, K)); } } // This code is contributed by Samim Hossain Mondal. |
Python3
# Python code to implement the above approach primes = [ 0 for i in range ( 1000 )]; # Function to implement # Sieve Of Eratosthenes def sieveOfEratosthenes(R): for i in range ( 0 , R + 1 ): primes[i] = 0 ; primes[ 1 ] = 1 ; for i in range ( 2 , pow (R, 2 ) + 1 ): if (primes[i] = = 0 ): for j in range (i + i, R + 1 , i): primes[j] = 1 ; # Function to return Kth smallest # prime number if it exists def kthSmallest(L, R, K): # To count the prime number count = 0 ; # Loop to iterate the from L to R for i in range (L,R + 1 ): # Calculate the count Of # primes numbers if (primes[i] = = 0 ): count + = 1 ; # if count equals K, then # Kth smallest prime number is # found then return the number if (count = = K): return i; # Kth smallest prime number is not in # this range then return -1 return - 1 ; # Driver code if __name__ = = '__main__' : # No of Queries Q = 3 ; L = 5 ; R = 20 ; sieveOfEratosthenes(R); # First Query K = 4 ; print (kthSmallest(L, R, K)); # Second Query K = 3 ; print (kthSmallest(L, R, K)); # Third Query K = 5 ; print (kthSmallest(L, R, K)); # This code is contributed by shikhasingrajput |
C#
// C# code to implement the above approach using System; class GFG { static int []primes = new int [1000]; // Function to implement // Sieve Of Eratosthenes static void sieveOfEratosthenes( int R) { for ( int i = 0; i < R + 1; i++) { primes[i] = 0; } primes[1] = 1; for ( int i = 2; i * i <= R; i++) { if (primes[i] == 0) { for ( int j = i + i; j <= R; j += i) primes[j] = 1; } } } // Function to return Kth smallest // prime number if it exists static int kthSmallest( int L, int R, int K) { // To count the prime number int count = 0; // Loop to iterate the from L to R for ( int i = L; i <= R; i++) { // Calculate the count Of // primes numbers if (primes[i] == 0) { count++; } // if count equals K, then // Kth smallest prime number is // found then return the number if (count == K) { return i; } } // Kth smallest prime number is not in // this range then return -1 return -1; } // Driver code public static void Main() { // No of Queries int Q = 3; int L = 5, R = 20; sieveOfEratosthenes(R); // First Query int K = 4; Console.WriteLine(kthSmallest(L, R, K)); // Second Query K = 3; Console.WriteLine(kthSmallest(L, R, K)); // Third Query K = 5; Console.WriteLine(kthSmallest(L, R, K)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript program for the above approach let primes; // Function to implement // Sieve Of Eratosthenes function sieveOfEratosthenes(R) { primes = new Array(R + 1).fill(0); primes[1] = 1; for (let i = 2; i * i <= R; i++) { if (primes[i] == 0) { for (let j = i + i; j <= R; j += i) primes[j] = 1; } } } // Function to return Kth smallest // prime number if it exists function kthSmallest(L, R, K) { // To count the prime number let count = 0; // Loop to iterate the from L to R for (let i = L; i <= R; i++) { // Calculate the count Of // primes numbers if (primes[i] == 0) { count++; } // If count equals K, then // Kth smallest prime number is // found then return the number if (count == K) { return i; } } // Kth smallest prime number is not in // this range then return -1 return -1; } // Driver Code // No of Queries let Q = 3; let L, R, K; L = 5, R = 20; sieveOfEratosthenes(R); // First Query K = 4; document.write(kthSmallest(L, R, K) + '<br>' ); // Second Query K = 3; document.write(kthSmallest(L, R, K) + '<br>' ); // Third Query K = 5; document.write(kthSmallest(L, R, K) + '<br>' ); // This code is contributed by Potta Lokesh </script> |
13 11 17
Time Complexity: O(N * log(log N)))
Auxiliary Space: O(N)
Please Login to comment...