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++

`// C++ program to find the position ` `// of the given prime number ` ` ` `#include <bits/stdc++.h> ` `#define limit 10000000 ` `using` `namespace` `std; ` `int` `position[limit + 1]; ` ` ` `// Function to precompute the position ` `// of every prime number using Sieve ` `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 code ` `int` `main() ` `{ ` ` ` `sieve(); ` ` ` ` ` `int` `n = 11; ` ` ` `cout << position[n]; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the position ` `// of the given prime number ` `class` `GFG{ ` ` ` `static` `final` `int` `limit = ` `10000000` `; ` `static` `int` `[]position = ` `new` `int` `[limit + ` `1` `]; ` ` ` `// Function to precompute the position ` `// of every prime number using Sieve ` `static` `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 code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `sieve(); ` ` ` ` ` `int` `n = ` `11` `; ` ` ` `System.out.print(position[n]); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the position ` `# of the given prime number ` `limit ` `=` `1000000` `position ` `=` `[` `0` `]` `*` `(limit ` `+` `1` `) ` ` ` `# Function to precompute the position ` `# of every prime number using Sieve ` `def` `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 code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `sieve() ` ` ` ` ` `n ` `=` `11` ` ` `print` `(position[n]) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the position ` `// of the given prime number ` `using` `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 Sieve ` `static` `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 code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `sieve(); ` ` ` ` ` `int` `n = 11; ` ` ` `Console.Write(position[n]); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

5

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.

## Recommended Posts:

- Find coordinates of a prime number in a Prime Spiral
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Find position of the given number among the numbers made of 4 and 7
- Sum of all the prime numbers with the maximum position of set bit ≤ D
- Quick ways to check for Prime and find next Prime in Java
- Count all prime numbers in a given range whose sum of digits is also prime
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Find element position in given monotonic sequence
- Find the position of box which occupies the given ball
- Find the Kth position element of the given sequence
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Find Largest Special Prime which is less than or equal to a given number
- Given a number x , find y such that x*y + 1 is not a prime
- Find all the prime numbers of given number of digits
- Find third number such that sum of all three number becomes prime
- Find the minimum number to be added to N to make it a prime number
- Prime numbers after prime P with sum S
- Sum of each element raised to (prime-1) % prime

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.