给定一个整数N ,任务是找到不能被前N 个素数整除的最小合数。
例子:
Input: N = 3
Output: 49
Explanation:
The first 3 prime numbers are {2, 3, 5}. The smallest composite integer not divisible by either 2, 3, or 5 is 49.
Input: N = 2
Output: 25
Explanation:
The first 2 prime numbers are {2, 3}. The smallest composite integer not divisible by either 2 or 3 is 25.
朴素的方法:解决问题的最简单方法是检查从 2 开始的每个数字的以下条件:
- 条件1:检查当前数是否为素数。如果是素数,则对下一个数字重复该过程。否则,如果数字是合数,则检查以下条件:
- 条件 2:找出前N 个素数,并检查这个合数是否不能被它们中的每一个整除。
- 如果当前数字满足上述两个条件,则打印该数字作为所需答案。
时间复杂度: O(M 3 N),其中M表示满足条件的合数。
辅助空间: O(N),用于存储 N 个素数。
有效的方法:可以使用以下观察来解决给定的问题:
The first composite number which is not divisible by any of the first N prime numbers = ((N + 1)th prime number)2
插图:
For N = 2
=> The first 2 prime numbers are {2, 3}
=> (N + 1)th prime number is 5
=> It can be observed that all the non prime numbers up to 24 {4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24} are divisible by either 2, or 3, or both.
=> The next composite number {25} is divisible by 5 only.
=> Therefore, it can be concluded that the first composite number which is not divisible by any of the first N prime numbers is the square of the (N + 1)th prime number.
这个想法是使用埃拉托色尼筛法找到第(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
Javascript
49
时间复杂度: O(MAX_SIZE log (log MAX_SIZE)),其中 MAX_SIZE 表示生成N 个素数的上限。
辅助空间: O(MAX_SIZE)