Kth smallest Prime Number in range L to R for Q queries
Last Updated :
11 Jan, 2022
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>
Time Complexity: O(N * log(log N)))
Auxiliary Space: O(N)
Similar Reads
Queries for k-th smallest in given ranges Given n ranges of the form [l, r] and an integer q denoting the number of queries. The task is to return the kth smallest element for each query (assume k>1) after combining all the ranges. In case the kth smallest element doesn't exist, return -1. Examples : Input: n = 2, q = 3, range[] = {{1, 4
15+ min read
Smallest prime number in given Range Given two integers L and R, denoting a range [L, R], the task is to find the smallest prime number present in the range. Examples: Input: L = 6, R = 11Output: 7Explanation: In the range of 6 to 116 is divisible by 1, 2, 3 and 6.7 is divisible by 1 and 7, hence this is a prime and the first in the ra
5 min read
K-Primes (Numbers with k prime factors) in a range Given three integers A, B and K. We need to find no. of K-prime numbers in the range [A, B]. A number is called K-prime if it has exactly K distinct prime factors. Examples: Input : A = 4, B = 10, K = 2. Output : 6 10 Given range is [4, 5, 6, 7, 8, 9, 10]. From the above range 6 and 10 have 2 distin
6 min read
Queries for the smallest and the largest prime number of given digit Given Q queries where every query consists of an integer D, the task is to find the smallest and the largest prime number with D digits. If no such prime number exists then print -1.Examples: Input: Q[] = {2, 5} Output: 11 97 10007 99991Input: Q[] = {4, 3, 1} Output: 1009 9973 101 997 1 7 Approach:
9 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