📜  最小的复合数不能被前N个质数整除

📅  最后修改于: 2021-04-27 23:25:30             🧑  作者: Mango

给定一个整数N ,任务是找到不能被前N个质数整除的最小复合数。

例子:

天真的方法:解决问题的最简单方法是检查从2开始的每个数字的以下条件:

  • 条件1:检查当前数字是否为素数。如果是素数,则对下一个数字重复该过程。否则,如果数字是复合数字,则检查以下情况:
  • 条件2:找到前N个质数,并检查该复合数是否不能被它们中的每一个除尽。
  • 如果当前号码同时满足上述两个条件,则将其打印为所需答案。

时间复杂度: O(M 3 N),其中M表示满足条件的复合数。
辅助空间: O(N),用于存储N个素数。

高效的方法:可以通过以下观察来解决给定的问题:

插图:

这个想法是使用Eratosthenes筛子找到第(N + 1)质数,并打印第(N + 1)质数的平方作为答案。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Initializing the max value
#define MAX_SIZE 1000005
 
// Function to generate N prime numbers
// using Sieve of Eratosthenes
void SieveOfEratosthenes(
    vector& StorePrimes)
{
    // Stores the primes
    bool IsPrime[MAX_SIZE];
 
    // Setting all numbers to be prime initially
    memset(IsPrime, true, sizeof(IsPrime));
 
    for (int p = 2; p * p < MAX_SIZE; p++) {
 
        // If a prime number is encountered
        if (IsPrime[p] == true) {
 
            // Set all its multiples as composites
            for (int i = p * p; i < MAX_SIZE; i += p)
                IsPrime[i] = false;
        }
    }
 
    // Store all the prime numbers
    for (int p = 2; p < MAX_SIZE; p++)
        if (IsPrime[p])
            StorePrimes.push_back(p);
}
 
// Function to find the square of
// the (N + 1)-th prime number
int Smallest_non_Prime(
    vector StorePrimes,
    int N)
{
    int x = StorePrimes[N];
    return x * x;
}
 
// Driver Code
int main()
{
    int N = 3;
 
    // Stores all prime numbers
    vector StorePrimes;
 
    SieveOfEratosthenes(StorePrimes);
 
    cout << Smallest_non_Prime(StorePrimes, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.Arrays;
import java.util.Vector;
 
class GFG{
 
// Initializing the max value
static final int MAX_SIZE = 1000005;
 
// Function to generate N prime numbers
// using Sieve of Eratosthenes
static void SieveOfEratosthenes(
    Vector StorePrimes)
{
     
    // Stores the primes
    boolean []IsPrime = new boolean[MAX_SIZE];
 
    // Setting all numbers to be prime initially
    Arrays.fill(IsPrime, true);
 
    for(int p = 2; p * p < MAX_SIZE; p++)
    {
         
        // If a prime number is encountered
        if (IsPrime[p] == true)
        {
             
            // Set all its multiples as composites
            for(int i = p * p; i < MAX_SIZE; i += p)
                IsPrime[i] = false;
        }
    }
 
    // Store all the prime numbers
    for(int p = 2; p < MAX_SIZE; p++)
        if (IsPrime[p])
            StorePrimes.add(p);
}
 
// Function to find the square of
// the (N + 1)-th prime number
static int Smallest_non_Prime(
    Vector StorePrimes,
    int N)
{
    int x = StorePrimes.get(N);
    return x * x;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3;
 
    // Stores all prime numbers
    Vector StorePrimes = new Vector();
 
    SieveOfEratosthenes(StorePrimes);
 
    System.out.print(Smallest_non_Prime(StorePrimes, N));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Initializing the max value
MAX_SIZE = 1000005
 
# Function to generate N prime numbers
# using Sieve of Eratosthenes
def SieveOfEratosthenes(StorePrimes):
     
    # Stores the primes
    IsPrime = [True for i in range(MAX_SIZE)]
 
    p = 2
    while (p * p < MAX_SIZE):
         
        # If a prime number is encountered
        if (IsPrime[p] == True):
             
            # Set all its multiples as composites
            for i in range(p * p, MAX_SIZE, p):
                IsPrime[i] = False
                 
        p += 1
 
    # Store all the prime numbers
    for p in range(2, MAX_SIZE):
        if (IsPrime[p]):
            StorePrimes.append(p)
 
# Function to find the square of
# the (N + 1)-th prime number
def Smallest_non_Prime(StorePrimes, N):
     
    x = StorePrimes[N]
     
    return x * x
 
# Driver Code
if __name__ == '__main__':
     
    N = 3
 
    # Stores all prime numbers
    StorePrimes = []
 
    SieveOfEratosthenes(StorePrimes)
 
    print(Smallest_non_Prime(StorePrimes, N))
 
# This code is contributed by bgangwar59


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Initializing the max value
static readonly int MAX_SIZE = 1000005;
 
// Function to generate N prime numbers
// using Sieve of Eratosthenes
static void SieveOfEratosthenes(
    List StorePrimes)
{
     
    // Stores the primes
    bool []IsPrime = new bool[MAX_SIZE];
 
    // Setting all numbers to be prime initially
    for(int i = 0; i < MAX_SIZE; i++)
        IsPrime[i] = true;
 
    for(int p = 2; p * p < MAX_SIZE; p++)
    {
         
        // If a prime number is encountered
        if (IsPrime[p] == true)
        {
             
            // Set all its multiples as composites
            for(int i = p * p; i < MAX_SIZE; i += p)
                IsPrime[i] = false;
        }
    }
 
    // Store all the prime numbers
    for(int p = 2; p < MAX_SIZE; p++)
        if (IsPrime[p])
            StorePrimes.Add(p);
}
 
// Function to find the square of
// the (N + 1)-th prime number
static int Smallest_non_Prime(
    List StorePrimes,
    int N)
{
    int x = StorePrimes[N];
    return x * x;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 3;
 
    // Stores all prime numbers
    List StorePrimes = new List();
 
    SieveOfEratosthenes(StorePrimes);
 
    Console.Write(Smallest_non_Prime(StorePrimes, N));
}
}
 
// This code is contributed by Amit Katiyar


输出:
49











时间复杂度: O(MAX_SIZE log(log MAX_SIZE)),其中MAX_SIZE表示生成N个素数的上限。
辅助空间: O(MAX_SIZE)