给定数字N(大于2)。任务是找到两个不同的质数,它们的乘积将等于给定数。可能有几种组合。仅打印第一对。
如果不可能将N表示为两个不同素数的乘积,请打印“不可能”。
例子:
Input : N = 15
Output : 3, 5
3 and 5 are both primes and,
3*5 = 15
Input : N = 39
Output : 3, 13
3 and 13 are both primes and,
3*13 = 39
这个想法是使用Eratosthenes筛子找到所有小于或等于给定数N的素数。一旦有了一个告诉所有素数的数组,我们就可以遍历该数组以查找具有给定乘积的一对。
下面是上述方法的实现:
C++
// C++ program to find a distinct prime number
// pair whose product is equal to given number
#include
using namespace std;
// Function to generate all prime
// numbers less than n
bool 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 print a prime pair
// with given product
void findPrimePair(int n)
{
int flag = 0;
// Generating primes using Sieve
bool isPrime[n + 1];
SieveOfEratosthenes(n, isPrime);
// Traversing all numbers to find first
// pair
for (int i = 2; i < n; i++) {
int x = n / i;
if (isPrime[i] && isPrime[x] and x != i
and x * i == n) {
cout << i << " " << x;
flag = 1;
return;
}
}
if (!flag)
cout << "No such pair found";
}
// Driven Code
int main()
{
int n = 39;
findPrimePair(n);
return 0;
}
Java
// Java program to find a distinct prime number
// pair whose product is equal to given number
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 print a prime pair
// with given product
static void findPrimePair(int n)
{
int flag = 0;
// Generating primes using Sieve
boolean[] isPrime = new boolean[n + 1];
SieveOfEratosthenes(n, isPrime);
// Traversing all numbers to find first
// pair
for (int i = 2; i < n; i++) {
int x = n / i;
if (isPrime[i] && isPrime[x] && x != i
&& x * i == n) {
System.out.println(i + " " + x);
flag = 1;
return;
}
}
if (flag == 0)
System.out.println("No such pair found");
}
// Driven Code
public static void main(String[] args)
{
int n = 39;
findPrimePair(n);
}
}
// This code is contributed by
// ihritik
Python3
# Python3 program to find a distinct
# prime number pair whose product
# is equal to given number
# from math lib. import everything
from math import *
# 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], isPrime[1] = False, False
for i in range(2, n + 1) :
isPrime[i] = True
for p in range(2, int(sqrt(n)) + 1) :
# If isPrime[p] is not changed,
# then it is a prime
if isPrime[p] == True :
for i in range(p * 2, n + 1, p) :
isPrime[i] = False
# Function to print a prime pair
# with given product
def findPrimePair(n) :
flag = 0
# Generating primes using Sieve
isPrime = [False] * (n + 1)
SieveOfEratosthenes(n, isPrime)
# Traversing all numbers to
# find first pair
for i in range(2, n) :
x = int(n / i)
if (isPrime[i] & isPrime[x] and
x != i and x * i == n) :
print(i, x)
flag = 1
break
if not flag :
print("No such pair found")
# Driver code
if __name__ == "__main__" :
# Function calling
n = 39;
findPrimePair(n)
# This code is contributed by ANKITRAI1
C#
// C# program to find a distinct prime number
// pair whose product is equal to given number
using System;
class GFG
{
// Function to generate all
// prime numbers less than n
static void SieveOfEratosthenes(int n,
bool[] isPrime)
{
// Initialize all entries of bool
// 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 print a prime
// pair with given product
static void findPrimePair(int n)
{
int flag = 0;
// Generating primes using Sieve
bool[] isPrime = new bool[n + 1];
SieveOfEratosthenes(n, isPrime);
// Traversing all numbers to
// find first pair
for (int i = 2; i < n; i++)
{
int x = n / i;
if (isPrime[i] && isPrime[x] &&
x != i && x * i == n)
{
Console.Write(i + " " + x);
flag = 1;
return;
}
}
if (flag == 0)
Console.Write("No such pair found");
}
// Driven Code
public static void Main()
{
int n = 39;
findPrimePair(n);
}
}
// This code is contributed by ChitraNayal
PHP
Javascript
输出:
3 13
时间复杂度: O(N * log log(N))
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。