给定数字N为质数,任务是找到给定质数在质数系列中的位置。
例子 :
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
朴素方法:针对给定输入,此问题的朴素方法是计算小于该数字的质数,并跟踪小于给定N的质数。如果计数为K ,则答案为K +1 。这种方法的时间复杂度是二次的。
高效方法:想法是对Eratosthenes筛子进行略微修改。可以计算所有最大值的质数,并将其位置与数组一起存储在数组中。显然,当质数存储在数组中时,存储该数字的索引是该数字在系列中的位置。预计算之后,可以在恒定时间内计算出答案。
下面是上述方法的实现:
C++
// C++ program to find the position
// of the given prime number
#include
#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;
}
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
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
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
Javascript
输出:
5