Given a number N which is a prime number, the task is to find the position of the given prime number in the series of Prime Numbers.
Input: N = 11
The prime numbers are 2, 3, 5, 7, 11, 13, 17, ….
Therefore, the position of 11 in this series is 5.
Input: N = 13
Naive Approach: The naive approach for this problem is for the given input, compute the prime numbers which are less than that number and keep a track of the number of primes less than the given N. If the count is K, then K + 1 would be the answer. The time complexity for this approach is quadratic.
Efficient Approach: The idea is to use the slight modification of Sieve of Eratosthenes. All the prime numbers up to the maximum value can be computed and stored in an array along with its position. Clearly, when the prime numbers are stored in an array, the index at which the number is stored is the position of the number in the series. After this precomputation, the answer can be calculated in constant time.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find coordinates of a prime number in a Prime Spiral
- Find position of the given number among the numbers made of 4 and 7
- Find the minimum number to be added to N to make it a prime number
- Sum of all the prime numbers with the maximum position of set bit ≤ D
- Given a number x , find y such that x*y + 1 is not a prime
- Program to find the next prime number
- Program to find the Nth Prime Number
- Find prime number K in an array such that (A[i] % K) is maximum
- Find sum of a number and its maximum prime factor
- Find largest prime factor of a number
- Find all the prime numbers of given number of digits
- Find a sequence of N prime numbers whose sum is a composite number
- Analysis of Different Methods to find Prime Number in Python
- Find Largest Special Prime which is less than or equal to a given number
- Find the number of primitive roots modulo prime
- Find third number such that sum of all three number becomes prime
- Quick ways to check for Prime and find next Prime in Java
- Print the nearest prime number formed by adding prime numbers to N
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.