📜  给定数的第N个素数

📅  最后修改于: 2021-04-27 05:49:24             🧑  作者: Mango

给定Q查询,该查询由两个整数组成,一个是number(1 <= number <= 10 6 ),另一个是N.,任务是查找给定数字的第N个素数。

例子:

天真的方法是分解每个数字并存储主要因素。打印由此存储的第N个素因数。
时间复杂度:每个查询O(log(n))。

一种有效的方法是预先计算数字的所有素数,并将数字按排序顺序存储在二维向量中。由于数量不超过10 6 ,因此唯一质数的数量最大为7-8左右(因为2 * 3 * 5 * 7 * 11 * 13 * 17 * 19> = 10 6 )。一旦存储了数字,就可以在O(1)中回答查询,因为第n-1索引的答案将在第n行中。

下面是上述方法的实现:

CPP
// C++ program to answer queries
// for N-th prime factor of a number
#include 
using namespace std;
const int N = 1000001;
  
// 2-D vector that stores prime factors
vector v[N];
  
// Function to pre-store prime
// factors of all numbers till 10^6
void preprocess()
{
    // calculate unique prime factors for
    // every number till 10^6
    for (int i = 1; i < N; i++) {
  
        int num = i;
  
        // find prime factors
        for (int j = 2; j <= sqrt(num); j++) {
            if (num % j == 0) {
  
                // store if prime factor
                v[i].push_back(j);
  
                while (num % j == 0) {
                    num = num / j;
                }
            }
        }
          
        if(num>2)
        v[i].push_back(num);
          
    }
}
  
// Function that returns answer
// for every query
int query(int number, int n)
{
    return v[number][n - 1];
}
  
// Driver Code
int main()
{
  
    // Function to pre-store unique prime factors
    preprocess();
  
    // 1st query
    int number = 6, n = 1;
    cout << query(number, n) << endl;
  
    // 2nd query
    number = 210, n = 3;
    cout << query(number, n) << endl;
  
    // 3rd query
    number = 210, n = 2;
    cout << query(number, n) << endl;
  
    // 4th query
    number = 60, n = 2;
    cout << query(number, n) << endl;
  
    return 0;
}


Java
// Java program to answer queries
// for N-th prime factor of a number
import java.util.*;
  
class GFG
{
      
static int N = 1000001;
  
// 2-D vector that stores prime factors
static Vector []v = new Vector[N];
  
// Function to pre-store prime
// factors of all numbers till 10^6
static void preprocess()
{
    // calculate unique prime factors for
    // every number till 10^6
    for (int i = 1; i < N; i++) 
    {
  
        int num = i;
  
        // find prime factors
        for (int j = 2; j <= Math.sqrt(num); j++) 
        {
            if (num % j == 0)
            {
  
                // store if prime factor
                v[i].add(j);
  
                while (num % j == 0) 
                {
                    num = num / j;
                }
            }
        }
        if(num > 2)
        v[i].add(num);
    }
}
  
// Function that returns answer
// for every query
static int query(int number, int n)
{
    return v[number].get(n - 1);
}
  
// Driver Code
public static void main(String[] args)
{
  
    for (int i = 0; i < N; i++)
        v[i] = new Vector();
  
    // Function to pre-store unique prime factors
    preprocess();
  
    // 1st query
    int number = 6, n = 1;
    System.out.print(query(number, n) +"\n");
  
    // 2nd query
    number = 210; n = 3;
    System.out.print(query(number, n) +"\n");
  
    // 3rd query
    number = 210; n = 2;
    System.out.print(query(number, n) +"\n");
  
    // 4th query
    number = 60; n = 2;
    System.out.print(query(number, n) +"\n");
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to answer queries
# for N-th prime factor of a number
from math import sqrt,ceil
N = 10001
  
# 2-D vector that stores prime factors
v = [[] for i in range(N)]
  
# Function to pre-store prime
# factors of all numbers till 10^6
def preprocess():
      
    # calculate unique prime factors for
    # every number till 10^6
    for i in range(1, N):
  
        num = i
  
        # find prime factors
        for j in range(2,ceil(sqrt(num)) + 1):
            if (num % j == 0):
                  
                # store if prime factor
                v[i].append(j)
  
                while (num % j == 0):
                    num = num // j
  
        if(num > 2):
            v[i].append(num)
  
# Function that returns answer
# for every query
def query(number, n):
    return v[number][n - 1]
  
# Driver Code
  
# Function to pre-store unique prime factors
preprocess()
  
# 1st query
number = 6
n = 1
print(query(number, n))
  
# 2nd query
number = 210
n = 3
print(query(number, n))
  
# 3rd query
number = 210
n = 2
print(query(number, n))
  
# 4th query
number = 60
n = 2
print(query(number, n))
  
# This code is contributed by mohit kumar 29


C#
// C# program to answer queries
// for N-th prime factor of a number
using System;
using System.Collections.Generic;
  
class GFG
{
      
static int N = 100001;
  
// 2-D vector that stores prime factors
static List []v = new List[N];
  
// Function to pre-store prime
// factors of all numbers till 10^6
static void preprocess()
{
    // calculate unique prime factors for
    // every number till 10^6
    for (int i = 1; i < N; i++) 
    {
  
        int num = i;
  
        // find prime factors
        for (int j = 2; j <= Math.Sqrt(num); j++) 
        {
            if (num % j == 0)
            {
  
                // store if prime factor
                v[i].Add(j);
  
                while (num % j == 0) 
                {
                    num = num / j;
                }
            }
        }
        if(num > 2)
        v[i].Add(num);
    }
}
  
// Function that returns answer
// for every query
static int query(int number, int n)
{
    return v[number][n - 1];
}
  
// Driver Code
public static void Main(String[] args)
{
  
    for (int i = 0; i < N; i++)
        v[i] = new List();
  
    // Function to pre-store unique prime factors
    preprocess();
  
    // 1st query
    int number = 6, n = 1;
    Console.Write(query(number, n) +"\n");
  
    // 2nd query
    number = 210; n = 3;
    Console.Write(query(number, n) +"\n");
  
    // 3rd query
    number = 210; n = 2; 
    Console.Write(query(number, n) +"\n");
  
    // 4th query
    number = 60; n = 2;
    Console.Write(query(number, n) +"\n");
}
}
  
// This code is contributed by PrinciRaj1992


输出:
2
5
3
3

时间复杂度:每个查询为O(1),预处理为O(maxN * log(maxN)),其中maxN = 10 6

辅助空间:最坏情况下为O(N * 8)