检查一个数是否可以表示为素数和合数的乘积
给定一个数N,任务是检查N是否可以表示为素数和合数的乘积。如果可以,则打印Yes ,否则打印No 。
例子:
Input: N = 52
Output: Yes
Explanation: 52 can be represented as the multiplication of 4 and 13, where 4 is a composite and 13 is a prime number.
Input: N = 49
Output: No
方法:这个问题可以在埃拉托色尼筛算法的帮助下解决。现在,要解决此问题,请按照以下步骤操作:
- 创建一个布尔数组isPrime ,其中第i 个元素如果是素数则为真,否则为假。
- 使用筛算法找到直到N的所有素数。
- 现在运行i=2 到 i
的循环,并在每次迭代中: - 检查这两个条件:
- 如果N可以被i整除。
- 如果i是质数而N/i不是,或者如果 i 不是质数而N/i是。
- 如果上述两个条件都满足,则返回true 。
- 否则,返回false 。
- 检查这两个条件:
- 根据上述观察,打印答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to generate all prime
// numbers less than N
void SieveOfEratosthenes(int N, bool isPrime[])
{
// Initialize all entries of boolean array
// as true. A value in isPrime[i] will finally
// be false if i is Not a prime, else true
// bool isPrime[N+1];
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= N; i++)
isPrime[i] = true;
for (int p = 2; p * p <= N; p++) {
// If isPrime[p] is not changed,
// then it is a prime
if (isPrime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= N; i += p)
isPrime[i] = false;
}
}
}
// Function to check if we can
// represent N as product of a prime
// and a composite number or not
bool isRepresentable(int N)
{
// Generating primes using Sieve
bool isPrime[N + 1];
SieveOfEratosthenes(N, isPrime);
// Traversing through the array
for (int i = 2; i < N; i++) {
if (N % i == 0) {
if (N % i == 0
and (isPrime[i] and !isPrime[N / i])
or (!isPrime[i] and isPrime[N / i])) {
return true;
}
}
}
return false;
}
// Driver Code
int main()
{
int N = 52;
if (isRepresentable(N)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program to implement the above approach
import java.util.*;
public class GFG
{
// Function to generate all prime
// numbers less than N
static void SieveOfEratosthenes(int N, boolean []isPrime)
{
// Initialize all entries of boolean array
// as true. A value in isPrime[i] will finally
// be false if i is Not a prime, else true
// bool isPrime[N+1];
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= N; i++)
isPrime[i] = true;
for (int p = 2; p * p <= N; p++) {
// If isPrime[p] is not changed,
// then it is a prime
if (isPrime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= N; i += p)
isPrime[i] = false;
}
}
}
// Function to check if we can
// represent N as product of a prime
// and a composite number or not
static boolean isRepresentable(int N)
{
// Generating primes using Sieve
boolean []isPrime = new boolean[N + 1];
SieveOfEratosthenes(N, isPrime);
// Traversing through the array
for (int i = 2; i < N; i++) {
if (N % i == 0) {
if (N % i == 0
&& (isPrime[i] && !isPrime[N / i])
|| (!isPrime[i] && isPrime[N / i])) {
return true;
}
}
}
return false;
}
// Driver Code
public static void main(String arg[])
{
int N = 52;
if (isRepresentable(N)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python program for the above approach
import math
# Function to generate all prime
# numbers less than N
def SieveOfEratosthenes(N, isPrime):
# Initialize all entries of boolean array
# as true. A value in isPrime[i] will finally
# be false if i is Not a prime, else true
# bool isPrime[N+1];
isPrime[0] = False
isPrime[1] = False
for i in range(2, N+1):
isPrime[i] = True
for p in range(2, int(math.sqrt(N)) + 1):
# If isPrime[p] is not changed,
# then it is a prime
if (isPrime[p] == True):
# Update all multiples of p
for i in range(p+2, N+1, p):
isPrime[i] = False
# Function to check if we can
# represent N as product of a prime
# and a composite number or not
def isRepresentable(N):
# Generating primes using Sieve
isPrime = [0 for _ in range(N + 1)]
SieveOfEratosthenes(N, isPrime)
# Traversing through the array
for i in range(2, N):
if (N % i == 0):
if (N % i == 0 and (isPrime[i] and not isPrime[N // i]) or (not isPrime[i] and isPrime[N // i])):
return True
return False
# Driver Code
if __name__ == "__main__":
N = 52
if (isRepresentable(N)):
print("Yes")
else:
print("No")
# This code is contributed by rakeshsahni
C#
// C# program to implement the above approach
using System;
class GFG
{
// Function to generate all prime
// numbers less than N
static void SieveOfEratosthenes(int N, bool []isPrime)
{
// Initialize all entries of boolean array
// as true. A value in isPrime[i] will finally
// be false if i is Not a prime, else true
// bool isPrime[N+1];
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= N; i++)
isPrime[i] = true;
for (int p = 2; p * p <= N; p++) {
// If isPrime[p] is not changed,
// then it is a prime
if (isPrime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= N; i += p)
isPrime[i] = false;
}
}
}
// Function to check if we can
// represent N as product of a prime
// and a composite number or not
static bool isRepresentable(int N)
{
// Generating primes using Sieve
bool []isPrime = new bool[N + 1];
SieveOfEratosthenes(N, isPrime);
// Traversing through the array
for (int i = 2; i < N; i++) {
if (N % i == 0) {
if (N % i == 0
&& (isPrime[i] && !isPrime[N / i])
|| (!isPrime[i] && isPrime[N / i])) {
return true;
}
}
}
return false;
}
// Driver Code
public static void Main()
{
int N = 52;
if (isRepresentable(N)) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
Yes
时间复杂度: O(N*log(logN))
辅助空间: O(N)