Kth prime number greater than N

Given a number N, the task is to print the Kth prime number greater than N.
Note: N and K are so given such that answer is always less than 10^6.

Examples:

Input: N = 5, K = 5
Output: 19

Input: N = 10, K = 3
Output: 17

A simple solution for this problem is to iterate from n+1 to 10^6 and for every number, check if it is prime and print the Kth prime number. This solution looks fine if there is only one query. But not efficient if there are multiple queries.



An efficient solution for this problem is to generate all primes less than 10^6 using Sieve of Eratosthenes and iterate from n+1 to 10^6 and then print the Kth prime number.

C++

// CPP program to print the Kth prime greater than N
#include
using namespace std;

// set the MAX_SIZE of the array to 10^6
const int MAX_SIZE = 1e6;

// initialize the prime array
bool prime[MAX_SIZE + 1];

void sieve()
{

// set all numbers as prime for time being
memset(prime, true, sizeof(prime));

for (int p = 2; p * p <= MAX_SIZE; 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 * p; i <= MAX_SIZE; i += p) prime[i] = false; } } } // Function to find the kth prime greater than n int kthPrimeGreaterThanN(int n, int k) { int res = -1; // looping through the numbers greater than n for (int i = n + 1; i < MAX_SIZE; i++) { // decrement k if i is prime if (prime[i] == true) k--; // store the kth prime greater than n if (k == 0) { res = i; break; } } return res; } // Driver code int main() { sieve(); int n = 2, k = 15; // Print the kth prime number greater than n cout << kthPrimeGreaterThanN(n, k); return 0; } [tabby title="Python 3"] # Python 3 program to print the Kth # prime greater than N # set the MAX_SIZE of the array to 10^6 MAX_SIZE = int(1e6) # initialize the prime array prime = [True] * (MAX_SIZE + 1) # Code for Sieve of Eratosthenes def seive(): p = 2 while (p * p <= MAX_SIZE): # if prime[p] is not changed, # then it is a prime if (prime[p] == True): # update all multiples of p for i in range(p * p, MAX_SIZE, p): prime[i] = False p += 1 # Function to find the kth prime # greater than n def kthPrimeGreaterThanN(n, k): res = -1 # looping through the numbers # greater than n for i in range(n + 1, MAX_SIZE): # decrement k if i is prime if (prime[i] == True): k -= 1 # store the kth prime greater than n if (k == 0): res = i break return res # Driver Code if __name__=='__main__': n = 2 k = 15 seive() # Print the kth prime number # greater than n print(kthPrimeGreaterThanN(n, k)) # This code is contributed by Rupesh Rao [tabbyending]

Output:

53


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rupesh_rao