Find the position of the given Prime Number

21 Nov, 2021
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.Examples :  
Input: N = 11 Output: 5 Explanation: The prime numbers are 2, 3, 5, 7, 11, 13, 17, .... Therefore, the position of 11 in this series is 5.Input: N = 13 Output: 6  
 
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: 
C++
Java
Python3
C#
Javascript
// C++ program to find the position// of the given prime number #include <bits/stdc++.h>#define limit 10000000using namespace std;int position[limit + 1]; // Function to precompute the position// of every prime number using Sievevoid sieve(){    // 0 and 1 are not prime numbers    position[0] = -1, position[1] = -1;     // Variable to store the position    int pos = 0;    for (int i = 2; i <= limit; i++) {        if (position[i] == 0) {             // Incrementing the position for            // every prime number            position[i] = ++pos;            for (int j = i * 2; j <= limit; j += i)                position[j] = -1;        }    }} // Driver codeint main(){    sieve();     int n = 11;    cout << position[n];    return 0;}
// Java program to find the position// of the given prime numberclass GFG{        static final int limit = 10000000;static int []position = new int[limit + 1];  // Function to precompute the position// of every prime number using Sievestatic void sieve(){    // 0 and 1 are not prime numbers    position[0] = -1;    position[1] = -1;      // Variable to store the position    int pos = 0;    for (int i = 2; i <= limit; i++) {        if (position[i] == 0) {              // Incrementing the position for            // every prime number            position[i] = ++pos;            for (int j = i * 2; j <= limit; j += i)                position[j] = -1;        }    }}  // Driver codepublic static void main(String[] args){    sieve();      int n = 11;    System.out.print(position[n]);}} // This code is contributed by Rajput-Ji
# Python3 program to find the position# of the given prime numberlimit = 1000000position = [0]*(limit + 1)  # Function to precompute the position# of every prime number using Sievedef sieve():     # 0 and 1 are not prime numbers    position[0] = -1    position[1] = -1      # Variable to store the position    pos = 0    for i in range(2, limit + 1):        if (position[i] == 0):              # Incrementing the position for            # every prime number            pos += 1            position[i] = pos            for j in range( i * 2, limit + 1 ,i):                position[j] = -1  # Driver codeif __name__ == "__main__":    sieve()      n = 11    print(position[n])     # This code is contributed by chitranayal
// C# program to find the position// of the given prime numberusing System; class GFG{         static readonly int limit = 1000000;static int []position = new int[limit + 1];   // Function to precompute the position// of every prime number using Sievestatic void sieve(){    // 0 and 1 are not prime numbers    position[0] = -1;    position[1] = -1;       // Variable to store the position    int pos = 0;    for (int i = 2; i <= limit; i++) {        if (position[i] == 0) {               // Incrementing the position for            // every prime number            position[i] = ++pos;            for (int j = i * 2; j <= limit; j += i)                position[j] = -1;        }    }}   // Driver codepublic static void Main(String[] args){    sieve();       int n = 11;    Console.Write(position[n]);}}  // This code is contributed by Princi Singh
<script> // Javascript program to find the position// of the given prime numbervar limit = 10000000var position = Array(limit+1).fill(0); // Function to precompute the position// of every prime number using Sievefunction sieve(){     // 0 and 1 are not prime numbers    position[0] = -1, position[1] = -1;     // Variable to store the position    var pos = 0;    for (var i = 2; i <= limit; i++)    {        if (position[i] == 0)        {             // Incrementing the position for            // every prime number            position[i] = ++pos;            for (var j = i * 2; j <= limit; j += i)                position[j] = -1;        }    }} // Driver codesieve();var n = 11;document.write( position[n]); // This code is contributed by noob2000.</script>
5
 
Time Complexity: O(limit2)
Auxiliary Space: O(limit)
ukasp
Rajput-Ji
princi singh
noob2000
souravmahato348
Prime Number
sieve
Mathematical
Mathematical
Prime Number
sieve
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Program for Decimal to Binary Conversion
Modulo Operator (%) in C/C++ with Examples
Modulo 10^9+7 (1000000007)